mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 06:21:50 -05:00
Merge pull request #2250 from alphagov/switch_providers_update
Update switch providers on slow delivery method and query
This commit is contained in:
@@ -37,7 +37,7 @@ from app.dao.notifications_dao import (
|
||||
)
|
||||
from app.dao.provider_details_dao import (
|
||||
get_current_provider,
|
||||
dao_toggle_sms_provider
|
||||
# dao_toggle_sms_provider
|
||||
)
|
||||
from app.dao.service_callback_api_dao import get_service_delivery_status_callback_api_for_service
|
||||
from app.dao.services_dao import (
|
||||
@@ -256,28 +256,25 @@ def switch_current_sms_provider_on_slow_delivery():
|
||||
Switch providers if there are at least two slow delivery notifications (more than four minutes)
|
||||
in the last ten minutes. Search from the time we last switched to the current provider.
|
||||
"""
|
||||
functional_test_provider_service_id = current_app.config.get('FUNCTIONAL_TEST_PROVIDER_SERVICE_ID')
|
||||
functional_test_provider_template_id = current_app.config.get('FUNCTIONAL_TEST_PROVIDER_SMS_TEMPLATE_ID')
|
||||
current_provider = get_current_provider('sms')
|
||||
if current_provider.updated_at > datetime.utcnow() - timedelta(minutes=10):
|
||||
current_app.logger.info("Slow delivery provider switched less than 10 minutes ago.")
|
||||
return
|
||||
slow_delivery_notifications = is_delivery_slow_for_provider(
|
||||
provider=current_provider.identifier,
|
||||
threshold=0.1,
|
||||
created_at=datetime.utcnow() - timedelta(minutes=10),
|
||||
delivery_time=timedelta(minutes=4),
|
||||
)
|
||||
|
||||
if functional_test_provider_service_id and functional_test_provider_template_id:
|
||||
current_provider = get_current_provider('sms')
|
||||
slow_delivery_notifications = is_delivery_slow_for_provider(
|
||||
provider=current_provider.identifier,
|
||||
threshold=2,
|
||||
sent_at=max(datetime.utcnow() - timedelta(minutes=10), current_provider.updated_at),
|
||||
delivery_time=timedelta(minutes=4),
|
||||
service_id=functional_test_provider_service_id,
|
||||
template_id=functional_test_provider_template_id
|
||||
if slow_delivery_notifications:
|
||||
current_app.logger.warning(
|
||||
'Slow delivery notifications detected for provider {}'.format(
|
||||
current_provider.identifier
|
||||
)
|
||||
)
|
||||
|
||||
if slow_delivery_notifications:
|
||||
current_app.logger.warning(
|
||||
'Slow delivery notifications detected for provider {}'.format(
|
||||
current_provider.identifier
|
||||
)
|
||||
)
|
||||
|
||||
dao_toggle_sms_provider(current_provider.identifier)
|
||||
# dao_toggle_sms_provider(current_provider.identifier)
|
||||
|
||||
|
||||
@notify_celery.task(name="delete-inbound-sms")
|
||||
|
||||
@@ -290,9 +290,6 @@ class Config(object):
|
||||
|
||||
SIMULATED_SMS_NUMBERS = ('+447700900000', '+447700900111', '+447700900222')
|
||||
|
||||
FUNCTIONAL_TEST_PROVIDER_SERVICE_ID = None
|
||||
FUNCTIONAL_TEST_PROVIDER_SMS_TEMPLATE_ID = None
|
||||
|
||||
DVLA_BUCKETS = {
|
||||
'job': '{}-dvla-file-per-job'.format(os.getenv('NOTIFY_ENVIRONMENT')),
|
||||
'notification': '{}-dvla-letter-api-files'.format(os.getenv('NOTIFY_ENVIRONMENT'))
|
||||
@@ -438,8 +435,6 @@ class Live(Config):
|
||||
INVALID_PDF_BUCKET_NAME = 'production-letters-invalid-pdf'
|
||||
STATSD_ENABLED = True
|
||||
FROM_NUMBER = 'GOVUK'
|
||||
FUNCTIONAL_TEST_PROVIDER_SERVICE_ID = '6c1d81bb-dae2-4ee9-80b0-89a4aae9f649'
|
||||
FUNCTIONAL_TEST_PROVIDER_SMS_TEMPLATE_ID = 'ba9e1789-a804-40b8-871f-cc60d4c1286f'
|
||||
PERFORMANCE_PLATFORM_ENABLED = True
|
||||
API_RATE_LIMIT_ENABLED = True
|
||||
CHECK_PROXY_HEADER = True
|
||||
|
||||
@@ -443,22 +443,34 @@ def get_total_sent_notifications_in_date_range(start_date, end_date, notificatio
|
||||
|
||||
|
||||
def is_delivery_slow_for_provider(
|
||||
sent_at,
|
||||
created_at,
|
||||
provider,
|
||||
threshold,
|
||||
delivery_time,
|
||||
service_id,
|
||||
template_id
|
||||
):
|
||||
count = db.session.query(Notification).filter(
|
||||
Notification.service_id == service_id,
|
||||
Notification.template_id == template_id,
|
||||
Notification.sent_at >= sent_at,
|
||||
Notification.status == NOTIFICATION_DELIVERED,
|
||||
count = db.session.query(
|
||||
case(
|
||||
[(
|
||||
Notification.status == NOTIFICATION_DELIVERED,
|
||||
(Notification.updated_at - Notification.sent_at) >= delivery_time
|
||||
)],
|
||||
else_=(datetime.utcnow() - Notification.sent_at) >= delivery_time
|
||||
).label("slow"), func.count()
|
||||
|
||||
).filter(
|
||||
Notification.created_at >= created_at,
|
||||
Notification.sent_at.isnot(None),
|
||||
Notification.status.in_([NOTIFICATION_DELIVERED, NOTIFICATION_SENDING]),
|
||||
Notification.sent_by == provider,
|
||||
(Notification.updated_at - Notification.sent_at) >= delivery_time,
|
||||
).count()
|
||||
return count >= threshold
|
||||
Notification.key_type != KEY_TYPE_TEST
|
||||
).group_by("slow").all()
|
||||
|
||||
counts = {c[0]: c[1] for c in count}
|
||||
total_notifications = sum(counts.values())
|
||||
if total_notifications:
|
||||
return counts.get(True, 0) / total_notifications >= threshold
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
|
||||
Reference in New Issue
Block a user