From f3fbf5e827071aa28864a9eb40a7cb44559c3241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aitor=20Mag=C3=A1n?= Date: Mon, 27 Dec 2021 11:04:52 +0100 Subject: [PATCH] Fix weekly report not being published on weeks without reports on friday --- helpers/db.py | 22 ++++++++++++++++------ tests/unit/helpers/test_db.py | 21 +++++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/helpers/db.py b/helpers/db.py index cdbfb51..e61aa4a 100644 --- a/helpers/db.py +++ b/helpers/db.py @@ -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) diff --git a/tests/unit/helpers/test_db.py b/tests/unit/helpers/test_db.py index 984e585..a78d91b 100644 --- a/tests/unit/helpers/test_db.py +++ b/tests/unit/helpers/test_db.py @@ -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() @@ -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) @@ -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()