Skip to content

Commit

Permalink
Merge pull request #13 from aitormagan/feature/new_report_days
Browse files Browse the repository at this point in the history
Feature/new report days
  • Loading branch information
aitormagan authored Mar 15, 2022
2 parents 4220264 + e5b421d commit 494b32b
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 80 deletions.
12 changes: 10 additions & 2 deletions constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from datetime import date
from datetime import date, timedelta

GRAPH_IMAGE_PATH = "render/d-solo/HukfaHZgk/covid19?orgId=1&panelId=2&width=1000&height=500&tz=Europe%2FMadrid"
VACCINE_IMAGE_PATH = "render/d-solo/TeEplNgRk/covid-vacunas-espana?orgId=1&panelId=2&width=1000&height=500&tz=Europe%2FMadrid"
DAYS_WITHOUT_REPORT = [date(2020, 12, 8), date(2020, 12, 25), date(2021, 1, 1), date(2021, 1, 6), date(2021, 3, 19),
date(2021, 10, 12), date(2021, 11, 1), date(2021, 11, 9), date(2021, 12, 6), date(2021, 12, 8),
date(2021, 12, 24), date(2021, 12, 31), date(2022, 1, 6)]
date(2021, 12, 24), date(2021, 12, 31), date(2022, 1, 6), date(2022, 2, 28)]
SPAIN = "España"

INITIAL_DAY_TWO_REPORTS_A_WEEK = date(2022, 3, 14)

