g1-stats/nbr-wallets.sh

204 lines
6.4 KiB
Bash
Raw Normal View History

2019-10-29 04:55:50 +01:00
#!/bin/bash
startTime=$(date +'%T')
day=$(date +'%d-%m-%y')
id=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
de=$(cat /dev/urandom | tr -dc '0-9' | fold -w 1 | head -n 1)
2019-10-29 04:55:50 +01:00
### Variable ###
DUNITER="http://192.168.9.54:45000"
DUNITER2="https://g1.duniter.org"
DUNITER3="https://duniter.g1.1000i100.fr"
indexhtml="/var/www/g1-stats/index.html"
2019-10-29 04:55:50 +01:00
###
echo "######"
echo "$day à startTime"
2019-10-29 05:29:10 +01:00
[[ -z $(which jq) || -z $(which bc) ]] && apt update && apt install jq bc
2019-10-29 04:55:50 +01:00
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]'
2019-10-29 04:55:50 +01:00
MEMBERS=($(curl -s ${DUNITER}/wot/members | jq '.results[].pubkey' | tr -d '"'))
nbrMembers=${#MEMBERS[@]}
lastBloc=${TXBLOCKS[-1]}
#nbrBlocs=${#TXBLOCKS[@]}
2019-10-29 04:55:50 +01:00
loopWalletUp() {
for i in ${TXBLOCKS[*]}; do
2019-10-29 04:55:50 +01:00
[[ -z ${TXBLOCKS[$i]} ]] && break
sleep 0.1
WALLETS=$(curl -s ${DUNITER2}/blockchain/block/${TXBLOCKS[$i]} | jq '.transactions[].outputs[]' | tr -d '"' | awk -F '(' '{ print $2 }' | tr -d ')' || exit)
bloc=${TXBLOCKS[$i]}
[[ $bloc == "153777" ]] && echo "en plein debug ... .." && sleep 10
wallets+=$(echo -e "$WALLETS \n" | sed 's/ /\\n/g')
2019-10-29 04:55:50 +01:00
progress=$(echo "scale=1; $bloc*100/$lastBloc/1" | bc)
2019-10-29 04:55:50 +01:00
if [[ $1 != "web" ]]; then
clear
echo "Heure de début: $startTime"
echo
echo "Scan en cours: $progress% - $bloc/$lastBloc"
fi
2019-10-29 04:55:50 +01:00
((iter++))
done
}
loopWalletUp
2019-10-29 04:55:50 +01:00
echo -e "$(echo -e "$wallets" | sort -u)" > $outFile
#cat /var/www/g1-stats/wallets-g1.txt > $outFile
loopMembers() {
2019-10-29 04:55:50 +01:00
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 [[ $1 != "web" ]]; then
clear
echo "Heure de début: $startTime"
echo
echo "Scan en cours: 100% - $bloc/$lastBloc"
echo "Ajouts des comptes membres ... $progress%"
fi
2019-10-29 04:55:50 +01:00
fi
if [[ -z $(grep "$i" $outFile) ]]; then
echo -e "$i" >> $outFile
fi
((iter++))
done
}
loopMembers
2019-10-29 04:55:50 +01:00
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)
2019-10-29 05:18:10 +01:00
web() {
cp /opt/g1-stats/stats.html $indexhtml
2019-10-29 05:18:10 +01:00
sed -i "s/_nbrTotalWallets/$nbrTotalWallets/g" $indexhtml
sed -i "s/_nbrSimpleWallets/$nbrSimpleWallets/g" $indexhtml
sed -i "s/_nbrMembers/$nbrMembers/g" $indexhtml
2019-10-29 05:18:10 +01:00
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/$day/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/_poucentSoldeMembers/$poucentSoldeMembers/g" $indexhtml
sed -i "s/_nonConsumedUDT/$nonConsumedUDT/g" $indexhtml
sed -i "s/_monetaryMass/$monetaryMass/g" $indexhtml
2019-10-29 05:18:10 +01:00
chown www-data:www-data $indexhtml 2>/dev/null
}
echo -e "\n ---\n"
echo "Noeud: $DUNITER"
2019-10-29 04:55:50 +01:00
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"
cat "$outFile" | grep . > /var/www/g1-stats/wallets-g1.txt
echo -e "${MEMBERS[@]}" | sed 's/ /\n/g' > /var/www/g1-stats/wallets-g1-membres.txt
simpleWallets=$(cat $outFile)
echo "Isolation des simples portefeuilles..."
for i in ${MEMBERS[@]}; do
simpleWallets=$(echo "$simpleWallets" | grep -v "$i")
done
echo -e "$simpleWallets" > /var/www/g1-stats/wallets-g1-simple.txt
echo -e "\n ---\n"
getSolde(){
solde=0
txInT=0
txOutT=0
nonConsumedUDT=0
nonConsumedUD=0
for i in $pubkeys; do
txInL=$(curl -s "http://g1.data.duniter.fr/g1/movement/_search?filter_path=hits.hits._source&size=10000&q=recipient:$i&pretty")
txOutL=$(curl -s "http://g1.data.duniter.fr/g1/movement/_search?filter_path=hits.hits._source&size=10000&q=issuer:$i&pretty")
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=$txInT
txOutSimple=$txOutT
soldeSimple=$solde
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=$txInT
txOutMembers=$txOutT
soldeMembers=$solde
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"
monetaryMass=$(curl -s ${DUNITER}/blockchain/current | jq .monetaryMass)
monetaryMass=$(echo -e "scale=2; ($monetaryMass/100)/1" | bc)
soldeWalletMembers=$(echo "scale=2; $soldeMembers+$soldeSimple" | bc)
poucentSoldeMembers=$(echo "scale=1; $soldeMembers*100/$monetaryMass/1" | bc)
echo -e "\n ---\n"
echo -e "Solde des membres (sans DU):\t $soldeWalletMembers"
echo -e "Masse Monétaire:\t $monetaryMass Ḡ1"
[[ $1 == "web" ]] && web
2019-10-29 04:55:50 +01:00
rm $outFile
echo "$day - Heure de fin: $(date +'%T')"