From b643aa54eccd8caa770c19e493d36ca86ddf868f Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Wed, 24 Apr 2019 10:27:57 +0100 Subject: [PATCH 1/4] Basic reports page, returns 200 and is visible in PA sidebar --- app/main/views/platform_admin.py | 9 +++++++++ app/navigation.py | 4 ++++ .../views/platform-admin/_base_template.html | 1 + app/templates/views/platform-admin/reports.html | 13 +++++++++++++ tests/app/main/views/test_platform_admin.py | 13 +++++++++++++ 5 files changed, 40 insertions(+) create mode 100644 app/templates/views/platform-admin/reports.html diff --git a/app/main/views/platform_admin.py b/app/main/views/platform_admin.py index e40e33f15..2e3464bba 100644 --- a/app/main/views/platform_admin.py +++ b/app/main/views/platform_admin.py @@ -187,6 +187,15 @@ def platform_admin_services(): ) +@main.route("/platform-admin/reports") +@login_required +@user_is_platform_admin +def platform_admin_reports(): + return render_template( + 'views/platform-admin/reports.html' + ) + + @main.route("/platform-admin/complaints") @login_required @user_is_platform_admin diff --git a/app/navigation.py b/app/navigation.py index 829a50489..14d054015 100644 --- a/app/navigation.py +++ b/app/navigation.py @@ -89,6 +89,7 @@ class HeaderNavigation(Navigation): 'platform_admin', 'platform_admin_letter_validation_preview', 'platform_admin_list_complaints', + 'platform_admin_reports', 'platform_admin_returned_letters', 'suspend_service', 'trial_services', @@ -493,6 +494,7 @@ class MainNavigation(Navigation): 'platform_admin', 'platform_admin_letter_validation_preview', 'platform_admin_list_complaints', + 'platform_admin_reports', 'platform_admin_returned_letters', 'pricing', 'privacy', @@ -706,6 +708,7 @@ class CaseworkNavigation(Navigation): 'organisations', 'platform_admin_letter_validation_preview', 'platform_admin_list_complaints', + 'platform_admin_reports', 'platform_admin_returned_letters', 'platform_admin', 'pricing', @@ -960,6 +963,7 @@ class OrgNavigation(Navigation): 'platform_admin', 'platform_admin_letter_validation_preview', 'platform_admin_list_complaints', + 'platform_admin_reports', 'platform_admin_returned_letters', 'pricing', 'privacy', diff --git a/app/templates/views/platform-admin/_base_template.html b/app/templates/views/platform-admin/_base_template.html index 90022f895..ef7fa200c 100644 --- a/app/templates/views/platform-admin/_base_template.html +++ b/app/templates/views/platform-admin/_base_template.html @@ -17,6 +17,7 @@ ('Trial mode services', url_for('main.trial_services')), ('Organisations', url_for('main.organisations')), ('Providers', url_for('main.view_providers')), + ('Reports', url_for('main.platform_admin_reports')), ('Email branding', url_for('main.email_branding')), ('Letter branding', url_for('main.letter_branding')), ('Inbound SMS numbers', url_for('main.inbound_sms_admin')), diff --git a/app/templates/views/platform-admin/reports.html b/app/templates/views/platform-admin/reports.html new file mode 100644 index 000000000..76bdec6e7 --- /dev/null +++ b/app/templates/views/platform-admin/reports.html @@ -0,0 +1,13 @@ +{% extends "views/platform-admin/_base_template.html" %} + +{% block per_page_title %} + Platform admin +{% endblock %} + +{% block platform_admin_content %} + +

+ Reports +

