mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-11 09:27:56 -04:00
Send zendesk ticket when services found with high failure rates
This commit is contained in:
@@ -45,6 +45,8 @@ from app.models import (
|
||||
from app.notifications.process_notifications import send_notification_to_queue
|
||||
from app.v2.errors import JobIncompleteError
|
||||
|
||||
from app.service.utils import get_services_with_high_failure_rates
|
||||
|
||||
|
||||
@notify_celery.task(name="run-scheduled-jobs")
|
||||
@statsd(namespace="tasks")
|
||||
@@ -253,3 +255,28 @@ def check_for_missing_rows_in_completed_jobs():
|
||||
current_app.logger.info(
|
||||
"Processing missing row: {} for job: {}".format(row_to_process.missing_row, job.id))
|
||||
process_row(row, template, job, job.service, sender_id=sender_id)
|
||||
|
||||
|
||||
@notify_celery.task(name='check-for-services-with-high-failure-rates-or-sending-to-tv-numbers')
|
||||
@statsd(namespace="tasks")
|
||||
def check_for_services_with_high_failure_rates_or_sending_to_tv_numbers():
|
||||
services_with_failures = get_services_with_high_failure_rates()
|
||||
# services_sending_to_tv_numbers = dao_find_services_sending_to_tv_numbers(number=100)
|
||||
|
||||
if services_with_failures:
|
||||
message = "{} service(s) have had high permanent-failure rates for sms messages in last 24 hours: ".format(
|
||||
len(services_with_failures)
|
||||
)
|
||||
for service in services_with_failures:
|
||||
message += "service id: {} failure rate: {}, ".format(service["id"], service["permanent_failure_rate"])
|
||||
|
||||
current_app.logger.exception(message)
|
||||
|
||||
if current_app.config['NOTIFY_ENVIRONMENT'] in ['live', 'production', 'test']:
|
||||
zendesk_client.create_ticket(
|
||||
subject="[{}] High failure rates for sms spotted for services".format(
|
||||
current_app.config['NOTIFY_ENVIRONMENT']
|
||||
),
|
||||
message=message,
|
||||
ticket_type=zendesk_client.TYPE_INCIDENT
|
||||
)
|
||||
|
||||
@@ -276,6 +276,11 @@ class Config(object):
|
||||
'schedule': crontab(day_of_week='mon-fri', hour='9,15', minute=0),
|
||||
'options': {'queue': QueueNames.PERIODIC}
|
||||
},
|
||||
'check-for-services-with-high-failure-rates-or-sending-to-tv-numbers': {
|
||||
'task': 'check-for-services-with-high-failure-rates-or-sending-to-tv-numbers',
|
||||
'schedule': crontab(day_of_week='mon-fri', hour=10, minute=30),
|
||||
'options': {'queue': QueueNames.PERIODIC}
|
||||
},
|
||||
'raise-alert-if-letter-notifications-still-sending': {
|
||||
'task': 'raise-alert-if-letter-notifications-still-sending',
|
||||
'schedule': crontab(hour=16, minute=30),
|
||||
|
||||
@@ -21,6 +21,19 @@ def format_statistics(statistics):
|
||||
return counts
|
||||
|
||||
|
||||
def get_rate_of_permanent_failures_for_service(statistics, threshold=100):
|
||||
counts = {"permanent_failure": 0, "all_other_statuses": 0}
|
||||
for row in statistics:
|
||||
if row.notification_type == 'sms':
|
||||
_count_if_status_is_permanent_failure_from_row(counts, row)
|
||||
|
||||
if counts['permanent_failure'] + counts['all_other_statuses'] >= threshold:
|
||||
rate = counts['permanent_failure'] / (counts['permanent_failure'] + counts['all_other_statuses'])
|
||||
else:
|
||||
rate = 0
|
||||
return rate
|
||||
|
||||
|
||||
def format_admin_stats(statistics):
|
||||
counts = create_stats_dict()
|
||||
|
||||
@@ -94,6 +107,13 @@ def _update_statuses_from_row(update_dict, row):
|
||||
update_dict['failed'] += row.count
|
||||
|
||||
|
||||
def _count_if_status_is_permanent_failure_from_row(update_dict, row):
|
||||
if row.status == 'permanent-failure':
|
||||
update_dict['permanent_failure'] += row.count
|
||||
else:
|
||||
update_dict['all_other_statuses'] += row.count
|
||||
|
||||
|
||||
def create_empty_monthly_notification_status_stats_dict(year):
|
||||
utc_month_starts = get_months_for_financial_year(year)
|
||||
# nested dicts - data[month][template type][status] = count
|
||||
|
||||
@@ -7,6 +7,11 @@ from app.models import (
|
||||
MOBILE_TYPE, EMAIL_TYPE,
|
||||
KEY_TYPE_TEST, KEY_TYPE_TEAM, KEY_TYPE_NORMAL)
|
||||
|
||||
from app.service import statistics
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
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)]
|
||||
@@ -52,3 +57,26 @@ def service_allowed_to_send_to(recipient, service, key_type, allow_whitelisted_r
|
||||
whitelist_members
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def get_services_with_high_failure_rates(rate=0.25, threshold=100):
|
||||
start_date = (datetime.utcnow() - timedelta(days=1)).date()
|
||||
end_date = datetime.utcnow().date()
|
||||
|
||||
stats = fetch_stats_for_all_services_by_date_range(
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
include_from_test_key=False,
|
||||
)
|
||||
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
|
||||
})
|
||||
return results
|
||||
|
||||
Reference in New Issue
Block a user