mirror of
https://github.com/GSA/notifications-api.git
synced 2026-06-03 04:48:29 -04:00
@@ -2,7 +2,7 @@ import uuid
|
||||
from datetime import timedelta
|
||||
|
||||
from flask import current_app
|
||||
from sqlalchemy import Float, cast, delete, select, Integer
|
||||
from sqlalchemy import Float, Integer, cast, delete, select
|
||||
from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.sql.expression import and_, asc, case, func
|
||||
|
||||
@@ -458,13 +458,19 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date):
|
||||
sub_stmt = (
|
||||
select(
|
||||
Job.id.label("job_id"),
|
||||
cast(Job.notification_count, Integer).label("notification_count"), # <-- i added cast here
|
||||
cast(Job.notification_count, Integer).label(
|
||||
"notification_count"
|
||||
), # <-- i added cast here
|
||||
NotificationAllTimeView.notification_type,
|
||||
NotificationAllTimeView.status,
|
||||
func.date_trunc("day", NotificationAllTimeView.created_at).label("day"),
|
||||
cast(func.count(NotificationAllTimeView.id), Integer).label("count"), # <-- i added cast here
|
||||
cast(func.count(NotificationAllTimeView.id), Integer).label(
|
||||
"count"
|
||||
), # <-- i added cast here
|
||||
)
|
||||
.join_from(NotificationAllTimeView, Job, NotificationAllTimeView.job_id == Job.id) # <-- i changed this to NotificationAllTimeView from notifications
|
||||
.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,
|
||||
@@ -483,40 +489,31 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date):
|
||||
|
||||
# Getting the total notifications through this query.
|
||||
|
||||
total_stmt = (
|
||||
select(
|
||||
sub_stmt.c.job_id,
|
||||
sub_stmt.c.notification_count,
|
||||
)
|
||||
.group_by(
|
||||
sub_stmt.c.job_id,
|
||||
sub_stmt.c.notification_count,
|
||||
)
|
||||
total_stmt = select(
|
||||
sub_stmt.c.job_id,
|
||||
sub_stmt.c.notification_count,
|
||||
).group_by(
|
||||
sub_stmt.c.job_id,
|
||||
sub_stmt.c.notification_count,
|
||||
)
|
||||
|
||||
total_notifications = sum(
|
||||
count
|
||||
for __, count in db.session.execute(total_stmt).all()
|
||||
count for __, count in db.session.execute(total_stmt).all()
|
||||
)
|
||||
|
||||
stmt = (
|
||||
select(
|
||||
cast(func.sum(sub_stmt.c.notification_count), Integer).label("total_notifications"), # <-- i added cast here
|
||||
sub_stmt.c.notification_type,
|
||||
sub_stmt.c.status,
|
||||
sub_stmt.c.day,
|
||||
cast(func.sum(sub_stmt.c.count), Integer).label("count"), # <-- i added cast here
|
||||
)
|
||||
.group_by( # <-- i added this group here
|
||||
sub_stmt.c.notification_type,
|
||||
sub_stmt.c.status,
|
||||
sub_stmt.c.day
|
||||
)
|
||||
stmt = select(
|
||||
sub_stmt.c.notification_type,
|
||||
sub_stmt.c.status,
|
||||
sub_stmt.c.day,
|
||||
cast(func.sum(sub_stmt.c.count), Integer).label(
|
||||
"count"
|
||||
), # <-- i added cast here
|
||||
).group_by( # <-- i added this group here
|
||||
sub_stmt.c.notification_type, sub_stmt.c.status, sub_stmt.c.day
|
||||
)
|
||||
return total_notifications, db.session.execute(stmt).all()
|
||||
|
||||
|
||||
|
||||
def dao_fetch_stats_for_service_from_days_for_user(
|
||||
service_id, start_date, end_date, user_id
|
||||
):
|
||||
@@ -760,7 +757,9 @@ 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, total_notifications=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:
|
||||
@@ -776,7 +775,10 @@ def get_specific_days_stats(data, start_date, days=None, end_date=None, total_no
|
||||
}
|
||||
|
||||
stats = {
|
||||
day.strftime("%Y-%m-%d"): statistics.format_statistics(rows, total_notifications=total_notifications,)
|
||||
day.strftime("%Y-%m-%d"): statistics.format_statistics(
|
||||
rows,
|
||||
total_notifications=total_notifications,
|
||||
)
|
||||
for day, rows in grouped_data.items()
|
||||
}
|
||||
|
||||
|
||||
@@ -230,9 +230,18 @@ 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(service_id, start_date, end_date,)
|
||||
total_notifications, 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,
|
||||
total_notifications=total_notifications,
|
||||
)
|
||||
|
||||
return stats
|
||||
|
||||
@@ -679,7 +688,9 @@ def get_single_month_notification_stats_for_service(service_id):
|
||||
start_date, end_date = get_month_start_and_end_date_in_utc(month_year)
|
||||
|
||||
# First element is total notifications used elsewhere.
|
||||
__, results = dao_fetch_stats_for_service_from_days(service_id, start_date, end_date)
|
||||
__, results = dao_fetch_stats_for_service_from_days(
|
||||
service_id, start_date, end_date
|
||||
)
|
||||
|
||||
stats = get_specific_days_stats(results, start_date, end_date=end_date)
|
||||
return jsonify(stats)
|
||||
|
||||
@@ -14,7 +14,11 @@ def format_statistics(statistics, total_notifications=None):
|
||||
# any row could be null, if the service either has no notifications in the notifications table,
|
||||
# or no historical data in the ft_notification_status table.
|
||||
if row.notification_type:
|
||||
_update_statuses_from_row(counts[row.notification_type], row, total_notifications=total_notifications,)
|
||||
_update_statuses_from_row(
|
||||
counts[row.notification_type],
|
||||
row,
|
||||
total_notifications=total_notifications,
|
||||
)
|
||||
|
||||
return counts
|
||||
|
||||
@@ -116,6 +120,7 @@ def _update_statuses_from_row(update_dict, row, total_notifications=None):
|
||||
# Update pending count directly
|
||||
update_dict[StatisticsType.PENDING] = pending_count
|
||||
|
||||
|
||||
def create_empty_monthly_notification_status_stats_dict(year):
|
||||
utc_month_starts = get_months_for_financial_year(year)
|
||||
# nested dicts - data[month][template type][status] = count
|
||||
|
||||
Reference in New Issue
Block a user