Merge pull request #876 from alphagov/use_new_template_stats_endpoint

Use new template stats endpoint
This commit is contained in:
minglis
2016-08-22 16:47:59 +01:00
committed by GitHub
6 changed files with 96 additions and 161 deletions

View File

@@ -66,11 +66,12 @@ def template_history(service_id):
template_statistics = aggregate_usage( template_statistics = aggregate_usage(
template_statistics_client.get_template_statistics_for_service(service_id) template_statistics_client.get_template_statistics_for_service(service_id)
) )
return render_template( return render_template(
'views/dashboard/all-template-statistics.html', 'views/dashboard/all-template-statistics.html',
template_statistics=template_statistics, template_statistics=template_statistics,
most_used_template_count=max( most_used_template_count=max(
[row['usage_count'] for row in template_statistics] or [0] [row['count'] for row in template_statistics] or [0]
) )
) )
@@ -98,32 +99,9 @@ def weekly(service_id):
def aggregate_usage(template_statistics): def aggregate_usage(template_statistics):
immutable_template = namedtuple('Template', ['template_type', 'name', 'id'])
# grouby requires the list to be sorted by template first
statistics_sorted_by_template = sorted(
(
(
immutable_template(**row['template']),
row['usage_count']
)
for row in template_statistics
),
key=lambda items: items[0]
)
# then group and sort the result by usage
return sorted( return sorted(
( template_statistics,
{ key=lambda template_statistic: template_statistic['template_name']
'usage_count': sum(usage[1] for usage in usages),
'template': template
}
for template, usages in groupby(statistics_sorted_by_template, lambda items: items[0])
),
key=lambda row: row['usage_count'],
reverse=True
) )
@@ -149,7 +127,7 @@ def get_dashboard_partials(service_id):
'views/dashboard/template-statistics.html', 'views/dashboard/template-statistics.html',
template_statistics=template_statistics, template_statistics=template_statistics,
most_used_template_count=max( most_used_template_count=max(
[row['usage_count'] for row in template_statistics] or [0] [row['count'] for row in template_statistics] or [0]
), ),
), ),
'has_template_statistics': bool(template_statistics), 'has_template_statistics': bool(template_statistics),

View File

@@ -28,6 +28,7 @@ class TemplateStatisticsApiClient(BaseAPIClient):
return [] return []
def get_template_statistics_for_template(self, service_id, template_id): def get_template_statistics_for_template(self, service_id, template_id):
return self.get( return self.get(
url='/service/{}/template-statistics/{}'.format(service_id, template_id) url='/service/{}/template-statistics/{}'.format(service_id, template_id)
)['data'] )['data']

View File

@@ -10,9 +10,6 @@
<div class="column-half"> <div class="column-half">
<h2 class="heading-large">Templates sent</h2> <h2 class="heading-large">Templates sent</h2>
</div> </div>
<div class="column-half">
<span class="align-with-heading-copy">1 April 2016 to date</span>
</div>
</div> </div>
{% include 'views/dashboard/template-statistics.html' %} {% include 'views/dashboard/template-statistics.html' %}

View File

@@ -17,18 +17,18 @@
) %} ) %}
{% call row_heading() %} {% call row_heading() %}
<span class="spark-bar-label"> <span class="spark-bar-label">
<a href="{{ url_for('.view_template', service_id=current_service.id, template_id=item.template.id) }}">{{ item.template.name }}</a> <a href="{{ url_for('.view_template', service_id=current_service.id, template_id=item.template_id) }}">{{ item.template_name }}</a>
<span class="file-list-hint"> <span class="file-list-hint">
{{ message_count_label(1, item.template.template_type, suffix='template')|capitalize }} {{ message_count_label(1, item.template_type, suffix='template')|capitalize }}
</span> </span>
</span> </span>
{% endcall %} {% endcall %}
{% call field() %} {% call field() %}
{% if template_statistics|length > 1 %} {% if template_statistics|length > 1 %}
<span class="spark-bar"> <span class="spark-bar">
<span style="width: {{ item.usage_count / most_used_template_count * 100 }}%"> <span style="width: {{ item.count / most_used_template_count * 100 }}%">
{{ big_number( {{ big_number(
item.usage_count, item.count,
smallest=True smallest=True
) }} ) }}
</span> </span>
@@ -36,7 +36,7 @@
{% else %} {% else %}
<span class="heading-small"> <span class="heading-small">
{{ big_number( {{ big_number(
item.usage_count, item.count,
smallest=True smallest=True
) }} ) }}
</span> </span>

