Files
notifications-admin/app/main/views/platform_admin.py
Rebecca Law 84445d154d When someone complains about an email from the platform we get a callback from SES.
A new platform admin page Email complaints has been added to surface those complaints.
Eventually the complaints will be visible to the services so they can remove the email address from their mailing list.

Next thing to implement is "x email complaints" warning on the platform admin summary page.
2018-06-06 15:22:48 +01:00

152 lines
4.8 KiB
Python

import itertools
from datetime import datetime
from flask import render_template, request
from flask_login import login_required
from app import complaint_api_client, service_api_client
from app.main import main
from app.main.forms import DateFilterForm
from app.statistics_utils import get_formatted_percentage
from app.utils import user_is_platform_admin
@main.route("/platform-admin")
@login_required
@user_is_platform_admin
def platform_admin():
form = DateFilterForm(request.args)
api_args = {'detailed': True,
'only_active': False, # specifically DO get inactive services
'include_from_test_key': form.include_from_test_key.data,
}
if form.start_date.data:
api_args['start_date'] = form.start_date.data
api_args['end_date'] = form.end_date.data or datetime.utcnow().date()
platform_stats = service_api_client.get_aggregate_platform_stats(api_args)
for stat in platform_stats.values():
stat['failure_rate'] = get_formatted_percentage(stat['failed'], stat['requested'])
return render_template(
'views/platform-admin/index.html',
include_from_test_key=form.include_from_test_key.data,
form=form,
global_stats=platform_stats,
)
@main.route("/platform-admin/live-services", endpoint='live_services')
@main.route("/platform-admin/trial-services", endpoint='trial_services')
@login_required
@user_is_platform_admin
def platform_admin_services():
form = DateFilterForm(request.args)
api_args = {'detailed': True,
'only_active': False, # specifically DO get inactive services
'include_from_test_key': form.include_from_test_key.data,
}
if form.start_date.data:
api_args['start_date'] = form.start_date.data
api_args['end_date'] = form.end_date.data or datetime.utcnow().date()
services = filter_and_sort_services(
service_api_client.get_services(api_args)['data'],
trial_mode_services=request.endpoint == 'main.trial_services',
)
return render_template(
'views/platform-admin/services.html',
include_from_test_key=form.include_from_test_key.data,
form=form,
services=list(format_stats_by_service(services)),
page_title='{} services'.format(
'Trial mode' if request.endpoint == 'main.trial_services' else 'Live'
),
global_stats=create_global_stats(services),
)
@main.route("/platform-admin/complaints")
@login_required
@user_is_platform_admin
def platform_admin_list_complaints():
complaints = complaint_api_client.get_all_complaints()
return render_template(
'views/platform-admin/complaints.html',
complaints=complaints,
page_title='All Complaints',
)
def sum_service_usage(service):
total = 0
for notification_type in service['statistics'].keys():
total += service['statistics'][notification_type]['requested']
return total
def filter_and_sort_services(services, trial_mode_services=False):
return [
service for service in sorted(
services,
key=lambda service: (
service['active'],
sum_service_usage(service),
service['created_at']
),
reverse=True,
)
if service['restricted'] == trial_mode_services
]
def create_global_stats(services):
stats = {
'email': {
'delivered': 0,
'failed': 0,
'requested': 0
},
'sms': {
'delivered': 0,
'failed': 0,
'requested': 0
},
'letter': {
'delivered': 0,
'failed': 0,
'requested': 0
}
}
for service in services:
for msg_type, status in itertools.product(('sms', 'email', 'letter'), ('delivered', 'failed', 'requested')):
stats[msg_type][status] += service['statistics'][msg_type][status]
for stat in stats.values():
stat['failure_rate'] = get_formatted_percentage(stat['failed'], stat['requested'])
return stats
def format_stats_by_service(services):
for service in services:
yield {
'id': service['id'],
'name': service['name'],
'stats': {
msg_type: {
'sending': stats['requested'] - stats['delivered'] - stats['failed'],
'delivered': stats['delivered'],
'failed': stats['failed'],
}
for msg_type, stats in service['statistics'].items()
},
'restricted': service['restricted'],
'research_mode': service['research_mode'],
'created_at': service['created_at'],
'active': service['active']
}