mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Check for services sending sms messages to tv numbers
This commit is contained in:
@@ -35,6 +35,7 @@ from app.dao.notifications_dao import (
|
||||
)
|
||||
from app.dao.provider_details_dao import dao_reduce_sms_provider_priority
|
||||
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
|
||||
from app.dao.services_dao import dao_find_services_sending_to_tv_numbers
|
||||
from app.models import (
|
||||
Job,
|
||||
JOB_STATUS_IN_PROGRESS,
|
||||
@@ -260,16 +261,33 @@ def check_for_missing_rows_in_completed_jobs():
|
||||
@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)
|
||||
start_date = (datetime.utcnow() - timedelta(days=1))
|
||||
end_date = datetime.utcnow()
|
||||
message = ""
|
||||
|
||||
services_with_failures = get_services_with_high_failure_rates(start_date=start_date, end_date=end_date)
|
||||
services_sending_to_tv_numbers = dao_find_services_sending_to_tv_numbers(
|
||||
threshold=100,
|
||||
start_date=start_date,
|
||||
end_date=end_date
|
||||
)
|
||||
|
||||
if services_with_failures:
|
||||
message = "{} service(s) have had high permanent-failure rates for sms messages in last 24 hours: ".format(
|
||||
message += "{} service(s) have had high permanent-failure rates for sms messages in last 24 hours:\n".format(
|
||||
len(services_with_failures)
|
||||
)
|
||||
for service in services_with_failures:
|
||||
message += "service id: {} failure rate: {}, ".format(service["id"], service["permanent_failure_rate"])
|
||||
message += "service id: {} failure rate: {},\n".format(service["id"], service["permanent_failure_rate"])
|
||||
elif services_sending_to_tv_numbers:
|
||||
message += "{} service(s) have sent over 100 sms messages to tv numbers in last 24 hours:\n".format(
|
||||
len(services_sending_to_tv_numbers)
|
||||
)
|
||||
for service in services_sending_to_tv_numbers:
|
||||
message += "service id: {}, count of sms to tv numbers: {},\n".format(
|
||||
service.service_id, service.notification_count
|
||||
)
|
||||
|
||||
if services_with_failures or services_sending_to_tv_numbers:
|
||||
current_app.logger.exception(message)
|
||||
|
||||
if current_app.config['NOTIFY_ENVIRONMENT'] in ['live', 'production', 'test']:
|
||||
|
||||
@@ -521,3 +521,27 @@ def dao_fetch_active_users_for_service(service_id):
|
||||
)
|
||||
|
||||
return query.all()
|
||||
|
||||
|
||||
def dao_find_services_sending_to_tv_numbers(start_date, end_date, threshold=100):
|
||||
|
||||
return db.session.query(
|
||||
Service.name.label('service_name'),
|
||||
Notification.service_id.label('service_id'),
|
||||
func.count(Notification.id).label('notification_count')
|
||||
).filter(
|
||||
Notification.service_id == Service.id,
|
||||
Notification.created_at >= start_date,
|
||||
Notification.created_at <= end_date,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.notification_type == SMS_TYPE,
|
||||
func.substr(Notification.normalised_to, 3, 7) == '7700900',
|
||||
Service.restricted == False, # noqa
|
||||
Service.research_mode == False,
|
||||
Service.active == True,
|
||||
).group_by(
|
||||
Notification.service_id,
|
||||
Service.name
|
||||
).having(
|
||||
func.count(Notification.id) > threshold
|
||||
).all()
|
||||
|
||||
@@ -8,7 +8,6 @@ from app.models import (
|
||||
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
|
||||
|
||||
@@ -59,13 +58,10 @@ def service_allowed_to_send_to(recipient, service, key_type, allow_whitelisted_r
|
||||
)
|
||||
|
||||
|
||||
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()
|
||||
|
||||
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,
|
||||
end_date=end_date,
|
||||
start_date=start_date.date(),
|
||||
end_date=end_date.date(),
|
||||
include_from_test_key=False,
|
||||
)
|
||||
results = []
|
||||
|
||||
Reference in New Issue
Block a user