#!/bin/bash ################################################################################ # Author: Poka (poka@p2p.legal) # Version: 0.0.3 # 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 [[ -z $(which jq) || -z $(which jo) || -z $(which curl) ]] && sudo apt update && sudo apt install jq jo bc curl -y # 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 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" } 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 "$jqCumulate" <<<$jsonData)";; -h|--help) helpOpt && exit 0;; *) isArg+="$i|";; esac done # Load arguments in respective variables cmd=$(cut -d'|' -f1 <<<"$isArg") dateRange=$(cut -d'|' -f2 <<<"$isArg") args="$(cut -d'|' -f2- <<<"$isArg" | sed 's/.$//' | tr '|' ' ')" # If no date, set last available [[ -z "$dateRange" ]] && dateRange=$(jq -r '.[].date' <<<$jsonData | tail -n1) # Check if date existe un JSON [[ ! $(grep -w "$dateRange" <<<$jsonData) && $cmd != "custom" ]] && echo "La date $dateRange n'existe pas en cache G1Stats" && exit 1 day() { jq '.[] | select(.date=="'$dateRange'")' <<<$jsonData } mass() { local jsonDated=$(jq '.[0: map(.date) | index("'$dateRange'")+1]' <<<$jsonData) local mMass=$(jq '.[] | .UD*.nbrMembers' <<<"$jsonDated" | awk '{ SUM += $1} END { printf "%.2f", SUM }') jo -p date=$dateRange monetaryMass=$mMass | jq . } total() { local cumDay=$(day) 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) 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 . else . + [\$x] end) | .[] |= {date: .date, UD: .UD, nbrMembers: .nbrMembers}" jq "$jqNewUD" <<<$jsonData } 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" } # Load functions case $cmd in '') day;; solde) total;; *) [[ $(type -t $cmd) == "function" ]] && $cmd || (echo -e "$cmd: Commande inconnue\n" && helpOpt);; esac