Merge pull request #1222 from alphagov/get-performance-stats

Get performance stats
This commit is contained in:
Imdad Ahad
2017-08-31 14:17:42 +01:00
committed by GitHub
10 changed files with 308 additions and 29 deletions

View File

@@ -29,7 +29,8 @@ from app.celery.scheduled_tasks import (
switch_current_sms_provider_on_slow_delivery,
timeout_job_statistics,
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.config import QueueNames, TaskNames
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")
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_session,
sample_template,
@@ -319,7 +320,7 @@ def test_send_daily_performance_stats_calls_with_correct_totals(
new_callable=PropertyMock
) as mock_active:
mock_active.return_value = True
send_daily_performance_platform_stats()
send_total_sent_notifications_to_performance_platform()
perf_mock.assert_has_calls([
call(get_london_midnight_in_utc(yesterday), 'sms', 2),

View File

@@ -136,24 +136,30 @@ def sample_service(
restricted=False,
limit=1000,
email_from=None,
permissions=[SMS_TYPE, EMAIL_TYPE]
permissions=[SMS_TYPE, EMAIL_TYPE],
research_mode=None
):
if user is None:
user = create_user()
if email_from is None:
email_from = service_name.lower().replace(' ', '.')
data = {
'name': service_name,
'message_limit': limit,
'restricted': restricted,
'email_from': email_from,
'created_by': user,
'letter_contact_block': 'London,\nSW1A 1AA',
'letter_contact_block': 'London,\nSW1A 1AA'
}
service = Service.query.filter_by(name=service_name).first()
if not service:
service = Service(**data)
dao_create_service(service, user, service_permissions=permissions)
if research_mode:
service.research_mode = research_mode
else:
if user not in service.users:
dao_add_user_to_service(service, user)
@@ -206,6 +212,7 @@ def sample_template(
})
template = Template(**data)
dao_create_template(template)
return template
@@ -631,14 +638,22 @@ def sample_notification_history(
status='created',
created_at=None,
notification_type=None,
key_type=KEY_TYPE_NORMAL
key_type=KEY_TYPE_NORMAL,
sent_at=None,
api_key=None
):
if created_at is None:
created_at = datetime.utcnow()
if sent_at is None:
sent_at = datetime.utcnow()
if notification_type is None:
notification_type = sample_template.template_type
if not api_key:
api_key = create_api_key(sample_template.service, key_type=key_type)
notification_history = NotificationHistory(
id=uuid.uuid4(),
service=sample_template.service,
@@ -647,7 +662,10 @@ def sample_notification_history(
status=status,
created_at=created_at,
notification_type=notification_type,
key_type=key_type
key_type=key_type,
api_key=api_key,
api_key_id=api_key and api_key.id,
sent_at=sent_at
)
notify_db.session.add(notification_history)
notify_db.session.commit()

View File

@@ -0,0 +1,135 @@
from datetime import date, datetime, timedelta
from freezegun import freeze_time
from app.dao.notifications_dao import dao_get_total_notifications_sent_per_day_for_performance_platform
from app.models import KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST
from tests.app.db import create_notification
from tests.app.conftest import (
sample_notification_history,
sample_service,
sample_template
)
BEGINNING_OF_DAY = date(2016, 10, 18)
END_OF_DAY = date(2016, 10, 19)
def test_get_total_notifications_filters_on_date_within_date_range(sample_template):
create_notification(sample_template, created_at=datetime(2016, 10, 17, 23, 59, 59))
create_notification(sample_template, created_at=BEGINNING_OF_DAY)
create_notification(sample_template, created_at=datetime(2016, 10, 18, 23, 59, 59))
create_notification(sample_template, created_at=END_OF_DAY)
result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY)
assert result.messages_total == 2
@freeze_time('2016-10-18T10:00')
def test_get_total_notifications_only_counts_api_notifications(sample_template, sample_job, sample_api_key):
create_notification(sample_template, one_off=True)
create_notification(sample_template, one_off=True)
create_notification(sample_template, job=sample_job)
create_notification(sample_template, job=sample_job)
create_notification(sample_template, api_key=sample_api_key)
result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY)
assert result.messages_total == 1
@freeze_time('2016-10-18T10:00')
def test_get_total_notifications_ignores_test_keys(sample_template):
# Creating multiple templates with normal and team keys but only 1 template
# with a test key to test that the count ignores letters
create_notification(sample_template, key_type=KEY_TYPE_NORMAL)
create_notification(sample_template, key_type=KEY_TYPE_NORMAL)
create_notification(sample_template, key_type=KEY_TYPE_TEAM)
create_notification(sample_template, key_type=KEY_TYPE_TEAM)
create_notification(sample_template, key_type=KEY_TYPE_TEST)
result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY)
assert result.messages_total == 4
@freeze_time('2016-10-18T10:00')
def test_get_total_notifications_ignores_letters(
sample_template,
sample_email_template,
sample_letter_template
):
# Creating multiple sms and email templates but only 1 letter template to
# test that the count ignores letters
create_notification(sample_template)
create_notification(sample_template)
create_notification(sample_email_template)
create_notification(sample_email_template)
create_notification(sample_letter_template)
result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY)
assert result.messages_total == 4
@freeze_time('2016-10-18T10:00')
def test_get_total_notifications_counts_messages_within_10_seconds(sample_template):
created_at = datetime.utcnow()
create_notification(sample_template, sent_at=created_at + timedelta(seconds=5))
create_notification(sample_template, sent_at=created_at + timedelta(seconds=10))
create_notification(sample_template, sent_at=created_at + timedelta(seconds=15))
result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY)
assert result.messages_total == 3
assert result.messages_within_10_secs == 2
@freeze_time('2016-10-18T10:00')
def test_get_total_notifications_counts_messages_that_have_not_sent(sample_template):
create_notification(sample_template, status='created', sent_at=None)
result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY)
assert result.messages_total == 1
assert result.messages_within_10_secs == 0
@freeze_time('2016-10-18T10:00')
def test_get_total_notifications_returns_zero_if_no_data(notify_db_session):
result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY)
assert result.messages_total == 0
assert result.messages_within_10_secs == 0
@freeze_time('2016-10-18T10:00')
def test_get_total_notifications_counts_ignores_research_mode(notify_db, notify_db_session):
created_at = datetime.utcnow()
service = sample_service(notify_db, notify_db_session, research_mode=True)
template = sample_template(notify_db, notify_db_session, service=service)
create_notification(template, status='created', sent_at=None)
sample_notification_history(
notify_db,
notify_db_session,
template,
notification_type='email',
sent_at=created_at + timedelta(seconds=5)
)
sample_notification_history(
notify_db,
notify_db_session,
template,
notification_type='sms',
sent_at=created_at + timedelta(seconds=5)
)
result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY)
assert result.messages_total == 2
assert result.messages_within_10_secs == 2

View File

@@ -123,7 +123,8 @@ def create_notification(
international=False,
phone_prefix=None,
scheduled_for=None,
normalised_to=None
normalised_to=None,
one_off=False,
):
if created_at is None:
created_at = datetime.utcnow()
@@ -132,7 +133,7 @@ def create_notification(
sent_at = sent_at or datetime.utcnow()
updated_at = updated_at or datetime.utcnow()
if job is None and api_key is None:
if not one_off and (job is None and api_key is None):
# we didn't specify in test - lets create it
api_key = ApiKey.query.filter(ApiKey.service == template.service, ApiKey.key_type == key_type).first()
if not api_key:

View 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