mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-22 08:58:46 -04:00
use new detailed service endpoint for dashboard totals
we don't want to use the old statistics endpoints any more also a couple of quality of life changes * moves some logic out of the _totals.html template * tidies up statistics_utils
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
from datetime import datetime, date, timedelta
|
||||
from collections import namedtuple
|
||||
from itertools import groupby
|
||||
import json
|
||||
|
||||
from flask import (
|
||||
render_template,
|
||||
redirect,
|
||||
url_for,
|
||||
session,
|
||||
jsonify,
|
||||
@@ -21,7 +19,7 @@ from app import (
|
||||
service_api_client,
|
||||
template_statistics_client
|
||||
)
|
||||
from app.statistics_utils import sum_of_statistics, add_rates_to, add_rate_to_jobs
|
||||
from app.statistics_utils import add_rates_to, get_formatted_percentage, add_rate_to_jobs
|
||||
from app.utils import user_has_permissions
|
||||
|
||||
|
||||
@@ -151,10 +149,13 @@ def get_dashboard_partials(service_id):
|
||||
job_api_client.get_job(service_id, limit_days=7)['data']
|
||||
))
|
||||
|
||||
service = service_api_client.get_service(service_id, detailed=True)
|
||||
|
||||
return {
|
||||
'totals': render_template(
|
||||
'views/dashboard/_totals.html',
|
||||
statistics=get_dashboard_totals(service_id)
|
||||
service_id=service_id,
|
||||
statistics=get_dashboard_totals(service)
|
||||
),
|
||||
'template-statistics': render_template(
|
||||
'views/dashboard/template-statistics.html',
|
||||
@@ -176,10 +177,11 @@ def get_dashboard_partials(service_id):
|
||||
}
|
||||
|
||||
|
||||
def get_dashboard_totals(service_id):
|
||||
return add_rates_to(sum_of_statistics(
|
||||
statistics_api_client.get_statistics_for_service(service_id, limit_days=7)['data']
|
||||
))
|
||||
def get_dashboard_totals(service):
|
||||
for msg_type in service['statistics']:
|
||||
msg_type['failed_percentage'] = get_formatted_percentage(msg_type['failed'], msg_type['requested'])
|
||||
msg_type['show_warning'] = msg_type['failed_percentage'] > 3
|
||||
return service['statistics']
|
||||
|
||||
|
||||
def calculate_usage(usage):
|
||||
|
||||
@@ -31,18 +31,12 @@ def sum_of_statistics(delivery_statistics):
|
||||
def add_rates_to(delivery_statistics):
|
||||
|
||||
return dict(
|
||||
emails_failure_rate=(
|
||||
"{0:.1f}".format(
|
||||
float(delivery_statistics['emails_failed']) / delivery_statistics['emails_requested'] * 100
|
||||
)
|
||||
if delivery_statistics['emails_requested'] else 0
|
||||
),
|
||||
sms_failure_rate=(
|
||||
"{0:.1f}".format(
|
||||
float(delivery_statistics['sms_failed']) / delivery_statistics['sms_requested'] * 100
|
||||
)
|
||||
if delivery_statistics['sms_requested'] else 0
|
||||
),
|
||||
email_failure_rate=get_formatted_percentage(
|
||||
delivery_statistics['email_failed'],
|
||||
delivery_statistics['email_requested']),
|
||||
sms_failure_rate=get_formatted_percentage(
|
||||
delivery_statistics['sms_failed'],
|
||||
delivery_statistics['sms_requested']),
|
||||
week_end_datetime=parser.parse(
|
||||
delivery_statistics.get('week_end', str(datetime.utcnow()))
|
||||
),
|
||||
@@ -50,6 +44,13 @@ def add_rates_to(delivery_statistics):
|
||||
)
|
||||
|
||||
|
||||
def get_formatted_percentage(x, tot):
|
||||
"""
|
||||
Return a percentage to one decimal place (respecting )
|
||||
"""
|
||||
return "{0:.1f}".format((float(x) / tot * 100)) if tot else 0
|
||||
|
||||
|
||||
def statistics_by_state(statistics):
|
||||
return {
|
||||
'sms': {
|
||||
|
||||
@@ -4,24 +4,24 @@
|
||||
<div class="grid-row">
|
||||
<div class="column-half">
|
||||
{{ big_number_with_status(
|
||||
statistics.emails_requested,
|
||||
message_count_label(statistics.emails_requested, 'email', suffix=''),
|
||||
statistics.emails_failed,
|
||||
statistics.get('emails_failure_rate', 0.0),
|
||||
statistics.get('emails_failure_rate', 0)|float > 3,
|
||||
failure_link=url_for(".view_notifications", service_id=current_service.id, message_type='email', status='failed'),
|
||||
link=url_for(".view_notifications", service_id=current_service.id, message_type='email', status='sending,delivered,failed')
|
||||
statistics['email']['requested'],
|
||||
message_count_label(statistics['email']['requested'], 'email', suffix=''),
|
||||
statistics['email']['failed'],
|
||||
statistics['email']['failed_percentage'],
|
||||
statistics['email']['show_warning'],
|
||||
failure_link=url_for(".view_notifications", service_id=id, message_type='email', status='failed'),
|
||||
link=url_for(".view_notifications", service_id=id, message_type='email', status='sending,delivered,failed')
|
||||
) }}
|
||||
</div>
|
||||
<div class="column-half">
|
||||
{{ big_number_with_status(
|
||||
statistics.sms_requested,
|
||||
message_count_label(statistics.sms_requested, 'sms', suffix=''),
|
||||
statistics.sms_failed,
|
||||
statistics.get('sms_failure_rate', 0.0),
|
||||
statistics.get('sms_failure_rate', 0)|float > 3,
|
||||
failure_link=url_for(".view_notifications", service_id=current_service.id, message_type='sms', status='failed'),
|
||||
link=url_for(".view_notifications", service_id=current_service.id, message_type='sms', status='sending,delivered,failed')
|
||||
statistics['sms']['requested'],
|
||||
message_count_label(statistics['sms']['requested'], 'sms', suffix=''),
|
||||
statistics['sms']['failed'],
|
||||
statistics['sms']['failed_percentage'],
|
||||
statistics['sms']['show_warning'],
|
||||
failure_link=url_for(".view_notifications", service_id=service_id, message_type='sms', status='failed'),
|
||||
link=url_for(".view_notifications", service_id=service_id, message_type='sms', status='sending,delivered,failed')
|
||||
) }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user