From b543db474fc33bca9a2efa7994a4fb7799927c94 Mon Sep 17 00:00:00 2001 From: Cliff Hill Date: Fri, 10 Jan 2025 16:07:49 -0500 Subject: [PATCH] Moving where the pending calculation is done. Signed-off-by: Cliff Hill --- app/dao/services_dao.py | 11 +++++++++-- app/service/statistics.py | 19 +++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index e9171d1fc..93b2c93ac 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -457,8 +457,9 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date): # Getting the total notifications through this query. - total_stmt = ( + total_substmt = ( select( + func.date_trunc("day", NotificationAllTimeView.created_at).label("day"), cast(Job.notification_count, Integer).label( "notification_count" ), # <-- i added cast here @@ -475,10 +476,16 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date): .group_by( Job.id, Job.notification_count, + func.date_trunc("day", NotificationAllTimeView.created_at), ) + .subquery() ) - total_notifications = sum(db.session.execute(total_stmt).scalars()) + total_stmt = select( + func.sum(total_substmt.c.notification_count).label("total_notifications") + ) + + total_notifications = db.session.execute(total_stmt).scalar_one() stmt = ( select( diff --git a/app/service/statistics.py b/app/service/statistics.py index 11e19f16b..593067745 100644 --- a/app/service/statistics.py +++ b/app/service/statistics.py @@ -2,7 +2,7 @@ from collections import defaultdict from datetime import datetime from app.dao.date_util import get_months_for_financial_year -from app.enums import KeyType, NotificationStatus, StatisticsType, TemplateType +from app.enums import KeyType, NotificationStatus, NotificationType, StatisticsType, TemplateType def format_statistics(statistics, total_notifications=None): @@ -17,9 +17,17 @@ def format_statistics(statistics, total_notifications=None): _update_statuses_from_row( counts[row.notification_type], row, - total_notifications=total_notifications, ) + # Update pending count directly + if NotificationType.SMS in counts and total_notifications is not None: + sms_dict = counts[NotificationType.SMS] + requested_count = sms_dict[StatisticsType.REQUESTED] + delivered_count = sms_dict[StatisticsType.DELIVERED] + failed_count = sms_dict[StatisticsType.FAILURE] + pending_count = total_notifications - (requested_count + delivered_count + failed_count) + sms_dict[StatisticsType.PENDING] = pending_count + return counts @@ -86,7 +94,7 @@ def create_zeroed_stats_dicts(): } -def _update_statuses_from_row(update_dict, row, total_notifications=None): +def _update_statuses_from_row(update_dict, row): requested_count = 0 delivered_count = 0 failed_count = 0 @@ -113,11 +121,6 @@ def _update_statuses_from_row(update_dict, row, total_notifications=None): update_dict[StatisticsType.FAILURE] += row.count failed_count += row.count - if total_notifications is not None: - # Update pending count directly - pending_count = total_notifications - (requested_count + delivered_count + failed_count) - update_dict[StatisticsType.PENDING] = pending_count - def create_empty_monthly_notification_status_stats_dict(year): utc_month_starts = get_months_for_financial_year(year)