mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Merge pull request #809 from alphagov/fix-perf-platform-stats-job
Fix perf platform stats job
This commit is contained in:
@@ -66,6 +66,7 @@ def create_app(app_name=None):
|
||||
notify_celery.init_app(application)
|
||||
encryption.init_app(application)
|
||||
redis_store.init_app(application)
|
||||
performance_platform_client.init_app(application)
|
||||
clients.init_app(sms_clients=[firetext_client, mmg_client, loadtest_client], email_clients=[aws_ses_client])
|
||||
|
||||
register_blueprint(application)
|
||||
|
||||
@@ -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'
|
||||
)
|
||||
|
||||
@@ -13,16 +13,24 @@ from app.utils import (
|
||||
|
||||
class PerformancePlatformClient:
|
||||
|
||||
@property
|
||||
def active(self):
|
||||
return self._active
|
||||
|
||||
@active.setter
|
||||
def active(self, value):
|
||||
self._active = value
|
||||
|
||||
def init_app(self, app):
|
||||
self.active = app.config.get('PERFORMANCE_PLATFORM_ENABLED')
|
||||
self._active = app.config.get('PERFORMANCE_PLATFORM_ENABLED')
|
||||
if self.active:
|
||||
self.bearer_token = app.config.get('PERFORMANCE_PLATFORM_TOKEN')
|
||||
self.performance_platform_url = current_app.config.get('PERFORMANCE_PLATFORM_URL')
|
||||
self.performance_platform_url = app.config.get('PERFORMANCE_PLATFORM_URL')
|
||||
|
||||
def send_performance_stats(self, date, channel, count, period):
|
||||
if self.active:
|
||||
payload = {
|
||||
'_timestamp': date,
|
||||
'_timestamp': str(date),
|
||||
'service': 'govuk-notify',
|
||||
'channel': channel,
|
||||
'count': count,
|
||||
@@ -61,9 +69,14 @@ class PerformancePlatformClient:
|
||||
headers=headers
|
||||
)
|
||||
|
||||
if resp.status_code != 200:
|
||||
if resp.status_code == 200:
|
||||
current_app.logger.info(
|
||||
"Updated performance platform successfully with payload {}".format(json.dumps(payload))
|
||||
)
|
||||
else:
|
||||
current_app.logger.error(
|
||||
"Performance platform update request failed with {} '{}'".format(
|
||||
"Performance platform update request failed for payload with response details: {} '{}'".format(
|
||||
json.dumps(payload),
|
||||
resp.status_code,
|
||||
resp.json())
|
||||
)
|
||||
|
||||
@@ -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')
|
||||
])
|
||||
|
||||
@@ -17,7 +17,7 @@ def client(mocker):
|
||||
client = PerformancePlatformClient()
|
||||
current_app = mocker.Mock(config={
|
||||
'PERFORMANCE_PLATFORM_ENABLED': True,
|
||||
'PERFORMANCE_PLATFORM_URL': 'performance-platform-url',
|
||||
'PERFORMANCE_PLATFORM_URL': 'https://performance-platform-url/',
|
||||
'PERFORMANCE_PLATFORM_TOKEN': 'token'
|
||||
})
|
||||
client.init_app(current_app)
|
||||
|
||||
Reference in New Issue
Block a user