Fix the query to get todays totals for a service.

The query had a group by on notification_type and notification_status, this not only slows the query down but is wrong. The query only looked at the first result, but this query would return as many rows as different notification types and status, meaning the results do not include the correct number.

Are we concerned that all status types are included. For example letters can be cancelled or have validation-failures which shouldn't be included in the daily limit check.
This commit is contained in:
Rebecca Law
2021-06-14 15:29:21 +01:00
parent 8e1a144f87
commit 08bb5c657f
2 changed files with 18 additions and 5 deletions

View File

@@ -437,15 +437,13 @@ def dao_fetch_todays_stats_for_service(service_id):
def fetch_todays_total_message_count(service_id):
start_date = get_london_midnight_in_utc(date.today())
result = db.session.query(
func.count(Notification.id).label('count')
).filter(
Notification.service_id == service_id,
Notification.key_type != KEY_TYPE_TEST,
func.date(Notification.created_at) == date.today()
).group_by(
Notification.notification_type,
Notification.status,
Notification.created_at >= start_date
).first()
return 0 if result is None else result.count