Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Commit

Permalink
Fetch ICU beds for children, see #219
Browse files Browse the repository at this point in the history
Signed-off-by: Soenke Huster <soenke.huster@eknoes.de>
  • Loading branch information
eknoes committed Dec 9, 2021
1 parent dcfaeb0 commit 76061b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
8 changes: 6 additions & 2 deletions covidbot/covid_data/covid_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,14 @@ def get_vaccination_data(self, district_id: int) -> Optional[VaccinationData]:

def get_icu_data(self, district_id: int) -> Optional[ICUData]:
with self.connection.cursor(dictionary=True) as cursor:
cursor.execute('SELECT date, clear, occupied, occupied_covid, covid_ventilated, updated FROM icu_beds '
cursor.execute('SELECT date, clear, occupied, occupied_covid, clear_children, occupied_children, covid_ventilated, updated FROM icu_beds '
'WHERE district_id=%s ORDER BY date DESC LIMIT 1', [district_id])
row = cursor.fetchone()
if row:
result = ICUData(date=row['date'], clear_beds=row['clear'], occupied_beds=row['occupied'],
occupied_covid=row['occupied_covid'],
occupied_beds_children=row['occupied_children'],
clear_beds_children=row['clear_children'],
covid_ventilated=row['covid_ventilated'], last_update=row['updated'])

cursor.execute('SELECT occupied, occupied_covid FROM icu_beds '
Expand Down Expand Up @@ -414,7 +416,9 @@ def __init__(self, connection: MySQLConnection):
# Intensive care information
cursor.execute('CREATE TABLE IF NOT EXISTS icu_beds (id INTEGER PRIMARY KEY AUTO_INCREMENT,'
'district_id INTEGER, date DATE, clear INTEGER, occupied INTEGER,'
'occupied_covid INTEGER, covid_ventilated INTEGER, updated DATETIME DEFAULT NOW(),'
'occupied_covid INTEGER, covid_ventilated INTEGER,'
'clear_children INTEGER, occupied_children INTEGER, '
'updated DATETIME DEFAULT NOW(),'
'FOREIGN KEY(district_id) REFERENCES counties(rs), UNIQUE(district_id, date))')

# Hospitalisation information
Expand Down
2 changes: 2 additions & 0 deletions covidbot/covid_data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class ICUFacts:
class ICUData:
date: date
clear_beds: int
clear_beds_children: int
occupied_beds: int
occupied_covid: int
occupied_beds_children: int
covid_ventilated: int
last_update: datetime
occupied_beds_trend: Optional[TrendValue] = None
Expand Down
17 changes: 8 additions & 9 deletions covidbot/covid_data/updater/icu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import csv
import logging
import re
from datetime import datetime, timedelta, date
from typing import Optional

Expand Down Expand Up @@ -35,19 +34,19 @@ def update(self) -> bool:
if row['gemeindeschluessel'] == '11000':
row['gemeindeschluessel'] = '11'
results.append((row['gemeindeschluessel'], row['daten_stand'], row['betten_frei_nur_erwachsen'], row['betten_belegt_nur_erwachsen'],
row['faelle_covid_aktuell'], row['faelle_covid_aktuell_invasiv_beatmet']))
row['faelle_covid_aktuell'], row['faelle_covid_aktuell_invasiv_beatmet'], int(row['betten_frei']) - int(row['betten_frei_nur_erwachsen']), int(row['betten_belegt']) - int(row['betten_belegt_nur_erwachsen'])))

with self.connection.cursor() as cursor:
for row in results:
cursor.execute("INSERT IGNORE INTO icu_beds (district_id, date, clear, occupied, occupied_covid,"
" covid_ventilated) VALUES (%s, %s, %s, %s, %s, %s)", row)
" covid_ventilated, occupied_children, clear_children) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", row)

# Calculate aggregated values for states
for i in range(2):
cursor.execute(
"INSERT IGNORE INTO icu_beds (district_id, date, clear, occupied, occupied_covid, covid_ventilated, updated) "
"INSERT IGNORE INTO icu_beds (district_id, date, clear, occupied, occupied_covid, covid_ventilated, occupied_children, clear_children, updated) "
"SELECT c.parent, date, SUM(clear), SUM(occupied), SUM(occupied_covid), "
"SUM(covid_ventilated), updated FROM icu_beds "
"SUM(covid_ventilated), SUM(occupied_children), SUM(clear_children), updated FROM icu_beds "
"INNER JOIN counties c on c.rs = icu_beds.district_id "
"GROUP BY c.parent, date "
"HAVING (COUNT(c.parent) = (SELECT COUNT(*) FROM counties WHERE parent=c.parent) OR c.parent > 0) AND parent IS NOT NULL")
Expand Down Expand Up @@ -104,19 +103,19 @@ def update(self) -> bool:
num_covid = None

row_contents = [row[key_district_id], row['date'], row['betten_frei_nur_erwachsen'], row['betten_belegt_nur_erwachsen'],
num_covid, num_ventilated]
num_covid, num_ventilated, int(row['betten_frei']) - int(row['betten_frei_nur_erwachsen']), int(row['betten_belegt']) - int(row['betten_belegt_nur_erwachsen'])]
results.append(row_contents)

cursor.executemany(
"INSERT IGNORE INTO icu_beds (district_id, date, clear, occupied, occupied_covid,"
" covid_ventilated) VALUES (%s, %s, %s, %s, %s, %s)", results)
" covid_ventilated, clear_children, occupied_children) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", results)

# Calculate aggregated values for states
for i in range(2):
cursor.execute(
"INSERT IGNORE INTO icu_beds (district_id, date, clear, occupied, occupied_covid, covid_ventilated, updated) "
"INSERT IGNORE INTO icu_beds (district_id, date, clear, occupied, occupied_covid, covid_ventilated, occupied_children, clear_children, updated) "
"SELECT c.parent, date, SUM(clear), SUM(occupied), SUM(occupied_covid), "
"SUM(covid_ventilated), updated FROM icu_beds "
"SUM(covid_ventilated), SUM(occupied_children), SUM(clear_children), updated FROM icu_beds "
"INNER JOIN counties c on c.rs = icu_beds.district_id "
"GROUP BY c.parent, date "
"HAVING (COUNT(c.parent) = (SELECT COUNT(*) FROM counties WHERE parent=c.parent) OR c.parent > 0) AND parent IS NOT NULL")
Expand Down

0 comments on commit 76061b1

Please sign in to comment.