Admin app uses the new API response formats.

This commit is contained in:
Martyn Inglis
2016-08-18 15:30:57 +01:00
parent d78c98970d
commit 55c4443a05
5 changed files with 73 additions and 87 deletions

View File

@@ -66,11 +66,12 @@ def template_history(service_id):
template_statistics = aggregate_usage(
template_statistics_client.get_template_statistics_for_service(service_id)
)
return render_template(
'views/dashboard/all-template-statistics.html',
template_statistics=template_statistics,
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,34 +99,28 @@ def weekly(service_id):
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]
template_statistics,
key=lambda template_statistic: template_statistic['template_name']
)
# then group and sort the result by usage
return sorted(
totals = sorted(
(
{
'usage_count': sum(usage[1] for usage in usages),
'template': template
'count': sum(usage['count'] for usage in usages),
'template_name': template_name,
'template_id': template_id,
'template_type': template_type
}
for template, usages in groupby(statistics_sorted_by_template, lambda items: items[0])
for (template_name, template_id, template_type), usages in groupby(statistics_sorted_by_template, lambda items: (items['template_name'], items['template_id'], items['template_type'])) # noqa
),
key=lambda row: row['usage_count'],
key=lambda row: row['count'],
reverse=True
)
return totals
def get_dashboard_partials(service_id):
@@ -149,7 +144,7 @@ def get_dashboard_partials(service_id):
'views/dashboard/template-statistics.html',
template_statistics=template_statistics,
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),

View File

@@ -16,12 +16,14 @@ class TemplateStatisticsApiClient(BaseAPIClient):
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']
def get_template_statistics_for_template(self, service_id, template_id):
return self.get(
url='/service/{}/template-statistics/{}'.format(service_id, template_id)
)['data']

View File

@@ -17,18 +17,18 @@
) %}
{% call row_heading() %}
<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">
{{ message_count_label(1, item.template.template_type, suffix='template')|capitalize }}
{{ message_count_label(1, item.template_type, suffix='template')|capitalize }}
</span>
</span>
{% endcall %}
{% call field() %}
{% if template_statistics|length > 1 %}
<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(
item.usage_count,
item.count,
smallest=True
) }}
</span>
@@ -36,7 +36,7 @@
{% else %}
<span class="heading-small">
{{ big_number(
item.usage_count,
item.count,
smallest=True
) }}
</span>

View File

@@ -14,48 +14,39 @@ from tests.conftest import SERVICE_ONE_ID
stub_template_stats = [
{
'template': {
'name': 'Brine Shrimp',
'template_type': 'sms',
'id': 1
},
'id': '6005e192-4738-4962-beec-ebd982d0b03f',
'day': '2016-04-06',
'usage_count': 6,
'service': '1491b86f-c950-48f5-bed1-2a55df027ecb'
'template_type': 'sms',
'template_name': 'one',
'template_id': 'id-1',
'count': 100,
'day': '2016-01-01'
},
{
'template': {
'name': 'Pickle feet',
'template_type': 'sms',
'id': 2
},
'id': '0bd529cd-a0fd-43e5-80ee-b95ef6b0d51f',
'day': '2016-04-06',
'usage_count': 6,
'service': '1491b86f-c950-48f5-bed1-2a55df027ecb'
},
'template_type': 'email',
'template_name': 'two',
'template_id': 'id-2',
'count': 200,
'day': '2016-01-01'
},
{
'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_type': 'sms',
'template_name': 'one',
'template_id': 'id-1',
'count': 300,
'day': '2016-01-02'
},
{
'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'
'template_type': 'sms',
'template_name': 'one',
'template_id': 'id-1',
'count': 400,
'day': '2016-01-02'
},
{
'template_type': 'email',
'template_name': 'two',
'template_id': 'id-2',
'count': 500,
'day': '2016-01-03'
},
]
@@ -83,7 +74,6 @@ def test_get_started(
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)
print(response.get_data(as_text=True))
assert response.status_code == 200
assert 'Get started' in response.get_data(as_text=True)
@@ -147,13 +137,13 @@ def test_should_show_recent_templates_on_dashboard(app_,
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 '206' in table_rows[0].find_all('td')[0].text
assert '800' in table_rows[0].find_all('td')[0].text
assert 'Brine Shrimp' in table_rows[1].find_all('th')[0].text
assert 'Text message template' in table_rows[1].find_all('th')[0].text
assert '13' in table_rows[1].find_all('td')[0].text
assert 'two' in table_rows[1].find_all('th')[0].text
assert 'Email template' in table_rows[1].find_all('th')[0].text
assert '700' in table_rows[1].find_all('td')[0].text
def test_should_show_all_templates_on_template_statistics_page(
@@ -186,13 +176,13 @@ def test_should_show_all_templates_on_template_statistics_page(
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 '206' in table_rows[0].find_all('td')[0].text
assert '800' in table_rows[0].find_all('td')[0].text
assert 'Brine Shrimp' in table_rows[1].find_all('th')[0].text
assert 'Text message template' in table_rows[1].find_all('th')[0].text
assert '13' in table_rows[1].find_all('td')[0].text
assert 'two' in table_rows[1].find_all('th')[0].text
assert 'Email template' in table_rows[1].find_all('th')[0].text
assert '700' in table_rows[1].find_all('td')[0].text
@freeze_time("2016-01-01 11:09:00.061258")
@@ -409,10 +399,14 @@ def test_aggregate_template_stats():
assert len(expected) == 2
for item in expected:
if item['template'].id == 1:
assert item['usage_count'] == 13
elif item['template'].id == 2:
assert item['usage_count'] == 206
if item['template_name'] == 'one':
assert item['count'] == 800
assert item['template_id'] == 'id-1'
assert item['template_type'] == 'sms'
elif item['template_name'] == 'two':
assert item['count'] == 700
assert item['template_id'] == 'id-2'
assert item['template_type'] == 'email'
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):
template = template_json(service_one['id'], fake_uuid, "Test template", "sms", "Something very interesting")
data = {
"usage_count": 1,
"template": {
"name": template['name'],
"template_type": template['template_type'],
"id": template['id']
},
"service": template['service'],
"id": str(generate_uuid()),
"day": "2016-04-04",
"updated_at": "2016-04-04T12:00:00.000000+00:00"
"count": 1,
"template_name": template['name'],
"template_type": template['template_type'],
"template_id": template['id'],
"day": "2016-04-04"
}
def _get_stats(service_id, limit_days=None):