diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 260008193..b81e54bc2 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -455,24 +455,41 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date): start_date = get_midnight_in_utc(start_date) end_date = get_midnight_in_utc(end_date + timedelta(days=1)) - stmt = ( + sub_stmt = ( select( + Job.id, + Job.notification_count, NotificationAllTimeView.notification_type, NotificationAllTimeView.status, func.date_trunc("day", NotificationAllTimeView.created_at).label("day"), func.count(NotificationAllTimeView.id).label("count"), ) - .filter( + .join_from( + Notification, + Job, + ) + .where( NotificationAllTimeView.service_id == service_id, NotificationAllTimeView.key_type != KeyType.TEST, NotificationAllTimeView.created_at >= start_date, NotificationAllTimeView.created_at < end_date, ) .group_by( + Job.id, + Job.notification_count, NotificationAllTimeView.notification_type, NotificationAllTimeView.status, func.date_trunc("day", NotificationAllTimeView.created_at), ) + .subquery() + ) + + stmt = select( + func.sum(sub_stmt.notification_count).label("total_notifications"), + sub_stmt.notification_type, + sub_stmt.status, + sub_stmt.day, + func.sum(sub_stmt.count).label("count"), ) return db.session.execute(stmt).all() diff --git a/app/service/statistics.py b/app/service/statistics.py index 12814b970..8bd41da25 100644 --- a/app/service/statistics.py +++ b/app/service/statistics.py @@ -96,7 +96,11 @@ def _update_statuses_from_row(update_dict, row): NotificationStatus.VIRUS_SCAN_FAILED, ): update_dict[StatisticsType.FAILURE] += row.count - elif row.status in (NotificationStatus.PENDING, NotificationStatus.CREATED, NotificationStatus.SENDING): + elif row.status in ( + NotificationStatus.PENDING, + NotificationStatus.CREATED, + NotificationStatus.SENDING, + ): update_dict[StatisticsType.PENDING] += row.count