forked from axiom-team/g1-stats
284 lines
11 KiB
Bash
Executable File
284 lines
11 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
################################################################################
|
|
# Author: Poka (poka@p2p.legal)
|
|
# Version: 0.2.0
|
|
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
|
|
# Git: https://git.p2p.legal/axiom-team/g1-stats
|
|
################################################################################
|
|
|
|
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
|
|
|
|
### 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=$(curl -s ${DUNITER}/blockchain/with/tx | jq '.result.blocks[]')
|
|
#TXBLOCKS+=$(curl -s ${DUNITER2}/blockchain/with/tx | jq '.result.blocks[]')
|
|
#TXBLOCKS+=$(curl -s ${DUNITER3}/blockchain/with/tx | jq '.result.blocks[]')
|
|
TXBLOCKS=($(echo "$TXBLOCKS" | sort -hu | awk '{printf $1" "}'))
|
|
unset 'TXBLOCKS[${#TXBLOCKS[@]}-1]'
|
|
MEMBERS=($(curl -s ${DUNITER}/wot/members | jq '.results[].pubkey' | tr -d '"'))
|
|
nbrMembers=${#MEMBERS[@]}
|
|
lastBloc=${TXBLOCKS[-1]}
|
|
isWeb=$1
|
|
|
|
### Extraction des adresses Ḡ1 actives ###
|
|
|
|
loopWalletUp() {
|
|
for i in ${TXBLOCKS[*]}; do
|
|
[[ -z ${TXBLOCKS[$i]} ]] && break
|
|
sleep 0.1
|
|
WALLETS0=$(curl -s ${DUNITER}/blockchain/block/${TXBLOCKS[$i]})
|
|
until WALLETS=$(echo $WALLETS0 | jq '.time,(.transactions[] | select(.issuers[0] as $issuers | .outputs[0]|contains($issuers)|not) | .outputs[0]')) 2>/dev/null; do
|
|
[[ $isWeb != "web" ]] && echo -e "iter $iter \n$WALLETS0"
|
|
sleep 2
|
|
WALLETS0=$(curl -s ${DUNITER}/blockchain/block/${TXBLOCKS[$i]})
|
|
done
|
|
[[ -z "$WALLETS" ]] && continue
|
|
|
|
# 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 | tr -d '"' | awk -F '(' '{ print $2 }' | tr -d ')')
|
|
|
|
# Journalisation
|
|
echo -e "$WALLETS \n" | sed 's/ /\n/g' | sed '/^[[:space:]]*$/d' | sort -u >> $SCRIPTPATH/tmp/$blockDate
|
|
|
|
bloc=${TXBLOCKS[$i]}
|
|
wallets+=$(echo -e "$WALLETS \n" | sed 's/ /\\n/g')
|
|
|
|
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
|
|
|
|
((iter++))
|
|
|
|
done
|
|
}
|
|
loopWalletUp
|
|
|
|
### 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
|
|
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)
|
|
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"
|
|
echo "Nombre total de wallet: $nbrTotalWallets"
|
|
echo "Nombre de membres: $nbrMembers (${pourcentMbrs}%)"
|
|
echo "Nombre de simple portefeuille: $nbrSimpleWallets (${pourcentWallets}%)"
|
|
echo -e "\n ---\n"
|
|
echo "Extraction wallets membres / Simples portefeuille"
|
|
echo -e "\n ---\n"
|
|
|
|
### Isolation des simples portefeuilles ###
|
|
simpleWallets=$(cat $outFile)
|
|
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
|
|
|
|
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
|
|
|
|
[[ $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)
|
|
soldeSimple=$(echo $soldeSimpleBrut | tr . , | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')
|
|
echo -e "Reçus simples wallets:\t $txInSimple"
|
|
echo -e "Envoyé simples wallets:\t $txOutSimple"
|
|
echo -e "Soldes simples wallets:\t $soldeSimple"
|
|
echo -e "\n ---\n"
|
|
echo "Récupération du solde des membres..."
|
|
|
|
pubkeys=${MEMBERS[@]}
|
|
getSolde
|
|
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)
|
|
soldeMembers=$(echo $soldeMembersBrut | tr . , | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')
|
|
echo -e "Reçus membres:\t $txInMembers"
|
|
echo -e "Envoyé membres:\t $txOutMembers"
|
|
echo -e "Soldes membres:\t $soldeMembers"
|
|
echo -e "Total DU non consumés:\t $nonConsumedUDT"
|
|
|
|
### Calcul de la masse monétaire et du pourcentage de Ḡ1 sur les simples portefeuilles ###
|
|
monetaryMass=$(curl -s ${DUNITER}/blockchain/current | jq .monetaryMass)
|
|
monetaryMassBrut=$(echo -e "scale=2; ($monetaryMass/100)/1" | bc)
|
|
monetaryMass=$(echo "$monetaryMassBrut" | tr . , | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')
|
|
soldeWalletMembers=$(echo "scale=2; $soldeMembersBrut+$soldeSimpleBrut" | bc | tr . , | sed ':a;s/\B[0-9]\{3\}\>/.&/;ta')
|
|
pourcentSimpleWallet=$(echo "scale=1; $soldeSimpleBrut*100/$monetaryMassBrut/1" | bc)
|
|
|
|
echo -e "\n ---\n"
|
|
echo -e "Solde des membres (sans DU):\t $soldeWalletMembers"
|
|
echo -e "Masse Monétaire:\t $monetaryMass Ḡ1"
|
|
|
|
|
|
# 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')"
|