summary refs log tree commit diff
path: root/.config/waybar
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2021-04-22 13:11:49 +0200
committerRobert Günzler <r@gnzler.io>2021-04-26 02:09:47 +0200
commit9930722b65ce48730fa04b269e828cdf2dcc7f73 (patch)
tree5a30c58a45bae231b9189fb551b5eb7c10ac2b7b /.config/waybar
parented3a44d2b34dfe76c1cb51e97dc9091660c4c0ba (diff)
waybar/covid: show trend
Diffstat (limited to '.config/waybar')
-rwxr-xr-x.config/waybar/modules/covid.sh38
1 files changed, 33 insertions, 5 deletions
diff --git a/.config/waybar/modules/covid.sh b/.config/waybar/modules/covid.sh
index b30321c..d8f59a4 100755
--- a/.config/waybar/modules/covid.sh
+++ b/.config/waybar/modules/covid.sh
@@ -3,17 +3,46 @@
 set -e
 [ -n "$DEBUG" ] && { set -x; curl_args='-v'; }
 
+# check requirements
+for tool in awk curl jq flock; do
+	if ! command -v "$tool" >/dev/null; then
+		printf "%s: %s not installed but required\n" "$(basename "$0")" "$tool" >&2
+		exit 1
+	fi
+done
+
 # incidence = (sumCasesLast7Days / EWZ) * 100000
-lat=52.54
-lon=13.44
+lat=${HOME_LAT:?}
+lon=${HOME_LAT:?}
 fields='GEN,RS,EWZ,EWZ_BL,BL_ID,cases,cases_per_100k,cases7_per_100k,cases7_bl_per_100k,last_update,BL,IBZ'
+# see https://experience.arcgis.com/experience/478220a4c454480e823b17327b2bf1d4
 uri="https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services/RKI_Landkreisdaten/FeatureServer/0/query?where=1%3D1&outFields=${fields}&geometry=${lon}%2C${lat}&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&returnGeometry=false&outSR=4326&f=json"
 
 _data=$(curl -sSL -H 'Accept: application/json' ${curl_args} ${uri} | jq -r '.features[0].attributes')
+[ -z "${_data}" ] && printf "{}\n"
+
+HISTFILE=${HOME}/covid-history.csv
 loc=$(printf "${_data}" | jq -r '.GEN')
+last_update=$(printf "${_data}" | jq -r '.last_update')
+# printf "${_data}" | jq -r '.'
 incidence_f=$(printf "%.1f" $(printf "${_data}" | jq -r '.cases7_bl_per_100k'))
 incidence_i=$(printf "%.0f" "${incidence_f}")
 
+# store
+tmp_HISTFILE=$(mktemp -u --tmpdir waybar-module-covid.XXXXX)
+(
+	cat ${HISTFILE}
+	ts=$(printf "${last_update}" | cut -d',' -f1 | tr '.' '-')
+	printf "%s,%s\n" "${ts}" "${incidence_f}"
+) | uniq | tee ${tmp_HISTFILE} >/dev/null
+flock -x ${HISTFILE} mv ${tmp_HISTFILE} ${HISTFILE}
+
+mxy=$(awk -F',' '{y=y+$2} END{print ((NR*(NR+1))/2)/NR, y/NR}' "${HISTFILE}")
+trend=$(awk -F',' --assign="mx=$(printf "$mxy" | cut -d' ' -f1)" --assign="my=$(printf "$mxy" | cut -d' ' -f2)" '{up= up+(NR-mx)*($2-my); down= down+(NR-mx)**2} END{if(down==0){print 0}else{print up/down};}' "${HISTFILE}")
+trend_arrow="󰔴"
+[ $(printf "%.0f" ${trend}) -gt 0 ] && trend_arrow="󰔵"
+[ $(printf "%.0f" ${trend}) -lt 0 ] && trend_arrow="󰔳"
+
 color='green'
 if [ "${incidence_i}" -eq "0" ]; then
 	color='gray'
@@ -38,6 +67,5 @@ if [ "${incidence_i}" -gt "250" ]; then
 fi
 
 jq -nr \
-	--arg "if" "${incidence_f}" \
-	--arg "ii" "${incidence_i}" \
-	"{ text: \$if, percentage: \$ii, class: \"${color}\", alt: \"${color}\", tooltip: \"7 Tage Inzidenz für \\n${loc}\" } | @text"
+	"{ text: \"${trend_arrow}${incidence_f}\", percentage: \"${incidence_i}\", class: \"${color}\", alt: \"${color}\",
+	tooltip: \"7 Tage Inzidenz für \\n${loc}\\nLast update: ${last_update}\" } | @text"