+ +{% endblock %} diff --git a/tests/app/main/views/test_platform_admin.py b/tests/app/main/views/test_platform_admin.py index 87a0f90b9..d04ac7115 100644 --- a/tests/app/main/views/test_platform_admin.py +++ b/tests/app/main/views/test_platform_admin.py @@ -927,3 +927,16 @@ def test_clear_cache_requires_option(client_request, platform_admin_user, mocker assert normalize_spaces(page.find('span', class_='error-message').text) == 'Not a valid choice' assert not redis.delete_cache_keys_by_pattern.called + + +def test_reports_page( + client, + platform_admin_user, + mocker, +): + mock_get_user(mocker, user=platform_admin_user) + client.login(platform_admin_user) + response = client.get(url_for('main.platform_admin_reports')) + + assert response.status_code == 200 + response.get_data(as_text=True) From b0a8c5edf65b79d4c5ac3c3432aa1b02a59fb72d Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Wed, 24 Apr 2019 16:23:12 +0100 Subject: [PATCH 2/4] Get live services csv report --- app/main/views/platform_admin.py | 53 ++++++++++++++++++- app/navigation.py | 4 ++ .../views/platform-admin/reports.html | 2 + app/utils.py | 1 - tests/app/main/views/test_platform_admin.py | 35 +++++++++++- 5 files changed, 91 insertions(+), 4 deletions(-) diff --git a/app/main/views/platform_admin.py b/app/main/views/platform_admin.py index 2e3464bba..f0ba1a60b 100644 --- a/app/main/views/platform_admin.py +++ b/app/main/views/platform_admin.py @@ -3,16 +3,26 @@ import re from collections import OrderedDict from datetime import datetime -from flask import abort, flash, redirect, render_template, request, url_for +from flask import ( + Response, + abort, + flash, + redirect, + render_template, + request, + url_for, +) from flask_login import login_required from notifications_python_client.errors import HTTPError from requests import RequestException from app import ( complaint_api_client, + format_date_numeric, letter_jobs_client, platform_stats_api_client, service_api_client, + user_api_client, ) from app.extensions import antivirus_client, redis_client from app.main import main @@ -28,6 +38,7 @@ from app.statistics_utils import ( ) from app.template_previews import validate_letter from app.utils import ( + Spreadsheet, generate_next_dict, generate_previous_dict, get_page_from_request, @@ -196,6 +207,46 @@ def platform_admin_reports(): ) +@main.route("/platform-admin/reports/live-services.csv") +@login_required +@user_is_platform_admin +def live_services_csv(): + services = service_api_client.get_services()["data"] + live_services_columns = [ + "Service ID", "Organisation", "Service name", "Consent to research", "Main contact", + "Contact email", "Contact mobile", "Live date", "SMS volume", "Email volume", "Letter volume" + ] + live_services_data = [] + live_services_data.append(live_services_columns) + for service in services: + if service["count_as_live"]: + main_contact = None + if service["go_live_user"]: + main_contact = user_api_client.get_user(service["go_live_user"]) + live_services_data.append([ + service["id"], + service["organisation"], + service["name"], + service["consent_to_research"], + main_contact.name if main_contact else None, + main_contact.email_address if main_contact else None, + main_contact.mobile_number if main_contact else None, + service["go_live_at"], + service["volume_sms"], + service["volume_email"], + service["volume_letter"], + ]) + return Response( + Spreadsheet.from_rows(live_services_data).as_csv_data, + mimetype='text/csv', + headers={ + 'Content-Disposition': 'inline; filename="{} live services report.csv"'.format( + format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")), + ) + } + ) + + @main.route("/platform-admin/complaints") @login_required @user_is_platform_admin diff --git a/app/navigation.py b/app/navigation.py index 14d054015..5e48d310e 100644 --- a/app/navigation.py +++ b/app/navigation.py @@ -85,6 +85,7 @@ class HeaderNavigation(Navigation): 'find_users_by_email', 'letter_branding', 'live_services', + 'live_services_csv', 'organisations', 'platform_admin', 'platform_admin_letter_validation_preview', @@ -477,6 +478,7 @@ class MainNavigation(Navigation): 'letter_branding', 'letter_branding_preview_image', 'live_services', + 'live_services_csv', 'letter_template', 'message_status', 'manage_org_users', @@ -690,6 +692,7 @@ class CaseworkNavigation(Navigation): 'letter_template', 'link_service_to_organisation', 'live_services', + 'live_services_csv', 'manage_org_users', 'manage_template_folder', 'manage_users', @@ -949,6 +952,7 @@ class OrgNavigation(Navigation): 'letter_template', 'link_service_to_organisation', 'live_services', + 'live_services_csv', 'manage_template_folder', 'manage_users', 'message_status', diff --git a/app/templates/views/platform-admin/reports.html b/app/templates/views/platform-admin/reports.html index 76bdec6e7..7dff974b4 100644 --- a/app/templates/views/platform-admin/reports.html +++ b/app/templates/views/platform-admin/reports.html @@ -10,4 +10,6 @@ Reports +Download live services information sheet + {% endblock %} diff --git a/app/utils.py b/app/utils.py index 175ce6318..ce47074cf 100644 --- a/app/utils.py +++ b/app/utils.py @@ -246,7 +246,6 @@ class Spreadsheet(): def from_rows(cls, rows, filename=''): with StringIO() as converted: output = csv.writer(converted) - for row in rows: output.writerow(row) return cls(converted.getvalue(), filename) diff --git a/tests/app/main/views/test_platform_admin.py b/tests/app/main/views/test_platform_admin.py index d04ac7115..946d94c28 100644 --- a/tests/app/main/views/test_platform_admin.py +++ b/tests/app/main/views/test_platform_admin.py @@ -932,11 +932,42 @@ def test_clear_cache_requires_option(client_request, platform_admin_user, mocker def test_reports_page( client, platform_admin_user, - mocker, + mocker ): mock_get_user(mocker, user=platform_admin_user) client.login(platform_admin_user) + response = client.get(url_for('main.platform_admin_reports')) assert response.status_code == 200 - response.get_data(as_text=True) + page = response.get_data(as_text=True) + assert "Download live services information sheet" in page + + +def test_get_live_services_report(client, platform_admin_user, mocker): + mock_get_user(mocker, user=platform_admin_user) + client.login(platform_admin_user) + + mocker.patch( + 'app.service_api_client.get_services', + return_value={'data': [ + {'id': '1', 'organisation': 'Forest', 'name': 'jessie the oak tree', 'consent_to_research': True, + 'go_live_at': '2014-03-29', 'volume_sms': 100, 'volume_email': 50, 'volume_letter': 20, + 'count_as_live': True, 'go_live_user': '123'}, + {'id': '2', 'organisation': 'Forest', 'name': 'james the pine tree', 'consent_to_research': None, + 'go_live_at': '2015-03-26', 'volume_sms': None, 'volume_email': 60, 'volume_letter': 0, + 'count_as_live': True, 'go_live_user': None}, + {'id': '3', 'organisation': 'Forest', 'name': 'gary the rock', 'consent_to_research': None, + 'go_live_at': None, 'volume_sms': None, 'volume_email': 0, 'volume_letter': 0, + 'count_as_live': False, 'go_live_user': None}, + ]} + ) + response = client.get(url_for('main.live_services_csv')) + assert response.status_code == 200 + report = response.get_data(as_text=True) + assert report.strip() == ( + 'Service ID,Organisation,Service name,Consent to research,' + + 'Main contact,Contact email,Contact mobile,Live date,SMS volume,Email volume,Letter volume\r\n1,Forest' + + ',jessie the oak tree,True,Platform admin user,platform@admin.gov.uk,07700 900762,2014-03-29,100,50,20\r\n2,' + + 'Forest,james the pine tree,,,,,2015-03-26,,60,0' + ) From 128bbd0d5fe566eb3d2e0c60878656a8dd832e79 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Thu, 25 Apr 2019 18:07:40 +0100 Subject: [PATCH 3/4] Add yearly notifications sent stats to live services report Update live_services_csv so it works with new endpoint --- app/main/views/platform_admin.py | 41 ++++++++++----------- app/notify_client/service_api_client.py | 6 +++ tests/app/main/views/test_platform_admin.py | 30 ++++++++------- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/app/main/views/platform_admin.py b/app/main/views/platform_admin.py index f0ba1a60b..6ca162a9c 100644 --- a/app/main/views/platform_admin.py +++ b/app/main/views/platform_admin.py @@ -22,7 +22,6 @@ from app import ( letter_jobs_client, platform_stats_api_client, service_api_client, - user_api_client, ) from app.extensions import antivirus_client, redis_client from app.main import main @@ -211,31 +210,31 @@ def platform_admin_reports(): @login_required @user_is_platform_admin def live_services_csv(): - services = service_api_client.get_services()["data"] + results = service_api_client.get_live_services_data()["data"] live_services_columns = [ "Service ID", "Organisation", "Service name", "Consent to research", "Main contact", - "Contact email", "Contact mobile", "Live date", "SMS volume", "Email volume", "Letter volume" + "Contact email", "Contact mobile", "Live date", "SMS volume intent", "Email volume intent", + "Letter volume intent", "SMS sent this year", "Emails sent this year", "Letters sent this year" ] live_services_data = [] live_services_data.append(live_services_columns) - for service in services: - if service["count_as_live"]: - main_contact = None - if service["go_live_user"]: - main_contact = user_api_client.get_user(service["go_live_user"]) - live_services_data.append([ - service["id"], - service["organisation"], - service["name"], - service["consent_to_research"], - main_contact.name if main_contact else None, - main_contact.email_address if main_contact else None, - main_contact.mobile_number if main_contact else None, - service["go_live_at"], - service["volume_sms"], - service["volume_email"], - service["volume_letter"], - ]) + for row in results: + live_services_data.append([ + row["service_id"], + row["organisation_name"], + row["service_name"], + row["consent_to_research"], + row["contact_name"], + row["contact_email"], + row["contact_mobile"], + row["live_date"], + row["sms_volume_intent"], + row["email_volume_intent"], + row["letter_volume_intent"], + row["sms_totals"], + row["email_totals"], + row["letter_totals"], + ]) return Response( Spreadsheet.from_rows(live_services_data).as_csv_data, mimetype='text/csv', diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index 86cbaaa58..ebe869702 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -47,6 +47,12 @@ class ServiceAPIClient(NotifyAdminAPIClient): """ return self.get('/service', params=params_dict) + def get_live_services_data(self, params_dict=None): + """ + Retrieve a list of live services data with contact names and notification counts. + """ + return self.get('/service/live-services-data', params=params_dict) + def get_active_services(self, params_dict=None): """ Retrieve a list of active services. diff --git a/tests/app/main/views/test_platform_admin.py b/tests/app/main/views/test_platform_admin.py index 946d94c28..f62206091 100644 --- a/tests/app/main/views/test_platform_admin.py +++ b/tests/app/main/views/test_platform_admin.py @@ -949,25 +949,27 @@ def test_get_live_services_report(client, platform_admin_user, mocker): client.login(platform_admin_user) mocker.patch( - 'app.service_api_client.get_services', + 'app.service_api_client.get_live_services_data', return_value={'data': [ - {'id': '1', 'organisation': 'Forest', 'name': 'jessie the oak tree', 'consent_to_research': True, - 'go_live_at': '2014-03-29', 'volume_sms': 100, 'volume_email': 50, 'volume_letter': 20, - 'count_as_live': True, 'go_live_user': '123'}, - {'id': '2', 'organisation': 'Forest', 'name': 'james the pine tree', 'consent_to_research': None, - 'go_live_at': '2015-03-26', 'volume_sms': None, 'volume_email': 60, 'volume_letter': 0, - 'count_as_live': True, 'go_live_user': None}, - {'id': '3', 'organisation': 'Forest', 'name': 'gary the rock', 'consent_to_research': None, - 'go_live_at': None, 'volume_sms': None, 'volume_email': 0, 'volume_letter': 0, - 'count_as_live': False, 'go_live_user': None}, + {'service_id': 1, 'service_name': 'jessie the oak tree', 'organisation_name': 'Forest', + 'consent_to_research': True, 'contact_name': 'Forest fairy', + 'contact_email': 'forest.fairy@digital.cabinet-office.gov.uk', 'contact_mobile': '+447700900986', + 'live_date': '2014-03-29', 'sms_volume_intent': 100, 'email_volume_intent': 50, + 'letter_volume_intent': 20, 'sms_totals': 300, 'email_totals': 1200, 'letter_totals': 0}, + {'service_id': 2, 'service_name': 'james the pine tree', 'organisation_name': 'Forest', + 'consent_to_research': None, 'contact_name': None, + 'contact_email': None, 'contact_mobile': None, + 'live_date': '2015-03-26', 'sms_volume_intent': None, 'email_volume_intent': 60, + 'letter_volume_intent': 0, 'sms_totals': 0, 'email_totals': 0, 'letter_totals': 0}, ]} ) response = client.get(url_for('main.live_services_csv')) assert response.status_code == 200 report = response.get_data(as_text=True) assert report.strip() == ( - 'Service ID,Organisation,Service name,Consent to research,' - + 'Main contact,Contact email,Contact mobile,Live date,SMS volume,Email volume,Letter volume\r\n1,Forest' - + ',jessie the oak tree,True,Platform admin user,platform@admin.gov.uk,07700 900762,2014-03-29,100,50,20\r\n2,' - + 'Forest,james the pine tree,,,,,2015-03-26,,60,0' + 'Service ID,Organisation,Service name,Consent to research,Main contact,Contact email,Contact mobile,' + + 'Live date,SMS volume intent,Email volume intent,Letter volume intent,SMS sent this year,' + + 'Emails sent this year,Letters sent this year\r\n1,Forest,jessie the oak tree,True,Forest fairy,' + + 'forest.fairy@digital.cabinet-office.gov.uk,+447700900986,2014-03-29,100,50,20,300,1200,0\r\n2,Forest,' + + 'james the pine tree,,,,,2015-03-26,,60,0,0,0,0' ) From 46e8a52f2b8666752d5c6c856b528d4c31f5fbb1 Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Tue, 30 Apr 2019 17:44:59 +0100 Subject: [PATCH 4/4] Add performance platform csv report and format live dates Drop redundant Response wrapper --- app/main/views/platform_admin.py | 54 ++++++++++++------- app/navigation.py | 4 ++ .../views/platform-admin/reports.html | 7 ++- tests/app/main/views/test_platform_admin.py | 47 +++++++++++++--- 4 files changed, 86 insertions(+), 26 deletions(-) diff --git a/app/main/views/platform_admin.py b/app/main/views/platform_admin.py index 6ca162a9c..dbab2bc3b 100644 --- a/app/main/views/platform_admin.py +++ b/app/main/views/platform_admin.py @@ -3,15 +3,7 @@ import re from collections import OrderedDict from datetime import datetime -from flask import ( - Response, - abort, - flash, - redirect, - render_template, - request, - url_for, -) +from flask import abort, flash, redirect, render_template, request, url_for from flask_login import login_required from notifications_python_client.errors import HTTPError from requests import RequestException @@ -227,7 +219,7 @@ def live_services_csv(): row["contact_name"], row["contact_email"], row["contact_mobile"], - row["live_date"], + datetime.strptime(row["live_date"], '%a, %d %b %Y %X %Z').strftime("%d-%m-%Y"), row["sms_volume_intent"], row["email_volume_intent"], row["letter_volume_intent"], @@ -235,15 +227,39 @@ def live_services_csv(): row["email_totals"], row["letter_totals"], ]) - return Response( - Spreadsheet.from_rows(live_services_data).as_csv_data, - mimetype='text/csv', - headers={ - 'Content-Disposition': 'inline; filename="{} live services report.csv"'.format( - format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")), - ) - } - ) + + return Spreadsheet.from_rows(live_services_data).as_csv_data, 200, { + 'Content-Type': 'text/csv; charset=utf-8', + 'Content-Disposition': 'inline; filename="{} live services report.csv"'.format( + format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")), + ) + } + + +@main.route("/platform-admin/reports/performance-platform.csv") +@login_required +@user_is_platform_admin +def performance_platform_csv(): + results = service_api_client.get_live_services_data()["data"] + live_services_columns = ["service_id", "agency", "service_name", "_timestamp", "service", "count"] + live_services_data = [] + live_services_data.append(live_services_columns) + for row in results: + live_services_data.append([ + row["service_id"], + row["organisation_name"], + row["service_name"], + datetime.strptime(row["live_date"], '%a, %d %b %Y %X %Z').strftime("%Y-%m-%dT%H:%M:%S") + "Z", + "govuk-notify", + 1 + ]) + + return Spreadsheet.from_rows(live_services_data).as_csv_data, 200, { + 'Content-Type': 'text/csv; charset=utf-8', + 'Content-Disposition': 'inline; filename="{} performance platform report.csv"'.format( + format_date_numeric(datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ")), + ) + } @main.route("/platform-admin/complaints") diff --git a/app/navigation.py b/app/navigation.py index 5e48d310e..f1bccc292 100644 --- a/app/navigation.py +++ b/app/navigation.py @@ -87,6 +87,7 @@ class HeaderNavigation(Navigation): 'live_services', 'live_services_csv', 'organisations', + 'performance_platform_csv', 'platform_admin', 'platform_admin_letter_validation_preview', 'platform_admin_list_complaints', @@ -493,6 +494,7 @@ class MainNavigation(Navigation): 'organisation_preview_letter_branding', 'organisation_settings', 'organisations', + 'performance_platform_csv', 'platform_admin', 'platform_admin_letter_validation_preview', 'platform_admin_list_complaints', @@ -709,6 +711,7 @@ class CaseworkNavigation(Navigation): 'organisation_preview_letter_branding', 'organisation_settings', 'organisations', + 'performance_platform_csv', 'platform_admin_letter_validation_preview', 'platform_admin_list_complaints', 'platform_admin_reports', @@ -964,6 +967,7 @@ class OrgNavigation(Navigation): 'old_terms', 'old_using_notify', 'organisations', + 'performance_platform_csv', 'platform_admin', 'platform_admin_letter_validation_preview', 'platform_admin_list_complaints', diff --git a/app/templates/views/platform-admin/reports.html b/app/templates/views/platform-admin/reports.html index 7dff974b4..2135c388c 100644 --- a/app/templates/views/platform-admin/reports.html +++ b/app/templates/views/platform-admin/reports.html @@ -10,6 +10,11 @@ Reports -Download live services information sheet +

+ Download live services csv report +

+

+ Download performance platform csv report +

{% endblock %} diff --git a/tests/app/main/views/test_platform_admin.py b/tests/app/main/views/test_platform_admin.py index f62206091..a2dad89d4 100644 --- a/tests/app/main/views/test_platform_admin.py +++ b/tests/app/main/views/test_platform_admin.py @@ -940,8 +940,14 @@ def test_reports_page( response = client.get(url_for('main.platform_admin_reports')) assert response.status_code == 200 - page = response.get_data(as_text=True) - assert "Download live services information sheet" in page + page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') + assert page.find( + 'a', text="Download live services csv report" + ).attrs['href'] == '/platform-admin/reports/live-services.csv' + + assert page.find( + 'a', text="Download performance platform csv report" + ).attrs['href'] == '/platform-admin/reports/performance-platform.csv' def test_get_live_services_report(client, platform_admin_user, mocker): @@ -954,12 +960,12 @@ def test_get_live_services_report(client, platform_admin_user, mocker): {'service_id': 1, 'service_name': 'jessie the oak tree', 'organisation_name': 'Forest', 'consent_to_research': True, 'contact_name': 'Forest fairy', 'contact_email': 'forest.fairy@digital.cabinet-office.gov.uk', 'contact_mobile': '+447700900986', - 'live_date': '2014-03-29', 'sms_volume_intent': 100, 'email_volume_intent': 50, + 'live_date': 'Sat, 29 Mar 2014 00:00:00 GMT', 'sms_volume_intent': 100, 'email_volume_intent': 50, 'letter_volume_intent': 20, 'sms_totals': 300, 'email_totals': 1200, 'letter_totals': 0}, {'service_id': 2, 'service_name': 'james the pine tree', 'organisation_name': 'Forest', 'consent_to_research': None, 'contact_name': None, 'contact_email': None, 'contact_mobile': None, - 'live_date': '2015-03-26', 'sms_volume_intent': None, 'email_volume_intent': 60, + 'live_date': 'Tue, 15 Mar 2016 00:00:00 GMT', 'sms_volume_intent': None, 'email_volume_intent': 60, 'letter_volume_intent': 0, 'sms_totals': 0, 'email_totals': 0, 'letter_totals': 0}, ]} ) @@ -970,6 +976,35 @@ def test_get_live_services_report(client, platform_admin_user, mocker): 'Service ID,Organisation,Service name,Consent to research,Main contact,Contact email,Contact mobile,' + 'Live date,SMS volume intent,Email volume intent,Letter volume intent,SMS sent this year,' + 'Emails sent this year,Letters sent this year\r\n1,Forest,jessie the oak tree,True,Forest fairy,' - + 'forest.fairy@digital.cabinet-office.gov.uk,+447700900986,2014-03-29,100,50,20,300,1200,0\r\n2,Forest,' - + 'james the pine tree,,,,,2015-03-26,,60,0,0,0,0' + + 'forest.fairy@digital.cabinet-office.gov.uk,+447700900986,29-03-2014,100,50,20,300,1200,0\r\n2,Forest,' + + 'james the pine tree,,,,,15-03-2016,,60,0,0,0,0' + ) + + +def test_get_performance_platform_report(client, platform_admin_user, mocker): + mock_get_user(mocker, user=platform_admin_user) + client.login(platform_admin_user) + + mocker.patch( + 'app.service_api_client.get_live_services_data', + return_value={'data': [ + {'service_id': 1, 'service_name': 'jessie the oak tree', 'organisation_name': 'Forest', + 'consent_to_research': True, 'contact_name': 'Forest fairy', + 'contact_email': 'forest.fairy@digital.cabinet-office.gov.uk', 'contact_mobile': '+447700900986', + 'live_date': 'Sat, 29 Mar 2014 00:00:00 GMT', 'sms_volume_intent': 100, 'email_volume_intent': 50, + 'letter_volume_intent': 20, 'sms_totals': 300, 'email_totals': 1200, 'letter_totals': 0}, + {'service_id': 2, 'service_name': 'james the pine tree', 'organisation_name': 'Forest', + 'consent_to_research': None, 'contact_name': None, + 'contact_email': None, 'contact_mobile': None, + 'live_date': 'Tue, 15 Mar 2016 00:00:00 GMT', 'sms_volume_intent': None, 'email_volume_intent': 60, + 'letter_volume_intent': 0, 'sms_totals': 0, 'email_totals': 0, 'letter_totals': 0}, + ]} + ) + response = client.get(url_for('main.performance_platform_csv')) + assert response.status_code == 200 + report = response.get_data(as_text=True) + assert report.strip() == ( + 'service_id,agency,service_name,_timestamp,service,count' + + '\r\n1,Forest,jessie the oak tree,2014-03-29T00:00:00Z,govuk-notify,1' + + '\r\n2,Forest,james the pine tree,2016-03-15T00:00:00Z,govuk-notify,1' )