From 2a502753a4a00f0e029af61a7d5072b42dbda40f Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 25 Jan 2017 15:59:06 +0000 Subject: [PATCH] Filter and navigate usage by financial year MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Right now we tell people that the usage page is for the current financial year. This is a lie – it’s for all time. So this commit calls through to the API to get the stats for (by default) the current financial year. We already do this for the monthly breakdown, this just does the same thing for the yearly totals. It also adds navigation to show the data for other financial years: - previous so you can go back and see your usage and verify that the bill you’re about to pay is correct - next so that you can check what your SMS allowance is going to be before you actually get into it --- app/main/views/dashboard.py | 26 +++++-- app/notify_client/service_api_client.py | 7 +- app/templates/components/pill.html | 7 +- app/templates/views/usage.html | 92 +++++++++++++------------ app/utils.py | 8 +++ tests/app/main/views/test_dashboard.py | 41 ++++++----- tests/app/test_utils.py | 4 ++ tests/conftest.py | 2 +- 8 files changed, 112 insertions(+), 75 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index d91cbbef9..f171faf26 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -19,7 +19,7 @@ from app import ( template_statistics_client ) from app.statistics_utils import get_formatted_percentage, add_rate_to_job -from app.utils import user_has_permissions +from app.utils import user_has_permissions, get_current_financial_year # This is a placeholder view method to be replaced @@ -79,17 +79,26 @@ def template_history(service_id): @login_required @user_has_permissions('manage_settings', admin_override=True) def usage(service_id): + current_financial_year = get_current_financial_year() try: - year = int(request.args.get('year', 2016)) + year = int(request.args.get('year', current_financial_year)) except ValueError: abort(404) return render_template( 'views/usage.html', - months=get_free_paid_breakdown_for_billable_units( + months=list(get_free_paid_breakdown_for_billable_units( year, service_api_client.get_billable_units(service_id, year) - ), - year=year, - **calculate_usage(service_api_client.get_service_usage(service_id)['data']) + )), + selected_year=year, + years=[ + ( + 'financial year', + year, + url_for('.usage', service_id=service_id, year=year), + '{} to {}'.format(year, year + 1), + ) for year in range(current_financial_year - 1, current_financial_year + 2) + ], + **calculate_usage(service_api_client.get_service_usage(service_id, year)['data']) ) @@ -156,7 +165,10 @@ def get_dashboard_partials(service_id): 'has_jobs': bool(immediate_jobs), 'usage': render_template( 'views/dashboard/_usage.html', - **calculate_usage(service_api_client.get_service_usage(service_id)['data']) + **calculate_usage(service_api_client.get_service_usage( + service_id, + get_current_financial_year(), + )['data']) ), } diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index 440301428..211eb46a1 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -213,8 +213,11 @@ class ServiceAPIClient(NotifyAdminAPIClient): def get_service_history(self, service_id): return self.get('/service/{0}/history'.format(service_id)) - def get_service_usage(self, service_id): - return self.get('/service/{0}/fragment/aggregate_statistics'.format(service_id)) + def get_service_usage(self, service_id, year=None): + return self.get( + '/service/{0}/fragment/aggregate_statistics'.format(service_id), + params=dict(year=year) + ) def get_weekly_notification_stats(self, service_id): return self.get(url='/service/{}/notifications/weekly'.format(service_id)) diff --git a/app/templates/components/pill.html b/app/templates/components/pill.html index 7f283d204..c77810e5c 100644 --- a/app/templates/components/pill.html +++ b/app/templates/components/pill.html @@ -3,7 +3,8 @@ {% macro pill( title, items=[], - current_value=None + current_value=None, + big_number_args={'smaller': True} ) %}