diff --git a/app/main/views/platform_admin.py b/app/main/views/platform_admin.py index 83eb477c2..d0a4b5c07 100644 --- a/app/main/views/platform_admin.py +++ b/app/main/views/platform_admin.py @@ -33,11 +33,7 @@ def platform_admin(): 'views/platform-admin/index.html', include_from_test_key=form.include_from_test_key.data, form=form, - **get_statistics(sorted( - services, - key=lambda service: (service['active'], sum_service_usage(service), service['created_at']), - reverse=True - )) + global_stats=create_global_stats(services), ) @@ -79,18 +75,6 @@ def sum_service_usage(service): return total -def get_statistics(services): - return { - 'global_stats': create_global_stats(services), - 'live_services': format_stats_by_service( - service for service in services if not service['restricted'] - ), - 'trial_mode_services': format_stats_by_service( - service for service in services if service['restricted'] - ), - } - - def filter_and_sort_services(services, trial_mode_services=False): return ( service for service in sorted( diff --git a/app/templates/views/platform-admin/index.html b/app/templates/views/platform-admin/index.html index e5af04d3e..9a3f0f17e 100644 --- a/app/templates/views/platform-admin/index.html +++ b/app/templates/views/platform-admin/index.html @@ -5,80 +5,6 @@ {% from "components/page-footer.html" import page_footer %} {% from "components/table.html" import mapping_table, field, stats_fields, row_group, row, right_aligned_field_heading, hidden_field_heading, text_field %} -{% macro stats_fields(channel, data) -%} - - {% call field(border=False) %} - {{ channel.title() }} - {% endcall %} - - {% call field(align='right', border=False) %} - {{ big_number(data[channel]['sending'], smaller=True) }} - {% endcall %} - - {% call field(align='right', border=False) %} - {{ big_number(data[channel]['delivered'], smaller=True) }} - {% endcall %} - - {% call field(align='right', status='error' if data[channel]['failed'], border=False) %} - {{ big_number(data[channel]['failed'], smaller=True) }} - {% endcall %} - -{%- endmacro %} - -{% macro services_table(services, caption) %} - {% call(item, row_number) mapping_table( - caption=caption, - caption_visible=True, - field_headings=[ - 'Service', - hidden_field_heading('Type'), - right_aligned_field_heading('Sending'), - right_aligned_field_heading('Delivered'), - right_aligned_field_heading('Failed') - ], - field_headings_visible=True - ) %} - - {% for service in services %} - - {% call row_group() %} - - {% call row() %} - {% call field(border=False) %} - {{ service['name'] }} - {% endcall %} - - {{ stats_fields('email', service['stats']) }} - {% endcall %} - - {% call row() %} - {% if not service['active'] %} - {% call field(status='default') %} - archived - {% endcall %} - {% elif service['research_mode'] %} - {% call field(border=False) %} - research mode - {% endcall %} - {% elif not service['restricted'] %} - {% call field(status='error') %} - Live - {% endcall %} - {% else %} - {{ text_field('') }} - {% endif %} - - {{ stats_fields('sms', service['stats']) }} - {% endcall %} - - {% endcall %} - - {% endfor %} - - {% endcall %} -{% endmacro %} - - {% block per_page_title %} Platform admin {% endblock %} @@ -98,8 +24,4 @@ {% include "views/platform-admin/_global_stats.html" %} - {{ services_table(live_services, 'Live services') }} - - {{ services_table(trial_mode_services, 'Trial mode services') }} - {% endblock %} diff --git a/tests/app/main/views/test_platform_admin.py b/tests/app/main/views/test_platform_admin.py index 9f32bc037..6c1872ef2 100644 --- a/tests/app/main/views/test_platform_admin.py +++ b/tests/app/main/views/test_platform_admin.py @@ -42,15 +42,15 @@ def test_should_403_if_not_platform_admin( assert response.status_code == 403 -@pytest.mark.parametrize('restricted, table_index, research_mode, displayed', [ - (True, 1, False, ''), - (False, 0, False, 'Live'), - (False, 0, True, 'research mode'), - (True, 1, True, 'research mode') +@pytest.mark.parametrize('endpoint, restricted, research_mode, displayed', [ + ('main.trial_services', True, False, ''), + ('main.live_services', False, False, 'Live'), + ('main.live_services', False, True, 'research mode'), + ('main.trial_services', True, True, 'research mode') ]) def test_should_show_research_and_restricted_mode( + endpoint, restricted, - table_index, research_mode, displayed, client, @@ -65,19 +65,18 @@ def test_should_show_research_and_restricted_mode( mock_get_detailed_services.return_value = {'data': services} mock_get_user(mocker, user=platform_admin_user) client.login(platform_admin_user) - response = client.get(url_for('main.platform_admin')) + response = client.get(url_for(endpoint)) assert response.status_code == 200 mock_get_detailed_services.assert_called_once_with({'detailed': True, 'include_from_test_key': True}) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') # get first column in second row, which contains flags as text. - table_body = page.find_all('table')[table_index].find_all('tbody')[0] + table_body = page.find_all('table')[0].find_all('tbody')[0] service_mode = table_body.find_all('tbody')[0].find_all('tr')[1].find_all('td')[0].text.strip() assert service_mode == displayed @pytest.mark.parametrize('endpoint, expected_services_shown', [ - ('main.platform_admin', 2), ('main.live_services', 1), ('main.trial_services', 1), ]) @@ -229,13 +228,13 @@ def test_format_stats_by_service_returns_correct_values(fake_uuid): assert ret[0]['stats']['sms']['failed'] == 11 -@pytest.mark.parametrize('restricted, table_index, research_mode', [ - (True, 1, False), - (False, 0, False) +@pytest.mark.parametrize('endpoint, restricted, research_mode', [ + ('main.trial_services', True, False), + ('main.live_services', False, False) ]) def test_should_show_email_and_sms_stats_for_all_service_types( + endpoint, restricted, - table_index, research_mode, client, platform_admin_user, @@ -256,13 +255,13 @@ def test_should_show_email_and_sms_stats_for_all_service_types( mock_get_detailed_services.return_value = {'data': services} mock_get_user(mocker, user=platform_admin_user) client.login(platform_admin_user) - response = client.get(url_for('main.platform_admin')) + response = client.get(url_for(endpoint)) assert response.status_code == 200 mock_get_detailed_services.assert_called_once_with({'detailed': True, 'include_from_test_key': True}) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') - table_body = page.find_all('table')[table_index].find_all('tbody')[0] + table_body = page.find_all('table')[0].find_all('tbody')[0] service_row_group = table_body.find_all('tbody')[0].find_all('tr') email_stats = service_row_group[0].find_all('div', class_='big-number-number') sms_stats = service_row_group[1].find_all('div', class_='big-number-number') @@ -277,17 +276,17 @@ def test_should_show_email_and_sms_stats_for_all_service_types( assert sms_failed == 11 -@pytest.mark.parametrize('restricted, table_index', [ - (False, 0), - (True, 1) +@pytest.mark.parametrize('endpoint, restricted', [ + ('main.live_services', False), + ('main.trial_services', True) ], ids=['live', 'trial']) def test_should_show_archived_services_last( + endpoint, client, platform_admin_user, mocker, mock_get_detailed_services, restricted, - table_index, ): services = [ service_json(name='C', restricted=restricted, active=False, created_at='2002-02-02 12:00:00'), @@ -301,13 +300,13 @@ def test_should_show_archived_services_last( mock_get_detailed_services.return_value = {'data': services} mock_get_user(mocker, user=platform_admin_user) client.login(platform_admin_user) - response = client.get(url_for('main.platform_admin')) + response = client.get(url_for(endpoint)) assert response.status_code == 200 mock_get_detailed_services.assert_called_once_with({'detailed': True, 'include_from_test_key': True}) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') - table_body = page.find_all('table')[table_index].find_all('tbody')[0] + table_body = page.find_all('table')[0].find_all('tbody')[0] services = [service.tr for service in table_body.find_all('tbody')] assert len(services) == 3 assert services[0].td.text.strip() == 'A' @@ -331,7 +330,7 @@ def test_shows_archived_label_instead_of_live_or_research_mode_label( mock_get_detailed_services.return_value = {'data': services} mock_get_user(mocker, user=platform_admin_user) client.login(platform_admin_user) - response = client.get(url_for('main.platform_admin')) + response = client.get(url_for('main.live_services')) assert response.status_code == 200 page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') @@ -375,13 +374,13 @@ def test_should_show_correct_sent_totals_for_platform_admin( assert sms_total == 40 -@pytest.mark.parametrize('restricted, table_index, research_mode', [ - (True, 1, False), - (False, 0, False) +@pytest.mark.parametrize('endpoint, restricted, research_mode', [ + ('main.trial_services', True, False), + ('main.live_services', False, False) ]) def test_should_order_services_by_usage_with_inactive_last( + endpoint, restricted, - table_index, research_mode, client, platform_admin_user, @@ -424,13 +423,13 @@ def test_should_order_services_by_usage_with_inactive_last( mock_get_detailed_services.return_value = {'data': services} mock_get_user(mocker, user=platform_admin_user) client.login(platform_admin_user) - response = client.get(url_for('main.platform_admin')) + response = client.get(url_for(endpoint)) assert response.status_code == 200 mock_get_detailed_services.assert_called_once_with({'detailed': True, 'include_from_test_key': True}) page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') - table_body = page.find_all('table')[table_index].find_all('tbody')[0] + table_body = page.find_all('table')[0].find_all('tbody')[0] services = [service.tr for service in table_body.find_all('tbody')] assert len(services) == 3 assert services[0].td.text.strip() == 'My Service 2'