mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-14 23:08:07 -04:00
Show all statistics fetched on the dashboard
We want to align all our stats to be for the last 7 days. This means summing up the stats response from the API to make the Big Number. Previously the big number was counting sent notifications as successful. This commit changes it to only look at delivered notifications. Right now, the API doesn’t have a way of filtering to only show the last 7 days. So for the moment the dashboard will show statistics for all time. The upshot of this is that we can link from the dashboard to the activity page when there are failures.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from datetime import date
|
||||
from collections import namedtuple
|
||||
from itertools import groupby
|
||||
from functools import reduce
|
||||
|
||||
from flask import (
|
||||
render_template,
|
||||
@@ -29,25 +30,21 @@ from app.utils import user_has_permissions
|
||||
@login_required
|
||||
@user_has_permissions('view_activity', admin_override=True)
|
||||
def service_dashboard(service_id):
|
||||
templates = service_api_client.get_service_templates(service_id)['data']
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
|
||||
if session.get('invited_user'):
|
||||
session.pop('invited_user', None)
|
||||
return redirect(url_for("main.tour", service_id=service_id, page=1))
|
||||
|
||||
statistics = statistics_api_client.get_statistics_for_service(service_id)['data']
|
||||
template_statistics = aggregate_usage(template_statistics_client.get_template_statistics_for_service(service_id))
|
||||
|
||||
return render_template(
|
||||
'views/dashboard/dashboard.html',
|
||||
jobs=jobs[:5],
|
||||
more_jobs_to_show=(len(jobs) > 5),
|
||||
free_text_messages_remaining='250,000',
|
||||
spent_this_month='0.00',
|
||||
statistics=add_rates_to(statistics),
|
||||
templates=templates,
|
||||
template_statistics=template_statistics)
|
||||
statistics=add_rates_to(
|
||||
statistics_api_client.get_statistics_for_service(service_id)['data']
|
||||
),
|
||||
templates=service_api_client.get_service_templates(service_id)['data'],
|
||||
template_statistics=aggregate_usage(
|
||||
template_statistics_client.get_template_statistics_for_service(service_id)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/dashboard.json")
|
||||
@@ -60,8 +57,12 @@ def service_dashboard_updates(service_id):
|
||||
return jsonify(**{
|
||||
'today': render_template(
|
||||
'views/dashboard/today.html',
|
||||
statistics=add_rates_to(statistics),
|
||||
template_statistics=template_statistics
|
||||
statistics=add_rates_to(
|
||||
statistics_api_client.get_statistics_for_service(service_id)['data']
|
||||
),
|
||||
template_statistics=aggregate_usage(
|
||||
template_statistics_client.get_template_statistics_for_service(service_id)
|
||||
)
|
||||
)
|
||||
})
|
||||
|
||||
@@ -71,24 +72,32 @@ def add_rates_to(delivery_statistics):
|
||||
if not delivery_statistics or not delivery_statistics[0]:
|
||||
return {}
|
||||
|
||||
today = None
|
||||
latest_stats = {}
|
||||
if delivery_statistics[0]['day'] == date.today().strftime('%Y-%m-%d'):
|
||||
today = delivery_statistics[0]
|
||||
latest_stats = delivery_statistics[0]
|
||||
sum_of_statistics = reduce(
|
||||
lambda x, y: {
|
||||
key: x.get(key, 0) + y.get(key, 0)
|
||||
for key in [
|
||||
'emails_delivered',
|
||||
'emails_requested',
|
||||
'emails_failed',
|
||||
'sms_requested',
|
||||
'sms_delivered',
|
||||
'sms_failed'
|
||||
]
|
||||
},
|
||||
delivery_statistics
|
||||
)
|
||||
|
||||
latest_stats.update({
|
||||
'emails_failure_rate': (
|
||||
"{0:.1f}".format((float(today['emails_failed']) / today['emails_requested'] * 100))
|
||||
if today and today['emails_requested'] else 0
|
||||
return dict(
|
||||
emails_failure_rate=(
|
||||
"{0:.1f}".format((float(sum_of_statistics['emails_failed']) / sum_of_statistics['emails_requested'] * 100))
|
||||
if sum_of_statistics.get('emails_requested') else 0
|
||||
),
|
||||
'sms_failure_rate': (
|
||||
"{0:.1f}".format((float(today['sms_failed']) / today['sms_requested'] * 100))
|
||||
if today and today['sms_requested'] else 0
|
||||
)
|
||||
})
|
||||
|
||||
return latest_stats
|
||||
sms_failure_rate=(
|
||||
"{0:.1f}".format((float(sum_of_statistics['sms_failed']) / sum_of_statistics['sms_requested'] * 100))
|
||||
if sum_of_statistics.get('sms_requested') else 0
|
||||
),
|
||||
**sum_of_statistics
|
||||
)
|
||||
|
||||
|
||||
def aggregate_usage(template_statistics):
|
||||
|
||||
Reference in New Issue
Block a user