diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 2fea2ddfd..edb6c521e 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -38,13 +38,8 @@ def service_dashboard(service_id): return render_template( 'views/dashboard/dashboard.html', - statistics=add_rates_to( - statistics_api_client.get_statistics_for_service(service_id, limit_days=7)['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) - ) + **get_dashboard_statistics_for_service(service_id) ) @@ -54,12 +49,7 @@ def service_dashboard_updates(service_id): return jsonify(**{ 'today': render_template( 'views/dashboard/today.html', - 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) - ) + **get_dashboard_statistics_for_service(service_id) ) }) @@ -125,3 +115,14 @@ def aggregate_usage(template_statistics): key=lambda row: row['usage_count'], reverse=True ) + + +def get_dashboard_statistics_for_service(service_id): + 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) + ) + } \ No newline at end of file diff --git a/app/notify_client/template_statistics_api_client.py b/app/notify_client/template_statistics_api_client.py index a47e934fb..2ac088f76 100644 --- a/app/notify_client/template_statistics_api_client.py +++ b/app/notify_client/template_statistics_api_client.py @@ -12,7 +12,11 @@ class TemplateStatisticsApiClient(BaseAPIClient): self.client_id = app.config['ADMIN_CLIENT_USER_NAME'] self.secret = app.config['ADMIN_CLIENT_SECRET'] - def get_template_statistics_for_service(self, service_id): + def get_template_statistics_for_service(self, service_id, limit_days=None): + params = {} + if limit_days is not None: + params['limit_days'] = limit_days return self.get( url='/service/{}/template-statistics'.format(service_id), + params=params )['data'] diff --git a/tests/app/main/notify_client/test_template_statistics_client.py b/tests/app/main/notify_client/test_template_statistics_client.py index 03fb33e4c..938eb4254 100644 --- a/tests/app/main/notify_client/test_template_statistics_client.py +++ b/tests/app/main/notify_client/test_template_statistics_client.py @@ -14,4 +14,4 @@ def test_template_statistics_client_calls_correct_api_endpoint(mocker, api_user_ client.get_template_statistics_for_service(some_service_id) - mock_get.assert_called_once_with(url=expected_url) + mock_get.assert_called_once_with(url=expected_url, params={}) diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index 9717e60c2..12082f1c7 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -78,7 +78,7 @@ def test_should_show_recent_templates_on_dashboard(app_, assert response.status_code == 200 response.get_data(as_text=True) mock_get_service_statistics.assert_called_once_with(SERVICE_ONE_ID, limit_days=7) - mock_template_stats.assert_called_once_with(SERVICE_ONE_ID) + mock_template_stats.assert_called_once_with(SERVICE_ONE_ID, limit_days=7) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') headers = [header.text.strip() for header in page.find_all('h2')] diff --git a/tests/conftest.py b/tests/conftest.py index d6dbdad17..9d68c6eb9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -826,7 +826,7 @@ def mock_get_template_statistics(mocker, service_one, fake_uuid): "day": "2016-04-04" } - def _get_stats(service_id): + def _get_stats(service_id, limit_days=None): return [data] return mocker.patch(