From 4b030b1583f980d24e6cdad5f2177059df37eadb Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Thu, 28 Jun 2018 08:39:25 +0100 Subject: [PATCH] Create platform-stats blueprint Created a platform-stats blueprint and moved the new platform stats endpoint to the new blueprint (it was previously in the service blueprint). Since the original platform stats route and the new platform stats route are now in different blueprints, their view functions can have the same name without any issues. --- app/__init__.py | 4 +++ app/dao/notifications_dao.py | 2 +- app/platform_stats/__init__.py | 0 app/platform_stats/rest.py | 24 ++++++++++++++++++ app/service/rest.py | 14 ----------- .../notification_dao/test_notification_dao.py | 18 ++++++------- tests/app/platform_stats/__init__.py | 0 tests/app/platform_stats/test_rest.py | 25 +++++++++++++++++++ tests/app/service/test_rest.py | 22 ---------------- 9 files changed, 63 insertions(+), 46 deletions(-) create mode 100644 app/platform_stats/__init__.py create mode 100644 app/platform_stats/rest.py create mode 100644 tests/app/platform_stats/__init__.py create mode 100644 tests/app/platform_stats/test_rest.py diff --git a/app/__init__.py b/app/__init__.py index 46554c32e..2ae2d497e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -113,6 +113,7 @@ def register_blueprint(application): from app.organisation.rest import organisation_blueprint from app.organisation.invite_rest import organisation_invite_blueprint from app.complaint.complaint_rest import complaint_blueprint + from app.platform_stats.rest import platform_stats_blueprint service_blueprint.before_request(requires_admin_auth) application.register_blueprint(service_blueprint, url_prefix='/service') @@ -192,6 +193,9 @@ def register_blueprint(application): complaint_blueprint.before_request(requires_admin_auth) application.register_blueprint(complaint_blueprint) + platform_stats_blueprint.before_request(requires_admin_auth) + application.register_blueprint(platform_stats_blueprint, url_prefix='/platform-stats') + def register_v2_blueprints(application): from app.v2.inbound_sms.get_inbound_sms import v2_inbound_sms_blueprint as get_inbound_sms diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 1d6130ad0..88319de34 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -612,7 +612,7 @@ def guess_notification_type(search_term): @statsd(namespace='dao') -def fetch_new_aggregate_stats_by_date_range_for_all_services(start_date, end_date): +def fetch_aggregate_stats_by_date_range_for_all_services(start_date, end_date): start_date = get_london_midnight_in_utc(start_date) end_date = get_london_midnight_in_utc(end_date + timedelta(days=1)) table = NotificationHistory diff --git a/app/platform_stats/__init__.py b/app/platform_stats/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/app/platform_stats/rest.py b/app/platform_stats/rest.py new file mode 100644 index 000000000..a41c8e3ee --- /dev/null +++ b/app/platform_stats/rest.py @@ -0,0 +1,24 @@ +from datetime import datetime + +from flask import Blueprint, jsonify, request + +from app.dao.notifications_dao import fetch_aggregate_stats_by_date_range_for_all_services +from app.errors import register_errors +from app.service.statistics import format_admin_stats + +platform_stats_blueprint = Blueprint('platform_stats', __name__) + +register_errors(platform_stats_blueprint) + + +@platform_stats_blueprint.route('') +def get_new_platform_stats(): + # If start and end date are not set, we are expecting today's stats. + today = str(datetime.utcnow().date()) + + start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date() + end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date() + data = fetch_aggregate_stats_by_date_range_for_all_services(start_date=start_date, end_date=end_date) + stats = format_admin_stats(data) + + return jsonify(stats) diff --git a/app/service/rest.py b/app/service/rest.py index 5622f20db..4889fd3b1 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -18,7 +18,6 @@ from app.dao.api_key_dao import ( get_unsigned_secret, expire_api_key) from app.dao.inbound_numbers_dao import dao_allocate_number_for_service -from app.dao.notifications_dao import fetch_new_aggregate_stats_by_date_range_for_all_services from app.dao.organisation_dao import dao_get_organisation_by_service_id from app.dao.service_sms_sender_dao import ( archive_sms_sender, @@ -132,19 +131,6 @@ def get_platform_stats(): return result -@service_blueprint.route('/platform-stats-new', methods=['GET']) -def get_new_platform_stats(): - # If start and end date are not set, we are expecting today's stats. - today = str(datetime.utcnow().date()) - - start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date() - end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date() - data = fetch_new_aggregate_stats_by_date_range_for_all_services(start_date=start_date, end_date=end_date) - stats = statistics.format_admin_stats(data) - - return jsonify(stats) - - @service_blueprint.route('', methods=['GET']) def get_services(): only_active = request.args.get('only_active') == 'True' diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index e4c408cd4..d7f1401f5 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -34,7 +34,7 @@ from app.dao.notifications_dao import ( dao_get_notifications_by_references, dao_get_notification_history_by_reference, notifications_not_yet_sent, - fetch_new_aggregate_stats_by_date_range_for_all_services, + fetch_aggregate_stats_by_date_range_for_all_services, ) from app.dao.services_dao import dao_update_service from app.models import ( @@ -1941,7 +1941,7 @@ def test_notifications_not_yet_sent_return_no_rows(sample_service, notification_ ('NotificationHistory', 3), ]) @freeze_time('2018-01-08') -def test_fetch_new_aggregate_stats_by_date_range_for_all_services_uses_the_correct_table( +def test_fetch_aggregate_stats_by_date_range_for_all_services_uses_the_correct_table( mocker, notify_db_session, table_name, @@ -1952,21 +1952,21 @@ def test_fetch_new_aggregate_stats_by_date_range_for_all_services_uses_the_corre # mock the table that should not be used, then check it is not being called unused_table_mock = mocker.patch('app.dao.services_dao.{}'.format(table_name)) - fetch_new_aggregate_stats_by_date_range_for_all_services(start_date, end_date) + fetch_aggregate_stats_by_date_range_for_all_services(start_date, end_date) unused_table_mock.assert_not_called() -def test_fetch_new_aggregate_stats_by_date_range_for_all_services_returns_empty_list_when_no_stats(notify_db_session): +def test_fetch_aggregate_stats_by_date_range_for_all_services_returns_empty_list_when_no_stats(notify_db_session): start_date = date(2018, 1, 1) end_date = date(2018, 1, 5) - result = fetch_new_aggregate_stats_by_date_range_for_all_services(start_date, end_date) + result = fetch_aggregate_stats_by_date_range_for_all_services(start_date, end_date) assert result == [] @freeze_time('2018-01-08') -def test_fetch_new_aggregate_stats_by_date_range_for_all_services_groups_stats( +def test_fetch_aggregate_stats_by_date_range_for_all_services_groups_stats( sample_template, sample_email_template, sample_letter_template, @@ -1984,7 +1984,7 @@ def test_fetch_new_aggregate_stats_by_date_range_for_all_services_groups_stats( create_notification(template=sample_letter_template, status='virus-scan-failed', created_at=today) - result = fetch_new_aggregate_stats_by_date_range_for_all_services(today, today) + result = fetch_aggregate_stats_by_date_range_for_all_services(today, today) assert len(result) == 5 assert result[0] == ('email', 'permanent-failure', 'normal', 3) @@ -1994,12 +1994,12 @@ def test_fetch_new_aggregate_stats_by_date_range_for_all_services_groups_stats( assert result[4] == ('letter', 'virus-scan-failed', 'normal', 1) -def test_fetch_new_aggregate_stats_by_date_range_for_all_services_uses_bst_date(sample_template): +def test_fetch_aggregate_stats_by_date_range_for_all_services_uses_bst_date(sample_template): query_day = datetime(2018, 6, 5).date() create_notification(sample_template, status='sent', created_at=datetime(2018, 6, 4, 23, 59)) create_notification(sample_template, status='created', created_at=datetime(2018, 6, 5, 23, 00)) - result = fetch_new_aggregate_stats_by_date_range_for_all_services(query_day, query_day) + result = fetch_aggregate_stats_by_date_range_for_all_services(query_day, query_day) assert len(result) == 1 assert result[0].status == 'sent' diff --git a/tests/app/platform_stats/__init__.py b/tests/app/platform_stats/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/app/platform_stats/test_rest.py b/tests/app/platform_stats/test_rest.py new file mode 100644 index 000000000..b2e380980 --- /dev/null +++ b/tests/app/platform_stats/test_rest.py @@ -0,0 +1,25 @@ +from datetime import date, datetime + +from freezegun import freeze_time + + +@freeze_time('2018-06-01') +def test_get_new_platform_stats_uses_todays_date_if_no_start_or_end_date_is_provided(admin_request, mocker): + today = datetime.now().date() + dao_mock = mocker.patch('app.platform_stats.rest.fetch_aggregate_stats_by_date_range_for_all_services') + mocker.patch('app.service.rest.statistics.format_statistics') + + admin_request.get('platform_stats.get_new_platform_stats') + + dao_mock.assert_called_once_with(start_date=today, end_date=today) + + +def test_get_new_platform_stats_can_filter_by_date(admin_request, mocker): + start_date = date(2017, 1, 1) + end_date = date(2018, 1, 1) + dao_mock = mocker.patch('app.platform_stats.rest.fetch_aggregate_stats_by_date_range_for_all_services') + mocker.patch('app.service.rest.statistics.format_statistics') + + admin_request.get('platform_stats.get_new_platform_stats', start_date=start_date, end_date=end_date) + + dao_mock.assert_called_once_with(start_date=start_date, end_date=end_date) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 44b928c38..b9665dfc9 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -3085,28 +3085,6 @@ def test_get_platform_stats_creates_zero_stats(client, notify_db_session): assert json_resp['sms'] == {'failed': 0, 'requested': 4, 'delivered': 3} -@freeze_time('2018-06-01') -def test_get_new_platform_stats_uses_todays_date_if_no_start_or_end_date_is_provided(admin_request, mocker): - today = datetime.now().date() - dao_mock = mocker.patch('app.service.rest.fetch_new_aggregate_stats_by_date_range_for_all_services') - mocker.patch('app.service.rest.statistics.format_statistics') - - admin_request.get('service.get_new_platform_stats') - - dao_mock.assert_called_once_with(start_date=today, end_date=today) - - -def test_get_new_platform_stats_can_filter_by_date(admin_request, mocker): - start_date = date(2017, 1, 1) - end_date = date(2018, 1, 1) - dao_mock = mocker.patch('app.service.rest.fetch_new_aggregate_stats_by_date_range_for_all_services') - mocker.patch('app.service.rest.statistics.format_statistics') - - admin_request.get('service.get_new_platform_stats', start_date=start_date, end_date=end_date) - - dao_mock.assert_called_once_with(start_date=start_date, end_date=end_date) - - @pytest.mark.parametrize('today_only, stats', [ (False, {'requested': 2, 'delivered': 1, 'failed': 0}), (True, {'requested': 1, 'delivered': 0, 'failed': 0})