Add yearly notifications sent stats to live services report

Update live_services_csv so it works with new endpoint
This commit is contained in:
Pea Tyczynska
2019-04-25 18:07:40 +01:00
parent b0a8c5edf6
commit 128bbd0d5f
3 changed files with 42 additions and 35 deletions

View File

@@ -22,7 +22,6 @@ from app import (
letter_jobs_client, letter_jobs_client,
platform_stats_api_client, platform_stats_api_client,
service_api_client, service_api_client,
user_api_client,
) )
from app.extensions import antivirus_client, redis_client from app.extensions import antivirus_client, redis_client
from app.main import main from app.main import main
@@ -211,31 +210,31 @@ def platform_admin_reports():
@login_required @login_required
@user_is_platform_admin @user_is_platform_admin
def live_services_csv(): def live_services_csv():
services = service_api_client.get_services()["data"] results = service_api_client.get_live_services_data()["data"]
live_services_columns = [ live_services_columns = [
"Service ID", "Organisation", "Service name", "Consent to research", "Main contact", "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 = []
live_services_data.append(live_services_columns) live_services_data.append(live_services_columns)
for service in services: for row in results:
if service["count_as_live"]: live_services_data.append([
main_contact = None row["service_id"],
if service["go_live_user"]: row["organisation_name"],
main_contact = user_api_client.get_user(service["go_live_user"]) row["service_name"],
live_services_data.append([ row["consent_to_research"],
service["id"], row["contact_name"],
service["organisation"], row["contact_email"],
service["name"], row["contact_mobile"],
service["consent_to_research"], row["live_date"],
main_contact.name if main_contact else None, row["sms_volume_intent"],
main_contact.email_address if main_contact else None, row["email_volume_intent"],
main_contact.mobile_number if main_contact else None, row["letter_volume_intent"],
service["go_live_at"], row["sms_totals"],
service["volume_sms"], row["email_totals"],
service["volume_email"], row["letter_totals"],
service["volume_letter"], ])
])
return Response( return Response(
Spreadsheet.from_rows(live_services_data).as_csv_data, Spreadsheet.from_rows(live_services_data).as_csv_data,
mimetype='text/csv', mimetype='text/csv',

View File

@@ -47,6 +47,12 @@ class ServiceAPIClient(NotifyAdminAPIClient):
""" """
return self.get('/service', params=params_dict) 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): def get_active_services(self, params_dict=None):
""" """
Retrieve a list of active services. Retrieve a list of active services.

View File

@@ -949,25 +949,27 @@ def test_get_live_services_report(client, platform_admin_user, mocker):
client.login(platform_admin_user) client.login(platform_admin_user)
mocker.patch( mocker.patch(
'app.service_api_client.get_services', 'app.service_api_client.get_live_services_data',
return_value={'data': [ return_value={'data': [
{'id': '1', 'organisation': 'Forest', 'name': 'jessie the oak tree', 'consent_to_research': True, {'service_id': 1, 'service_name': 'jessie the oak tree', 'organisation_name': 'Forest',
'go_live_at': '2014-03-29', 'volume_sms': 100, 'volume_email': 50, 'volume_letter': 20, 'consent_to_research': True, 'contact_name': 'Forest fairy',
'count_as_live': True, 'go_live_user': '123'}, 'contact_email': 'forest.fairy@digital.cabinet-office.gov.uk', 'contact_mobile': '+447700900986',
{'id': '2', 'organisation': 'Forest', 'name': 'james the pine tree', 'consent_to_research': None, 'live_date': '2014-03-29', 'sms_volume_intent': 100, 'email_volume_intent': 50,
'go_live_at': '2015-03-26', 'volume_sms': None, 'volume_email': 60, 'volume_letter': 0, 'letter_volume_intent': 20, 'sms_totals': 300, 'email_totals': 1200, 'letter_totals': 0},
'count_as_live': True, 'go_live_user': None}, {'service_id': 2, 'service_name': 'james the pine tree', 'organisation_name': 'Forest',
{'id': '3', 'organisation': 'Forest', 'name': 'gary the rock', 'consent_to_research': None, 'consent_to_research': None, 'contact_name': None,
'go_live_at': None, 'volume_sms': None, 'volume_email': 0, 'volume_letter': 0, 'contact_email': None, 'contact_mobile': None,
'count_as_live': False, 'go_live_user': 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')) response = client.get(url_for('main.live_services_csv'))
assert response.status_code == 200 assert response.status_code == 200
report = response.get_data(as_text=True) report = response.get_data(as_text=True)
assert report.strip() == ( assert report.strip() == (
'Service ID,Organisation,Service name,Consent to research,' 'Service ID,Organisation,Service name,Consent to research,Main contact,Contact email,Contact mobile,'
+ 'Main contact,Contact email,Contact mobile,Live date,SMS volume,Email volume,Letter volume\r\n1,Forest' + 'Live date,SMS volume intent,Email volume intent,Letter volume intent,SMS sent this year,'
+ ',jessie the oak tree,True,Platform admin user,platform@admin.gov.uk,07700 900762,2014-03-29,100,50,20\r\n2,' + 'Emails sent this year,Letters sent this year\r\n1,Forest,jessie the oak tree,True,Forest fairy,'
+ 'Forest,james the pine tree,,,,,2015-03-26,,60,0' + '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'
) )