From 4f184437028e397934e756db9d605a1c535e5136 Mon Sep 17 00:00:00 2001 From: poka Date: Sat, 3 Oct 2020 05:20:34 +0200 Subject: [PATCH] Make explorer.sh standalone --- explorer.sh | 93 +++++++++++++++++++++++++++++++++++++++++++++++++-------- lib/cumulate.jq | 9 ------ 2 files changed, 80 insertions(+), 22 deletions(-) delete mode 100644 lib/cumulate.jq diff --git a/explorer.sh b/explorer.sh index 76df14e..c86a6c8 100755 --- a/explorer.sh +++ b/explorer.sh @@ -1,36 +1,103 @@ #!/bin/bash -# Récupération du chemin absolut du répertoir du script actuel -GPATH="$( cd "$(dirname "$0")" ; pwd -P )" -source $GPATH/.env -jsonFile="$GPATH/db/daily.json" +################################################################################ +# Author: Poka (poka@p2p.legal) +# Version: 0.0.2 +# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/) +# Git: https://git.p2p.legal/axiom-team/g1-stats +################################################################################ -jsonData=$(cat $jsonFile) +### + # 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 +[[ -z $(which jq) || -z $(which jo) || -z $(which curl) ]] && sudo apt update && sudo apt install jq jo bc curl + +# Path of the current script +GPATH="$( cd "$(dirname "$0")" ; pwd -P )" +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 + + \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 select \".rMembers==0 and .sMembers==0\" + Display custom filter. Here we get only days where members received and sent nothing" +} + +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 - -c|--cumulate) jsonData="$(jq -f $GPATH/lib/cumulate.jq $jsonFile)";; - -h|--help) echo "TODO";; + -c|--cumulate) jsonData="$(jq "$jqCumulate" <<<$jsonData)";; + -h|--help) helpOpt && exit 0;; *) isOptions+="$i|";; esac done +# If cumulate option is disable, load normal JSON +#[[ ! $(grep -Ew '\-c|\-\-cumulate' <<<"$@") ]] && jsonData=$(cat $jsonFile) + +# Load arguments in respective variables cmd=$(cut -d'|' -f1 <<<"$isOptions") dateRange=$(cut -d'|' -f2 <<<"$isOptions") args="$(cut -d'|' -f2- <<<"$isOptions" | sed 's/.$//' | tr '|' ' ')" +# If no date, set last available [[ -z "$dateRange" ]] && dateRange=$(jq -r '.[].date' <<<$jsonData | tail -n1) -[[ ! $(grep "$dateRange" <<<$jsonData) && $cmd != "select" ]] && echo "La date $dateRange n'existe pas en cache G1Stats" && exit 1 - +# Check if date existe un JSON +[[ ! $(grep -w "$dateRange" <<<$jsonData) && $cmd != "select" ]] && echo "La date $dateRange n'existe pas en cache G1Stats" && exit 1 day() { jq '.[] | select(.date=="'$dateRange'")' <<<$jsonData } mass() { - jsonDated=$(jq '.[0: map(.date) | index("'$dateRange'")+1]' <<<$jsonData | jq -f $GPATH/lib/cumulate.jq) - jq '.[] | .UD*.nbrMembers' <<<"$jsonDated" | awk '{ SUM += $1} END { printf "%.2f", SUM }'; echo + jsonDated=$(jq '.[0: map(.date) | index("'$dateRange'")+1]' <<<$jsonData) + mMass=$(jq '.[] | .UD*.nbrMembers' <<<"$jsonDated" | awk '{ SUM += $1} END { printf "%.2f", SUM }') + jo -p date=$dateRange monetaryMass=$mMass | jq . } solde() { @@ -51,10 +118,10 @@ selectFunc() { jq '.[] | select('"$args"')' <<<$jsonData } - +# Load functions case $cmd in select) selectFunc;; - '') jsonData="$(jq -f $GPATH/lib/cumulate.jq $jsonFile)"; day;; + '') day;; *) $cmd;; esac diff --git a/lib/cumulate.jq b/lib/cumulate.jq deleted file mode 100644 index 9b0218e..0000000 --- a/lib/cumulate.jq +++ /dev/null @@ -1,9 +0,0 @@ -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 + . )] \ No newline at end of file