Query directly for services with high failure rate

This commit is contained in:
Pea Tyczynska
2019-12-05 16:07:06 +00:00
parent b8de67ae54
commit 1b7b26bf24
8 changed files with 116 additions and 204 deletions

View File

@@ -11,8 +11,7 @@ from app.service.statistics import (
create_stats_dict,
create_zeroed_stats_dicts,
format_admin_stats,
format_statistics,
get_rate_of_permanent_failures_for_service
format_statistics
)
StatsRow = collections.namedtuple('row', ('notification_type', 'status', 'count'))
@@ -74,26 +73,6 @@ 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),
'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,8 +1,5 @@
from app.dao.date_util import get_current_financial_year_start_year
from freezegun import freeze_time
from app.service.utils import get_services_with_high_failure_rates
from collections import namedtuple
from datetime import datetime, timedelta
# see get_financial_year for conversion of financial years.
@@ -16,37 +13,3 @@ 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
MockServicesNotificationCounts = namedtuple(
'ServicesSendingToTVNumbers',
[
'service_id',
'status',
'count',
]
)
@freeze_time("2019-12-02 12:00:00.000000")
def test_get_services_with_high_failure_rates(mocker, notify_db_session):
mock_query_results = [
MockServicesNotificationCounts('123', 'delivered', 150),
MockServicesNotificationCounts('123', 'permanent-failure', 50), # these will show up
MockServicesNotificationCounts('456', 'delivered', 150),
MockServicesNotificationCounts('456', 'permanent-failure', 5), # ratio too low
MockServicesNotificationCounts('789', 'permanent-failure', 5), # below threshold
MockServicesNotificationCounts('444', 'delivered', 100),
MockServicesNotificationCounts('444', 'permanent-failure', 100), # these will show up
]
mocker.patch(
'app.service.utils.dao_find_real_sms_notification_count_by_status_for_live_services',
return_value=mock_query_results
)
start_date = (datetime.utcnow() - timedelta(days=1))
end_date = datetime.utcnow()
assert get_services_with_high_failure_rates(start_date, end_date) == [
{'id': '123', 'permanent_failure_rate': 0.25},
{'id': '444', 'permanent_failure_rate': 0.5}
]