diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 93b2c93ac..739f385ac 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -482,10 +482,13 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date): ) total_stmt = select( - func.sum(total_substmt.c.notification_count).label("total_notifications") + func.date_trunc("day", NotificationAllTimeView.created_at).label("day"), + func.sum(total_substmt.c.notification_count).label("total_notifications"), + ).group_by( + func.date_trunc("day", NotificationAllTimeView.created_at), ) - total_notifications = db.session.execute(total_stmt).scalar_one() + total_notifications = {day: count for day, count in db.session.execute(total_stmt)} stmt = ( select( @@ -777,7 +780,11 @@ def get_specific_days_stats( stats = { day.strftime("%Y-%m-%d"): statistics.format_statistics( rows, - total_notifications=total_notifications, + total_notifications=( + total_notifications.get(day, 0) + if total_notifications is not None + else None + ), ) for day, rows in grouped_data.items() } diff --git a/app/service/statistics.py b/app/service/statistics.py index 593067745..68ba4f3ca 100644 --- a/app/service/statistics.py +++ b/app/service/statistics.py @@ -2,7 +2,13 @@ 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, NotificationType, StatisticsType, TemplateType +from app.enums import ( + KeyType, + NotificationStatus, + NotificationType, + StatisticsType, + TemplateType, +) def format_statistics(statistics, total_notifications=None): @@ -25,7 +31,9 @@ def format_statistics(statistics, total_notifications=None): 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) + pending_count = total_notifications - ( + requested_count + delivered_count + failed_count + ) sms_dict[StatisticsType.PENDING] = pending_count return counts