mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-18 05:21:51 -05:00
Send results of processing-time query to performance platform
This commit is contained in:
@@ -8,7 +8,7 @@ from sqlalchemy.exc import SQLAlchemyError
|
|||||||
|
|
||||||
from app.aws import s3
|
from app.aws import s3
|
||||||
from app import notify_celery
|
from app import notify_celery
|
||||||
from app.performance_platform import total_sent_notifications
|
from app.performance_platform import total_sent_notifications, processing_time
|
||||||
from app import performance_platform_client
|
from app import performance_platform_client
|
||||||
from app.dao.date_util import get_month_start_and_end_date_in_utc
|
from app.dao.date_util import get_month_start_and_end_date_in_utc
|
||||||
from app.dao.inbound_sms_dao import delete_inbound_sms_created_more_than_a_week_ago
|
from app.dao.inbound_sms_dao import delete_inbound_sms_created_more_than_a_week_ago
|
||||||
@@ -176,27 +176,32 @@ def timeout_notifications():
|
|||||||
@statsd(namespace="tasks")
|
@statsd(namespace="tasks")
|
||||||
def send_daily_performance_platform_stats():
|
def send_daily_performance_platform_stats():
|
||||||
if performance_platform_client.active:
|
if performance_platform_client.active:
|
||||||
count_dict = total_sent_notifications.get_total_sent_notifications_yesterday()
|
send_total_sent_notifications_to_performance_platform()
|
||||||
email_sent_count = count_dict.get('email').get('count')
|
processing_time.send_processing_time_to_performance_platform()
|
||||||
sms_sent_count = count_dict.get('sms').get('count')
|
|
||||||
start_date = count_dict.get('start_date')
|
|
||||||
|
|
||||||
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
def send_total_sent_notifications_to_performance_platform():
|
||||||
start_date,
|
count_dict = total_sent_notifications.get_total_sent_notifications_yesterday()
|
||||||
'sms',
|
email_sent_count = count_dict.get('email').get('count')
|
||||||
sms_sent_count
|
sms_sent_count = count_dict.get('sms').get('count')
|
||||||
)
|
start_date = count_dict.get('start_date')
|
||||||
|
|
||||||
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
current_app.logger.info(
|
||||||
start_date,
|
"Attempting to update performance platform for date {} with email count {} and sms count {}"
|
||||||
'email',
|
.format(start_date, email_sent_count, sms_sent_count)
|
||||||
email_sent_count
|
)
|
||||||
)
|
|
||||||
|
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
||||||
|
start_date,
|
||||||
|
'sms',
|
||||||
|
sms_sent_count
|
||||||
|
)
|
||||||
|
|
||||||
|
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
||||||
|
start_date,
|
||||||
|
'email',
|
||||||
|
email_sent_count
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@notify_celery.task(name='switch-current-sms-provider-on-slow-delivery')
|
@notify_celery.task(name='switch-current-sms-provider-on-slow-delivery')
|
||||||
|
|||||||
36
app/performance_platform/processing_time.py
Normal file
36
app/performance_platform/processing_time.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from flask import current_app
|
||||||
|
|
||||||
|
from app.utils import get_midnight_for_day_before, get_london_midnight_in_utc
|
||||||
|
from app.dao.notifications_dao import dao_get_total_notifications_sent_per_day_for_perfomance_platform
|
||||||
|
from app import performance_platform_client
|
||||||
|
|
||||||
|
|
||||||
|
def send_processing_time_to_performance_platform():
|
||||||
|
today = datetime.utcnow()
|
||||||
|
start_date = get_midnight_for_day_before(today)
|
||||||
|
end_date = get_london_midnight_in_utc(today)
|
||||||
|
|
||||||
|
result = dao_get_total_notifications_sent_per_day_for_perfomance_platform(start_date, end_date)
|
||||||
|
|
||||||
|
current_app.logger.info(
|
||||||
|
'Sending processing-time to performance platform for date {}. Total: {}, under 10 secs {}'.format(
|
||||||
|
start_date, result.messages_total, result.messages_within_10_secs
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
send_processing_time_data(start_date, 'messages-total', result.messages_total)
|
||||||
|
send_processing_time_data(start_date, 'messages-within-10-secs', result.messages_within_10_secs)
|
||||||
|
|
||||||
|
|
||||||
|
def send_processing_time_data(date, status, count):
|
||||||
|
payload = performance_platform_client.format_payload(
|
||||||
|
dataset='processing-time',
|
||||||
|
date=date,
|
||||||
|
group_name='status',
|
||||||
|
group_value=status,
|
||||||
|
count=count
|
||||||
|
)
|
||||||
|
|
||||||
|
performance_platform_client.send_stats_to_performance_platform(payload)
|
||||||
@@ -29,7 +29,8 @@ from app.celery.scheduled_tasks import (
|
|||||||
switch_current_sms_provider_on_slow_delivery,
|
switch_current_sms_provider_on_slow_delivery,
|
||||||
timeout_job_statistics,
|
timeout_job_statistics,
|
||||||
timeout_notifications,
|
timeout_notifications,
|
||||||
populate_monthly_billing)
|
populate_monthly_billing,
|
||||||
|
send_total_sent_notifications_to_performance_platform)
|
||||||
from app.clients.performance_platform.performance_platform_client import PerformancePlatformClient
|
from app.clients.performance_platform.performance_platform_client import PerformancePlatformClient
|
||||||
from app.config import QueueNames, TaskNames
|
from app.config import QueueNames, TaskNames
|
||||||
from app.dao.jobs_dao import dao_get_job_by_id
|
from app.dao.jobs_dao import dao_get_job_by_id
|
||||||
@@ -285,7 +286,7 @@ def test_send_daily_performance_stats_calls_does_not_send_if_inactive(client, mo
|
|||||||
|
|
||||||
|
|
||||||
@freeze_time("2016-01-11 12:30:00")
|
@freeze_time("2016-01-11 12:30:00")
|
||||||
def test_send_daily_performance_stats_calls_with_correct_totals(
|
def test_send_total_sent_notifications_to_performance_platform_calls_with_correct_totals(
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
sample_template,
|
sample_template,
|
||||||
@@ -319,7 +320,7 @@ def test_send_daily_performance_stats_calls_with_correct_totals(
|
|||||||
new_callable=PropertyMock
|
new_callable=PropertyMock
|
||||||
) as mock_active:
|
) as mock_active:
|
||||||
mock_active.return_value = True
|
mock_active.return_value = True
|
||||||
send_daily_performance_platform_stats()
|
send_total_sent_notifications_to_performance_platform()
|
||||||
|
|
||||||
perf_mock.assert_has_calls([
|
perf_mock.assert_has_calls([
|
||||||
call(get_london_midnight_in_utc(yesterday), 'sms', 2),
|
call(get_london_midnight_in_utc(yesterday), 'sms', 2),
|
||||||
|
|||||||
47
tests/app/performance_platform/test_processing_time.py
Normal file
47
tests/app/performance_platform/test_processing_time.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
from freezegun import freeze_time
|
||||||
|
|
||||||
|
from tests.app.db import create_notification
|
||||||
|
from app.performance_platform.processing_time import (
|
||||||
|
send_processing_time_to_performance_platform,
|
||||||
|
send_processing_time_data
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time('2016-10-18T02:00')
|
||||||
|
def test_send_processing_time_to_performance_platform_generates_correct_calls(mocker, sample_template):
|
||||||
|
send_mock = mocker.patch('app.performance_platform.processing_time.send_processing_time_data')
|
||||||
|
|
||||||
|
created_at = datetime.utcnow() - timedelta(days=1)
|
||||||
|
|
||||||
|
create_notification(sample_template, created_at=created_at, sent_at=created_at + timedelta(seconds=5))
|
||||||
|
create_notification(sample_template, created_at=created_at, sent_at=created_at + timedelta(seconds=15))
|
||||||
|
create_notification(sample_template, created_at=datetime.utcnow() - timedelta(days=2))
|
||||||
|
|
||||||
|
send_processing_time_to_performance_platform()
|
||||||
|
|
||||||
|
send_mock.assert_any_call(datetime(2016, 10, 16, 23, 0), 'messages-total', 2)
|
||||||
|
send_mock.assert_any_call(datetime(2016, 10, 16, 23, 0), 'messages-within-10-secs', 1)
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_processing_time_to_performance_platform_creates_correct_call_to_perf_platform(mocker):
|
||||||
|
send_stats = mocker.patch('app.performance_platform.total_sent_notifications.performance_platform_client.send_stats_to_performance_platform') # noqa
|
||||||
|
|
||||||
|
send_processing_time_data(
|
||||||
|
date=datetime(2016, 10, 15, 23, 0, 0),
|
||||||
|
status='foo',
|
||||||
|
count=142
|
||||||
|
)
|
||||||
|
|
||||||
|
assert send_stats.call_count == 1
|
||||||
|
|
||||||
|
request_args = send_stats.call_args[0][0]
|
||||||
|
assert request_args['dataType'] == 'processing-time'
|
||||||
|
assert request_args['service'] == 'govuk-notify'
|
||||||
|
assert request_args['period'] == 'day'
|
||||||
|
assert request_args['status'] == 'foo'
|
||||||
|
assert request_args['_timestamp'] == '2016-10-16T00:00:00'
|
||||||
|
assert request_args['count'] == 142
|
||||||
|
expected_base64_id = 'MjAxNi0xMC0xNlQwMDowMDowMGdvdnVrLW5vdGlmeWZvb3Byb2Nlc3NpbmctdGltZWRheQ=='
|
||||||
|
assert request_args['_id'] == expected_base64_id
|
||||||
Reference in New Issue
Block a user