diff --git a/app/dao/fact_notification_status_dao.py b/app/dao/fact_notification_status_dao.py index eb319c177..9d0041348 100644 --- a/app/dao/fact_notification_status_dao.py +++ b/app/dao/fact_notification_status_dao.py @@ -3,6 +3,8 @@ from datetime import datetime, timedelta, time from flask import current_app from sqlalchemy import func from sqlalchemy.dialects.postgresql import insert +from sqlalchemy.sql.expression import literal +from sqlalchemy.types import DateTime from app import db from app.models import Notification, NotificationHistory, FactNotificationStatus @@ -95,19 +97,15 @@ def fetch_notification_status_for_service_by_month(start_date, end_date, service def fetch_notification_status_for_service_for_day(bst_day, service_id): return db.session.query( # return current month as a datetime so the data has the same shape as the ft_notification_status query - get_london_month_from_utc_column(func.current_date()), + literal(bst_day.replace(day=1), type_=DateTime).label('month'), Notification.notification_type, - Notification.status, - func.count() + Notification.status.label('notification_status'), + func.count().label('count') ).filter( Notification.created_at >= get_london_midnight_in_utc(bst_day), Notification.created_at < get_london_midnight_in_utc(bst_day + timedelta(days=1)), Notification.service_id == service_id ).group_by( - Notification.template_id, - Notification.service_id, - 'job_id', Notification.notification_type, - Notification.key_type, Notification.status ).all() diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 68f0ea781..c9c86cc4c 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -291,48 +291,6 @@ def _stats_for_service_query(service_id): ) -@statsd(namespace="dao") -def dao_fetch_monthly_historical_stats_for_service(service_id, year): - month = get_london_month_from_utc_column(NotificationHistory.created_at) - - start_date, end_date = get_financial_year(year) - rows = db.session.query( - NotificationHistory.notification_type, - NotificationHistory.status, - month, - func.count(NotificationHistory.id).label('count') - ).filter( - NotificationHistory.service_id == service_id, - NotificationHistory.created_at.between(start_date, end_date) - ).group_by( - NotificationHistory.notification_type, - NotificationHistory.status, - month - ).order_by( - month - ) - - months = { - datetime.strftime(created_date, '%Y-%m'): { - template_type: dict.fromkeys( - NOTIFICATION_STATUS_TYPES, - 0 - ) - for template_type in TEMPLATE_TYPES - } - for created_date in [ - datetime(year, month, 1) for month in range(4, 13) - ] + [ - datetime(year + 1, month, 1) for month in range(1, 4) - ] - } - - for notification_type, status, created_date, count in rows: - months[datetime.strftime(created_date, "%Y-%m")][notification_type][status] = count - - return months - - @statsd(namespace='dao') def dao_fetch_todays_stats_for_all_services(include_from_test_key=True, only_active=True): today = date.today() diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 8cd5acca7..e9a85a2eb 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -24,7 +24,6 @@ from app.dao.services_dao import ( delete_service_and_all_associated_db_objects, dao_fetch_stats_for_service, dao_fetch_todays_stats_for_service, - dao_fetch_monthly_historical_stats_for_service, fetch_todays_total_message_count, dao_fetch_todays_stats_for_all_services, fetch_stats_by_date_range_for_all_services, @@ -80,7 +79,6 @@ from tests.app.conftest import ( def test_should_have_decorated_services_dao_functions(): - assert dao_fetch_monthly_historical_stats_for_service.__wrapped__.__name__ == 'dao_fetch_monthly_historical_stats_for_service' # noqa assert dao_fetch_todays_stats_for_service.__wrapped__.__name__ == 'dao_fetch_todays_stats_for_service' # noqa assert dao_fetch_stats_for_service.__wrapped__.__name__ == 'dao_fetch_stats_for_service' # noqa @@ -648,62 +646,6 @@ def test_fetch_stats_should_not_gather_notifications_older_than_7_days(notify_db assert stats['created'] == 1 -def test_fetch_monthly_historical_stats_separates_months(notify_db, notify_db_session, sample_template): - notification_history = functools.partial( - create_notification_history, - notify_db, - notify_db_session, - sample_template - ) - # _before_start_of_financial_year - notification_history(created_at=datetime(2016, 3, 31)) - # start_of_financial_year - notification_history(created_at=datetime(2016, 4, 1)) - # start_of_summer - notification_history(created_at=datetime(2016, 6, 20)) - # start_of_autumn - notification_history(created_at=datetime(2016, 9, 30, 23, 30, 0)) # October because BST - # start_of_winter - notification_history(created_at=datetime(2016, 12, 1), status='delivered') - # start_of_spring - notification_history(created_at=datetime(2017, 3, 11)) - # end_of_financial_year - notification_history(created_at=datetime(2017, 3, 31)) - # _after_end_of_financial_year - notification_history(created_at=datetime(2017, 3, 31, 23, 30)) # after because BST - - result = dao_fetch_monthly_historical_stats_for_service(sample_template.service_id, 2016) - - for day, status, count in ( - ('2016-04', 'sending', 0), - ('2016-04', 'delivered', 0), - ('2016-04', 'pending', 0), - ('2016-04', 'failed', 0), - ('2016-04', 'technical-failure', 0), - ('2016-04', 'temporary-failure', 0), - ('2016-04', 'permanent-failure', 0), - - ('2016-06', 'created', 1), - - ('2016-10', 'created', 1), - - ('2016-12', 'created', 0), - ('2016-12', 'delivered', 1), - - ('2017-03', 'created', 2), - ): - assert result[day]['sms'][status] == count - assert result[day]['email'][status] == 0 - assert result[day]['letter'][status] == 0 - - assert result.keys() == { - '2016-04', '2016-05', '2016-06', - '2016-07', '2016-08', '2016-09', - '2016-10', '2016-11', '2016-12', - '2017-01', '2017-02', '2017-03', - } - - def test_dao_fetch_todays_total_message_count_returns_count_for_today(notify_db, notify_db_session, sample_notification):