enums part 2

This commit is contained in:
Beverly Nguyen
2025-07-11 14:18:54 -07:00
parent 0267a82687
commit e9f6b93d85
7 changed files with 141 additions and 84 deletions

View File

@@ -14,6 +14,7 @@ from app import (
service_api_client,
template_statistics_client,
)
from app.enums import JobStatus, NotificationStatus
from app.main import main
from app.main.views.user_profile import set_timezone
from app.statistics_utils import get_formatted_percentage
@@ -42,7 +43,9 @@ def service_dashboard(service_id):
job_response = job_api_client.get_jobs(service_id)["data"]
service_data_retention_days = 7
active_jobs = [job for job in job_response if job["job_status"] != "cancelled"]
active_jobs = [
job for job in job_response if job["job_status"] != JobStatus.CANCELLED
]
job_lists = [
{**job_dict, "finished_processing": job_is_finished(job_dict)}
for job_dict in active_jobs
@@ -69,7 +72,9 @@ def service_dashboard(service_id):
def job_is_finished(job_dict):
done_statuses = DELIVERED_STATUSES + FAILURE_STATUSES + ["cancelled"]
done_statuses = (
DELIVERED_STATUSES + FAILURE_STATUSES + [NotificationStatus.CANCELLED]
)
processed_count = sum(
stat["count"]
for stat in job_dict["statistics"]
@@ -106,8 +111,18 @@ def get_local_daily_stats_for_last_x_days(stats_utc, user_timezone, days):
]
aggregator = {
d: {
"sms": {"delivered": 0, "failure": 0, "pending": 0, "requested": 0},
"email": {"delivered": 0, "failure": 0, "pending": 0, "requested": 0},
"sms": {
NotificationStatus.DELIVERED: 0,
"failure": 0,
NotificationStatus.PENDING: 0,
"requested": 0,
},
"email": {
NotificationStatus.DELIVERED: 0,
"failure": 0,
NotificationStatus.PENDING: 0,
"requested": 0,
},
}
for d in days_list
}
@@ -121,7 +136,12 @@ def get_local_daily_stats_for_last_x_days(stats_utc, user_timezone, days):
if local_day in aggregator:
for msg_type in ["sms", "email"]:
for status in ["delivered", "failure", "pending", "requested"]:
for status in [
NotificationStatus.DELIVERED,
"failure",
NotificationStatus.PENDING,
"requested",
]:
aggregator[local_day][msg_type][status] += data[msg_type][status]
return aggregator
@@ -231,7 +251,9 @@ def usage(service_id):
def filter_out_cancelled_stats(template_statistics):
return [s for s in template_statistics if s["status"] != "cancelled"]
return [
s for s in template_statistics if s["status"] != NotificationStatus.CANCELLED
]
def aggregate_template_usage(template_statistics, sort_key="count"):
@@ -265,7 +287,7 @@ def get_dashboard_totals(statistics):
for msg_type in statistics.values():
msg_type["failed_percentage"] = get_formatted_percentage(
msg_type["failed"], msg_type["requested"]
msg_type[NotificationStatus.FAILED], msg_type["requested"]
)
msg_type["show_warning"] = float(msg_type["failed_percentage"]) > 3
@@ -314,7 +336,9 @@ def aggregate_status_types(counts_dict):
return get_dashboard_totals(
{
"{}_counts".format(message_type): {
"failed": sum(stats.get(status, 0) for status in FAILURE_STATUSES),
NotificationStatus.FAILED: sum(
stats.get(status, 0) for status in FAILURE_STATUSES
),
"requested": sum(stats.get(status, 0) for status in REQUESTED_STATUSES),
}
for message_type, stats in counts_dict.items()

View File

@@ -23,7 +23,7 @@ from app import (
notification_api_client,
service_api_client,
)
from app.enums import JobStatus
from app.enums import JobStatus, NotificationStatus
from app.formatters import get_time_left, message_count_noun
from app.main import main
from app.main.forms import SearchNotificationsForm
@@ -304,22 +304,30 @@ def get_status_filters(service, message_type, statistics):
if message_type is None:
stats = {
key: sum(statistics[message_type][key] for message_type in {"email", "sms"})
for key in {"requested", "delivered", "failed"}
for key in {
"requested",
NotificationStatus.DELIVERED,
NotificationStatus.FAILED,
}
}
else:
stats = statistics[message_type]
if stats.get("failure") is not None:
stats["failed"] = stats["failure"]
stats[NotificationStatus.FAILED] = stats["failure"]
stats["pending"] = stats["requested"] - stats["delivered"] - stats["failed"]
stats[NotificationStatus.PENDING] = (
stats["requested"]
- stats[NotificationStatus.DELIVERED]
- stats[NotificationStatus.FAILED]
)
filters = [
# key, label, option
("requested", "total", "sending,delivered,failed"),
("pending", "pending", "sending,pending"),
("delivered", "delivered", "delivered"),
("failed", "failed", "failed"),
(NotificationStatus.PENDING, "pending", "sending,pending"),
(NotificationStatus.DELIVERED, "delivered", "delivered"),
(NotificationStatus.FAILED, "failed", "failed"),
]
return [
# return list containing label, option, link, count

View File

@@ -25,6 +25,7 @@ from app import (
service_api_client,
user_api_client,
)
from app.enums import NotificationStatus
from app.extensions import redis_client
from app.main import main
from app.main.forms import (
@@ -769,32 +770,41 @@ def filter_and_sort_services(services, trial_mode_services=False):
def create_global_stats(services):
stats = {
"email": {"delivered": 0, "failed": 0, "requested": 0},
"sms": {"delivered": 0, "failed": 0, "requested": 0},
"email": {
NotificationStatus.DELIVERED: 0,
NotificationStatus.FAILED: 0,
"requested": 0,
},
"sms": {
NotificationStatus.DELIVERED: 0,
NotificationStatus.FAILED: 0,
"requested": 0,
},
}
# Issue #1323. The back end is now sending 'failure' instead of
# 'failed'. Adjust it here, but keep it flexible in case
# the backend reverts to 'failed'.
for service in services:
if service["statistics"]["sms"].get("failure") is not None:
service["statistics"]["sms"]["failed"] = service["statistics"]["sms"][
"failure"
]
service["statistics"]["sms"][NotificationStatus.FAILED] = service[
"statistics"
]["sms"]["failure"]
if service["statistics"]["email"].get("failure") is not None:
service["statistics"]["email"]["failed"] = service["statistics"]["email"][
"failure"
]
service["statistics"]["email"][NotificationStatus.FAILED] = service[
"statistics"
]["email"]["failure"]
for service in services:
for msg_type, status in itertools.product(
("sms", "email"), ("delivered", "failed", "requested")
("sms", "email"),
(NotificationStatus.DELIVERED, NotificationStatus.FAILED, "requested"),
):
stats[msg_type][status] += service["statistics"][msg_type][status]
for stat in stats.values():
stat["failure_rate"] = get_formatted_percentage(
stat["failed"], stat["requested"]
stat[NotificationStatus.FAILED], stat["requested"]
)
return stats