diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 257495b91..b1a3fa2fb 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -56,21 +56,6 @@ from app.utils import get_london_midnight_in_utc from app.utils import midnight_n_days_ago, escape_special_characters -@statsd(namespace="dao") -def dao_get_last_template_usage(template_id, template_type, service_id): - # By adding the service_id to the filter the performance of the query is greatly improved. - # Using a max(Notification.created_at) is better than order by and limit one. - # But the effort to change the endpoint to return a datetime only is more than the gain. - return Notification.query.filter( - Notification.template_id == template_id, - Notification.key_type != KEY_TYPE_TEST, - Notification.notification_type == template_type, - Notification.service_id == service_id - ).order_by( - desc(Notification.created_at) - ).first() - - @statsd(namespace="dao") def dao_get_last_date_template_was_used(template_id, service_id): last_date_from_notifications = db.session.query( diff --git a/app/template_statistics/rest.py b/app/template_statistics/rest.py index 3c5653936..9fa98aba2 100644 --- a/app/template_statistics/rest.py +++ b/app/template_statistics/rest.py @@ -1,11 +1,10 @@ from flask import Blueprint, jsonify, request from app import DATETIME_FORMAT -from app.dao.notifications_dao import dao_get_last_template_usage, dao_get_last_date_template_was_used +from app.dao.notifications_dao import dao_get_last_date_template_was_used from app.dao.templates_dao import dao_get_template_by_id_and_service_id from app.dao.fact_notification_status_dao import fetch_notification_status_for_service_for_today_and_7_previous_days -from app.schemas import notification_with_template_schema from app.errors import register_errors, InvalidRequest template_statistics = Blueprint('template_statistics', @@ -44,18 +43,6 @@ def get_template_statistics_for_service_by_day(service_id): ]) -@template_statistics.route('/') -def get_template_statistics_for_template_id(service_id, template_id): - template = dao_get_template_by_id_and_service_id(template_id, service_id) - - data = None - notification = dao_get_last_template_usage(template_id, template.template_type, template.service_id) - if notification: - data = notification_with_template_schema.dump(notification).data - - return jsonify(data=data) - - @template_statistics.route('/last-used/') def get_last_used_datetime_for_template(service_id, template_id): # Check the template and service exist diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 7cfa9e76e..4f406da9b 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -13,7 +13,6 @@ from app.dao.notifications_dao import ( dao_created_scheduled_notification, dao_delete_notifications_by_id, dao_get_last_notification_added_for_job_id, - dao_get_last_template_usage, dao_get_notifications_by_recipient_or_reference, dao_get_notification_count_for_job_id, dao_get_scheduled_notifications, @@ -62,7 +61,6 @@ from tests.app.db import ( def test_should_have_decorated_notifications_dao_functions(): - assert dao_get_last_template_usage.__wrapped__.__name__ == 'dao_get_last_template_usage' # noqa assert dao_create_notification.__wrapped__.__name__ == 'dao_create_notification' # noqa assert update_notification_status_by_id.__wrapped__.__name__ == 'update_notification_status_by_id' # noqa assert dao_update_notification.__wrapped__.__name__ == 'dao_update_notification' # noqa diff --git a/tests/app/dao/notification_dao/test_notification_dao_template_usage.py b/tests/app/dao/notification_dao/test_notification_dao_template_usage.py index 22198f2db..9d5d8c62f 100644 --- a/tests/app/dao/notification_dao/test_notification_dao_template_usage.py +++ b/tests/app/dao/notification_dao/test_notification_dao_template_usage.py @@ -1,59 +1,6 @@ from datetime import datetime, timedelta -import pytest -from app.dao.notifications_dao import dao_get_last_template_usage, dao_get_last_date_template_was_used -from tests.app.db import create_notification, create_template, create_ft_notification_status - - -def test_last_template_usage_should_get_right_data(sample_notification): - results = dao_get_last_template_usage(sample_notification.template_id, 'sms', sample_notification.service_id) - assert results.template.name == 'sms Template Name' - assert results.template.template_type == 'sms' - assert results.created_at == sample_notification.created_at - assert results.template_id == sample_notification.template_id - assert results.id == sample_notification.id - - -@pytest.mark.parametrize('notification_type', ['sms', 'email', 'letter']) -def test_last_template_usage_should_be_able_to_get_all_template_usage_history_order_by_notification_created_at( - sample_service, - notification_type -): - template = create_template(sample_service, template_type=notification_type) - - create_notification(template, created_at=datetime.utcnow() - timedelta(seconds=1)) - create_notification(template, created_at=datetime.utcnow() - timedelta(seconds=2)) - create_notification(template, created_at=datetime.utcnow() - timedelta(seconds=3)) - most_recent = create_notification(template) - - results = dao_get_last_template_usage(template.id, notification_type, template.service_id) - assert results.id == most_recent.id - - -def test_last_template_usage_should_ignore_test_keys( - sample_template, - sample_team_api_key, - sample_test_api_key -): - one_minute_ago = datetime.utcnow() - timedelta(minutes=1) - two_minutes_ago = datetime.utcnow() - timedelta(minutes=2) - - team_key = create_notification( - template=sample_template, - created_at=two_minutes_ago, - api_key=sample_team_api_key) - create_notification( - template=sample_template, - created_at=one_minute_ago, - api_key=sample_test_api_key) - - results = dao_get_last_template_usage(sample_template.id, 'sms', sample_template.service_id) - assert results.id == team_key.id - - -def test_last_template_usage_should_be_able_to_get_no_template_usage_history_if_no_notifications_using_template( - sample_template): - results = dao_get_last_template_usage(sample_template.id, 'sms', sample_template.service_id) - assert not results +from app.dao.notifications_dao import dao_get_last_date_template_was_used +from tests.app.db import create_notification, create_ft_notification_status def test_dao_get_last_date_template_was_used_returns_bst_date_from_stats_table( diff --git a/tests/app/template_statistics/test_rest.py b/tests/app/template_statistics/test_rest.py index 6733cb293..05eff5d93 100644 --- a/tests/app/template_statistics/test_rest.py +++ b/tests/app/template_statistics/test_rest.py @@ -134,63 +134,7 @@ def test_get_template_statistics_for_service_by_day_returns_empty_list_if_no_tem assert len(json_resp['data']) == 0 -# get_template_statistics_for_template - - -def test_get_template_statistics_for_template_returns_last_notification(admin_request, sample_template): - create_notification(sample_template) - create_notification(sample_template) - notification_3 = create_notification(sample_template) - - json_resp = admin_request.get( - 'template_statistics.get_template_statistics_for_template_id', - service_id=notification_3.service_id, - template_id=notification_3.template_id - ) - - assert json_resp['data']['id'] == str(notification_3.id) - - -def test_get_template_statistics_for_template_returns_empty_if_no_statistics( - admin_request, - sample_template, -): - json_resp = admin_request.get( - 'template_statistics.get_template_statistics_for_template_id', - service_id=sample_template.service_id, - template_id=sample_template.id - ) - - assert not json_resp['data'] - - -def test_get_template_statistics_for_template_raises_error_for_nonexistent_template( - admin_request, - sample_service, - fake_uuid -): - json_resp = admin_request.get( - 'template_statistics.get_template_statistics_for_template_id', - service_id=sample_service.id, - template_id=fake_uuid, - _expected_status=404 - ) - - assert json_resp['message'] == 'No result found' - assert json_resp['result'] == 'error' - - -def test_get_template_statistics_for_template_returns_empty_for_old_notification( - admin_request, - sample_notification_history -): - json_resp = admin_request.get( - 'template_statistics.get_template_statistics_for_template_id', - service_id=sample_notification_history.service_id, - template_id=sample_notification_history.template_id - ) - - assert not json_resp['data'] +# get_last_used_datetime_for_template def test_get_last_used_datetime_for_template(