Updated query

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2025-01-08 15:41:26 -05:00
parent 1f62b99433
commit 05055aa60a
2 changed files with 24 additions and 3 deletions

View File

@@ -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()

View File

@@ -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