From 46bfb541c8843f1d9506f2bf672dbdec21f82842 Mon Sep 17 00:00:00 2001 From: Alexey Bezhan Date: Tue, 15 Jan 2019 15:55:04 +0000 Subject: [PATCH 1/2] Group new template-statistics response by template New API template-statistics response returns notification counts for each template and status combination. This can be used for both service statistics counts and template statistics by grouping the counts either by status or by template. --- app/main/views/dashboard.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index f678babf6..ab67df524 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -1,6 +1,7 @@ import calendar from datetime import datetime from functools import partial +from itertools import groupby from flask import ( Response, @@ -265,16 +266,24 @@ def get_inbox_partials(service_id): )} -def aggregate_usage(template_statistics, sort_key='count'): - return sorted( - template_statistics, - key=lambda template_statistic: template_statistic[sort_key], - reverse=True - ) +def aggregate_template_usage(template_statistics, sort_key='count'): + templates = [] + for k, v in groupby(sorted(template_statistics, key=lambda x: x['template_id']), key=lambda x: x['template_id']): + template_stats = [s for s in v if s.get('status') != 'cancelled'] + + templates.append({ + "template_id": k, + "template_name": template_stats[0]['template_name'], + "template_type": template_stats[0]['template_type'], + "is_precompiled_letter": template_stats[0]['is_precompiled_letter'], + "count": sum(s['count'] for s in template_stats) + }) + + return sorted(templates, key=lambda x: x[sort_key], reverse=True) def get_dashboard_partials(service_id): - template_statistics = aggregate_usage( + template_statistics = aggregate_template_usage( template_statistics_client.get_template_statistics_for_service(service_id, limit_days=7) ) From 4fe239abb2f1975fc031457b0f55a812a730a179 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Tue, 15 Jan 2019 16:19:23 +0000 Subject: [PATCH 2/2] Add support for new template statistics API response The new API response for template statistics returns separate count for each status. We get rid of template stats for cancelled notifications and group the rest of the statuses together. --- app/main/views/dashboard.py | 3 ++- tests/app/main/views/test_dashboard.py | 13 ++++++++----- tests/conftest.py | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index ab67df524..1fece1d0b 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -268,8 +268,9 @@ def get_inbox_partials(service_id): def aggregate_template_usage(template_statistics, sort_key='count'): templates = [] + template_statistics = [s for s in template_statistics if s.get("status") != "cancelled"] for k, v in groupby(sorted(template_statistics, key=lambda x: x['template_id']), key=lambda x: x['template_id']): - template_stats = [s for s in v if s.get('status') != 'cancelled'] + template_stats = list(v) templates.append({ "template_id": k, diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index 4b986c573..263bea12a 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -35,19 +35,22 @@ stub_template_stats = [ 'template_type': 'sms', 'template_name': 'one', 'template_id': 'id-1', - 'count': 100 + 'count': 100, + 'is_precompiled_letter': False }, { 'template_type': 'email', 'template_name': 'two', 'template_id': 'id-2', - 'count': 200 + 'count': 200, + 'is_precompiled_letter': False }, { 'template_type': 'letter', 'template_name': 'three', 'template_id': 'id-3', - 'count': 300 + 'count': 300, + 'is_precompiled_letter': False }, { 'template_type': 'letter', @@ -1033,8 +1036,8 @@ def test_route_for_service_permissions( def test_aggregate_template_stats(): - from app.main.views.dashboard import aggregate_usage - expected = aggregate_usage(copy.deepcopy(stub_template_stats)) + from app.main.views.dashboard import aggregate_template_usage + expected = aggregate_template_usage(copy.deepcopy(stub_template_stats)) assert len(expected) == 4 assert expected[0]['template_name'] == 'four' diff --git a/tests/conftest.py b/tests/conftest.py index d05fcf349..4aadfa241 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2268,7 +2268,7 @@ def mock_get_template_statistics(mocker, service_one, fake_uuid): "template_name": template['name'], "template_type": template['template_type'], "template_id": template['id'], - "day": "2016-04-04" + "is_precompiled_letter": False } def _get_stats(service_id, limit_days=None):