Removing total_notification calculations.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2025-01-10 16:52:30 -05:00
parent 467357c031
commit 3f8c49d829
3 changed files with 6 additions and 67 deletions

View File

@@ -455,41 +455,6 @@ 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))
# Getting the total notifications through this query.
total_substmt = (
select(
func.date_trunc("day", NotificationAllTimeView.created_at).label("day"),
cast(Job.notification_count, Integer).label(
"notification_count"
), # <-- i added cast here
)
.join_from(
NotificationAllTimeView, Job, NotificationAllTimeView.job_id == Job.id
) # <-- i changed this to NotificationAllTimeView from notifications
.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,
func.date_trunc("day", NotificationAllTimeView.created_at),
)
.subquery()
)
total_stmt = select(
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 = {day: count for day, count in db.session.execute(total_stmt)}
stmt = (
select(
NotificationAllTimeView.notification_type,
@@ -514,7 +479,7 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date):
data = db.session.execute(stmt).all()
return total_notifications, data
return data
def dao_fetch_stats_for_service_from_days_for_user(
@@ -760,9 +725,7 @@ def fetch_notification_stats_for_service_by_month_by_user(
return db.session.execute(stmt).all()
def get_specific_days_stats(
data, start_date, days=None, end_date=None, total_notifications=None
):
def get_specific_days_stats(data, start_date, days=None, end_date=None):
if days is not None and end_date is not None:
raise ValueError("Only set days OR set end_date, not both.")
elif days is not None:
@@ -778,14 +741,7 @@ def get_specific_days_stats(
}
stats = {
day.strftime("%Y-%m-%d"): statistics.format_statistics(
rows,
total_notifications=(
total_notifications.get(day, 0)
if total_notifications is not None
else None
),
)
day.strftime("%Y-%m-%d"): statistics.format_statistics(rows)
for day, rows in grouped_data.items()
}

View File

@@ -230,18 +230,13 @@ def get_service_statistics_for_specific_days(service_id, start, days=1):
end_date = datetime.strptime(start, "%Y-%m-%d")
start_date = end_date - timedelta(days=days - 1)
total_notifications, results = dao_fetch_stats_for_service_from_days(
results = dao_fetch_stats_for_service_from_days(
service_id,
start_date,
end_date,
)
stats = get_specific_days_stats(
results,
start_date,
days=days,
total_notifications=total_notifications,
)
stats = get_specific_days_stats(results, start_date, days=days)
return stats

View File

@@ -5,13 +5,12 @@ from app.dao.date_util import get_months_for_financial_year
from app.enums import (
KeyType,
NotificationStatus,
NotificationType,
StatisticsType,
TemplateType,
)
def format_statistics(statistics, total_notifications=None):
def format_statistics(statistics):
# statistics come in a named tuple with uniqueness from 'notification_type', 'status' - however missing
# statuses/notification types won't be represented and the status types need to be simplified/summed up
# so we can return emails/sms * created, sent, and failed
@@ -25,17 +24,6 @@ def format_statistics(statistics, total_notifications=None):
row,
)
# 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