diff --git a/app/service/statistics.py b/app/service/statistics.py index ac148795b..9b76f4d0f 100644 --- a/app/service/statistics.py +++ b/app/service/statistics.py @@ -92,7 +92,7 @@ def create_empty_monthly_notification_status_stats_dict(year): # nested dicts - data[month][template type][status] = count return { convert_utc_to_bst(start).strftime('%Y-%m'): { - template_type: {} + template_type: defaultdict(int) for template_type in TEMPLATE_TYPES } for start in utc_month_starts @@ -103,6 +103,6 @@ def add_monthly_notification_status_stats(data, stats): for row in stats: month = row.month.strftime('%Y-%m') - data[month][row.notification_type][row.notification_status] = row.count + data[month][row.notification_type][row.notification_status] += row.count return data diff --git a/tests/app/service/test_statistics.py b/tests/app/service/test_statistics.py index a1aea04d4..d002ea4bc 100644 --- a/tests/app/service/test_statistics.py +++ b/tests/app/service/test_statistics.py @@ -194,13 +194,13 @@ def test_add_monthly_notification_status_stats(): # this data won't be affected data['2018-05']['email']['sending'] = 32 - # this data will get overwritten + # this data will get combined with the 8 from row_data data['2018-05']['sms']['sending'] = 16 add_monthly_notification_status_stats(data, rows) assert data == { '2018-04': {'sms': {'sending': 1, 'delivered': 2}, 'email': {'sending': 4}, 'letter': {}}, - '2018-05': {'sms': {'sending': 8}, 'email': {'sending': 32}, 'letter': {}}, + '2018-05': {'sms': {'sending': 24}, 'email': {'sending': 32}, 'letter': {}}, '2018-06': {'sms': {}, 'email': {}, 'letter': {}}, }