g1-stats/explorer.sh

150 lines
4.7 KiB
Bash
Raw Permalink Normal View History

2020-09-19 04:01:10 +02:00
#!/bin/bash
2020-10-03 05:20:34 +02:00
################################################################################
# Author: Poka (poka@p2p.legal)
# Version: 0.0.3
2020-10-03 05:20:34 +02:00
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
# Git: https://git.p2p.legal/axiom-team/g1-stats
################################################################################
###
# Please change this path if needed, in relative path from explorer.sh script
# If no file found, then this script will download it from https://g1-stats.axiom-team.fr and prompt it
jsonFile="db/daily.json"
###
# Check if jq, jo and curl are installed, or install them
2020-10-03 05:29:50 +02:00
[[ -z $(which jq) || -z $(which jo) || -z $(which curl) ]] && sudo apt update && sudo apt install jq jo bc curl -y
2020-10-03 05:20:34 +02:00
# Path of the current script
GPATH="$( cd "$(dirname "$0")" ; pwd -P )"
2020-10-03 05:20:34 +02:00
jsonFile="$GPATH/$jsonFile"
if [[ -f $jsonFile ]]; then
jsonData=$(cat $jsonFile)
else
echo "Warning: No JSON file found, we download it from https://g1-stats.axiom-team.fr/data/daily.json" >&2
jsonData="$(curl -s https://g1-stats.axiom-team.fr/data/daily.json)"
fi
# Help display
helpOpt() {
echo -e "Welcome to Ḡ1Stats Explorer V$(head $0 | awk '/# Version:/ { print $3 }')
\rThis tool can be use in 2 differents mode:
\r- Isolate mode (default): Display data of a selected day
\r- Cumulative mode (option -c or --cumulate): Display cumulative data from begining of Ḡ1 Blockchain (08-03-17) until selected day
\rExamples:
\r$0
Default view show last day data in cumulative mode
\r$0 day 08-03-20
Display 8th Mars 2020's data
\r$0 day 08-03-20 -c
Display 8th Mars 2020's cumulative data from begining
2020-10-03 05:20:34 +02:00
\r$0 mass -c [day]
Display monetary mass on current day, or selected day
\r$0 solde [day]
Display solde (Received - Sent) for simple wallets and members wallets on a day
\r$0 total [day]
Display Sent and Received Ḡ1 in total on a day
\r$0 custom \".rMembers==0 and .sMembers==0\"
Display custom filter. Here we get only days where members received and sent nothing
\r$0 anniversary -c
Display UD reassessment days, the new UD value and the number of members this day"
2020-10-03 05:20:34 +02:00
}
jqCumulate="def roundit: (.*100.0)+ 0.5|floor/100.0;
[foreach .[] as \$row (null;
.nbrMembers += \$row.nbrMembers |
.nbrWallets += \$row.nbrWallets |
.sMembers = (\$row.sMembers + .sMembers|roundit) |
.rMembers = (\$row.rMembers + .rMembers|roundit) |
.sWallets = (\$row.sWallets + .sWallets|roundit) |
.rWallets = (\$row.rWallets + .rWallets|roundit);
\$row + . )]"
# Parse options
for i in $@; do
case "$i" in
2020-10-03 05:20:34 +02:00
-c|--cumulate) jsonData="$(jq "$jqCumulate" <<<$jsonData)";;
-h|--help) helpOpt && exit 0;;
*) isArg+="$i|";;
esac
done
2020-10-03 05:20:34 +02:00
# Load arguments in respective variables
cmd=$(cut -d'|' -f1 <<<"$isArg")
dateRange=$(cut -d'|' -f2 <<<"$isArg")
args="$(cut -d'|' -f2- <<<"$isArg" | sed 's/.$//' | tr '|' ' ')"
2020-10-03 05:20:34 +02:00
# If no date, set last available
[[ -z "$dateRange" ]] && dateRange=$(jq -r '.[].date' <<<$jsonData | tail -n1)
2020-10-03 05:20:34 +02:00
# Check if date existe un JSON
[[ ! $(grep -w "$dateRange" <<<$jsonData) && $cmd != "custom" ]] && echo "La date $dateRange n'existe pas en cache G1Stats" && exit 1
2020-09-19 04:01:10 +02:00
day() {
jq '.[] | select(.date=="'$dateRange'")' <<<$jsonData
}
2020-09-19 04:01:10 +02:00
mass() {
local jsonDated=$(jq '.[0: map(.date) | index("'$dateRange'")+1]' <<<$jsonData)
local mMass=$(jq '.[] | .UD*.nbrMembers' <<<"$jsonDated" | awk '{ SUM += $1} END { printf "%.2f", SUM }')
2020-10-03 05:20:34 +02:00
jo -p date=$dateRange monetaryMass=$mMass | jq .
}
2020-10-05 22:21:16 +02:00
total() {
local cumDay=$(day)
2020-10-05 22:21:16 +02:00
local totalExchange=$(jq '(.rWallets+.rMembers)*100.0+ 0.5|floor/100.0' <<<$cumDay)
local soldeWallets=$(jq '(.rWallets-.sWallets)*100.0+ 0.5|floor/100.0' <<<$cumDay)
local soldeMembers=$(jq '(.rMembers-.sMembers)*100.0+ 0.5|floor/100.0' <<<$cumDay)
2020-10-05 22:21:16 +02:00
jo -p date=$dateRange totalExchange=$totalExchange soldeWallets=$soldeWallets soldeMembers=$soldeMembers | jq .
}
totall() {
for i in $(jq -r .[].date <<<$jsonData); do
dateRange=$i
total
done
}
custom() {
jq '.[] | select('"$args"')' <<<$jsonData
}
anniversary() {
local jqNewUD="reduce .[] as \$x (null;
if . == null then [\$x]
elif .[-1].UD == \$x.UD then .
2020-11-08 12:10:52 +01:00
else . + [\$x] end) | .[] |=
{date: .date, UD: .UD, nbrMembers: .nbrMembers}"
jq "$jqNewUD" <<<$jsonData
}
2022-01-02 00:01:35 +01:00
today() {
todayDate=$(date +'%d-%m-%y')
jq '.[] | select(.date=="'$todayDate'")' <<<$jsonData
}
update() {
curl -s https://g1-stats.axiom-team.fr/data/daily.json > $jsonFile && echo "Data have been updated" || echo "Error: Can't update data"
}
print() {
jq . <<<"$jsonData"
}
2020-10-03 05:20:34 +02:00
# Load functions
case $cmd in
2020-10-03 05:20:34 +02:00
'') day;;
2020-10-05 22:21:16 +02:00
solde) total;;
*) [[ $(type -t $cmd) == "function" ]] && $cmd || (echo -e "$cmd: Commande inconnue\n" && helpOpt);;
esac