Skip to content

Commit

Permalink
Fix weekly report not being published on weeks without reports on friday
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitor Magán committed Dec 27, 2021
1 parent 74bde90 commit f3fbf5e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
22 changes: 16 additions & 6 deletions helpers/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,36 @@ def get_stat_accumulated_until_day(self, measurement: Measurement, day):
f"time <= '{day.strftime(self.DATE_FORMAT)}' group by ccaa;"
return self._get_report(query)

def _get_report(self, query):
def get_last_value_from_week(self, mesaurement: Measurement, day):
monday = day + timedelta(0 - day.weekday())
sunday = day + timedelta(6 - day.weekday())

query = f"SELECT * FROM {mesaurement.value} where " \
f"time >= '{monday.strftime(self.DATE_FORMAT)} 00:00:00' and " \
f"time <= '{sunday.strftime(self.DATE_FORMAT)} 23:59:59' " \
f"group by ccaa order by desc limit 1"

return self._get_report(query, "value")

def _get_report(self, query, key="sum"):
query_result = self.client.query(query)
ccaa_map = {}

for item in query_result.items():
for values in item[1]:
ccaa_map[item[0][1]["ccaa"]] = values["sum"]
ccaa_map[item[0][1]["ccaa"]] = values[key]

return ccaa_map

def get_all_stats_group_by_week(self, day):
week_friday = day + timedelta(4 - day.weekday())
pcrs = self.get_stat_group_by_week(Measurement.PCRS, day)
deaths = self.get_stat_group_by_week(Measurement.DEATHS, day)
pcrs_last_24h = self.get_stat_group_by_week(Measurement.PCRS_LAST_24H, day)
admitted = self.get_stat_group_by_week(Measurement.ADMITTED_PEOPLE, day)
icu = self.get_stat_group_by_week(Measurement.ICU_PEOPLE, day)
accumulated_incidence = self.get_stat_group_by_day(Measurement.ACCUMULATED_INCIDENCE, week_friday)
percentage_admitted = self.get_stat_group_by_day(Measurement.PERCENTAGE_ADMITTED, week_friday)
percentage_icu = self.get_stat_group_by_day(Measurement.PERCENTAGE_ICU, week_friday)
accumulated_incidence = self.get_last_value_from_week(Measurement.ACCUMULATED_INCIDENCE, day)
percentage_admitted = self.get_last_value_from_week(Measurement.PERCENTAGE_ADMITTED, day)
percentage_icu = self.get_last_value_from_week(Measurement.PERCENTAGE_ICU, day)
vaccinations = self.get_stat_group_by_week(Measurement.VACCINATIONS, day)
completed_vaccinations = self.get_stat_group_by_week(Measurement.COMPLETED_VACCINATIONS, day)

Expand Down
21 changes: 17 additions & 4 deletions tests/unit/helpers/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ def test_given_day_when_get_stat_accumulated_until_day_then_get_report_called(se
influx._get_report.assert_called_once_with(
f"SELECT sum(value) FROM pcrs where time <= '2020-08-01' group by ccaa;")

def test_given_date_when_get_last_value_from_week_then_dates_set_accordingly(self):
influx = Influx()
influx._get_report = MagicMock()
date = datetime(2022, 1, 2)
stat = Measurement.ACCUMULATED_INCIDENCE

result = influx.get_last_value_from_week(stat, date)

self.assertEqual(result, influx._get_report.return_value)
influx._get_report.assert_called_once_with(
"SELECT * FROM accumulated_incidence where time >= '2021-12-27 00:00:00' and "
"time <= '2022-01-02 23:59:59' group by ccaa order by desc limit 1", "value")

def test_given_database_info_when_get_report_then_map_returned(self):
with patch.object(Influx, 'client'):
self._influx = Influx()
Expand Down Expand Up @@ -115,7 +128,7 @@ def test_when_get_all_stats_group_by_week_then_three_value_returned(self):
influx = Influx()
influx._pack_elements = MagicMock()
influx.get_stat_group_by_week = MagicMock()
influx.get_stat_group_by_day = MagicMock()
influx.get_last_value_from_week = MagicMock()
date = datetime(2020, 10, 11)

result = influx.get_all_stats_group_by_week(date)
Expand All @@ -128,9 +141,9 @@ def test_when_get_all_stats_group_by_week_then_three_value_returned(self):
call(Measurement.ADMITTED_PEOPLE, date),
call(Measurement.ICU_PEOPLE, date),
call(Measurement.VACCINATIONS, date)])
influx.get_stat_group_by_day.assert_has_calls([call(Measurement.ACCUMULATED_INCIDENCE, datetime(2020, 10, 9)),
call(Measurement.PERCENTAGE_ADMITTED, datetime(2020, 10, 9)),
call(Measurement.PERCENTAGE_ICU, datetime(2020, 10, 9))])
influx.get_last_value_from_week.assert_has_calls([call(Measurement.ACCUMULATED_INCIDENCE, date),
call(Measurement.PERCENTAGE_ADMITTED, date),
call(Measurement.PERCENTAGE_ICU, date)])

def test_when_get_all_stats_accumulated_until_day_then_two_value_returned(self):
influx = Influx()
Expand Down

0 comments on commit f3fbf5e

Please sign in to comment.