Skip to content

Commit 25229a2

Browse files
Fix time range for for daily stats (#2097)
* fix(daily stats): for daily stats, use the time range of now back until midnight UTC as this is what is used to apply limits in the app * chore: formatting * fix: remove testing code * chore: remove unused function --------- Co-authored-by: Jumana B <[email protected]>
1 parent 4008f12 commit 25229a2

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

app/dao/fact_notification_status_dao.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,19 @@ def fetch_notification_status_for_service_for_day(bst_day, service_id):
239239

240240

241241
def fetch_notification_status_for_service_for_today_and_7_previous_days(service_id, by_template=False, limit_days=7):
242-
ft_start_date = utc_midnight_n_days_ago(limit_days)
242+
if limit_days == 1:
243+
ft_start_date = utc_midnight_n_days_ago(limit_days - 1)
244+
# For daily stats, service limits reset at 12:00am UTC each night, so we need to fetch the data from 12:00 UTC to now
245+
start = utc_midnight_n_days_ago(0)
246+
end = datetime.utcnow()
247+
else:
248+
ft_start_date = utc_midnight_n_days_ago(limit_days)
243249

244-
# The nightly task that populates ft_notification_status counts collects notifications from
245-
# 5AM the day before to 5AM of the current day. So we need to match that timeframe when
246-
# we fetch notifications for the current day.
247-
start = (tz_aware_midnight_n_days_ago(1) + timedelta(hours=5)).replace(minute=0, second=0, microsecond=0)
248-
end = (tz_aware_midnight_n_days_ago(0) + timedelta(hours=5)).replace(minute=0, second=0, microsecond=0)
250+
# The nightly task that populates ft_notification_status counts collects notifications from
251+
# 5AM the day before to 5AM of the current day. So we need to match that timeframe when
252+
# we fetch notifications for the current day.
253+
start = (tz_aware_midnight_n_days_ago(1) + timedelta(hours=5)).replace(minute=0, second=0, microsecond=0)
254+
end = (tz_aware_midnight_n_days_ago(0) + timedelta(hours=5)).replace(minute=0, second=0, microsecond=0)
249255

250256
stats_for_7_days = db.session.query(
251257
FactNotificationStatus.notification_type.label("notification_type"),

tests/app/dao/test_fact_notification_status_dao.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,29 +1158,22 @@ def test_fetch_notification_status_for_service_for_today_handles_midnight_utc(
11581158
email_template = create_template(service=service_1, template_type=EMAIL_TYPE)
11591159

11601160
# create notifications that should not be included in today's count
1161-
create_ft_notification_status(date(2018, 10, 29), "email", service_1, count=30)
1161+
create_ft_notification_status(date(2018, 10, 24), "email", service_1, count=30)
1162+
create_ft_notification_status(date(2018, 10, 31), "email", service_1, count=20)
1163+
11621164
save_notification(create_notification(email_template, created_at=datetime(2018, 10, 31, 0, 0, 0), status="delivered"))
11631165
save_notification(create_notification(email_template, created_at=datetime(2018, 10, 31, 11, 59, 59), status="delivered"))
11641166
save_notification(create_notification(email_template, created_at=datetime(2018, 10, 31, 11, 59, 59), status="delivered"))
11651167
save_notification(create_notification(email_template, created_at=datetime(2018, 10, 31, 23, 59, 59), status="delivered"))
11661168

11671169
# create notifications that should be included in count
1168-
create_ft_notification_status(date(2018, 10, 31), "email", service_1, count=5)
1169-
create_ft_notification_status(date(2018, 10, 30), "email", service_1, count=5)
1170-
save_notification(create_notification(email_template, created_at=datetime(2018, 10, 31, 13, 0, 0), status="delivered"))
1171-
save_notification(create_notification(email_template, created_at=datetime(2018, 10, 31, 6, 0, 0), status="delivered"))
1172-
save_notification(create_notification(email_template, created_at=datetime(2018, 11, 1, 22, 59, 59), status="delivered"))
1170+
save_notification(create_notification(email_template, created_at=datetime(2018, 11, 1, 13, 0, 0), status="delivered"))
1171+
save_notification(create_notification(email_template, created_at=datetime(2018, 11, 1, 6, 0, 0), status="delivered"))
1172+
save_notification(create_notification(email_template, created_at=datetime(2018, 11, 1, 17, 59, 59), status="delivered"))
11731173

1174-
# checking the daily stats for this day should give us the 2 created after 12am UTC
1174+
# checking the daily stats for this day should give us the 3 created after 12am UTC
11751175
results = sorted(
11761176
fetch_notification_status_for_service_for_today_and_7_previous_days(service_1.id, limit_days=1),
11771177
key=lambda x: (x.notification_type, x.status),
11781178
)
1179-
assert results[0][2] == 6
1180-
1181-
# checking the daily stats for the last 2 days should give us the 2 created after 12am UTC and the 1 from the day before
1182-
results = sorted(
1183-
fetch_notification_status_for_service_for_today_and_7_previous_days(service_1.id, limit_days=2),
1184-
key=lambda x: (x.notification_type, x.status),
1185-
)
1186-
assert results[0][2] == 11
1179+
assert results[0][2] == 3

0 commit comments

Comments
 (0)