From 93d7ca405254e53492d4cc8947344c5cf46fb355 Mon Sep 17 00:00:00 2001 From: Beverly Nguyen Date: Mon, 6 May 2024 17:23:38 -0700 Subject: [PATCH 1/2] added pending and requested status to monthly stats dict --- app/main/views/dashboard.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 8453ef369..89d05d6a6 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -24,6 +24,7 @@ from app.utils import ( DELIVERED_STATUSES, FAILURE_STATUSES, REQUESTED_STATUSES, + SENDING_STATUSES, service_has_permission, ) from app.utils.csv import Spreadsheet @@ -341,6 +342,9 @@ def get_dashboard_partials(service_id): service_id, free_sms_fragment_limit=free_sms_allowance ) + monthly_stats = format_monthly_stats_to_list( + service_api_client.get_monthly_notification_stats(service_id, get_current_financial_year())["data"] + ) yearly_usage = billing_api_client.get_annual_usage_for_service( service_id, get_current_financial_year(), @@ -366,6 +370,7 @@ def get_dashboard_partials(service_id): ), "usage": render_template( "views/dashboard/_usage.html", + monthly_stats=monthly_stats, **get_annual_usage_breakdown(yearly_usage, free_sms_allowance), ), } @@ -424,6 +429,8 @@ def aggregate_status_types(counts_dict): "{}_counts".format(message_type): { "failed": sum(stats.get(status, 0) for status in FAILURE_STATUSES), "requested": sum(stats.get(status, 0) for status in REQUESTED_STATUSES), + "delivered": sum(stats.get(status, 0) for status in DELIVERED_STATUSES), + "pending": sum(stats.get(status, 0) for status in SENDING_STATUSES), } for message_type, stats in counts_dict.items() } From 758c1cd8a0f59c337a60106536c7981898d8cc57 Mon Sep 17 00:00:00 2001 From: Beverly Nguyen Date: Wed, 29 May 2024 12:13:09 -0700 Subject: [PATCH 2/2] added new api for month, by year, previous 7 day stats --- app/main/views/dashboard.py | 34 ++++++++++++++++++++----- app/notify_client/service_api_client.py | 18 +++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 89d05d6a6..33e98e104 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -19,6 +19,7 @@ from app import ( ) from app.formatters import format_date_numeric, format_datetime_numeric, get_time_left from app.main import main +from app.models.user import User from app.statistics_utils import get_formatted_percentage from app.utils import ( DELIVERED_STATUSES, @@ -324,6 +325,11 @@ def aggregate_notifications_stats(template_statistics): def get_dashboard_partials(service_id): + current_financial_year = get_current_financial_year() + current_month = get_current_month_for_financial_year(current_financial_year) + start_date = datetime.now().strftime('%Y-%m-%d') + days=7 + all_statistics = template_statistics_client.get_template_statistics_for_service( service_id, limit_days=7 ) @@ -336,19 +342,30 @@ def get_dashboard_partials(service_id): ) # These 2 calls will update the dashboard sms allowance count while in trial mode. billing_api_client.get_monthly_usage_for_service( - service_id, get_current_financial_year() + service_id, current_financial_year ) billing_api_client.create_or_update_free_sms_fragment_limit( service_id, free_sms_fragment_limit=free_sms_allowance ) - - monthly_stats = format_monthly_stats_to_list( - service_api_client.get_monthly_notification_stats(service_id, get_current_financial_year())["data"] - ) yearly_usage = billing_api_client.get_annual_usage_for_service( service_id, - get_current_financial_year(), + current_financial_year, ) + + #Previous 7 day stats + daily_stats = service_api_client.get_service_notification_statistics_by_day(service_id, start_date=start_date, days=days) + + #Single month stats + single_month_notification_stats = service_api_client.get_single_month_notification_stats(service_id, year=current_financial_year, month=current_month) + + #monthly stats by year + monthly_stats = format_monthly_stats_to_list( + service_api_client.get_monthly_notification_stats(service_id, current_financial_year)["data"] + ) + + # user=User.from_id(user_id), + # single_month_notification_stats = service_api_client.get_single_month_notification_stats_by_user(service_id, user, year=current_financial_year, month=current_month) + return { "upcoming": render_template( "views/dashboard/_upcoming.html", @@ -441,6 +458,11 @@ def get_months_for_financial_year(year, time_format="%B"): return [month.strftime(time_format) for month in (get_months_for_year(1, 13, year))] +def get_current_month_for_financial_year(year): + current_month = datetime.now().month + return current_month + + def get_months_for_year(start, end, year): return [datetime(year, month, 1) for month in range(start, end)] diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index d34516b8b..f2cc2f934 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -43,6 +43,24 @@ class ServiceAPIClient(NotifyAdminAPIClient): params={"limit_days": limit_days}, )["data"] + def get_service_notification_statistics_by_day(self, service_id, start_date=None, days=None): + if start_date is None: + start_date = datetime.now().strftime('%Y-%m-%d') + + return self.get( + "/service/{0}/statistics/{1}/{2}".format(service_id, start_date, days), + )["data"] + + def get_single_month_notification_stats(self, service_id, year, month): + return self.get( + "/service/{0}/notifications/month?year={1}&month={2}".format(service_id, year, month), + ) + + # def get_single_month_notification_stats(self, service_id, user_id, year, month): + # return self.get( + # "/service/{0}/notifications//month?year={1}&month={2}".format(service_id, user_id, year, month), + # ) + def get_services(self, params_dict=None): """ Retrieve a list of services.