Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some data types and refactor to help with tracking down the ajax bug #1614

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions app/main/views/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from app.models.enum.notification_statuses import NotificationStatuses
from app.models.enum.template_types import TemplateType
from app.statistics_utils import add_rate_to_job, get_formatted_percentage
from app.types import ServiceStatisticsSimple, ServiceStatistics, TemplateStatistics, StatisticsSimple, Statistics
from app.utils import (
DELIVERED_STATUSES,
FAILURE_STATUSES,
Expand Down Expand Up @@ -207,7 +208,7 @@ def monthly(service_id):
)


def filter_out_cancelled_stats(template_statistics):
def filter_out_cancelled_stats(template_statistics: list[TemplateStatistics]):
return [s for s in template_statistics if s["status"] != "cancelled"]


Expand All @@ -233,11 +234,12 @@ def aggregate_template_usage(template_statistics, sort_key="count"):
return sorted(templates, key=lambda x: x[sort_key], reverse=True)


def aggregate_notifications_stats(template_statistics):
def aggregate_notifications_stats(template_statistics: list[TemplateStatistics]) -> ServiceStatisticsSimple:
template_statistics = filter_out_cancelled_stats(template_statistics)
notifications = {
template_type: {status: 0 for status in ("requested", "delivered", "failed")}
for template_type in ["sms", "email", "letter"]
notifications: ServiceStatisticsSimple = {
"sms": {"requested": 0, "delivered": 0, "failed": 0},
"email": {"requested": 0, "delivered": 0, "failed": 0},
"letter": {"requested": 0, "delivered": 0, "failed": 0}
}
for stat in template_statistics:
notifications[stat["template_type"]]["requested"] += stat["count"]
Expand Down Expand Up @@ -369,12 +371,22 @@ def calculate_bounce_rate(all_statistics_daily, dashboard_totals_daily):
return bounce_rate


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["show_warning"] = float(msg_type["failed_percentage"]) > 3
return statistics
def add_to_stats(simple_stats: StatisticsSimple) -> Statistics:
failed_percentage = get_formatted_percentage(simple_stats["failed"], simple_stats["requested"])
return {
**simple_stats,
"failed_percentage": failed_percentage,
"show_warning": float(failed_percentage) > 3,
}


def get_dashboard_totals(statistics: ServiceStatisticsSimple) -> ServiceStatistics:
return {
"sms": add_to_stats(statistics["sms"]),
"email": add_to_stats(statistics["email"]),
"letter": add_to_stats(statistics["letter"]),
}


def calculate_usage(usage, free_sms_fragment_limit):
sms_breakdowns = [breakdown for breakdown in usage if breakdown["notification_type"] == "sms"]
Expand Down
5 changes: 3 additions & 2 deletions app/notify_client/template_statistics_api_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import List
from app.notify_client import NotifyAdminAPIClient

from app.types import TemplateStatistics

class TemplateStatisticsApiClient(NotifyAdminAPIClient):
def get_template_statistics_for_service(self, service_id, limit_days=None):
def get_template_statistics_for_service(self, service_id, limit_days=None) -> List[TemplateStatistics]:
params = {}
if limit_days is not None:
params["limit_days"] = limit_days
Expand Down
29 changes: 29 additions & 0 deletions app/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,32 @@ class EmailReplyTo(TypedDict):
is_default: bool
service_id: str
updated_at: Optional[bool]


class TemplateStatistics(TypedDict):
count: int
template_id: str
template_name: str
template_type: str
is_precompiled_letter: bool
status: str


class StatisticsSimple(TypedDict):
requested: int
delivered: int
failed: int

class Statistics(StatisticsSimple):
failed_percentage: str
show_warning: bool

class ServiceStatisticsSimple(TypedDict):
sms: StatisticsSimple
email: StatisticsSimple
letter: StatisticsSimple

class ServiceStatistics(TypedDict):
sms: Statistics
email: Statistics
letter: Statistics