infra/roles/monitoring-server/templates/berlinrona.py.j2

78 lines
2.1 KiB
Django/Jinja

#!/usr/bin/python3
"""data from: https://daten.berlin.de/datensaetze/covid-19-berlin-verteilung-den-bezirken
https://daten.berlin.de/datensaetze/covid-19-berlin-verteilung-nach-altersgruppen
"""
from datetime import datetime, timezone
import sys
import json
import requests
from influxdb import InfluxDBClient
from loguru import logger
def valorzero(value):
if value is None:
return 0
else:
return value
logger.remove()
logger.add(
"{{ userlist[myusername].home }}/berlinrona-api.log",
format="{time}${message}",
filter=lambda r: "api" in r["extra"],
rotation="50 MB",
compression="zip"
)
logger.add(
"{{ userlist[myusername].home }}/berlinrona-datapoints.log",
format="{message}",
filter=lambda r: "datapoint" in r["extra"],
rotation="50 MB",
compression="zip"
)
url = "https://www.berlin.de/lageso/gesundheit/infektionskrankheiten/corona/tabelle-bezirke/index.php/index/all.json?q="
if __name__ == "__main__":
time = datetime.utcnow().replace(tzinfo=timezone.utc).isoformat()
r = requests.get(url)
r.raise_for_status()
j = r.json()
logger.bind(api=True).info(j)
points = []
for item in j['index']:
point = {
'measurement': 'coronavirus',
'tags': {
'bezirk': item['bezirk']
},
'time': time,
'fields': {
'recovered': int(valorzero(item.get('genesen'))),
'cases': int(valorzero(item.get('fallzahl'))),
'incidence': float(valorzero(item.get('inzidenz'))),
'diff': float(valorzero(item.get('differenz')))
}
}
logger.bind(datapoint=True).info(point)
points.append(point)
if "-v" in sys.argv:
print(point['tags']['bezirk'], point['fields'])
client = InfluxDBClient(
host='{{ influxdb_url }}',
port=443,
username='berlinrona',
password="{{ influxdb_user_database['berlinrona'] }}",
ssl=True,
verify_ssl=True,
database='berlinrona')
client.write_points(points, time_precision='h')