diff --git a/.gitignore b/.gitignore index dd01378..8e1e0e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .env renameHistory.sh -tmp* db/* +cache diff --git a/g1-stats.sh b/g1-stats.sh index 05778ca..ed32415 100755 --- a/g1-stats.sh +++ b/g1-stats.sh @@ -31,13 +31,33 @@ isWeb=$1 ### Extraction des adresses Ḡ1 actives ### -echo "[" > $SCRIPTPATH/db/recus.json loopWalletUp() { - for i in ${TXBLOCKS[*]}; do + 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 + + for i in ${TXBLOCKS[*]:$startIter}; do [[ -z $i ]] && break sleep 0.05 + + # Récupère la date et l'objet transaction du bloc courant WALLETS0=$(curl -s ${DUNITER}/blockchain/block/$i) - until WALLETS=$(echo $WALLETS0 | jq -r '.medianTime,(.transactions[] | select(.issuers[0] as $issuers | .outputs[0]|contains($issuers)|not) | .outputs[0])' 2>/dev/null); do + until WALLETS=$(echo $WALLETS0 | jq -r '.medianTime, .transactions[]' 2>/dev/null); do [[ $isWeb != "web" ]] && echo -e "iter $iter \n$WALLETS0" sleep 2 WALLETS0=$(curl -s ${DUNITER}/blockchain/block/$i) @@ -50,6 +70,37 @@ loopWalletUp() { 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) @@ -59,16 +110,19 @@ loopWalletUp() { 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 - for i in $WALLETS; do - pubkey=$(echo $i | awk -F '(' '{ print $2 }' | tr -d ')') - value=$(echo $i | awk -F: '{ print $1 }') + # 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)) @@ -79,6 +133,10 @@ loopWalletUp() { 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 @@ -90,43 +148,32 @@ loopWalletUp() { ((iter++)) +# [[ $iter -ge 10 ]] && break #kopa 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-$iter } loopWalletUp $SCRIPTPATH/sum.sh + exit -### Isolation des membres et wallets dans les fichiers de journalisation -lsDays=$(ls $SCRIPTPATH/tmp) -for i in $lsDays; do - walletDay=$(cat $SCRIPTPATH/tmp/$i | sort -u | tee $SCRIPTPATH/tmp/$i) - for j in $walletDay; do - if [[ $(printf '%s\n' ${MEMBERS[*]} | grep $j) ]]; then - echo $j >> $SCRIPTPATH/tmp/${i}_mbr - sed -i "/$j/d" $SCRIPTPATH/tmp/$i - fi - done - mv $SCRIPTPATH/tmp/$i $SCRIPTPATH/tmp/${i}_wallets -done - -### TODO -# dateLDay=$(date -d "19-05-24" +%s) -# dateLDay=$(($dateLDay-60*60*24)) -# curl -s ${DUNITER}/tx/history/TENGx7WtzFsTXwnbrPEvb6odX2WnqYcnnrjiiLvp1mS/times/1558562400/1558648800 - - - -### Ecriture des adresses actives dans un fichier tampon ### -echo -e "$(echo -e "$wallets" | sort -u)" > $outFile ### Ajout des membres sans transaction au fichier tampon ### loopMembers() { -iter=0 +local iter=0 for i in ${MEMBERS[*]}; do progress=$(echo "scale=0; $iter*100/$nbrMembers/1" | bc)