randomly choose from providers based on priority

todo: make sure if they don't add up to 100 we do something sensible,
especially if they're both 0.
This commit is contained in:
Leo Hemsted
2019-10-14 15:01:08 +01:00
parent 4fd6f33af2
commit 6f38cbbcf1
5 changed files with 74 additions and 41 deletions

View File

@@ -1,3 +1,4 @@
import random
from urllib import parse
from datetime import datetime
@@ -39,7 +40,7 @@ def send_sms_to_provider(notification):
return
if notification.status == 'created':
provider = provider_to_use(SMS_TYPE, notification.id, notification.international)
provider = provider_to_use(SMS_TYPE, notification.international)
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
@@ -81,7 +82,7 @@ def send_email_to_provider(notification):
technical_failure(notification=notification)
return
if notification.status == 'created':
provider = provider_to_use(EMAIL_TYPE, notification.id)
provider = provider_to_use(EMAIL_TYPE)
template_dict = dao_get_template_by_id(notification.template_id, notification.template_version).__dict__
@@ -128,18 +129,20 @@ def update_notification_to_sending(notification, provider):
dao_update_notification(notification)
def provider_to_use(notification_type, notification_id, international=False):
active_providers_in_order = [
def provider_to_use(notification_type, international=False):
active_providers = [
p for p in get_provider_details_by_notification_type(notification_type, international) if p.active
]
if not active_providers_in_order:
if not active_providers:
current_app.logger.error(
"{} {} failed as no active providers".format(notification_type, notification_id)
"{} failed as no active providers".format(notification_type)
)
raise Exception("No active {} providers".format(notification_type))
return clients.get_client_by_name_and_type(active_providers_in_order[0].identifier, notification_type)
chosen_provider = random.choices(active_providers, weights=[p.priority for p in active_providers])[0]
return clients.get_client_by_name_and_type(chosen_provider.identifier, notification_type)
def get_logo_url(base_url, logo_file):