diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index c362e19d9..51cd68a9a 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -251,7 +251,9 @@ def delete_service_and_all_associated_db_objects(service): @statsd(namespace="dao") def dao_fetch_stats_for_service(service_id): - return _stats_for_service_query(service_id).all() + return _stats_for_service_query(service_id).filter( + func.date(Notification.created_at) >= date.today() - timedelta(days=7) + ).all() @statsd(namespace="dao") diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index c1c2264df..ab2e5602b 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -625,6 +625,29 @@ def test_fetch_stats_for_today_only_includes_today(notify_db, notify_db_session, assert stats['created'] == 1 +def test_fetch_stats_should_not_gather_notifications_older_than_7_days(notify_db, notify_db_session, sample_template): + + # 8 days ago + create_notification(notify_db, None, to_field='1', status='delivered', created_at='2001-01-01T12:00:00') + + # 7 days ago, 2hours ago + create_notification(notify_db, None, to_field='2', status='failed', created_at='2001-01-02T10:00:00') + + # 7 days ago + create_notification(notify_db, None, to_field='2', status='failed', created_at='2001-01-02T12:00:00') + + # right_now + create_notification(notify_db, None, to_field='3', status='created', created_at='2001-01-09T12:00:00') + + with freeze_time('2001-01-09T12:00:00'): + stats = dao_fetch_stats_for_service(sample_template.service_id) + + stats = {row.status: row.count for row in stats} + assert 'delivered' not in stats + assert stats['failed'] == 2 + assert stats['created'] == 1 + + def test_fetch_monthly_historical_stats_separates_months(notify_db, notify_db_session, sample_template): notification_history = functools.partial( create_notification_history,