From 53b6cdcfabaa7ffaf4c3ff0f4c0624da81d3d379 Mon Sep 17 00:00:00 2001 From: Imdad Ahad Date: Mon, 30 Jan 2017 18:24:18 +0000 Subject: [PATCH] Only send stats from celery task if active + refactor tests --- app/celery/scheduled_tasks.py | 38 ++++++++++-------- tests/app/celery/test_scheduled_tasks.py | 49 +++++++++++++++++++----- 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/app/celery/scheduled_tasks.py b/app/celery/scheduled_tasks.py index cfed62ea5..921f12fdc 100644 --- a/app/celery/scheduled_tasks.py +++ b/app/celery/scheduled_tasks.py @@ -116,20 +116,28 @@ def timeout_notifications(): @notify_celery.task(name='send-daily-performance-platform-stats') @statsd(namespace="tasks") -def send_daily_performance_stats(): - count_dict = performance_platform_client.get_total_sent_notifications_yesterday() - start_date = count_dict.get('start_date') +def send_daily_performance_platform_stats(): + if performance_platform_client.active: + count_dict = performance_platform_client.get_total_sent_notifications_yesterday() + email_sent_count = count_dict.get('email').get('count') + sms_sent_count = count_dict.get('sms').get('count') + start_date = count_dict.get('start_date') - performance_platform_client.send_performance_stats( - start_date, - 'sms', - count_dict.get('sms').get('count'), - 'day' - ) + current_app.logger.info( + "Attempting to update performance platform for date {} with email count {} and sms count {}" + .format(start_date, email_sent_count, sms_sent_count) + ) - performance_platform_client.send_performance_stats( - start_date, - 'email', - count_dict.get('email').get('count'), - 'day' - ) + performance_platform_client.send_performance_stats( + start_date, + 'sms', + sms_sent_count, + 'day' + ) + + performance_platform_client.send_performance_stats( + start_date, + 'email', + email_sent_count, + 'day' + ) diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index efc8ed715..4f7b44288 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -13,8 +13,9 @@ from app.celery.scheduled_tasks import ( delete_invitations, timeout_notifications, run_scheduled_jobs, - send_daily_performance_stats + send_daily_performance_platform_stats ) +from app.clients.performance_platform.performance_platform_client import PerformancePlatformClient from app.dao.jobs_dao import dao_get_job_by_id from app.utils import get_london_midnight_in_utc from tests.app.conftest import ( @@ -22,7 +23,7 @@ from tests.app.conftest import ( sample_job as create_sample_job, sample_notification_history as create_notification_history ) -from unittest.mock import call +from unittest.mock import call, patch, PropertyMock def test_should_have_decorated_tasks_functions(): @@ -33,7 +34,7 @@ def test_should_have_decorated_tasks_functions(): assert delete_invitations.__wrapped__.__name__ == 'delete_invitations' assert run_scheduled_jobs.__wrapped__.__name__ == 'run_scheduled_jobs' assert remove_csv_files.__wrapped__.__name__ == 'remove_csv_files' - assert send_daily_performance_stats.__wrapped__.__name__ == 'send_daily_performance_stats' + assert send_daily_performance_platform_stats.__wrapped__.__name__ == 'send_daily_performance_platform_stats' def test_should_call_delete_successful_notifications_more_than_week_in_task(notify_api, mocker): @@ -183,8 +184,32 @@ def test_will_remove_csv_files_for_jobs_older_than_seven_days(notify_db, notify_ s3.remove_job_from_s3.assert_called_once_with(job_1.service_id, job_1.id) +def test_send_daily_performance_stats_calls_does_not_send_if_inactive( + notify_db, + notify_db_session, + sample_template, + mocker +): + send_mock = mocker.patch('app.celery.scheduled_tasks.performance_platform_client.send_performance_stats') + + with patch.object( + PerformancePlatformClient, + 'active', + new_callable=PropertyMock + ) as mock_active: + mock_active.return_value = False + send_daily_performance_platform_stats() + + assert send_mock.call_count == 0 + + @freeze_time("2016-01-11 12:30:00") -def test_send_daily_performance_stats_calls_with_correct_totals(notify_db, notify_db_session, sample_template, mocker): +def test_send_daily_performance_stats_calls_with_correct_totals( + notify_db, + notify_db_session, + sample_template, + mocker +): perf_mock = mocker.patch('app.celery.scheduled_tasks.performance_platform_client.send_performance_stats') notification_history = partial( @@ -207,9 +232,15 @@ def test_send_daily_performance_stats_calls_with_correct_totals(notify_db, notif notification_history(notification_type='email') notification_history(notification_type='email') - send_daily_performance_stats() + with patch.object( + PerformancePlatformClient, + 'active', + new_callable=PropertyMock + ) as mock_active: + mock_active.return_value = True + send_daily_performance_platform_stats() - perf_mock.assert_has_calls([ - call(get_london_midnight_in_utc(yesterday), 'sms', 2, 'day'), - call(get_london_midnight_in_utc(yesterday), 'email', 3, 'day') - ]) + perf_mock.assert_has_calls([ + call(get_london_midnight_in_utc(yesterday), 'sms', 2, 'day'), + call(get_london_midnight_in_utc(yesterday), 'email', 3, 'day') + ])