mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 16:31:15 -05:00
Simplify failure rate by building separate query
This commit is contained in:
@@ -2,6 +2,8 @@ import itertools
|
||||
|
||||
from notifications_utils.recipients import allowed_to_send_to
|
||||
|
||||
from app.dao.services_dao import dao_find_real_sms_notification_count_by_status_for_live_services
|
||||
|
||||
from app.models import (
|
||||
ServiceWhitelist,
|
||||
MOBILE_TYPE, EMAIL_TYPE,
|
||||
@@ -9,8 +11,6 @@ from app.models import (
|
||||
|
||||
from app.service import statistics
|
||||
|
||||
from app.dao.fact_notification_status_dao import fetch_stats_for_all_services_by_date_range
|
||||
|
||||
|
||||
def get_recipients_from_request(request_json, key, type):
|
||||
return [(type, recipient) for recipient in request_json.get(key)]
|
||||
@@ -59,20 +59,18 @@ def service_allowed_to_send_to(recipient, service, key_type, allow_whitelisted_r
|
||||
|
||||
|
||||
def get_services_with_high_failure_rates(start_date, end_date, rate=0.25, threshold=100):
|
||||
stats = fetch_stats_for_all_services_by_date_range(
|
||||
start_date=start_date.date(),
|
||||
end_date=end_date.date(),
|
||||
include_from_test_key=False,
|
||||
stats = dao_find_real_sms_notification_count_by_status_for_live_services(
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
)
|
||||
results = []
|
||||
for service_id, rows in itertools.groupby(stats, lambda x: x.service_id):
|
||||
rows = list(rows)
|
||||
if not rows[0].restricted and not rows[0].research_mode and rows[0].active:
|
||||
permanent_failure_rate = statistics.get_rate_of_permanent_failures_for_service(rows, threshold=threshold)
|
||||
if permanent_failure_rate >= rate:
|
||||
results.append({
|
||||
'id': str(rows[0].service_id),
|
||||
'name': rows[0].name,
|
||||
'permanent_failure_rate': permanent_failure_rate
|
||||
})
|
||||
|
||||
permanent_failure_rate = statistics.get_rate_of_permanent_failures_for_service(rows, threshold=threshold)
|
||||
if permanent_failure_rate >= rate:
|
||||
results.append({
|
||||
'id': str(rows[0].service_id),
|
||||
'permanent_failure_rate': permanent_failure_rate
|
||||
})
|
||||
return results
|
||||
|
||||
Reference in New Issue
Block a user