Add usage to the dashboard

Takes the number of emails and SMS fragments sent from:
https://github.com/alphagov/notifications-api/pull/273

Using these numbers it’s possible to show:
- how much of your allowance is left
- or how much you have spent

For now the allowance and rates are hard coded.

Only for users that have manage service.
This commit is contained in:
Chris Hill-Scott
2016-04-20 15:37:17 +01:00
parent f6ba5ec8d0
commit 00030bc254
14 changed files with 197 additions and 13 deletions

View File

@@ -80,6 +80,16 @@ def template_history(service_id):
)
@main.route("/services/<service_id>/usage")
@login_required
@user_has_permissions('manage_settings', admin_override=True)
def usage(service_id):
return render_template(
'views/usage.html',
**get_dashboard_statistics_for_service(service_id)
)
def add_rates_to(delivery_statistics):
keys = [
@@ -148,11 +158,25 @@ def aggregate_usage(template_statistics):
def get_dashboard_statistics_for_service(service_id):
usage = service_api_client.get_service_usage(service_id)
sms_free_allowance = 250000
sms_rate = 0.018
sms_sent = usage['data'].get('sms_count', 0)
emails_sent = usage['data'].get('email_count', 0)
return {
'statistics': add_rates_to(
statistics_api_client.get_statistics_for_service(service_id, limit_days=7)['data']
),
'template_statistics': aggregate_usage(
template_statistics_client.get_template_statistics_for_service(service_id, limit_days=7)
)
),
'emails_sent': emails_sent,
'sms_free_allowance': sms_free_allowance,
'sms_sent': sms_sent,
'sms_allowance_remaining': max(0, (sms_free_allowance - sms_sent)),
'sms_chargeable': max(0, sms_sent - sms_free_allowance),
'sms_rate': sms_rate,
}