# Include in days without report all mondays, wednesdays and thursdays from the 14th of March...
for i in range(0, (date(2022, 3, 30) - INITIAL_DAY_TWO_REPORTS_A_WEEK).days + 1):
day = INITIAL_DAY_TWO_REPORTS_A_WEEK + timedelta(i)
if day.weekday() in [0, 2, 3]:
DAYS_WITHOUT_REPORT.append(day)
4 changes: 0 additions & 4 deletions helpers/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def _get_report(self, query, key="sum"):
def get_all_stats_group_by_week(self, day):
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_last_value_from_week(Measurement.ACCUMULATED_INCIDENCE, day)
Expand All @@ -107,7 +106,6 @@ def get_all_stats_group_by_week(self, day):
return self._pack_elements(**{
Measurement.PCRS.value: pcrs,
Measurement.DEATHS.value: deaths,
Measurement.PCRS_LAST_24H.value: pcrs_last_24h,
Measurement.ADMITTED_PEOPLE.value: admitted,
Measurement.ICU_PEOPLE.value: icu,
Measurement.ACCUMULATED_INCIDENCE.value: accumulated_incidence,
Expand All @@ -120,7 +118,6 @@ def get_all_stats_group_by_week(self, day):
def get_all_stats_group_by_day(self, day):
pcrs = self.get_stat_group_by_day(Measurement.PCRS, day)
deaths = self.get_stat_group_by_day(Measurement.DEATHS, day)
pcrs_last_24h = self.get_stat_group_by_day(Measurement.PCRS_LAST_24H, day)
admitted = self.get_stat_group_by_day(Measurement.ADMITTED_PEOPLE, day)
icu = self.get_stat_group_by_day(Measurement.ICU_PEOPLE, day)
accumulated_incidence = self.get_stat_group_by_day(Measurement.ACCUMULATED_INCIDENCE, day)
Expand All @@ -132,7 +129,6 @@ def get_all_stats_group_by_day(self, day):
return self._pack_elements(**{
Measurement.PCRS.value: pcrs,
Measurement.DEATHS.value: deaths,
Measurement.PCRS_LAST_24H.value: pcrs_last_24h,
Measurement.ADMITTED_PEOPLE.value: admitted,
Measurement.ICU_PEOPLE.value: icu,
Measurement.ACCUMULATED_INCIDENCE.value: accumulated_incidence,
Expand Down
22 changes: 9 additions & 13 deletions helpers/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,23 @@ def get_completed_vaccination_sentence(territorial_unit, stat, accumulated, toda
_format_number(today_total))


def get_report_by_ccaa(date_in_header, ccaas_today, ccaas_yesterday, ccaas_accumulated_today):
def get_report_by_ccaa(date_in_header, ccaas_today, ccaas_yesterday, ccaas_accumulated_today, vaccine_info=False):
tweets = []
for ccaa in filter(lambda x: x in CCAA_POPULATION.keys(), sorted(ccaas_today.keys())):
tweets.append(get_territorial_unit_report(ccaa, date_in_header, ccaas_today[ccaa],
ccaas_yesterday[ccaa], ccaas_accumulated_today[ccaa]))
ccaas_yesterday[ccaa], ccaas_accumulated_today[ccaa],
vaccination_info=vaccine_info))

return tweets


def get_global_report(date_in_header, ccaas_today, ccaas_yesterday, ccaas_accumulated_today):
def get_global_report(date_in_header, ccaas_today, ccaas_yesterday, ccaas_accumulated_today, vaccine_info=False):
global_today_data = get_global_data(ccaas_today)
global_yesterday_data = get_global_data(ccaas_yesterday)
global_accumulated_data = get_global_data(ccaas_accumulated_today)

return get_territorial_unit_report("🇪🇸 España", date_in_header, global_today_data, global_yesterday_data,
global_accumulated_data)
global_accumulated_data, vaccination_info=vaccine_info)


def get_global_data(dict_to_unpack):
Expand Down Expand Up @@ -89,19 +90,16 @@ def calculate_global_incidence(dict_to_unpack, measurement):
return total_cases / population * 100000 if population else 0


def get_territorial_unit_report(territorial_unit, header_date, today_data, yesterday_data, accumulated_today):
def get_territorial_unit_report(territorial_unit, header_date, today_data, yesterday_data, accumulated_today,
vaccination_info=False):

sentences = list()
sentences.append(f"{territorial_unit} - {header_date}:")
sentences.append("")
sentences.append(get_report_sentence("🧪 PCRs", today_data.get(Measurement.PCRS),
sentences.append(get_report_sentence("🧪 PCRs+/AGs+", today_data.get(Measurement.PCRS),
yesterday_data.get(Measurement.PCRS),
accumulated_today.get(Measurement.PCRS)))

if Measurement.PCRS_LAST_24H in today_data:
sentences.append(get_report_sentence("🧪 PCRs 24h", today_data.get(Measurement.PCRS_LAST_24H),
yesterday_data.get(Measurement.PCRS_LAST_24H)))

sentences.append(get_report_sentence_with_unit("💥 IA",
today_data.get(Measurement.ACCUMULATED_INCIDENCE),
yesterday_data.get(Measurement.ACCUMULATED_INCIDENCE),
Expand All @@ -112,9 +110,7 @@ def get_territorial_unit_report(territorial_unit, header_date, today_data, yeste
accumulated_today.get(Measurement.DEATHS)))
sentences.append("")

# PCRs last 24 hour is not present in Weekly info.
# Vaccines info cannot be shown in daily reports because info. is not updated at the same time.
if Measurement.PCRS_LAST_24H not in today_data:
if vaccination_info:
sentences.append(get_report_sentence("💉 Dosis", today_data.get(Measurement.VACCINATIONS),
yesterday_data.get(Measurement.VACCINATIONS),
accumulated_today.get(Measurement.VACCINATIONS)))
Expand Down
19 changes: 11 additions & 8 deletions main_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ def subtract_days_ignoring_weekends(initial_date, days_to_substract):

def update_database(today):
pcrs_report = SpainCovid19MinistryReport(today, 1)
deaths_report = SpainCovid19MinistryReport(today, 5, (152, 490, 152+343, 490+265))
hospital_report = SpainCovid19MinistryReport(today, 3, (179, 77, 179+280, 77+707))

try:
accumulated_pcrs_today = pcrs_report.get_column_data(1)
except Exception:
Expand All @@ -57,19 +54,25 @@ def update_database(today):
pcrs_report = SpainCovid19MinistryReport(today, 1, (239, 56, 239 + 283, 56 + 756))
accumulated_pcrs_today = pcrs_report.get_column_data(1)

accumulated_deaths_today = deaths_report.get_column_data(1)
today_percentage_admitted = hospital_report.get_column_data(3, cast=float)
today_percentage_icu = hospital_report.get_column_data(6, cast=float)
today_pcrs_last_24h = pcrs_report.get_column_data(2)
accumulated_deaths_today = pcrs_report.get_column_data(5)
try:
accumulated_incidence = pcrs_report.get_column_data(3, 1, float)
except:
accumulated_incidence = pcrs_report.get_column_data(4, 0, float)

hospital_report = SpainCovid19MinistryReport(today, 4)
try:
# First attempt
today_percentage_admitted = hospital_report.get_column_data(3, cast=float)
today_percentage_icu = hospital_report.get_column_data(6, cast=float)
except:
hospital_report = SpainCovid19MinistryReport(today, 4, (179, 77, 179+280, 77+707))
today_percentage_admitted = hospital_report.get_column_data(3, cast=float)
today_percentage_icu = hospital_report.get_column_data(6, cast=float)

update_stat(Measurement.PCRS, accumulated_pcrs_today, today)
update_stat(Measurement.DEATHS, accumulated_deaths_today, today)

influx.insert_stats(Measurement.PCRS_LAST_24H, today, today_pcrs_last_24h)
influx.insert_stats(Measurement.ACCUMULATED_INCIDENCE, today, accumulated_incidence)
influx.insert_stats(Measurement.PERCENTAGE_ADMITTED, today, today_percentage_admitted)
influx.insert_stats(Measurement.PERCENTAGE_ICU, today, today_percentage_icu)
Expand Down
4 changes: 2 additions & 2 deletions main_weekly.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def main():
accumulated_today = delete_pcrs24h(influx.get_all_stats_accumulated_until_day(date))
date_header = get_date_header(date)

spain_report = get_global_report(date_header, today_data, last_week_data, accumulated_today)
spain_report = get_global_report(date_header, today_data, last_week_data, accumulated_today, vaccine_info=True)
graph_url = get_graph_url(additional_vars={"group_by": "1w,4d"})
last_id = twitter.publish_tweet_with_media(spain_report, graph_url)

tweets = get_report_by_ccaa(date_header, today_data, last_week_data, accumulated_today)
tweets = get_report_by_ccaa(date_header, today_data, last_week_data, accumulated_today, vaccine_info=True)
last_id = twitter.publish_tweets(tweets, last_id)
twitter.publish_tweet(get_final_tweet(), last_id)

Expand Down
2 changes: 0 additions & 2 deletions tests/unit/helpers/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ def test_when_get_all_stats_group_by_day_then_three_value_returned(self):

influx.get_stat_group_by_day.assert_has_calls([call(Measurement.PCRS, date),
call(Measurement.DEATHS, date),
call(Measurement.PCRS_LAST_24H, date),
call(Measurement.ADMITTED_PEOPLE, date),
call(Measurement.ICU_PEOPLE, date),
call(Measurement.ACCUMULATED_INCIDENCE, date),
Expand All @@ -137,7 +136,6 @@ def test_when_get_all_stats_group_by_week_then_three_value_returned(self):

influx.get_stat_group_by_week.assert_has_calls([call(Measurement.PCRS, date),
call(Measurement.DEATHS, date),
call(Measurement.PCRS_LAST_24H, date),
call(Measurement.ADMITTED_PEOPLE, date),
call(Measurement.ICU_PEOPLE, date),
call(Measurement.VACCINATIONS, date)])
Expand Down
37 changes: 18 additions & 19 deletions tests/unit/helpers/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def test_given_data_when_get_report_by_ccaa_then_report_returned(self, get_terri

# Note: info is published in alphabetical order
get_territorial_unit_report_mock.assert_has_calls([
call(ccaa2, data_header, today_data[ccaa2], yesterday_data[ccaa2], accumulated_data[ccaa2]),
call(ccaa1, data_header, today_data[ccaa1], yesterday_data[ccaa1], accumulated_data[ccaa1])
call(ccaa2, data_header, today_data[ccaa2], yesterday_data[ccaa2], accumulated_data[ccaa2], vaccination_info=False),
call(ccaa1, data_header, today_data[ccaa1], yesterday_data[ccaa1], accumulated_data[ccaa1], vaccination_info=False)
])

@patch("helpers.reports.get_territorial_unit_report")
Expand Down Expand Up @@ -73,7 +73,8 @@ def test_given_data_when_get_global_report_then_report_returned(self, get_global
self.assertEqual(get_territorial_unit_report_mock.return_value, result)

get_territorial_unit_report_mock.assert_called_once_with("🇪🇸 España", data_header, global_today_data,
global_yesterday_data, global_accumulated_data)
global_yesterday_data, global_accumulated_data,
vaccination_info=False)
get_global_data_mock.assert_has_calls([call(today_data),
call(yesterday_data),
call(accumulated_today)])
Expand Down Expand Up @@ -211,35 +212,34 @@ def test_given_two_ccaas_when_calculate_global_incidence_then_same_value_returne

@patch("helpers.reports.get_report_sentence")
@patch("helpers.reports.get_report_sentence_with_unit")
def test_given_pcrs_24_data_when_get_territorial_unit_report_then_tweet_with_pcrs24h_returned(self,
get_report_sentence_with_unit_mock,
get_report_sentence_mock):
def test_given_vaccination_info_False_when_get_territorial_unit_report_then_tweet_with_pcrs24h_returned(self,
get_report_sentence_with_unit_mock,
get_report_sentence_mock):

territorial_unit = MagicMock()
date_header = MagicMock()
today_data = {
Measurement.PCRS: MagicMock(),
Measurement.PCRS_LAST_24H: MagicMock(),
Measurement.DEATHS: MagicMock(),
Measurement.PERCENTAGE_ADMITTED: MagicMock(),
Measurement.PERCENTAGE_ICU: MagicMock()
}
yesterday_data = MagicMock()
accumulated_today = MagicMock()
pcrs = "pcrs"
pcrs24h = "pcrs24h"
deaths = "deaths"
admitted = "admitted"
uci = "uci"
get_report_sentence_mock.side_effect = [pcrs, pcrs24h, deaths]
get_report_sentence_mock.side_effect = [pcrs, deaths]
accumulated_string = "0,21"
get_report_sentence_with_unit_mock.side_effect = [accumulated_string, admitted, uci]

expected_tweet = f"{territorial_unit} - {date_header}:\n\n{pcrs}\n{pcrs24h}\n{accumulated_string}" \
expected_tweet = f"{territorial_unit} - {date_header}:\n\n{pcrs}\n{accumulated_string}" \
f"\n\n{deaths}\n\n{admitted}\n{uci}"

self.assertEqual(expected_tweet, get_territorial_unit_report(territorial_unit, date_header, today_data,
yesterday_data, accumulated_today))
yesterday_data, accumulated_today,
vaccination_info=False))

get_report_sentence_with_unit_mock.assert_has_calls([call("💥 IA",
today_data.get(Measurement.ACCUMULATED_INCIDENCE),
Expand All @@ -252,21 +252,19 @@ def test_given_pcrs_24_data_when_get_territorial_unit_report_then_tweet_with_pcr
yesterday_data.get(Measurement.PERCENTAGE_ICU), "%")])

get_report_sentence_mock.assert_has_calls([
call("🧪 PCRs", today_data.get(Measurement.PCRS),
call("🧪 PCRs+/AGs+", today_data.get(Measurement.PCRS),
yesterday_data.get(Measurement.PCRS),
accumulated_today.get(Measurement.PCRS)),
call("🧪 PCRs 24h", today_data.get(Measurement.PCRS_LAST_24H),
yesterday_data.get(Measurement.PCRS_LAST_24H)),
call("😢 Muertes", today_data.get(Measurement.DEATHS),
yesterday_data.get(Measurement.DEATHS),
accumulated_today.get(Measurement.DEATHS))
])

@patch("helpers.reports.get_report_sentence")
@patch("helpers.reports.get_report_sentence_with_unit")
def test_given_no_pcrs_24_data_when_get_territorial_unit_report_then_tweet_without_pcrs24h_returned(self,
get_report_sentence_with_unit_mock,
get_report_sentence_mock):
def test_given_vaccination_info_True_when_get_territorial_unit_report_then_tweet_without_pcrs24h_returned(self,
get_report_sentence_with_unit_mock,
get_report_sentence_mock):

territorial_unit = MagicMock()
date_header = MagicMock()
Expand All @@ -292,15 +290,16 @@ def test_given_no_pcrs_24_data_when_get_territorial_unit_report_then_tweet_witho
f"\n\n{deaths}\n\n{vaccinations}\n{completed_vaccinations}"

self.assertEqual(expected_tweet, get_territorial_unit_report(territorial_unit, date_header, today_data,
yesterday_data, accumulated_today))
yesterday_data, accumulated_today,
vaccination_info=True))

get_report_sentence_with_unit_mock.assert_has_calls([call("💥 IA",
today_data.get(Measurement.ACCUMULATED_INCIDENCE),
yesterday_data.get(Measurement.ACCUMULATED_INCIDENCE),
"/100.000 hab.")])

get_report_sentence_mock.assert_has_calls([
call("🧪 PCRs", today_data.get(Measurement.PCRS),
call("🧪 PCRs+/AGs+", today_data.get(Measurement.PCRS),
yesterday_data.get(Measurement.PCRS),
accumulated_today.get(Measurement.PCRS)),
call("😢 Muertes", today_data.get(Measurement.DEATHS),
Expand Down
Loading

0 comments on commit 494b32b

Please sign in to comment.