mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-19 05:59:44 -04:00
Merge pull request #619 from alphagov/platform-admin-stats
Headline stats on the platform admin page
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
from datetime import datetime, date, timedelta
|
||||
from dateutil import parser
|
||||
from collections import namedtuple
|
||||
from itertools import groupby
|
||||
from functools import reduce
|
||||
|
||||
from flask import (
|
||||
render_template,
|
||||
@@ -21,7 +19,7 @@ from app import (
|
||||
service_api_client,
|
||||
template_statistics_client
|
||||
)
|
||||
|
||||
from app.statistics_utils import sum_of_statistics, add_rates_to
|
||||
from app.utils import user_has_permissions
|
||||
|
||||
|
||||
@@ -114,53 +112,6 @@ def weekly(service_id):
|
||||
)
|
||||
|
||||
|
||||
def sum_of_statistics(delivery_statistics):
|
||||
|
||||
statistics_keys = (
|
||||
'emails_delivered',
|
||||
'emails_requested',
|
||||
'emails_failed',
|
||||
'sms_requested',
|
||||
'sms_delivered',
|
||||
'sms_failed'
|
||||
)
|
||||
|
||||
if not delivery_statistics or not delivery_statistics[0]:
|
||||
return {
|
||||
key: 0 for key in statistics_keys
|
||||
}
|
||||
|
||||
return reduce(
|
||||
lambda x, y: {
|
||||
key: x.get(key, 0) + y.get(key, 0)
|
||||
for key in statistics_keys
|
||||
},
|
||||
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
|
||||
),
|
||||
week_end_datetime=parser.parse(
|
||||
delivery_statistics.get('week_end', str(datetime.utcnow()))
|
||||
),
|
||||
**delivery_statistics
|
||||
)
|
||||
|
||||
|
||||
def aggregate_usage(template_statistics):
|
||||
|
||||
immutable_template = namedtuple('Template', ['template_type', 'name', 'id'])
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
from datetime import datetime
|
||||
|
||||
import pytz
|
||||
from flask import render_template
|
||||
from flask_login import login_required
|
||||
|
||||
from app import statistics_api_client
|
||||
from app.main import main
|
||||
from app.utils import user_has_permissions
|
||||
from app.statistics_utils import sum_of_statistics, add_rates_to
|
||||
|
||||
|
||||
@main.route("/platform-admin")
|
||||
@@ -10,5 +15,12 @@ from app.utils import user_has_permissions
|
||||
@user_has_permissions(admin_override=True)
|
||||
def platform_admin():
|
||||
return render_template(
|
||||
'views/platform-admin.html'
|
||||
'views/platform-admin.html',
|
||||
global_stats=get_global_stats()
|
||||
)
|
||||
|
||||
|
||||
def get_global_stats():
|
||||
day = datetime.now(tz=pytz.timezone('Europe/London')).date()
|
||||
all_stats = statistics_api_client.get_statistics_for_all_services_for_day(day)['data']
|
||||
return add_rates_to(sum_of_statistics(all_stats))
|
||||
|
||||
Reference in New Issue
Block a user