Merge pull request #477 from alphagov/all-time-greatest-templates

Show template usage for all time on it’s own page
This commit is contained in:
Chris Hill-Scott
2016-04-21 09:08:49 +01:00
8 changed files with 102 additions and 21 deletions

View File

@@ -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,16 +49,23 @@ 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)
)
})
@main.route("/services/<service_id>/template-activity")
@login_required
@user_has_permissions('view_activity', admin_override=True)
def template_history(service_id):
return render_template(
'views/dashboard/all-template-statistics.html',
template_statistics=aggregate_usage(
template_statistics_client.get_template_statistics_for_service(service_id)
)
)
def add_rates_to(delivery_statistics):
if not delivery_statistics or not delivery_statistics[0]:
@@ -125,3 +127,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)
)
}

View File

@@ -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']

View File

@@ -0,0 +1,17 @@
{% extends "withnav_template.html" %}
{% block page_title %}
{{ current_service.name }} GOV.UK Notify
{% endblock %}
{% block maincolumn_content %}
<h1 class='heading-large'>
Templates used this year
</h1>
<p>
1 April 2016 to date
</p>
{% with period = "" %}
{% include 'views/dashboard/template-statistics.html' %}
{% endwith %}
{% endblock %}

View File

@@ -3,8 +3,8 @@
template_statistics,
caption="By template",
caption_visible=False,
empty_message='You havent set up any templates yet',
field_headings=['Template', hidden_field_heading('Type'), right_aligned_field_heading('Messages processed')]
empty_message='You havent used any templates {}'.format(period),
field_headings=['Template', hidden_field_heading('Type'), right_aligned_field_heading('Messages sent')]
) %}
{% call field() %}
<a href="{{ url_for('.view_template', service_id=current_service.id, template_id=item.template.id) }}">

View File

@@ -25,5 +25,9 @@
) }}
</div>
</div>
{% include 'views/dashboard/template-statistics.html' %}
{% with period = "in the last 7 days" %}
{% include 'views/dashboard/template-statistics.html' %}
{% endwith %}
<p class='table-show-more-link'>
<a href="{{ url_for('.template_history', service_id=current_service.id) }}">See all templates used this year</a>
</p>

View File

@@ -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={})

View File

@@ -78,14 +78,14 @@ 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')]
assert 'Test Service' in headers
assert 'In the last 7 days' in headers
template_usage_headers = [th.text.strip() for th in page.thead.find_all('th')]
for th in ['Template', 'Type', 'Messages processed']:
for th in ['Template', 'Type', 'Messages sent']:
assert th in template_usage_headers
table_rows = page.tbody.find_all('tr')
@@ -101,6 +101,49 @@ def test_should_show_recent_templates_on_dashboard(app_,
assert table_data[2].text.strip() == '13'
def test_should_show_all_templates_on_template_statistics_page(
app_,
mocker,
api_user_active,
mock_get_service,
mock_get_service_templates,
mock_get_service_statistics,
mock_get_user,
mock_get_user_by_email,
mock_login,
mock_get_jobs,
mock_has_permissions
):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
return_value=copy.deepcopy(stub_template_stats))
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
response = client.get(url_for('main.template_history', service_id=SERVICE_ONE_ID))
assert response.status_code == 200
response.get_data(as_text=True)
mock_template_stats.assert_called_once_with(SERVICE_ONE_ID)
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
headers = [header.text.strip() for header in page.find_all('h2')]
table_rows = page.tbody.find_all('tr')
assert len(table_rows) == 2
first_row = page.tbody.find_all('tr')[0]
table_data = first_row.find_all('td')
assert len(table_data) == 3
assert table_data[2].text.strip() == '206'
second_row = page.tbody.find_all('tr')[1]
table_data = second_row.find_all('td')
assert len(table_data) == 3
assert table_data[2].text.strip() == '13'
def _test_dashboard_menu(mocker, app_, usr, service, permissions):
with app_.test_request_context():
with app_.test_client() as client:

View File

@@ -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(