Fix getting service IDs for status aggregation

Addresses [1].

Previously the query would always use UTC midnight, even after we
had switched to BST (+1h). We store timestamps as naive UTC in our
DB - without a timezone - but we want the query to work in terms
of GMT / BST so we adjust for that - BST midnight is 11PM in UTC.

[1]: https://github.com/alphagov/notifications-api/pull/3437#discussion_r791998690
This commit is contained in:
Ben Thorner
2022-02-10 10:37:32 +00:00
parent 6e8f121548
commit 966c4db8c6
2 changed files with 32 additions and 4 deletions

View File

@@ -46,7 +46,11 @@ from app.models import (
NotificationHistory,
ProviderDetails,
)
from app.utils import escape_special_characters, midnight_n_days_ago
from app.utils import (
escape_special_characters,
get_london_midnight_in_utc,
midnight_n_days_ago,
)
def dao_get_last_date_template_was_used(template_id, service_id):
@@ -797,6 +801,9 @@ def get_service_ids_with_notifications_before(notification_type, timestamp):
def get_service_ids_with_notifications_on_date(notification_type, date):
start_date = get_london_midnight_in_utc(date)
end_date = get_london_midnight_in_utc(date + timedelta(days=1))
return {
row.service_id
for row in db.session.query(
@@ -804,7 +811,7 @@ def get_service_ids_with_notifications_on_date(notification_type, date):
).filter(
Notification.notification_type == notification_type,
# using >= + < is much more efficient than date(created_at)
Notification.created_at >= date,
Notification.created_at < date + timedelta(days=1)
Notification.created_at >= start_date,
Notification.created_at < end_date,
).distinct()
}