diff --git a/app/celery/scheduled_tasks.py b/app/celery/scheduled_tasks.py index cde969034..818206e76 100644 --- a/app/celery/scheduled_tasks.py +++ b/app/celery/scheduled_tasks.py @@ -40,10 +40,6 @@ from app.dao.provider_details_dao import ( dao_toggle_sms_provider ) from app.dao.service_callback_api_dao import get_service_delivery_status_callback_api_for_service -from app.dao.services_dao import ( - dao_fetch_monthly_historical_stats_by_template -) -from app.dao.stats_template_usage_by_month_dao import insert_or_update_stats_for_template from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago from app.exceptions import NotificationTechnicalFailureException from app.models import ( @@ -405,21 +401,6 @@ def check_job_status(): raise JobIncompleteError("Job(s) {} have not completed.".format(job_ids)) -@notify_celery.task(name='daily-stats-template-usage-by-month') -@statsd(namespace="tasks") -def daily_stats_template_usage_by_month(): - results = dao_fetch_monthly_historical_stats_by_template() - - for result in results: - if result.template_id: - insert_or_update_stats_for_template( - result.template_id, - result.month, - result.year, - result.count - ) - - @notify_celery.task(name='raise-alert-if-no-letter-ack-file') @statsd(namespace="tasks") def letter_raise_alert_if_no_ack_file_for_zip(): diff --git a/app/config.py b/app/config.py index a0569534e..f69e30869 100644 --- a/app/config.py +++ b/app/config.py @@ -195,11 +195,6 @@ class Config(object): 'schedule': crontab(hour=0, minute=5), 'options': {'queue': QueueNames.PERIODIC} }, - 'daily-stats-template-usage-by-month': { - 'task': 'daily-stats-template-usage-by-month', - 'schedule': crontab(hour=0, minute=10), - 'options': {'queue': QueueNames.PERIODIC} - }, 'create-nightly-billing': { 'task': 'create-nightly-billing', 'schedule': crontab(hour=0, minute=15), diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index fb5cba297..342ba7dfb 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -11,9 +11,7 @@ from app.dao.dao_utils import ( transactional, version_class ) -from app.dao.date_util import get_financial_year from app.dao.service_sms_sender_dao import insert_service_sms_sender -from app.dao.stats_template_usage_by_month_dao import dao_get_template_usage_stats_by_service from app.models import ( AnnualBilling, ApiKey, @@ -389,75 +387,3 @@ def dao_fetch_monthly_historical_stats_by_template(): year, month ).all() - - -@statsd(namespace="dao") -def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year): - - results = dao_get_template_usage_stats_by_service(service_id, year) - - stats = [] - for result in results: - stat = type("", (), {})() - stat.template_id = result.template_id - stat.template_type = result.template_type - stat.name = str(result.name) - stat.month = result.month - stat.year = result.year - stat.count = result.count - stat.is_precompiled_letter = result.is_precompiled_letter - stats.append(stat) - - month = get_london_month_from_utc_column(Notification.created_at) - year_func = func.date_trunc("year", Notification.created_at) - start_date = datetime.combine(date.today(), time.min) - - fy_start, fy_end = get_financial_year(year) - - if fy_start < datetime.now() < fy_end: - today_results = db.session.query( - Notification.template_id, - Template.is_precompiled_letter, - Template.name, - Template.template_type, - extract('month', month).label('month'), - extract('year', year_func).label('year'), - func.count().label('count') - ).join( - Template, Notification.template_id == Template.id, - ).filter( - Notification.created_at >= start_date, - Notification.service_id == service_id, - # we don't want to include test keys - Notification.key_type != KEY_TYPE_TEST - ).group_by( - Notification.template_id, - Template.hidden, - Template.name, - Template.template_type, - month, - year_func - ).order_by( - Notification.template_id - ).all() - - for today_result in today_results: - add_to_stats = True - for stat in stats: - if today_result.template_id == stat.template_id and today_result.month == stat.month \ - and today_result.year == stat.year: - stat.count = stat.count + today_result.count - add_to_stats = False - - if add_to_stats: - new_stat = type("StatsTemplateUsageByMonth", (), {})() - new_stat.template_id = today_result.template_id - new_stat.template_type = today_result.template_type - new_stat.name = today_result.name - new_stat.month = int(today_result.month) - new_stat.year = int(today_result.year) - new_stat.count = today_result.count - new_stat.is_precompiled_letter = today_result.is_precompiled_letter - stats.append(new_stat) - - return stats diff --git a/app/dao/stats_template_usage_by_month_dao.py b/app/dao/stats_template_usage_by_month_dao.py deleted file mode 100644 index 541ab7193..000000000 --- a/app/dao/stats_template_usage_by_month_dao.py +++ /dev/null @@ -1,60 +0,0 @@ -from notifications_utils.statsd_decorators import statsd -from sqlalchemy import or_, and_, desc - -from app import db -from app.dao.dao_utils import transactional -from app.models import StatsTemplateUsageByMonth, Template - - -@transactional -@statsd(namespace="dao") -def insert_or_update_stats_for_template(template_id, month, year, count): - result = db.session.query( - StatsTemplateUsageByMonth - ).filter( - StatsTemplateUsageByMonth.template_id == template_id, - StatsTemplateUsageByMonth.month == month, - StatsTemplateUsageByMonth.year == year - ).update( - { - 'count': count - } - ) - if result == 0: - monthly_stats = StatsTemplateUsageByMonth( - template_id=template_id, - month=month, - year=year, - count=count - ) - - db.session.add(monthly_stats) - - -@statsd(namespace="dao") -def dao_get_template_usage_stats_by_service(service_id, year): - return db.session.query( - StatsTemplateUsageByMonth.template_id, - Template.name, - Template.template_type, - Template.is_precompiled_letter, - StatsTemplateUsageByMonth.month, - StatsTemplateUsageByMonth.year, - StatsTemplateUsageByMonth.count - ).join( - Template, StatsTemplateUsageByMonth.template_id == Template.id - ).filter( - Template.service_id == service_id - ).filter( - or_( - and_( - StatsTemplateUsageByMonth.month.in_([4, 5, 6, 7, 8, 9, 10, 11, 12]), - StatsTemplateUsageByMonth.year == year - ), and_( - StatsTemplateUsageByMonth.month.in_([1, 2, 3]), - StatsTemplateUsageByMonth.year == year + 1 - ) - ) - ).order_by( - desc(StatsTemplateUsageByMonth.month) - ).all() diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index b2fee242a..e97b4643c 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -1,4 +1,3 @@ -import functools from datetime import datetime, timedelta from functools import partial from unittest.mock import call, patch, PropertyMock @@ -31,7 +30,6 @@ from app.celery.scheduled_tasks import ( send_total_sent_notifications_to_performance_platform, switch_current_sms_provider_on_slow_delivery, timeout_notifications, - daily_stats_template_usage_by_month, letter_raise_alert_if_no_ack_file_for_zip, replay_created_notifications ) @@ -46,8 +44,6 @@ from app.dao.provider_details_dao import ( ) from app.exceptions import NotificationTechnicalFailureException from app.models import ( - NotificationHistory, - StatsTemplateUsageByMonth, JOB_STATUS_IN_PROGRESS, JOB_STATUS_ERROR, LETTER_TYPE, @@ -69,7 +65,6 @@ from tests.app.db import ( from tests.app.conftest import ( sample_job as create_sample_job, sample_notification_history as create_notification_history, - sample_template as create_sample_template, datetime_in_past ) @@ -806,151 +801,6 @@ def test_check_job_status_task_sets_jobs_to_error(mocker, sample_template): assert job_2.job_status == JOB_STATUS_IN_PROGRESS -def test_daily_stats_template_usage_by_month(notify_db, notify_db_session): - notification_history = functools.partial( - create_notification_history, - notify_db, - notify_db_session, - status='delivered' - ) - - template_one = create_sample_template(notify_db, notify_db_session) - template_two = create_sample_template(notify_db, notify_db_session) - - notification_history(created_at=datetime(2017, 10, 1), sample_template=template_one) - notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two) - notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two) - notification_history(created_at=datetime.now(), sample_template=template_two) - - daily_stats_template_usage_by_month() - - result = db.session.query( - StatsTemplateUsageByMonth - ).order_by( - StatsTemplateUsageByMonth.year, - StatsTemplateUsageByMonth.month - ).all() - - assert len(result) == 2 - - assert result[0].template_id == template_two.id - assert result[0].month == 4 - assert result[0].year == 2016 - assert result[0].count == 2 - - assert result[1].template_id == template_one.id - assert result[1].month == 10 - assert result[1].year == 2017 - assert result[1].count == 1 - - -def test_daily_stats_template_usage_by_month_no_data(): - daily_stats_template_usage_by_month() - - results = db.session.query(StatsTemplateUsageByMonth).all() - - assert len(results) == 0 - - -def test_daily_stats_template_usage_by_month_multiple_runs(notify_db, notify_db_session): - notification_history = functools.partial( - create_notification_history, - notify_db, - notify_db_session, - status='delivered' - ) - - template_one = create_sample_template(notify_db, notify_db_session) - template_two = create_sample_template(notify_db, notify_db_session) - - notification_history(created_at=datetime(2017, 11, 1), sample_template=template_one) - notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two) - notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two) - notification_history(created_at=datetime.now(), sample_template=template_two) - - daily_stats_template_usage_by_month() - - template_three = create_sample_template(notify_db, notify_db_session) - - notification_history(created_at=datetime(2017, 10, 1), sample_template=template_three) - notification_history(created_at=datetime(2017, 9, 1), sample_template=template_three) - notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two) - notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two) - notification_history(created_at=datetime.now(), sample_template=template_two) - - daily_stats_template_usage_by_month() - - result = db.session.query( - StatsTemplateUsageByMonth - ).order_by( - StatsTemplateUsageByMonth.year, - StatsTemplateUsageByMonth.month - ).all() - - assert len(result) == 4 - - assert result[0].template_id == template_two.id - assert result[0].month == 4 - assert result[0].year == 2016 - assert result[0].count == 4 - - assert result[1].template_id == template_three.id - assert result[1].month == 9 - assert result[1].year == 2017 - assert result[1].count == 1 - - assert result[2].template_id == template_three.id - assert result[2].month == 10 - assert result[2].year == 2017 - assert result[2].count == 1 - - assert result[3].template_id == template_one.id - assert result[3].month == 11 - assert result[3].year == 2017 - assert result[3].count == 1 - - -def test_dao_fetch_monthly_historical_stats_by_template_null_template_id_not_counted(notify_db, notify_db_session): - notification_history = functools.partial( - create_notification_history, - notify_db, - notify_db_session, - status='delivered' - ) - - template_one = create_sample_template(notify_db, notify_db_session, template_name='1') - history = notification_history(created_at=datetime(2017, 2, 1), sample_template=template_one) - - NotificationHistory.query.filter( - NotificationHistory.id == history.id - ).update( - { - 'template_id': None - } - ) - - daily_stats_template_usage_by_month() - - result = db.session.query( - StatsTemplateUsageByMonth - ).all() - - assert len(result) == 0 - - notification_history(created_at=datetime(2017, 2, 1), sample_template=template_one) - - daily_stats_template_usage_by_month() - - result = db.session.query( - StatsTemplateUsageByMonth - ).order_by( - StatsTemplateUsageByMonth.year, - StatsTemplateUsageByMonth.month - ).all() - - assert len(result) == 1 - - def mock_s3_get_list_match(bucket_name, subfolder='', suffix='', last_modified=None): if subfolder == '2018-01-11/zips_sent': return ['NOTIFY.20180111175007.ZIP.TXT', 'NOTIFY.20180111175008.ZIP.TXT'] diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 024b1c019..766f2ef59 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -1,5 +1,5 @@ import uuid -from datetime import datetime, timedelta +from datetime import datetime import pytest from freezegun import freeze_time @@ -7,7 +7,6 @@ from sqlalchemy.exc import IntegrityError, SQLAlchemyError from sqlalchemy.orm.exc import FlushError, NoResultFound from app import db -from app.celery.scheduled_tasks import daily_stats_template_usage_by_month from app.dao.inbound_numbers_dao import ( dao_set_inbound_number_to_service, dao_get_available_inbound_numbers, @@ -32,7 +31,6 @@ from app.dao.services_dao import ( dao_fetch_active_users_for_service, dao_fetch_service_by_inbound_number, dao_fetch_monthly_historical_stats_by_template, - dao_fetch_monthly_historical_usage_by_template_for_service ) from app.dao.users_dao import save_model_user, create_user_code from app.models import ( @@ -901,441 +899,9 @@ def test_dao_fetch_monthly_historical_stats_by_template(notify_db_session): assert result[1].count == 1 -def test_dao_fetch_monthly_historical_usage_by_template_for_service_no_stats_today( - notify_db_session, -): - service = create_service() - template_one = create_template(service=service, template_name='1') - template_two = create_template(service=service, template_name='2') - - n = create_notification(created_at=datetime(2017, 10, 1), template=template_one, status='delivered') - create_notification(created_at=datetime(2017, 4, 1), template=template_two, status='delivered') - create_notification(created_at=datetime(2017, 4, 1), template=template_two, status='delivered') - create_notification(created_at=datetime.now(), template=template_two, status='delivered') - - daily_stats_template_usage_by_month() - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 2 - - assert result[0].template_id == template_two.id - assert result[0].name == template_two.name - assert result[0].template_type == template_two.template_type - assert result[0].month == 4 - assert result[0].year == 2017 - assert result[0].count == 2 - - assert result[1].template_id == template_one.id - assert result[1].name == template_one.name - assert result[1].template_type == template_two.template_type - assert result[1].month == 10 - assert result[1].year == 2017 - assert result[1].count == 1 - - -@freeze_time("2017-11-10 11:09:00.000000") -def test_dao_fetch_monthly_historical_usage_by_template_for_service_add_to_historical( - notify_db_session, -): - service = create_service() - template_one = create_template(service=service, template_name='1') - template_two = create_template(service=service, template_name='2') - template_three = create_template(service=service, template_name='3') - - date = datetime.now() - day = date.day - month = date.month - year = date.year - - n = create_notification(created_at=datetime(2017, 9, 1), template=template_one, status='delivered') - create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two, - status='delivered') - create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two, - status='delivered') - - daily_stats_template_usage_by_month() - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 2 - - assert result[0].template_id == template_one.id - assert result[0].name == template_one.name - assert result[0].template_type == template_one.template_type - assert result[0].month == 9 - assert result[0].year == 2017 - assert result[0].count == 1 - - assert result[1].template_id == template_two.id - assert result[1].name == template_two.name - assert result[1].template_type == template_two.template_type - assert result[1].month == 11 - assert result[1].year == 2017 - assert result[1].count == 2 - - create_notification( - template=template_three, - created_at=datetime.now(), - status='delivered' - ) - create_notification( - template=template_two, - created_at=datetime.now(), - status='delivered' - ) - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 3 - - assert result[0].template_id == template_one.id - assert result[0].name == template_one.name - assert result[0].template_type == template_one.template_type - assert result[0].month == 9 - assert result[0].year == 2017 - assert result[0].count == 1 - - assert result[1].template_id == template_two.id - assert result[1].name == template_two.name - assert result[1].template_type == template_two.template_type - assert result[1].month == month - assert result[1].year == year - assert result[1].count == 3 - - assert result[2].template_id == template_three.id - assert result[2].name == template_three.name - assert result[2].template_type == template_three.template_type - assert result[2].month == 11 - assert result[2].year == 2017 - assert result[2].count == 1 - - -@freeze_time("2017-11-10 11:09:00.000000") -def test_dao_fetch_monthly_historical_usage_by_template_for_service_does_add_old_notification( - notify_db_session, -): - template_one, template_three, template_two = create_email_sms_letter_template() - - date = datetime.now() - day = date.day - month = date.month - year = date.year - - n = create_notification(created_at=datetime(2017, 9, 1), template=template_one, status='delivered') - create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two, - status='delivered') - create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two, - status='delivered') - - daily_stats_template_usage_by_month() - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 2 - - assert result[0].template_id == template_one.id - assert result[0].name == template_one.name - assert result[0].template_type == template_one.template_type - assert result[0].month == 9 - assert result[0].year == 2017 - assert result[0].count == 1 - - assert result[1].template_id == template_two.id - assert result[1].name == template_two.name - assert result[1].template_type == template_two.template_type - assert result[1].month == 11 - assert result[1].year == 2017 - assert result[1].count == 2 - - create_notification( - template=template_three, - created_at=datetime.utcnow() - timedelta(days=2), - status='delivered' - ) - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 2 - - -@freeze_time("2017-11-10 11:09:00.000000") -def test_dao_fetch_monthly_historical_usage_by_template_for_service_get_this_year_only( - notify_db_session, -): - template_one, template_three, template_two = create_email_sms_letter_template() - - date = datetime.now() - day = date.day - month = date.month - year = date.year - - n = create_notification(created_at=datetime(2016, 9, 1), template=template_one, status='delivered') - create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two, - status='delivered') - create_notification(created_at=datetime(year, month, day) - timedelta(days=1), template=template_two, - status='delivered') - - daily_stats_template_usage_by_month() - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 1 - - assert result[0].template_id == template_two.id - assert result[0].name == template_two.name - assert result[0].template_type == template_two.template_type - assert result[0].month == 11 - assert result[0].year == 2017 - assert result[0].count == 2 - - create_notification( - template=template_three, - created_at=datetime.utcnow() - timedelta(days=2) - ) - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 1 - - create_notification( - template=template_three, - created_at=datetime.utcnow() - ) - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 2 - - def create_email_sms_letter_template(): service = create_service() template_one = create_template(service=service, template_name='1', template_type='email') template_two = create_template(service=service, template_name='2', template_type='sms') template_three = create_template(service=service, template_name='3', template_type='letter') return template_one, template_three, template_two - - -@freeze_time("2017-11-10 11:09:00.000000") -def test_dao_fetch_monthly_historical_usage_by_template_for_service_combined_historical_current( - notify_db_session, -): - template_one = create_template(service=create_service(), template_name='1') - - date = datetime.now() - day = date.day - month = date.month - year = date.year - - n = create_notification(status='delivered', created_at=datetime(year, month, day) - timedelta(days=30), - template=template_one) - - daily_stats_template_usage_by_month() - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 1 - - assert result[0].template_id == template_one.id - assert result[0].name == template_one.name - assert result[0].template_type == template_one.template_type - assert result[0].month == 10 - assert result[0].year == 2017 - assert result[0].count == 1 - - create_notification( - template=template_one, - created_at=datetime.utcnow() - ) - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 2 - - assert result[0].template_id == template_one.id - assert result[0].name == template_one.name - assert result[0].template_type == template_one.template_type - assert result[0].month == 10 - assert result[0].year == 2017 - assert result[0].count == 1 - - assert result[1].template_id == template_one.id - assert result[1].name == template_one.name - assert result[1].template_type == template_one.template_type - assert result[1].month == 11 - assert result[1].year == 2017 - assert result[1].count == 1 - - -@freeze_time("2017-11-10 11:09:00.000000") -def test_dao_fetch_monthly_historical_usage_by_template_for_service_does_not_return_double_precision_values( - notify_db_session, -): - template_one = create_template(service=create_service()) - - n = create_notification( - template=template_one, - created_at=datetime.utcnow() - ) - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.month, x.year) - ) - - assert len(result) == 1 - - assert result[0].template_id == template_one.id - assert result[0].name == template_one.name - assert result[0].template_type == template_one.template_type - assert result[0].month == 11 - assert len(str(result[0].month)) == 2 - assert result[0].year == 2017 - assert len(str(result[0].year)) == 4 - assert result[0].count == 1 - - -@freeze_time("2018-03-10 11:09:00.000000") -def test_dao_fetch_monthly_historical_usage_by_template_for_service_returns_financial_year( - notify_db, - notify_db_session, -): - service = create_service() - template_one = create_template(service=service, template_name='1', template_type='email') - - date = datetime.now() - day = date.day - year = date.year - - create_notification(template=template_one, status='delivered', created_at=datetime(year - 1, 1, day)) - create_notification(template=template_one, status='delivered', created_at=datetime(year - 1, 3, day)) - create_notification(template=template_one, status='delivered', created_at=datetime(year - 1, 4, day)) - create_notification(template=template_one, status='delivered', created_at=datetime(year - 1, 5, day)) - create_notification(template=template_one, status='delivered', created_at=datetime(year, 1, day)) - create_notification(template=template_one, status='delivered', created_at=datetime(year, 2, day)) - - daily_stats_template_usage_by_month() - - n = create_notification( - template=template_one, - created_at=datetime.utcnow() - ) - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017), - key=lambda x: (x.year, x.month) - ) - - assert len(result) == 5 - - assert result[0].month == 4 - assert result[0].year == 2017 - assert result[1].month == 5 - assert result[1].year == 2017 - assert result[2].month == 1 - assert result[2].year == 2018 - assert result[3].month == 2 - assert result[3].year == 2018 - assert result[4].month == 3 - assert result[4].year == 2018 - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2014), - key=lambda x: (x.year, x.month) - ) - - assert len(result) == 0 - - -@freeze_time("2018-03-10 11:09:00.000000") -def test_dao_fetch_monthly_historical_usage_by_template_for_service_only_returns_for_service( - notify_db_session -): - template_one = create_template(service=create_service(), template_name='1', template_type='email') - - date = datetime.now() - day = date.day - year = date.year - - create_notification(template=template_one, created_at=datetime(year, 1, day)) - create_notification(template=template_one, created_at=datetime(year, 2, day)) - create_notification(template=template_one, created_at=datetime(year, 3, day)) - - service_two = create_service(service_name='other_service', user=create_user()) - template_two = create_template(service=service_two, template_name='1', template_type='email') - - create_notification(template=template_two) - create_notification(template=template_two) - - daily_stats_template_usage_by_month() - - x = dao_fetch_monthly_historical_usage_by_template_for_service(template_one.service_id, 2017) - - result = sorted( - x, - key=lambda x: (x.year, x.month) - ) - - assert len(result) == 3 - - result = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(service_two.id, 2017), - key=lambda x: (x.year, x.month) - ) - - assert len(result) == 1 - - -@freeze_time("2018-01-01 11:09:00.000000") -def test_dao_fetch_monthly_historical_usage_by_template_for_service_ignores_test_api_keys(notify_db_session): - service = create_service() - template_1 = create_template(service, template_name='1') - template_2 = create_template(service, template_name='2') - template_3 = create_template(service, template_name='3') - - create_notification(template_1, key_type=KEY_TYPE_TEST) - create_notification(template_2, key_type=KEY_TYPE_TEAM) - create_notification(template_3, key_type=KEY_TYPE_NORMAL) - - results = sorted( - dao_fetch_monthly_historical_usage_by_template_for_service(service.id, 2017), - key=lambda x: x.name - ) - - assert len(results) == 2 - # template_1 only used with test keys - assert results[0].template_id == template_2.id - assert results[0].count == 1 - - assert results[1].template_id == template_3.id - assert results[1].count == 1 diff --git a/tests/app/dao/test_stats_template_usage_by_month_dao.py b/tests/app/dao/test_stats_template_usage_by_month_dao.py deleted file mode 100644 index 676e00952..000000000 --- a/tests/app/dao/test_stats_template_usage_by_month_dao.py +++ /dev/null @@ -1,155 +0,0 @@ -from app import db -from app.dao.stats_template_usage_by_month_dao import ( - insert_or_update_stats_for_template, - dao_get_template_usage_stats_by_service -) -from app.models import StatsTemplateUsageByMonth, LETTER_TYPE, PRECOMPILED_TEMPLATE_NAME - -from tests.app.db import create_service, create_template - - -def test_create_stats_for_template(notify_db_session, sample_template): - assert StatsTemplateUsageByMonth.query.count() == 0 - - insert_or_update_stats_for_template(sample_template.id, 1, 2017, 10) - stats_by_month = StatsTemplateUsageByMonth.query.filter( - StatsTemplateUsageByMonth.template_id == sample_template.id - ).all() - - assert len(stats_by_month) == 1 - assert stats_by_month[0].template_id == sample_template.id - assert stats_by_month[0].month == 1 - assert stats_by_month[0].year == 2017 - assert stats_by_month[0].count == 10 - - -def test_update_stats_for_template(notify_db_session, sample_template): - assert StatsTemplateUsageByMonth.query.count() == 0 - - insert_or_update_stats_for_template(sample_template.id, 1, 2017, 10) - insert_or_update_stats_for_template(sample_template.id, 1, 2017, 20) - insert_or_update_stats_for_template(sample_template.id, 2, 2017, 30) - - stats_by_month = StatsTemplateUsageByMonth.query.filter( - StatsTemplateUsageByMonth.template_id == sample_template.id - ).order_by(StatsTemplateUsageByMonth.template_id).all() - - assert len(stats_by_month) == 2 - - assert stats_by_month[0].template_id == sample_template.id - assert stats_by_month[0].month == 1 - assert stats_by_month[0].year == 2017 - assert stats_by_month[0].count == 20 - - assert stats_by_month[1].template_id == sample_template.id - assert stats_by_month[1].month == 2 - assert stats_by_month[1].year == 2017 - assert stats_by_month[1].count == 30 - - -def test_dao_get_template_usage_stats_by_service(sample_service): - - email_template = create_template(service=sample_service, template_type="email") - - new_service = create_service(service_name="service_one") - - template_new_service = create_template(service=new_service) - - db.session.add(StatsTemplateUsageByMonth( - template_id=email_template.id, - month=4, - year=2017, - count=10 - )) - - db.session.add(StatsTemplateUsageByMonth( - template_id=template_new_service.id, - month=4, - year=2017, - count=10 - )) - - result = dao_get_template_usage_stats_by_service(sample_service.id, 2017) - - assert len(result) == 1 - - -def test_dao_get_template_usage_stats_by_service_for_precompiled_letters(sample_service): - - letter_template = create_template(service=sample_service, template_type=LETTER_TYPE) - - precompiled_letter_template = create_template( - service=sample_service, template_name=PRECOMPILED_TEMPLATE_NAME, hidden=True, template_type=LETTER_TYPE) - - db.session.add(StatsTemplateUsageByMonth( - template_id=letter_template.id, - month=5, - year=2017, - count=10 - )) - - db.session.add(StatsTemplateUsageByMonth( - template_id=precompiled_letter_template.id, - month=4, - year=2017, - count=20 - )) - - result = dao_get_template_usage_stats_by_service(sample_service.id, 2017) - - assert len(result) == 2 - assert [ - (letter_template.id, 'letter Template Name', 'letter', False, 5, 2017, 10), - (precompiled_letter_template.id, PRECOMPILED_TEMPLATE_NAME, 'letter', True, 4, 2017, 20) - ] == result - - -def test_dao_get_template_usage_stats_by_service_specific_year(sample_service): - - email_template = create_template(service=sample_service, template_type="email") - - db.session.add(StatsTemplateUsageByMonth( - template_id=email_template.id, - month=3, - year=2017, - count=10 - )) - - db.session.add(StatsTemplateUsageByMonth( - template_id=email_template.id, - month=4, - year=2017, - count=10 - )) - - db.session.add(StatsTemplateUsageByMonth( - template_id=email_template.id, - month=3, - year=2018, - count=10 - )) - - db.session.add(StatsTemplateUsageByMonth( - template_id=email_template.id, - month=4, - year=2018, - count=10 - )) - - result = dao_get_template_usage_stats_by_service(sample_service.id, 2017) - - assert len(result) == 2 - - assert result[0].template_id == email_template.id - assert result[0].name == email_template.name - assert result[0].template_type == email_template.template_type - assert result[0].month == 4 - assert result[0].year == 2017 - assert result[0].count == 10 - - assert result[1].template_id == email_template.id - assert result[1].name == email_template.name - assert result[1].template_type == email_template.template_type - assert result[1].month == 3 - assert result[1].year == 2018 - assert result[1].count == 10 diff --git a/tests/app/service/test_statistics_rest.py b/tests/app/service/test_statistics_rest.py index edf9e5b9a..519612de5 100644 --- a/tests/app/service/test_statistics_rest.py +++ b/tests/app/service/test_statistics_rest.py @@ -4,7 +4,6 @@ from datetime import datetime, date import pytest from freezegun import freeze_time -from app.celery.scheduled_tasks import daily_stats_template_usage_by_month from app.models import ( EMAIL_TYPE, SMS_TYPE, @@ -55,22 +54,6 @@ def test_get_template_usage_by_month_returns_correct_data( assert resp_json[1]["count"] == 1 -@freeze_time('2017-11-11 02:00') -def test_get_template_usage_by_month_returns_no_data(admin_request, sample_template): - create_notification(sample_template, created_at=datetime(2016, 4, 1), status='created') - - daily_stats_template_usage_by_month() - - create_notification(sample_template, created_at=datetime.utcnow()) - - resp_json = admin_request.get( - 'service.get_monthly_template_usage', - service_id=sample_template.service_id, - year=2015 - ) - assert resp_json['stats'] == [] - - @freeze_time('2017-11-11 02:00') def test_get_template_usage_by_month_returns_two_templates(admin_request, sample_template, sample_service): template_one = create_template(