mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-11 10:28:55 -04:00
Removing total_notification calculations.
Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
+3
-47
@@ -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)
|
start_date = get_midnight_in_utc(start_date)
|
||||||
end_date = get_midnight_in_utc(end_date + timedelta(days=1))
|
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 = (
|
stmt = (
|
||||||
select(
|
select(
|
||||||
NotificationAllTimeView.notification_type,
|
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()
|
data = db.session.execute(stmt).all()
|
||||||
|
|
||||||
return total_notifications, data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def dao_fetch_stats_for_service_from_days_for_user(
|
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()
|
return db.session.execute(stmt).all()
|
||||||
|
|
||||||
|
|
||||||
def get_specific_days_stats(
|
def get_specific_days_stats(data, start_date, days=None, end_date=None):
|
||||||
data, start_date, days=None, end_date=None, total_notifications=None
|
|
||||||
):
|
|
||||||
if days is not None and end_date is not None:
|
if days is not None and end_date is not None:
|
||||||
raise ValueError("Only set days OR set end_date, not both.")
|
raise ValueError("Only set days OR set end_date, not both.")
|
||||||
elif days is not None:
|
elif days is not None:
|
||||||
@@ -778,14 +741,7 @@ def get_specific_days_stats(
|
|||||||
}
|
}
|
||||||
|
|
||||||
stats = {
|
stats = {
|
||||||
day.strftime("%Y-%m-%d"): statistics.format_statistics(
|
day.strftime("%Y-%m-%d"): statistics.format_statistics(rows)
|
||||||
rows,
|
|
||||||
total_notifications=(
|
|
||||||
total_notifications.get(day, 0)
|
|
||||||
if total_notifications is not None
|
|
||||||
else None
|
|
||||||
),
|
|
||||||
)
|
|
||||||
for day, rows in grouped_data.items()
|
for day, rows in grouped_data.items()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-7
@@ -230,18 +230,13 @@ def get_service_statistics_for_specific_days(service_id, start, days=1):
|
|||||||
end_date = datetime.strptime(start, "%Y-%m-%d")
|
end_date = datetime.strptime(start, "%Y-%m-%d")
|
||||||
start_date = end_date - timedelta(days=days - 1)
|
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,
|
service_id,
|
||||||
start_date,
|
start_date,
|
||||||
end_date,
|
end_date,
|
||||||
)
|
)
|
||||||
|
|
||||||
stats = get_specific_days_stats(
|
stats = get_specific_days_stats(results, start_date, days=days)
|
||||||
results,
|
|
||||||
start_date,
|
|
||||||
days=days,
|
|
||||||
total_notifications=total_notifications,
|
|
||||||
)
|
|
||||||
|
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
|||||||
@@ -5,13 +5,12 @@ from app.dao.date_util import get_months_for_financial_year
|
|||||||
from app.enums import (
|
from app.enums import (
|
||||||
KeyType,
|
KeyType,
|
||||||
NotificationStatus,
|
NotificationStatus,
|
||||||
NotificationType,
|
|
||||||
StatisticsType,
|
StatisticsType,
|
||||||
TemplateType,
|
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
|
# 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
|
# 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
|
# so we can return emails/sms * created, sent, and failed
|
||||||
@@ -25,17 +24,6 @@ def format_statistics(statistics, total_notifications=None):
|
|||||||
row,
|
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
|
return counts
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user