From 8c4618378f2c1ed266cca518caf1ea801602e5d1 Mon Sep 17 00:00:00 2001 From: poka Date: Sat, 3 Oct 2020 03:49:11 +0200 Subject: [PATCH] Change explorer mechanic: Add --cumulate option for all functions --- explorer.sh | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/explorer.sh b/explorer.sh index 9a42486..76df14e 100755 --- a/explorer.sh +++ b/explorer.sh @@ -3,35 +3,33 @@ # 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" + +jsonData=$(cat $jsonFile) # Parse options for i in $@; do case "$i" in - -c|--cumulate) echo "On cumule !";; - -h|--help) echo "Demande d'aide";; + -c|--cumulate) jsonData="$(jq -f $GPATH/lib/cumulate.jq $jsonFile)";; + -h|--help) echo "TODO";; *) isOptions+="$i|";; esac done -jsonFile="$GPATH/db/daily.json" cmd=$(cut -d'|' -f1 <<<"$isOptions") dateRange=$(cut -d'|' -f2 <<<"$isOptions") args="$(cut -d'|' -f2- <<<"$isOptions" | sed 's/.$//' | tr '|' ' ')" -[[ -z "$dateRange" ]] && dateRange=$(jq -r '.[].date' $jsonFile | tail -n1) -[[ ! $(grep "$dateRange" $jsonFile) && $cmd != "select" ]] && echo "La date $dateRange n'existe pas en cache G1Stats" && exit 1 +[[ -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 -cumulate() { - jsonCum="$(jq -f $GPATH/lib/cumulate.jq $jsonFile)" - jq '.[] | select(.date=="'$dateRange'")' <<<$jsonCum -} day() { - jq '.[] | select(.date=="'$dateRange'")' $jsonFile + jq '.[] | select(.date=="'$dateRange'")' <<<$jsonData } mass() { - jsonDated=$(jq '.[0: map(.date) | index("'$dateRange'")+1]' $jsonFile | jq -f $GPATH/lib/cumulate.jq) + 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 } @@ -50,13 +48,13 @@ total() { } selectFunc() { - jq '.[] | select('"$args"')' $jsonFile + jq '.[] | select('"$args"')' <<<$jsonData } case $cmd in select) selectFunc;; - '') cumulate;; + '') jsonData="$(jq -f $GPATH/lib/cumulate.jq $jsonFile)"; day;; *) $cmd;; esac