Send zendesk ticket when services found with high failure rates

This commit is contained in:
Pea Tyczynska
2019-11-29 21:24:17 +00:00
parent b6320182be
commit d72ab4f4a6
7 changed files with 171 additions and 4 deletions

View File

@@ -16,6 +16,7 @@ from app.celery.scheduled_tasks import (
check_precompiled_letter_state,
check_templated_letter_state,
check_for_missing_rows_in_completed_jobs,
check_for_services_with_high_failure_rates_or_sending_to_tv_numbers,
switch_current_sms_provider_on_slow_delivery,
)
from app.config import QueueNames, TaskNames
@@ -494,3 +495,30 @@ def test_check_for_missing_rows_in_completed_jobs_uses_sender_id(mocker, sample_
mock_process_row.assert_called_once_with(
mock.ANY, mock.ANY, job, job.service, sender_id=fake_uuid
)
@pytest.mark.parametrize("failure_rates, expected_message", [
[
[{"id": "123", "name": "Service 1", "permanent_failure_rate": 0.3}],
"1 service(s) have had high permanent-failure rates for sms messages in last "
"24 hours: service id: 123 failure rate: 0.3, "
]
])
def test_check_for_services_with_high_failure_rates_or_sending_to_tv_numbers(
mocker, notify_db_session, failure_rates, expected_message
):
mock_logger = mocker.patch('app.celery.tasks.current_app.logger.exception')
mock_create_ticket = mocker.patch('app.celery.scheduled_tasks.zendesk_client.create_ticket')
mock_failure_rates = mocker.patch(
'app.celery.scheduled_tasks.get_services_with_high_failure_rates', return_value=failure_rates
)
check_for_services_with_high_failure_rates_or_sending_to_tv_numbers()
assert mock_failure_rates.called
mock_logger.assert_called_once_with(expected_message)
mock_create_ticket.assert_called_with(
message=expected_message,
subject="[test] High failure rates for sms spotted for services",
ticket_type='incident'
)

View File

@@ -6,12 +6,13 @@ from freezegun import freeze_time
import pytest
from app.service.statistics import (
format_admin_stats,
format_statistics,
add_monthly_notification_status_stats,
create_empty_monthly_notification_status_stats_dict,
create_stats_dict,
create_zeroed_stats_dicts,
create_empty_monthly_notification_status_stats_dict,
add_monthly_notification_status_stats
format_admin_stats,
format_statistics,
get_rate_of_permanent_failures_for_service
)
StatsRow = collections.namedtuple('row', ('notification_type', 'status', 'count'))
@@ -73,6 +74,32 @@ def test_format_statistics(stats, email_counts, sms_counts, letter_counts):
}
@pytest.mark.idparametrize("statistics, expected_result", {
'counts_rate_for_sms': ([
StatsRow('sms', 'permanent-failure', 100),
StatsRow('sms', 'delivered', 300),
], 0.25),
'ignores_other_notification_types': ([
StatsRow('sms', 'permanent-failure', 100),
StatsRow('sms', 'delivered', 300),
StatsRow('email', 'delivered', 300),
StatsRow('letter', 'created', 300),
], 0.25),
'only_counts_permanent_failure_as_failed': ([
StatsRow('sms', 'permanent-failure', 100),
StatsRow('sms', 'temporary-failure', 100),
StatsRow('sms', 'delivered', 300),
], 0.2),
'below_threshold': ([
StatsRow('sms', 'permanent-failure', 5),
StatsRow('sms', 'delivered', 3),
], 0),
})
def test_get_rate_of_permanent_failures_for_service(statistics, expected_result):
rate = get_rate_of_permanent_failures_for_service(statistics)
assert rate == expected_result
def test_create_zeroed_stats_dicts():
assert create_zeroed_stats_dicts() == {
'sms': {'requested': 0, 'delivered': 0, 'failed': 0},

View File

@@ -1,5 +1,7 @@
from app.dao.date_util import get_current_financial_year_start_year
from freezegun import freeze_time
from tests.app.db import create_service, create_notification, create_template
from app.service.utils import get_services_with_high_failure_rates
# see get_financial_year for conversion of financial years.
@@ -13,3 +15,33 @@ def test_get_current_financial_year_start_year_before_march():
def test_get_current_financial_year_start_year_after_april():
current_fy = get_current_financial_year_start_year()
assert current_fy == 2017
@freeze_time("2019-12-02 12:00:00.000000")
def test_get_services_with_high_failure_rates(notify_db_session):
service_1 = create_service(service_name="Service 1")
service_3 = create_service(service_name="Service 3", restricted=True) # restricted
service_4 = create_service(service_name="Service 4", research_mode=True) # research mode
service_5 = create_service(service_name="Service 5", active=False) # not active
services = [service_1, service_3, service_4, service_5]
for service in services:
template = create_template(service)
create_notification(template, status="permanent-failure")
for x in range(0, 3):
create_notification(template, status="delivered")
service_6 = create_service(service_name="Service 6") # notifications too old
with freeze_time("2019-11-30 15:00:00.000000"):
template_6 = create_template(service_6)
for x in range(0, 4):
create_notification(template_6, status="permanent-failure")
service_2 = create_service(service_name="Service 2") # below threshold
template_2 = create_template(service_2)
create_notification(template_2, status="permanent-failure")
assert get_services_with_high_failure_rates(threshold=3) == [{
'id': str(service_1.id),
'name': service_1.name,
'permanent_failure_rate': 0.25
}]