summary refs log tree commit diff
path: root/.config/waybar/modules/covid.sh
blob: e44c108fa22b3d32c4bdd7f14aef43204744e4b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh

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=${HOME_LAT:?}
lon=${HOME_LON:?}
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 "%s" "${_data}" | jq -r '.GEN')
last_update=$(printf "%s" "${_data}" | jq -r '.last_update')
# printf "${_data}" | jq -r '.'
incidence_f=$(printf "%.1f" "$(printf "%s" "${_data}" | jq -r '.cases7_bl_per_100k')")
incidence_i=$(printf "%.0f" "${incidence_f}")

# store
if [ -n "${last_update}" ] && [ -n "${incidence_f}" ]; then
	tmp_HISTFILE=$(mktemp -u --tmpdir waybar-module-covid.XXXXX)
	(
		cat "${HISTFILE}"
		ts=$(printf "%s" "${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}"
fi

mxy=$(tail -n 7 "${HISTFILE}" | awk -F',' '{y=y+$2} END{print ((NR*(NR+1))/2)/NR, y/NR}')
trend=$(awk -F',' --assign="mx=$(printf "%s" "$mxy" | cut -d' ' -f1)" --assign="my=$(printf "%s" "$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'
fi
if [ "${incidence_i}" -gt "1" ]; then
	color='green'
fi
if [ "${incidence_i}" -gt "25" ]; then
	color='yellow'
fi
if [ "${incidence_i}" -gt "35" ]; then
	color='orange'
fi
if [ "${incidence_i}" -gt "50" ]; then
	color='red'
fi
if [ "${incidence_i}" -gt "100" ]; then
	color='darkred'
fi
if [ "${incidence_i}" -gt "250" ]; then
	color='darkdarkred'
fi

tooltip="
7 Tage Inzidenz für ${loc}
last update: ${last_update}

history (${trend_arrow}):
$(awk -F, '{print $1 " " $2}' "${HISTFILE}" | tail -n10 | tac)
"

jq -nr \
	"{ text: \"${trend_arrow}${incidence_f}\", percentage: \"${incidence_i}\", class: \"${color}\", alt: \"${color}\", tooltip: \"${tooltip}\" } | @text"