diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 836f5628b..85449f2fb 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -429,8 +429,10 @@ def dao_fetch_stats_for_service(service_id, limit_days): def dao_fetch_todays_stats_for_service(service_id): + today = date.today() + start_date = get_london_midnight_in_utc(today) return _stats_for_service_query(service_id).filter( - func.date(Notification.created_at) == date.today() + Notification.created_at >= start_date ).all() diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 9671e7896..fff928206 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -874,6 +874,93 @@ def test_fetch_stats_for_today_only_includes_today(notify_db_session): assert stats['created'] == 1 +def test_fetch_stats_for_today_only_includes_today_when_clocks_spring_forward(notify_db_session): + template = create_template(service=create_service()) + with freeze_time('2021-03-27T23:59:59'): + # just before midnight yesterday in UTC -- not included + create_notification(template=template, to_field='1', status='permanent-failure') + with freeze_time('2021-03-28T00:01:00'): + # just after midnight yesterday in UTC -- included + create_notification(template=template, to_field='2', status='failed') + with freeze_time('2021-03-28T12:00:00'): + # we have entered BST at this point but had not for the previous two notifications --included + # collect stats for this timestamp + create_notification(template=template, to_field='3', status='created') + stats = dao_fetch_todays_stats_for_service(template.service_id) + + stats = {row.status: row.count for row in stats} + assert 'delivered' not in stats + assert stats['failed'] == 1 + assert stats['created'] == 1 + assert not stats.get('permanent-failure') + assert not stats.get('temporary-failure') + + +def test_fetch_stats_for_today_only_includes_today_during_bst(notify_db_session): + template = create_template(service=create_service()) + with freeze_time('2021-03-28T22:59:59'): + # just before midnight BST -- not included + create_notification(template=template, to_field='1', status='permanent-failure') + with freeze_time('2021-03-28T23:00:01'): + # just after midnight BST -- included + create_notification(template=template, to_field='2', status='failed') + with freeze_time('2021-03-29T12:00:00'): + # well after midnight BST -- included + # collect stats for this timestamp + create_notification(template=template, to_field='3', status='created') + stats = dao_fetch_todays_stats_for_service(template.service_id) + + stats = {row.status: row.count for row in stats} + assert 'delivered' not in stats + assert stats['failed'] == 1 + assert stats['created'] == 1 + assert not stats.get('permanent-failure') + + +def test_fetch_stats_for_today_only_includes_today_when_clocks_fall_back(notify_db_session): + template = create_template(service=create_service()) + with freeze_time('2021-10-30T22:59:59'): + # just before midnight BST -- not included + create_notification(template=template, to_field='1', status='permanent-failure') + with freeze_time('2021-10-31T23:00:01'): + # just after midnight BST -- included + create_notification(template=template, to_field='2', status='failed') + # clocks go back to UTC on 31 October at 2am + with freeze_time('2021-10-31T12:00:00'): + # well after midnight -- included + # collect stats for this timestamp + create_notification(template=template, to_field='3', status='created') + stats = dao_fetch_todays_stats_for_service(template.service_id) + + stats = {row.status: row.count for row in stats} + assert 'delivered' not in stats + assert stats['failed'] == 1 + assert stats['created'] == 1 + assert not stats.get('permanent-failure') + + +def test_fetch_stats_for_today_only_includes_during_utc(notify_db_session): + template = create_template(service=create_service()) + with freeze_time('2021-10-30T12:59:59'): + # just before midnight UTC -- not included + create_notification(template=template, to_field='1', status='permanent-failure') + with freeze_time('2021-10-31T00:00:01'): + # just after midnight UTC -- included + create_notification(template=template, to_field='2', status='failed') + # clocks go back to UTC on 31 October at 2am + with freeze_time('2021-10-31T12:00:00'): + # well after midnight -- included + # collect stats for this timestamp + create_notification(template=template, to_field='3', status='created') + stats = dao_fetch_todays_stats_for_service(template.service_id) + + stats = {row.status: row.count for row in stats} + assert 'delivered' not in stats + assert stats['failed'] == 1 + assert stats['created'] == 1 + assert not stats.get('permanent-failure') + + @pytest.mark.parametrize('created_at, limit_days, rows_returned', [ ('Sunday 8th July 2018 12:00', 7, 0), ('Sunday 8th July 2018 22:59', 7, 0),