Split main file in function.sh

This commit is contained in:
poka 2020-09-15 04:05:07 +02:00
parent 78710b5c42
commit 5cf7df8a8e
6 changed files with 298 additions and 371 deletions

277
functions.sh Executable file
View File

@ -0,0 +1,277 @@
#!/bin/bash
loopWalletUp() {
local REGEX_PUBKEYS="[a-zA-Z0-9]{42,44}"
# On vérifie le cache existant et on démarre au bloc dernier bloc en cache
if ls $SCRIPTPATH/cache/walletsUp-* > /dev/null 2>&1; then
local startFile=$(ls $SCRIPTPATH/cache/walletsUp-* | tail -n1)
local startIter=$(echo "$startFile" | awk -F '-' '{ print $NF }')
local startBloc=$(head -n1 "$startFile")
[[ $lastBloc == $startBloc ]] && return 10
wallets=$(tail -n +2 "$startFile")$'\n'
local iter=$startIter
rm $startFile
else
local startIter=0
local startBloc=0
local iter=0
echo "[" > $SCRIPTPATH/db/recus.json
fi
rm -f debug.log #kopa
for i in ${!TXBLOCKS[*]}; do
[[ -z ${TXBLOCKS[$i]} ]] && break
[[ $startIter != 0 && $i -le $startIter ]] && continue
sleep 0.05
# Récupère la date et l'objet transaction du bloc courant
WALLETS0=$(curl -s ${DUNITER}/blockchain/block/${TXBLOCKS[$i]})
until blocBrut=$(echo $WALLETS0 | jq -r '.medianTime, .transactions[]' 2>/dev/null); do
[[ $isWeb != "web" ]] && echo -e "iter $iter \n$WALLETS0" | tee -a debug.log
sleep 2
WALLETS0=$(curl -s ${DUNITER}/blockchain/block/${TXBLOCKS[$i]})
done
[[ -z "$blocBrut" ]] && continue
bloc=${TXBLOCKS[$i]}
# Récupération de la date du block
blockDate=$(echo "$blocBrut" | head -n1)
blockDate=$(date -d"@$blockDate" +%y-%m-%d -u)
local walletsReceivedToday=$(echo "$blocBrut" | tail -n +2)
# Si cette itération proviens d'un cache, alors on aggrège la dernière date
if [[ $startBloc != 0 ]]; then
lastDate=$(cat db/recus.json | jq -r '.[].date' | tail -n1)
if [[ "$blockDate" == "$lastDate" ]]; then
valueReceivedW=$(cat db/recus.json | jq -r '.[].rWallets' | tail -n1 | tr -d '.,')
valueReceivedM=$(cat db/recus.json | jq -r '.[].rMembres' | tail -n1 | tr -d '.,')
valueSentW=$(cat db/recus.json | jq -r '.[].sWallets' | tail -n1 | tr -d '.,')
valueSentM=$(cat db/recus.json | jq -r '.[].sMembres' | tail -n1 | tr -d '.,')
local recusJson=$(head -n -8 db/recus.json)
echo "$recusJson" > $SCRIPTPATH/db/recus.json
else
local recusJson=$(head -n -4 db/recus.json)
echo -e "$recusJson\n }," > $SCRIPTPATH/db/recus.json
fi
unset recusJson
startBloc=0
fi
# Sélectionne les ligne SIG en retirant les issuers
OIFS="$IFS"
IFS='{'
for j in $walletsReceivedToday; do
[[ -z $j ]] && continue
wIssuers=$(echo "{$j" | jq -r '.issuers[0]')
walletsSentTodayTmp+=$wIssuers$'\n'
walletsReceivedTodayTmp+=$(echo "{$j" | jq -r '.outputs[]' | grep -v "$wIssuers")$'\n'
done
IFS="$OIFS"
walletsSentToday=$(echo -e "$walletsSentTodayTmp" | head -n -1)
walletsReceivedToday=$(echo -e "$walletsReceivedTodayTmp" | head -n -1)
unset walletsReceivedTodayTmp walletsSentTodayTmp
# Exporte les valeurs de la journée dans le fichier JSON
jsonify() {
[[ -z $valueReceivedW ]] && valueReceivedW=0 || valueReceivedW=$(echo "scale=2; $valueReceivedW/100" | bc)
[[ -z $valueReceivedM ]] && valueReceivedM=0 || valueReceivedM=$(echo "scale=2; $valueReceivedM/100" | bc)
[[ -z $valueSentM ]] && valueSentM=0 || valueSentM=$(echo "scale=2; $valueSentM/100" | bc)
[[ -z $valueSentW ]] && valueSentW=0 || valueSentW=$(echo "scale=2; $valueSentW/100" | bc)
local jsonTPL=$(sed s/_DATE/$blockDateLast/g $SCRIPTPATH/tpl/recus.json)
local jsonTPL=$(sed s/_RWALLETS/$valueReceivedW/g <<< $jsonTPL)
local jsonTPL=$(sed s/_SWALLETS/$valueSentW/g <<< $jsonTPL)
local jsonTPL=$(sed s/_RMEMBRES/$valueReceivedM/g <<< $jsonTPL)
local jsonTPL=$(sed s/_SMEMBRES/$valueSentM/g <<< $jsonTPL)
echo -e "$jsonTPL" | tr -d '\\' >> $SCRIPTPATH/db/recus.json
}
# Si le date du bloc courant est différente du bloc précedent, alors on stock les valeurs journalières
if [[ -n $blockDateLast && $blockDateLast != $blockDate ]]; then
jsonify
unset valueReceivedM valueReceivedW
fi
# Sauvegarde la date de ce bloc pour l'itération suivante
blockDateLast=$blockDate
# Ajoute la valeur des transactions reçus de ce bloc au reste de la journée
for k in $walletsReceivedToday; do
local pubkey=$(echo $k | awk -F '(' '{ print $2 }' | tr -d ')')
local value=$(echo $k | awk -F: '{ print $1 }')
if [[ $(echo "$MEMBERS" | grep $pubkey) ]]; then
valueReceivedM=$(($valueReceivedM+$value))
pubkeyReceiveM+=${pubkey}\\n
else
valueReceivedW=$(($valueReceivedW+$value))
pubkeyReceiveW+=${pubkey}\\n
fi
done
# Ajoute la valeur des transactions envoyés de ce bloc au reste de la journée
for k in $walletsSentToday; do
local pubkey=$(echo $k | awk -F '(' '{ print $2 }' | tr -d ')')
local value=$(echo $k | awk -F: '{ print $1 }')
if [[ $(echo "$MEMBERS" | grep $pubkey) ]]; then
valueSentM=$(($valueSentM+$value))
pubkeySentM+=${pubkey}\\n
else
valueSentW=$(($valueSentW+$value))
pubkeySentW+=${pubkey}\\n
fi
done
# Stock les clés publiques de ce bloc dans la variable $wallets
wallets+=$(echo "$walletsReceivedToday" | grep -Eo $REGEX_PUBKEYS)$'\n'
# Affiche la progression de la boucle si on est pas en mode web
progress=$(echo "scale=1; $bloc*100/$lastBloc/1" | bc)
if [[ $isWeb != "web" ]]; then
clear
echo "Heure de début: $startTime"
echo
echo "Scan en cours: $progress% - $bloc/$lastBloc"
echo "Date: $blockDate"
fi
# Debug #
echo -e "i: $i\niter: $iter\nBloc: $bloc\n---" >> debug.log
((iter++))
# [[ $i -ge 10 ]] && break #kopaa
done
jsonify
# Retire la dernière virgule et ajoute le crochet de fin de JSON
sed -i '$ s/,//g' $SCRIPTPATH/db/recus.json
echo "]" >> $SCRIPTPATH/db/recus.json
# On supprime les doublons et les lignes vides
wallets=$(echo -e "$wallets" | sort -u | awk 'NF')$'\n'
# On écrit les pubkeys avec transaction dans un fichier de cache pour la prochaine itération
# ((iter--))
[[ ! -d $SCRIPTPATH/cache ]] && mkdir $SCRIPTPATH/cache
echo -e "$lastBloc\n$wallets" > $SCRIPTPATH/cache/walletsUp-$i
}
loopMembers() {
local iter=0
for i in ${MEMBERS[*]}; do
local progress=$(echo "scale=0; $iter*100/$nbrMembers/1" | bc)
if [[ $progress =~ ^(0|10|20|30|40|50|60|70|80|90|99)$ ]]; then
[[ $progress == 99 ]] && progress=100
if [[ $isWeb != "web" ]]; then
clear
echo "Heure de début: $startTime"
echo
echo "Scan en cours: 100% - $bloc/$lastBloc"
echo "Ajouts des comptes membres ... $progress%"
fi
fi
if [[ -z $(echo "$wallets" | grep "$i") ]]; then
wallets+=$'\n'"$i"
fi
((iter++))
done
}
web() {
[ ! -d $WEBPATH/history/ ] && mkdir -p $WEBPATH/history/
[ ! -d $WEBPATH/graph/ ] && mkdir -p $WEBPATH/graph/
[ ! -d $WEBPATH/css ] && cp -r $SCRIPTPATH/tpl/css $WEBPATH/
[ ! -d $WEBPATH/js ] && cp -r $SCRIPTPATH/tpl/js $WEBPATH/
cp $SCRIPTPATH/tpl/index.html $indexhtml
datePrevious=$(date +'%y-%m-%d' -d "$day -1 day")
[[ -z $(ls -l $WEBPATH/history/ | grep $datePrevious) && -z $(grep '"display:none;" class="previous"' $indexhtml) ]] && sed -i "s/class=\"previous\"/style=\"display:none;\" class=\"previous\"/g" $indexhtml
dateNext=$(date +'%y-%m-%d' -d "$day +1 day")
[[ -z $(ls -l $WEBPATH/history/ | grep $dateNext) && -z $(grep '"display:none;" class="next"' $indexhtml) ]] && sed -i "s/class=\"next\"/style=\"display:none;\" class=\"next\"/g" $indexhtml
sed -i "s/_nbrTotalWallets/$nbrTotalWallets/g" $indexhtml
sed -i "s/_nbrSimpleWallets/$nbrSimpleWallets/g" $indexhtml
sed -i "s/_nbrMembers/$nbrMembers/g" $indexhtml
sed -i "s/_pourcentMbrs/$pourcentMbrs/g" $indexhtml
sed -i "s/_pourcentWallets/$pourcentWallets/g" $indexhtml
# sed -i "s/_node/$DUNITER/g" $indexhtml
sed -i "s/_heure/$startTime/g" $indexhtml
sed -i "s/_day/$dayP/g" $indexhtml
sed -i "s/_txInSimple/$txInSimple/g" $indexhtml
sed -i "s/_txOutSimple/$txOutSimple/g" $indexhtml
sed -i "s/_soldeSimple/$soldeSimple/g" $indexhtml
sed -i "s/_txInMembers/$txInMembers/g" $indexhtml
sed -i "s/_txOutMembers/$txOutMembers/g" $indexhtml
sed -i "s/_soldeMembers/$soldeMembers/g" $indexhtml
sed -i "s/_pourcentSimpleWallet/$pourcentSimpleWallet/g" $indexhtml
sed -i "s/_nonConsumedUDT/$nonConsumedUDT/g" $indexhtml
sed -i "s/_monetaryMass/$monetaryMass/g" $indexhtml
sed -i "s/_sleepyG1/$sleepyG1/g" $indexhtml
[[ -z $(grep '"display:none;" class="previous"' $indexhtml) ]] && sed -i "s/_datePrevious/$datePrevious/g" $indexhtml && setPrevious="Oui"
[[ -z $(grep '"display:none;" class="next"' $indexhtml) ]] && sed -i "s/_dateNext/$dateNext/g" $indexhtml && setNext="Oui"
echo "$wallets" | grep . > $WEBPATH/wallets-g1.txt
echo -e "${MEMBERS[@]}" | sed 's/ /\n/g' > $WEBPATH/wallets-g1-membres.txt
echo -e "$simpleWallets" > $WEBPATH/wallets-g1-simple.txt
if [[ "$startTime" == "00:00" ]]; then
cp $indexhtml $WEBPATH/history/index_$day.html
sed -i "s/css\/style.css/..\/css\/style.css/g" $WEBPATH/history/index_$day.html
sed -i "s/logo-axiom-team2.svg/..\/logo-axiom-team2.svg/g" $WEBPATH/history/index_$day.html
sed -i "s/_dateNext/$day/g" $WEBPATH/history/index_$datePrevious.html
sed -i "s/style=\"display:none;\" class=\"next\"/class=\"next\"/g" $WEBPATH/history/index_$datePrevious.html
fi
# Export JSON for graph
$SCRIPTPATH/transform_json.sh
# chown www-data for nginx needs
chown -R www-data:www-data $WEBPATH
}
getSolde(){
pubkey=$@
solde=0
txInT=0
txOutT=0
nonConsumedUDT=0
nonConsumedUD=0
for i in $pubkeys; do
until txOutL=$(curl -s "$ESNODE/g1/movement/_search?filter_path=hits.hits._source&size=10000&q=issuer:$i&pretty"); do
echo "Erreur: $i"
sleep 1
done
if [[ $1 == "mbr" ]]; then
nonConsumedUD=$(curl -s ${DUNITER}/ud/history/$i | jq '.history.history[].amount' | awk '{s+=$1} END {print s}') || nonConsumedUD=0
[[ -z $nonConsumedUD ]] && nonConsumedUD=0
nonConsumedUDT=$(echo -e "scale=2; ($nonConsumedUD/100)+$nonConsumedUDT" | bc)
fi
[[ $txOutL != "{ }" ]] && txOut=$(echo "$txOutL" | jq '.hits.hits[]._source.amount' | awk '{s+=$1} END {print s}') || txOut=0
solde=$(echo -e "scale=2; (($txIn-$txOut+$nonConsumedUD)/100)+$solde" | bc)
txOutT=$(echo -e "scale=2; ($txOut/100)+$txOutT" | bc)
done
}
sumSoldes() {
sumWBrut=$(jq -r '.[].rWallets' $1 | awk '{ SUM += $1} END { printf "%.2f", SUM }')
sumMBrut=$(jq -r '.[].rMembres' $1 | awk '{ SUM += $1} END { printf "%.2f", SUM }')
sumTBrut=$(jq -r '.[] | .rWallets, .rMembres' $1 | awk '{ SUM += $1} END { printf "%.2f", SUM }')
sumW=$(echo $sumWBrut | sed ':a;s/\B[0-9]\{3\}\>/ &/;ta')
sumM=$(echo $sumMBrut | sed ':a;s/\B[0-9]\{3\}\>/ &/;ta')
sumT=$(echo $sumTBrut | sed ':a;s/\B[0-9]\{3\}\>/ &/;ta')
echo "Wallets: $sumW"
echo "Membres: $sumM"
echo "Total: $sumT"
}

