Skip to content

Commit c02f2cc

Browse files
authored
Merge pull request #4736 from alphagov/adjust-high-failure-rates-task
Adjust the logic of the task to check for high failure rates
2 parents fc159b5 + 0b08e6f commit c02f2cc

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

app/celery/scheduled_tasks.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,7 @@ def check_for_services_with_high_failure_rates_or_sending_to_tv_numbers():
473473
f"for sms messages in last 24 hours:\n"
474474
)
475475
for service in services_with_failures:
476-
service_dashboard = "{}/services/{}".format(
477-
current_app.config["ADMIN_BASE_URL"],
478-
str(service.service_id),
479-
)
476+
service_dashboard = f"{current_app.config['ADMIN_BASE_URL']}/services/{service.service_id}"
480477
message += f"service: {service_dashboard} failure rate: {service.permanent_failure_rate},\n"
481478

482479
current_app.logger.warning(
@@ -486,16 +483,13 @@ def check_for_services_with_high_failure_rates_or_sending_to_tv_numbers():
486483
extra={"service_id": service.service_id, "permanent_failure_rate": service.permanent_failure_rate},
487484
)
488485

489-
elif services_sending_to_tv_numbers:
486+
if services_sending_to_tv_numbers:
490487
message += (
491488
f"{len(services_sending_to_tv_numbers)} service(s) have sent over 500 sms messages to "
492489
f"tv numbers in last 24 hours:\n"
493490
)
494491
for service in services_sending_to_tv_numbers:
495-
service_dashboard = "{}/services/{}".format(
496-
current_app.config["ADMIN_BASE_URL"],
497-
str(service.service_id),
498-
)
492+
service_dashboard = f"{current_app.config['ADMIN_BASE_URL']}/services/{service.service_id}"
499493
message += f"service: {service_dashboard} count of sms to tv numbers: {service.notification_count},\n"
500494

501495
current_app.logger.warning(

tests/app/celery/test_scheduled_tasks.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -802,26 +802,42 @@ def test_update_status_of_fully_processed_jobs(mocker, sample_email_template, mo
802802

803803

804804
@pytest.mark.parametrize(
805-
"failure_rates, sms_to_tv_numbers, expected_log, expected_message",
805+
"failure_rates, sms_to_tv_numbers, expected_logs, expected_message",
806806
[
807807
[
808-
[MockServicesWithHighFailureRate("123", 0.3)],
808+
[MockServicesWithHighFailureRate("123", 0.3), MockServicesWithHighFailureRate("456", 0.7)],
809809
[],
810-
"Service 123 has had a high permanent-failure rate (0.3) for text messages in the last 24 hours",
811-
"1 service(s) have had high permanent-failure rates for sms messages in last "
812-
"24 hours:\nservice: {}/services/{} failure rate: 0.3,\n".format(Config.ADMIN_BASE_URL, "123"),
810+
[
811+
"Service 123 has had a high permanent-failure rate (0.3) for text messages in the last 24 hours",
812+
"Service 456 has had a high permanent-failure rate (0.7) for text messages in the last 24 hours",
813+
],
814+
"2 service(s) have had high permanent-failure rates for sms messages in last 24 hours:\n"
815+
f"service: {Config.ADMIN_BASE_URL}/services/123 failure rate: 0.3,\n"
816+
f"service: {Config.ADMIN_BASE_URL}/services/456 failure rate: 0.7,\n",
813817
],
814818
[
815819
[],
816820
[MockServicesSendingToTVNumbers("123", 567)],
817-
"Service 123 has sent 567 text messages to tv numbers in the last 24 hours",
821+
["Service 123 has sent 567 text messages to tv numbers in the last 24 hours"],
822+
"1 service(s) have sent over 500 sms messages to tv numbers in last 24 hours:\n"
823+
f"service: {Config.ADMIN_BASE_URL}/services/123 count of sms to tv numbers: 567,\n",
824+
],
825+
[
826+
[MockServicesWithHighFailureRate("123", 0.3)],
827+
[MockServicesSendingToTVNumbers("456", 567)],
828+
[
829+
"Service 123 has had a high permanent-failure rate (0.3) for text messages in the last 24 hours",
830+
"Service 456 has sent 567 text messages to tv numbers in the last 24 hours",
831+
],
832+
"1 service(s) have had high permanent-failure rates for sms messages in last 24 hours:\n"
833+
f"service: {Config.ADMIN_BASE_URL}/services/123 failure rate: 0.3,\n"
818834
"1 service(s) have sent over 500 sms messages to tv numbers in last 24 hours:\n"
819-
"service: {}/services/{} count of sms to tv numbers: 567,\n".format(Config.ADMIN_BASE_URL, "123"),
835+
f"service: {Config.ADMIN_BASE_URL}/services/456 count of sms to tv numbers: 567,\n",
820836
],
821837
],
822838
)
823839
def test_check_for_services_with_high_failure_rates_or_sending_to_tv_numbers(
824-
notify_db_session, failure_rates, sms_to_tv_numbers, expected_log, expected_message, caplog, mocker
840+
notify_db_session, failure_rates, sms_to_tv_numbers, expected_logs, expected_message, caplog, mocker
825841
):
826842
mock_create_ticket = mocker.spy(NotifySupportTicket, "__init__")
827843
mock_send_ticket_to_zendesk = mocker.patch(
@@ -842,7 +858,7 @@ def test_check_for_services_with_high_failure_rates_or_sending_to_tv_numbers(
842858

843859
assert mock_failure_rates.called
844860
assert mock_sms_to_tv_numbers.called
845-
assert expected_log in caplog.messages
861+
assert set(expected_logs) == set(caplog.messages)
846862
mock_create_ticket.assert_called_with(
847863
ANY,
848864
message=expected_message + zendesk_actions,

0 commit comments

Comments
 (0)