View File

@@ -11,70 +11,36 @@ from app.main.views.dashboard import get_dashboard_totals, format_weekly_stats_t
from tests import validate_route_permission from tests import validate_route_permission
from tests.conftest import SERVICE_ONE_ID from tests.conftest import SERVICE_ONE_ID
stub_template_stats = [ stub_template_stats = [
{ {
'template': { 'template_type': 'sms',
'name': 'Brine Shrimp', 'template_name': 'one',
'template_type': 'sms', 'template_id': 'id-1',
'id': 1 'count': 100
},
'id': '6005e192-4738-4962-beec-ebd982d0b03f',
'day': '2016-04-06',
'usage_count': 6,
'service': '1491b86f-c950-48f5-bed1-2a55df027ecb'
}, },
{ {
'template': { 'template_type': 'email',
'name': 'Pickle feet', 'template_name': 'two',
'template_type': 'sms', 'template_id': 'id-2',
'id': 2 'count': 200
}, }
'id': '0bd529cd-a0fd-43e5-80ee-b95ef6b0d51f',
'day': '2016-04-06',
'usage_count': 6,
'service': '1491b86f-c950-48f5-bed1-2a55df027ecb'
},
{
'template': {
'name': 'Brine Shrimp',
'template_type': 'sms',
'id': 1
},
'id': '24531628-ffff-4082-a443-9f6db5af83d9',
'day': '2016-04-05',
'usage_count': 7,
'service': '1491b86f-c950-48f5-bed1-2a55df027ecb'
},
{
'template': {
'name': 'Pickle feet',
'template_type': 'sms',
'id': 2
},
'id': '0bd529cd-a0fd-43e5-80ee-b95ef6b0d51f',
'day': '2016-03-06',
'usage_count': 200,
'service': '1491b86f-c950-48f5-bed1-2a55df027ecb'
},
] ]
def test_get_started( def test_get_started(
app_, app_,
mocker, mocker,
api_user_active, api_user_active,
mock_get_service, mock_get_service,
mock_get_service_templates_when_no_templates_exist, mock_get_service_templates_when_no_templates_exist,
mock_get_user, mock_get_user,
mock_get_user_by_email, mock_get_user_by_email,
mock_login, mock_login,
mock_get_jobs, mock_get_jobs,
mock_has_permissions, mock_has_permissions,
mock_get_detailed_service, mock_get_detailed_service,
mock_get_usage mock_get_usage
): ):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service', mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
return_value=copy.deepcopy(stub_template_stats)) return_value=copy.deepcopy(stub_template_stats))
@@ -83,24 +49,23 @@ def test_get_started(
response = client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID)) response = client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
# mock_get_service_templates_when_no_templates_exist.assert_called_once_with(SERVICE_ONE_ID) # mock_get_service_templates_when_no_templates_exist.assert_called_once_with(SERVICE_ONE_ID)
print(response.get_data(as_text=True))
assert response.status_code == 200 assert response.status_code == 200
assert 'Get started' in response.get_data(as_text=True) assert 'Get started' in response.get_data(as_text=True)
def test_get_started_is_hidden_once_templates_exist( def test_get_started_is_hidden_once_templates_exist(
app_, app_,
mocker, mocker,
api_user_active, api_user_active,
mock_get_service, mock_get_service,
mock_get_service_templates, mock_get_service_templates,
mock_get_user, mock_get_user,
mock_get_user_by_email, mock_get_user_by_email,
mock_login, mock_login,
mock_get_jobs, mock_get_jobs,
mock_has_permissions, mock_has_permissions,
mock_get_detailed_service, mock_get_detailed_service,
mock_get_usage mock_get_usage
): ):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service', mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
return_value=copy.deepcopy(stub_template_stats)) return_value=copy.deepcopy(stub_template_stats))
@@ -125,7 +90,6 @@ def test_should_show_recent_templates_on_dashboard(app_,
mock_has_permissions, mock_has_permissions,
mock_get_detailed_service, mock_get_detailed_service,
mock_get_usage): mock_get_usage):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service', mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
return_value=copy.deepcopy(stub_template_stats)) return_value=copy.deepcopy(stub_template_stats))
@@ -147,28 +111,27 @@ def test_should_show_recent_templates_on_dashboard(app_,
assert len(table_rows) == 2 assert len(table_rows) == 2
assert 'Pickle feet' in table_rows[0].find_all('th')[0].text assert 'one' in table_rows[0].find_all('th')[0].text
assert 'Text message template' in table_rows[0].find_all('th')[0].text assert 'Text message template' in table_rows[0].find_all('th')[0].text
assert '206' in table_rows[0].find_all('td')[0].text assert '100' in table_rows[0].find_all('td')[0].text
assert 'Brine Shrimp' in table_rows[1].find_all('th')[0].text assert 'two' in table_rows[1].find_all('th')[0].text
assert 'Text message template' in table_rows[1].find_all('th')[0].text assert 'Email template' in table_rows[1].find_all('th')[0].text
assert '13' in table_rows[1].find_all('td')[0].text assert '200' in table_rows[1].find_all('td')[0].text
def test_should_show_all_templates_on_template_statistics_page( def test_should_show_all_templates_on_template_statistics_page(
app_, app_,
mocker, mocker,
api_user_active, api_user_active,
mock_get_service, mock_get_service,
mock_get_service_templates, mock_get_service_templates,
mock_get_user, mock_get_user,
mock_get_user_by_email, mock_get_user_by_email,
mock_login, mock_login,
mock_get_jobs, mock_get_jobs,
mock_has_permissions mock_has_permissions
): ):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service', mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
return_value=copy.deepcopy(stub_template_stats)) return_value=copy.deepcopy(stub_template_stats))
@@ -186,32 +149,31 @@ def test_should_show_all_templates_on_template_statistics_page(
assert len(table_rows) == 2 assert len(table_rows) == 2
assert 'Pickle feet' in table_rows[0].find_all('th')[0].text assert 'one' in table_rows[0].find_all('th')[0].text
assert 'Text message template' in table_rows[0].find_all('th')[0].text assert 'Text message template' in table_rows[0].find_all('th')[0].text
assert '206' in table_rows[0].find_all('td')[0].text assert '100' in table_rows[0].find_all('td')[0].text
assert 'Brine Shrimp' in table_rows[1].find_all('th')[0].text assert 'two' in table_rows[1].find_all('th')[0].text
assert 'Text message template' in table_rows[1].find_all('th')[0].text assert 'Email template' in table_rows[1].find_all('th')[0].text
assert '13' in table_rows[1].find_all('td')[0].text assert '200' in table_rows[1].find_all('td')[0].text
@freeze_time("2016-01-01 11:09:00.061258") @freeze_time("2016-01-01 11:09:00.061258")
def test_should_show_recent_jobs_on_dashboard( def test_should_show_recent_jobs_on_dashboard(
app_, app_,
mocker, mocker,
api_user_active, api_user_active,
mock_get_service, mock_get_service,
mock_get_service_templates, mock_get_service_templates,
mock_get_user, mock_get_user,
mock_get_user_by_email, mock_get_user_by_email,
mock_login, mock_login,
mock_get_template_statistics, mock_get_template_statistics,
mock_get_detailed_service, mock_get_detailed_service,
mock_get_jobs, mock_get_jobs,
mock_has_permissions, mock_has_permissions,
mock_get_usage mock_get_usage
): ):
with app_.test_request_context(), app_.test_client() as client: with app_.test_request_context(), app_.test_client() as client:
client.login(api_user_active) client.login(api_user_active)
response = client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID)) response = client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
@@ -226,10 +188,10 @@ def test_should_show_recent_jobs_on_dashboard(
assert len(table_rows) == 4 assert len(table_rows) == 4
for index, filename in enumerate(( for index, filename in enumerate((
"export 1/1/2016.xls", "export 1/1/2016.xls",
"all email addresses.xlsx", "all email addresses.xlsx",
"applicants.ods", "applicants.ods",
"thisisatest.csv", "thisisatest.csv",
)): )):
assert filename in table_rows[index].find_all('th')[0].text assert filename in table_rows[index].find_all('th')[0].text
assert 'Uploaded 1 January at 11:09' in table_rows[index].find_all('th')[0].text assert 'Uploaded 1 January at 11:09' in table_rows[index].find_all('th')[0].text
@@ -259,7 +221,6 @@ def test_menu_send_messages(mocker,
mock_get_template_statistics, mock_get_template_statistics,
mock_get_detailed_service, mock_get_detailed_service,
mock_get_usage): mock_get_usage):
with app_.test_request_context(): with app_.test_request_context():
resp = _test_dashboard_menu( resp = _test_dashboard_menu(
mocker, mocker,
@@ -271,11 +232,11 @@ def test_menu_send_messages(mocker,
assert url_for( assert url_for(
'main.choose_template', 'main.choose_template',
service_id=service_one['id'], service_id=service_one['id'],
template_type='email')in page template_type='email') in page
assert url_for( assert url_for(
'main.choose_template', 'main.choose_template',
service_id=service_one['id'], service_id=service_one['id'],
template_type='sms')in page template_type='sms') in page
assert url_for('main.manage_users', service_id=service_one['id']) in page assert url_for('main.manage_users', service_id=service_one['id']) in page
assert url_for('main.service_settings', service_id=service_one['id']) not in page assert url_for('main.service_settings', service_id=service_one['id']) not in page
@@ -408,11 +369,14 @@ def test_aggregate_template_stats():
expected = aggregate_usage(copy.deepcopy(stub_template_stats)) expected = aggregate_usage(copy.deepcopy(stub_template_stats))
assert len(expected) == 2 assert len(expected) == 2
for item in expected: assert expected[0]['template_name'] == 'one'
if item['template'].id == 1: assert expected[0]['count'] == 100
assert item['usage_count'] == 13 assert expected[0]['template_id'] == 'id-1'
elif item['template'].id == 2: assert expected[0]['template_type'] == 'sms'
assert item['usage_count'] == 206 assert expected[1]['template_name'] == 'two'
assert expected[1]['count'] == 200
assert expected[1]['template_id'] == 'id-2'
assert expected[1]['template_type'] == 'email'
def test_service_dashboard_updates_gets_dashboard_totals(mocker, def test_service_dashboard_updates_gets_dashboard_totals(mocker,

View File

@@ -1068,16 +1068,11 @@ def mock_remove_user_from_service(mocker):
def mock_get_template_statistics(mocker, service_one, fake_uuid): def mock_get_template_statistics(mocker, service_one, fake_uuid):
template = template_json(service_one['id'], fake_uuid, "Test template", "sms", "Something very interesting") template = template_json(service_one['id'], fake_uuid, "Test template", "sms", "Something very interesting")
data = { data = {
"usage_count": 1, "count": 1,
"template": { "template_name": template['name'],
"name": template['name'], "template_type": template['template_type'],
"template_type": template['template_type'], "template_id": template['id'],
"id": template['id'] "day": "2016-04-04"
},
"service": template['service'],
"id": str(generate_uuid()),
"day": "2016-04-04",
"updated_at": "2016-04-04T12:00:00.000000+00:00"
} }
def _get_stats(service_id, limit_days=None): def _get_stats(service_id, limit_days=None):