Explorer: Add anniversary command; change select to custom command

This commit is contained in:
poka 2020-10-05 08:56:30 +02:00
parent 2125a33bfa
commit a3bdf1011e
1 changed files with 33 additions and 18 deletions

View File

@ -2,7 +2,7 @@
################################################################################
# Author: Poka (poka@p2p.legal)
# Version: 0.0.2
# Version: 0.0.3
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
# Git: https://git.p2p.legal/axiom-team/g1-stats
################################################################################
@ -13,7 +13,6 @@
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
@ -54,8 +53,11 @@ helpOpt() {
\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"
\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;
@ -77,9 +79,6 @@ for i in $@; do
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")
@ -88,39 +87,55 @@ args="$(cut -d'|' -f2- <<<"$isOptions" | 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 != "select" ]] && echo "La date $dateRange n'existe pas en cache G1Stats" && exit 1
[[ ! $(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() {
jsonDated=$(jq '.[0: map(.date) | index("'$dateRange'")+1]' <<<$jsonData)
mMass=$(jq '.[] | .UD*.nbrMembers' <<<"$jsonDated" | awk '{ SUM += $1} END { printf "%.2f", SUM }')
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 .
}
solde() {
cumDay=$(day)
soldeWallets=$(jq '(.rWallets-.sWallets)*100.0+ 0.5|floor/100.0' <<<$cumDay)
soldeMembers=$(jq '(.rMembers-.sMembers)*100.0+ 0.5|floor/100.0' <<<$cumDay)
local cumDay=$(day)
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 soldeWallets=$soldeWallets soldeMembers=$soldeMembers | jq .
}
total() {
cumDay=$(day)
totalReceived=$(jq '(.rWallets+.rMembers)*100.0+ 0.5|floor/100.0' <<<$cumDay)
totalSent=$(jq '(.sWallets+.sMembers)*100.0+ 0.5|floor/100.0' <<<$cumDay)
local cumDay=$(day)
local totalReceived=$(jq '(.rWallets+.rMembers)*100.0+ 0.5|floor/100.0' <<<$cumDay)
local totalSent=$(jq '(.sWallets+.sMembers)*100.0+ 0.5|floor/100.0' <<<$cumDay)
jo -p date=$dateRange totalReceived=$totalReceived totalSent=$totalSent | jq .
}
selectFunc() {
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
}
# Load functions
case $cmd in
select) selectFunc;;
'') day;;
*) $cmd;;
esac