mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-13 23:14:32 -05: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>
|
||||
|
||||
@@ -13,11 +13,11 @@ class TestClient(FlaskClient):
|
||||
session['user_id'] = user.id
|
||||
session['_fresh'] = True
|
||||
if mocker:
|
||||
mocker.patch('app.user_api_client.get_user', return_value=user)
|
||||
mocker.patch('app.events_api_client.create_event')
|
||||
if mocker and service:
|
||||
session['service_id'] = service['id']
|
||||
mocker.patch('app.service_api_client.get_service', return_value={'data': service})
|
||||
get_user_mock = mocker.patch('app.user_api_client.get_user', return_value=user)
|
||||
create_event_mock = mocker.patch('app.events_api_client.create_event')
|
||||
if service:
|
||||
session['service_id'] = service['id']
|
||||
get_service_mock = mocker.patch('app.service_api_client.get_service', return_value={'data': service})
|
||||
login_user(user, remember=True)
|
||||
|
||||
def login_fresh(self):
|
||||
|
||||
@@ -417,18 +417,19 @@ def test_aggregate_template_stats():
|
||||
assert item['usage_count'] == 206
|
||||
|
||||
|
||||
def test_service_dashboard_updates_gets_detailed_service(mocker,
|
||||
def test_service_dashboard_updates_gets_dashboard_totals(mocker,
|
||||
app_,
|
||||
active_user_with_permissions,
|
||||
service_one,
|
||||
mock_get_service,
|
||||
mock_get_service_statistics,
|
||||
mock_get_usage,
|
||||
):
|
||||
mock_get_usage):
|
||||
dashboard_totals = mocker.patch('app.main.views.dashboard.get_dashboard_totals', return_value={
|
||||
'email': {'requested': 0, 'delivered': 0, 'failed': 0},
|
||||
'sms': {'requested': 0, 'delivered': 0, 'failed': 0}
|
||||
})
|
||||
|
||||
with app_.test_request_context(), app_.test_client() as client:
|
||||
client.login(active_user_with_permissions, mocker, service_one)
|
||||
response = client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
|
||||
|
||||
assert response.status_code == 200
|
||||
# mock_get_service_statistics.assert_not_called()
|
||||
# mock_get_service.assert_called_once_with(service_one.id, detailed=True)
|
||||
|
||||
Reference in New Issue
Block a user