View File

@ -9,17 +9,16 @@
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
if [[ -e $SCRIPTPATH/.env ]]; then source $SCRIPTPATH/.env; else echo "Veuillez créer votre fichier .env inspiré de .env.example" && exit 1; fi
source $SCRIPTPATH/functions.sh
### Initialisation des données ###
startTime=$(date +'%H:%M')
day=$(date +'%y-%m-%d')
dayP=$(date +'%d-%m-%y')
id=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
indexhtml="$WEBPATH/index.html"
echo -e "\n############# $day à $startTime #############\n"
[[ -z $(which jq) || -z $(which bc) ]] && apt update && apt install jq bc
echo "Initialisation ..."
outFile="/tmp/g1-stats-$day-$startTime_$id"
#TXBLOCKS=$(cat $SCRIPTPATH/db/txblocs)
TXBLOCKS=$(curl -s ${DUNITER}/blockchain/with/tx | jq '.result.blocks[]')
TXBLOCKS=($(echo "$TXBLOCKS" | sort -hu | awk '{printf $1" "}'))
@ -30,239 +29,26 @@ lastBloc=${TXBLOCKS[-1]}
isWeb=$1
### Extraction des adresses Ḡ1 actives ###
loopWalletUp() {
local REGEX_PUBKEYS="[a-zA-Z0-9]{42,44}"
# On vérifie le cache existant et on démarre au bloc dernier bloc en cache
if ls $SCRIPTPATH/cache/walletsUp-* > /dev/null 2>&1; then
local startFile=$(ls $SCRIPTPATH/cache/walletsUp-* | tail -n1)
local startIter=$(echo "$startFile" | awk -F '-' '{ print $NF }')
local startBloc=$(head -n1 "$startFile")
[[ $lastBloc == $startBloc ]] && return 10
wallets=$(tail -n +2 "$startFile")$'\n'
local iter=$startIter
rm $startFile
else
local startIter=0
local startBloc=0
local iter=0
echo "[" > $SCRIPTPATH/db/recus.json
fi
rm -f debug.log #kopa
for i in ${!TXBLOCKS[*]}; do
[[ -z ${TXBLOCKS[$i]} ]] && break
[[ $startIter != 0 && $i -le $startIter ]] && continue
sleep 0.05
# Récupère la date et l'objet transaction du bloc courant
WALLETS0=$(curl -s ${DUNITER}/blockchain/block/${TXBLOCKS[$i]})
until WALLETS=$(echo $WALLETS0 | jq -r '.medianTime, .transactions[]' 2>/dev/null); do
[[ $isWeb != "web" ]] && echo -e "iter $iter \n$WALLETS0" | tee -a debug.log
sleep 2
WALLETS0=$(curl -s ${DUNITER}/blockchain/block/${TXBLOCKS[$i]})
done
[[ -z "$WALLETS" ]] && continue
bloc=${TXBLOCKS[$i]}
# Récupération de la date du block
blockDate=$(echo "$WALLETS" | head -n1)
blockDate=$(date -d"@$blockDate" +%y-%m-%d -u)
WALLETS=$(echo "$WALLETS" | tail -n +2)
# Si cette itération proviens d'un cache, alors on aggrège la dernière date
if [[ $startBloc != 0 ]]; then
lastDate=$(cat db/recus.json | jq -r '.[].date' | tail -n1)
if [[ "$blockDate" == "$lastDate" ]]; then
valueBlocW=$(cat db/recus.json | jq -r '.[].rWallets' | tail -n1 | tr -d '.,')
valueBlocM=$(cat db/recus.json | jq -r '.[].rMembres' | tail -n1 | tr -d '.,')
local recusJson=$(head -n -6 db/recus.json)
echo "$recusJson" > $SCRIPTPATH/db/recus.json
else
local recusJson=$(head -n -2 db/recus.json)
echo -e "$recusJson\n }," > $SCRIPTPATH/db/recus.json
fi
unset recusJson
startBloc=0
fi
# Sélectionne les ligne SIG en retirant les issuers
OIFS="$IFS"
IFS='{'
for j in $WALLETS; do
[[ -z $j ]] && continue
wIssuers=$(echo "{$j" | jq -r '.issuers[0]')
WALLETS1+=$(echo "{$j" | jq -r '.outputs[]' | grep -v "$wIssuers")
WALLETS1+="\n"
done
IFS="$OIFS"
WALLETS=$(echo -e "$WALLETS1" | head -n -1)
unset WALLETS1
# Exporte les valeurs de la journée dans le fichier JSON
jsonify() {
[[ -z $valueBlocW ]] && valueBlocW=0 || valueBlocW=$(echo "scale=2; $valueBlocW/100" | bc)
[[ -z $valueBlocM ]] && valueBlocM=0 || valueBlocM=$(echo "scale=2; $valueBlocM/100" | bc)
local jsonTPL=$(sed s/_DATE/$blockDateLast/g $SCRIPTPATH/tpl/recus.json)
local jsonTPL=$(sed s/_RWALLETS/$valueBlocW/g <<< $jsonTPL)
local jsonTPL=$(sed s/_RMEMBRES/$valueBlocM/g <<< $jsonTPL)
echo -e "$jsonTPL" | tr -d '\\' >> $SCRIPTPATH/db/recus.json
}
# Si le date du bloc courant est différente du bloc précedent, alors on stock les valeurs journalières
if [[ -n $blockDateLast && $blockDateLast != $blockDate ]]; then
jsonify
unset valueBlocM valueBlocW
fi
# Sauvegarde la date de ce bloc pour l'itération suivante
blockDateLast=$blockDate
# Ajoute la valeur des transactions de ce bloc au reste de la journée
for k in $WALLETS; do
pubkey=$(echo $k | awk -F '(' '{ print $2 }' | tr -d ')')
value=$(echo $k | awk -F: '{ print $1 }')
if [[ $(echo "$MEMBERS" | grep $pubkey) ]]; then
valueBlocM=$(($valueBlocM+$value))
pubkeyBlocM+=${pubkey}\\n
else
valueBlocW=$(($valueBlocW+$value))
pubkeyBlocW+=${pubkey}\\n
fi
done
# Stock les clés publiques de ce bloc dans la variable $wallets
wallets+=$(echo "$WALLETS" | grep -Eo $REGEX_PUBKEYS)$'\n'
# Affiche la progression de la boucle si on est pas en mode web
progress=$(echo "scale=1; $bloc*100/$lastBloc/1" | bc)
if [[ $isWeb != "web" ]]; then
clear
echo "Heure de début: $startTime"
echo
echo "Scan en cours: $progress% - $bloc/$lastBloc"
echo "Date: $blockDate"
fi
# Debug #
echo -e "i: $i\niter: $iter\nBloc: $bloc\n---" >> debug.log
((iter++))
# [[ $i -ge 10 ]] && break #kopaa
done
jsonify
# Retire la dernière virgule et ajoute le crochet de fin de JSON
sed -i '$ s/,//g' $SCRIPTPATH/db/recus.json
echo "]" >> $SCRIPTPATH/db/recus.json
# On supprime les doublons et les lignes vides
wallets=$(echo -e "$wallets" | sort -u | awk 'NF')$'\n'
# On écrit les pubkeys avec transaction dans un fichier de cache pour la prochaine itération
# ((iter--))
[[ ! -d $SCRIPTPATH/cache ]] && mkdir $SCRIPTPATH/cache
echo -e "$lastBloc\n$wallets" > $SCRIPTPATH/cache/walletsUp-$i
}
loopWalletUp
$SCRIPTPATH/sum.sh
### Calcul la somme des soldes portefeuilles et membres ###
sumSoldes db/recus.json
endDate=$(date +'%H:%M')
echo "Heure de fin: $endDate"
exit
### Ajout des membres sans transaction au fichier tampon ###
loopMembers() {
local iter=0
for i in ${MEMBERS[*]}; do
progress=$(echo "scale=0; $iter*100/$nbrMembers/1" | bc)
if [[ $progress =~ ^(0|10|20|30|40|50|60|70|80|90|99)$ ]]; then
[[ $progress == 99 ]] && progress=100
if [[ $isWeb != "web" ]]; then
clear
echo "Heure de début: $startTime"
echo
echo "Scan en cours: 100% - $bloc/$lastBloc"
echo "Ajouts des comptes membres ... $progress%"
fi
fi
if [[ -z $(grep "$i" $outFile) ]]; then
echo -e "$i" >> $outFile
fi
((iter++))
done
}
loopMembers
### Calcul du nombre de wallets ###
nbrTotalWallets=$(cat $outFile | wc -l)
nbrTotalWallets=$(echo "$wallets" | wc -l)
nbrSimpleWallets=$(echo "$nbrTotalWallets-$nbrMembers" | bc)
pourcentMbrs=$(echo "scale=1; $nbrMembers*100/$nbrTotalWallets/1" | bc)
pourcentWallets=$(echo "scale=1; $nbrSimpleWallets*100/$nbrTotalWallets/1" | bc)
### Renseignement de l'index web et indexation de l'historique ###
web() {
[ ! -d $WEBPATH/history/ ] && mkdir -p $WEBPATH/history/
[ ! -d $WEBPATH/graph/ ] && mkdir -p $WEBPATH/graph/
[ ! -d $WEBPATH/css ] && cp -r $SCRIPTPATH/tpl/css $WEBPATH/
[ ! -d $WEBPATH/js ] && cp -r $SCRIPTPATH/tpl/js $WEBPATH/
cp $SCRIPTPATH/tpl/index.html $indexhtml
datePrevious=$(date +'%y-%m-%d' -d "$day -1 day")
[[ -z $(ls -l $WEBPATH/history/ | grep $datePrevious) && -z $(grep '"display:none;" class="previous"' $indexhtml) ]] && sed -i "s/class=\"previous\"/style=\"display:none;\" class=\"previous\"/g" $indexhtml
dateNext=$(date +'%y-%m-%d' -d "$day +1 day")
[[ -z $(ls -l $WEBPATH/history/ | grep $dateNext) && -z $(grep '"display:none;" class="next"' $indexhtml) ]] && sed -i "s/class=\"next\"/style=\"display:none;\" class=\"next\"/g" $indexhtml
sed -i "s/_nbrTotalWallets/$nbrTotalWallets/g" $indexhtml
sed -i "s/_nbrSimpleWallets/$nbrSimpleWallets/g" $indexhtml
sed -i "s/_nbrMembers/$nbrMembers/g" $indexhtml
sed -i "s/_pourcentMbrs/$pourcentMbrs/g" $indexhtml
sed -i "s/_pourcentWallets/$pourcentWallets/g" $indexhtml
# sed -i "s/_node/$DUNITER/g" $indexhtml
sed -i "s/_heure/$startTime/g" $indexhtml
sed -i "s/_day/$dayP/g" $indexhtml
sed -i "s/_txInSimple/$txInSimple/g" $indexhtml
sed -i "s/_txOutSimple/$txOutSimple/g" $indexhtml
sed -i "s/_soldeSimple/$soldeSimple/g" $indexhtml
sed -i "s/_txInMembers/$txInMembers/g" $indexhtml
sed -i "s/_txOutMembers/$txOutMembers/g" $indexhtml
sed -i "s/_soldeMembers/$soldeMembers/g" $indexhtml
sed -i "s/_pourcentSimpleWallet/$pourcentSimpleWallet/g" $indexhtml
sed -i "s/_nonConsumedUDT/$nonConsumedUDT/g" $indexhtml
sed -i "s/_monetaryMass/$monetaryMass/g" $indexhtml
sed -i "s/_sleepyG1/$sleepyG1/g" $indexhtml
[[ -z $(grep '"display:none;" class="previous"' $indexhtml) ]] && sed -i "s/_datePrevious/$datePrevious/g" $indexhtml && setPrevious="Oui"
[[ -z $(grep '"display:none;" class="next"' $indexhtml) ]] && sed -i "s/_dateNext/$dateNext/g" $indexhtml && setNext="Oui"
cat "$outFile" | grep . > $WEBPATH/wallets-g1.txt
echo -e "${MEMBERS[@]}" | sed 's/ /\n/g' > $WEBPATH/wallets-g1-membres.txt
echo -e "$simpleWallets" > $WEBPATH/wallets-g1-simple.txt
if [[ "$startTime" == "00:00" ]]; then
cp $indexhtml $WEBPATH/history/index_$day.html
sed -i "s/css\/style.css/..\/css\/style.css/g" $WEBPATH/history/index_$day.html
sed -i "s/logo-axiom-team2.svg/..\/logo-axiom-team2.svg/g" $WEBPATH/history/index_$day.html
sed -i "s/_dateNext/$day/g" $WEBPATH/history/index_$datePrevious.html
sed -i "s/style=\"display:none;\" class=\"next\"/class=\"next\"/g" $WEBPATH/history/index_$datePrevious.html
fi
# Export JSON for graph
$SCRIPTPATH/transform_json.sh
# chown www-data for nginx needs
chown -R www-data:www-data $WEBPATH
}
### Affichage du nombre de wallets ###
echo -e "\n ---\n"
echo "Noeud: $DUNITER"
@ -274,50 +60,16 @@ echo "Extraction wallets membres / Simples portefeuille"
echo -e "\n ---\n"
### Isolation des simples portefeuilles ###
simpleWallets=$(cat $outFile)
simpleWallets="$wallets"
echo "Isolation des simples portefeuilles..."
for i in ${MEMBERS[@]}; do
simpleWallets=$(echo "$simpleWallets" | grep -v "$i")
done
### Boucle d'obtention des soldes ###
getSolde(){
solde=0
txInT=0
txOutT=0
nonConsumedUDT=0
nonConsumedUD=0
for i in $pubkeys; do
until txInL=$(curl -s "$ESNODE/g1/movement/_search?filter_path=hits.hits._source&size=10000&q=recipient:$i&pretty"); do
echo "Erreur: $i"
sleep 2
done
until txOutL=$(curl -s "$ESNODE/g1/movement/_search?filter_path=hits.hits._source&size=10000&q=issuer:$i&pretty"); do
echo "Erreur: $i"
sleep 2
done
##TODO: Get Solde from JSON Sum for next ... kopa
if [[ $1 == "mbr" ]]; then
nonConsumedUD=$(curl -s ${DUNITER}/ud/history/$i | jq -r '.history.history[].amount' | awk '{s+=$1} END {print s}') || nonConsumedUD=0
[[ -z $nonConsumedUD ]] && nonConsumedUD=0
nonConsumedUDT=$(echo -e "scale=2; ($nonConsumedUD/100)+$nonConsumedUDT" | bc)
fi
getSolde $simpleWallets
[[ $txInL != "{ }" ]] && txIn=$(echo "$txInL" | jq '.hits.hits[]._source.amount' | awk '{s+=$1} END {print s}') || txIn=0
[[ $txOutL != "{ }" ]] && txOut=$(echo "$txOutL" | jq '.hits.hits[]._source.amount' | awk '{s+=$1} END {print s}') || txOut=0
solde=$(echo -e "scale=2; (($txIn-$txOut+$nonConsumedUD)/100)+$solde" | bc)
txInT=$(echo -e "scale=2; (($txIn+$nonConsumedUD)/100)+$txInT" | bc)
txOutT=$(echo -e "scale=2; ($txOut/100)+$txOutT" | bc)
done
}
echo "Récupération du solde des simples wallets..."
pubkeys=$simpleWallets
getSolde
txInSimple=$(echo $txInT | tr . , | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')
txOutSimple=$(echo $txOutT | tr . , | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')
soldeSimpleBrut=$(echo $solde)
@ -328,8 +80,8 @@ echo -e "Soldes simples wallets:\t $soldeSimple"
echo -e "\n ---\n"
echo "Récupération du solde des membres..."
pubkeys=${MEMBERS[@]}
getSolde
getSolde ${MEMBERS[@]}
txInMembers=$(echo $txInT | tr . , | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')
txOutMembers=$(echo $txOutT | tr . , | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')
soldeMembersBrut=$(echo $solde)
@ -350,18 +102,14 @@ echo -e "\n ---\n"
echo -e "Solde des membres (sans DU):\t $soldeWalletMembers"
echo -e "Masse Monétaire:\t $monetaryMass Ḡ1"
### Renseignement de l'index web et indexation de l'historique ###
[[ $isWeb == "web" ]] && web
# Analyse
echo -e "\n ---\n Analyse\n ---\n"
sleepyG1=$(echo -e "scale=1; 100-$txOutT*100/$monetaryMassBrut" | bc | tr . ,)
echo -e "$sleepyG1% des Ḡ1 n'ont jamais été utilisés."
[[ $isWeb == "web" ]] && web
### Fin de programme ###
rm $outFile
day=$(date +'%d-%m-%y')
echo "$day - Heure de fin: $(date +'%H:%M')"
echo "Début de la journalisation ..."
$SCRIPTPATH/soldeByDays.sh

8
restore-bk.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
cacheFile=$(ls cache/bk-walletsUp-*)
cacheID=$(echo $cacheFile | awk -F '-' '{ print $NF }')
cp $cacheFile cache/walletsUp-$cacheID
cp db/bk-recus.json db/recus.json

View File

@ -1,80 +0,0 @@
#!/bin/bash
startTime=$(date +'%H:%M')
day=$(date +'%y-%m-%d')
echo -e "\n############# $day à $startTime #############\n"
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
if [[ -e $SCRIPTPATH/.env ]]; then source $SCRIPTPATH/.env; else echo "Veuillez créer votre fichier .env inspiré de .env.example" && exit 1; fi
rm $SCRIPTPATH/tmp/*_solde 2>/dev/null
! [[ -d $SCRIPTPATH/db ]] && mkdir $SCRIPTPATH/db
echo "[" > $SCRIPTPATH/db/recus.json
lsDays=$(ls $SCRIPTPATH/tmp/) # | awk -F/ '{ print $NF }') # | awk -F_ '{ print $1 }')
nbrD=$(for i in $lsDays; do cat $SCRIPTPATH/tmp/$i; done | wc -l)
daysLoop() {
k=0
l=0
for i in $lsDays; do
# [[ $l -ge 20 ]] && break ## ((k++)) && continue
# ((l++))
walletDay=$(cat $SCRIPTPATH/tmp/$i)
[[ -z $walletDay ]] && continue
dateT=$(echo $i | awk -F_ '{ print $1 }')
dateLDay=$(date -d "$dateT" +%s -u)
dateLDay=$(($dateLDay-3600*2))
dateHDay=$(($dateLDay+60*60*24))
dateHDay=$(($dateHDay+3600*2))
[[ $i =~ "_mbr" ]] && type="Membre" || type="Wallet"
for j in $walletDay; do
echo "$(echo $i | awk -F_ '{ print $1 }'): $j"
curl -s ${DUNITER}/tx/history/$j/times/$dateLDay/$dateHDay | jq '.history.received[].outputs[]' | grep $j | awk -F: '{ print $1 }' | tr -d '" ' >> $SCRIPTPATH/tmp/${i}_solde
((k++))
clear
echo -e "\n############# $day à $startTime #############\n"
progress=$(echo "scale=1; $k*100/$nbrD/1" | bc)
echo "Scan $type: $progress% - $k/$nbrD"
done
# Somme de la journée
soldes=$(cat $SCRIPTPATH/tmp/${i}_solde | awk '{total+=$1}END{print total}')
echo $soldes > $SCRIPTPATH/tmp/${i}_solde
# Écriture en JSON
jsonify() {
local jsonTPL=$(sed s/_DATE/$dateT/g $SCRIPTPATH/tpl/recus.json)
local jsonTPL=$(sed s/_RWALLETS/$1/g <<< $jsonTPL)
local jsonTPL=$(sed s/_RMEMBRES/$2/g <<< $jsonTPL)
echo -e "$jsonTPL" | tr -d '\\' >> $SCRIPTPATH/db/recus.json
}
soldes=$(echo "scale=2; $soldes/100" | bc)
if [[ $i =~ "_mbr" ]]; then
jsonify _RWALLETS_$dateT $soldes
elif [[ $i =~ "_wallets" ]]; then
if [[ ! $(grep $dateT $SCRIPTPATH/db/recus.json) ]]; then
jsonify $soldes 0
else
sed -i s/_RWALLETS_$dateT/$soldes/g $SCRIPTPATH/db/recus.json
fi
fi
done
}
daysLoop
# Final touch
sed -i '$ s/,//g' $SCRIPTPATH/db/recus.json
echo "]" >> $SCRIPTPATH/db/recus.json
awk '{gsub(/_RWALLETS.*/,"0\",")}1;' $SCRIPTPATH/db/recus.json > /tmp/tmpawk && mv /tmp/tmpawk $SCRIPTPATH/db/recus.json
day=$(date +'%d-%m-%y')
echo "$day - Heure de fin: $(date +'%H:%M')"
$SCRIPTPATH/sum.sh

26
sum.sh
View File

@ -1,26 +0,0 @@
#!/bin/bash
old() {
echo "Wallets: $(echo $(($(cat tmp/*wallets_solde | awk '{total+=$1}END{print total}')/100)) | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')"
echo "Membres: $(echo $(($(cat tmp/*mbr_solde | awk '{total+=$1}END{print total}')/100)) | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')"
echo "Total: $(echo $(($(cat tmp/*_solde | awk '{total+=$1}END{print total}')/100)) | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')"
echo "Fichiers vides:"
for i in $(ls tmp/*_solde); do ! [[ $(cat $i) ]] && echo $i ; done
}
json() {
sumW=$(jq -r '.[].rWallets' $1 | awk '{ SUM += $1} END { printf "%.2f", SUM }' | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
sumM=$(jq -r '.[].rMembres' $1 | awk '{ SUM += $1} END { printf "%.2f", SUM }' | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
sumT=$(jq -r '.[] | .rWallets, .rMembres' $1 | awk '{ SUM += $1} END { printf "%.2f", SUM }' | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta')
echo "Wallets: $sumW"
echo "Membres: $sumM"
echo "Total: $sumT"
# echo "Fichiers vides:"
# for i in $(ls tmp/*_solde); do ! [[ $(cat $i) ]] && echo $i ; done
}
json db/recus.json
#json db/recus.json.bk

View File

@ -1 +1 @@
{\n \"date\": \"_DATE\",\n \"rWallets\": _RWALLETS,\n \"rMembres\": _RMEMBRES\n },
{\n \"date\": \"_DATE\",\n \"rWallets\": _RWALLETS,\n \"sWallets\": _SWALLETS,\n \"rMembres\": _RMEMBRES,\n \"sMembres\": _SMEMBRES\n },