2020-10-29 11:12:28 +00:00
|
|
|
import uuid
|
|
|
|
|
|
2020-07-09 18:22:29 +01:00
|
|
|
from flask import current_app
|
|
|
|
|
from notifications_utils.statsd_decorators import statsd
|
|
|
|
|
|
2020-10-20 11:57:26 +01:00
|
|
|
from app import cbc_proxy_client, notify_celery
|
2020-10-29 11:12:28 +00:00
|
|
|
from app.config import QueueNames
|
2020-11-16 12:47:38 +00:00
|
|
|
from app.models import BroadcastEventMessageType
|
2020-11-16 18:48:00 +00:00
|
|
|
from app.dao.broadcast_message_dao import dao_get_broadcast_event_by_id, create_broadcast_provider_message
|
2020-07-09 18:22:29 +01:00
|
|
|
|
|
|
|
|
|
2020-08-04 19:21:22 +01:00
|
|
|
@notify_celery.task(name="send-broadcast-event")
|
|
|
|
|
@statsd(namespace="tasks")
|
2020-10-23 17:54:33 +01:00
|
|
|
def send_broadcast_event(broadcast_event_id):
|
2020-11-16 12:47:38 +00:00
|
|
|
for provider in current_app.config['ENABLED_CBCS']:
|
2020-10-29 11:12:28 +00:00
|
|
|
# TODO: Decide whether to send to each provider based on platform admin, service level settings, broadcast
|
|
|
|
|
# level settings, etc.
|
|
|
|
|
send_broadcast_provider_message.apply_async(
|
|
|
|
|
kwargs={'broadcast_event_id': broadcast_event_id, 'provider': provider},
|
|
|
|
|
queue=QueueNames.NOTIFY
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name="send-broadcast-provider-message")
|
|
|
|
|
@statsd(namespace="tasks")
|
|
|
|
|
def send_broadcast_provider_message(broadcast_event_id, provider):
|
2020-08-04 19:21:22 +01:00
|
|
|
broadcast_event = dao_get_broadcast_event_by_id(broadcast_event_id)
|
|
|
|
|
|
2020-11-16 18:48:00 +00:00
|
|
|
broadcast_provider_message = create_broadcast_provider_message(broadcast_event, provider)
|
|
|
|
|
|
2020-10-23 17:54:33 +01:00
|
|
|
current_app.logger.info(
|
|
|
|
|
f'invoking cbc proxy to send '
|
|
|
|
|
f'broadcast_event {broadcast_event.reference} '
|
|
|
|
|
f'msgType {broadcast_event.message_type}'
|
|
|
|
|
)
|
2020-10-20 11:57:26 +01:00
|
|
|
|
2020-10-23 17:54:33 +01:00
|
|
|
areas = [
|
2020-10-26 15:16:33 +00:00
|
|
|
{"polygon": polygon}
|
|
|
|
|
for polygon in broadcast_event.transmitted_areas["simple_polygons"]
|
2020-10-23 17:54:33 +01:00
|
|
|
]
|
2020-10-23 16:44:11 +01:00
|
|
|
|
2020-11-17 12:35:22 +00:00
|
|
|
cbc_proxy_provider_client = cbc_proxy_client.get_proxy(provider)
|
|
|
|
|
|
2020-10-23 17:54:33 +01:00
|
|
|
if broadcast_event.message_type == BroadcastEventMessageType.ALERT:
|
2020-11-17 12:35:22 +00:00
|
|
|
cbc_proxy_provider_client.create_and_send_broadcast(
|
2020-11-16 18:48:00 +00:00
|
|
|
identifier=str(broadcast_provider_message.id),
|
2020-10-20 11:57:26 +01:00
|
|
|
headline="GOV.UK Notify Broadcast",
|
|
|
|
|
description=broadcast_event.transmitted_content['body'],
|
2020-10-23 16:44:11 +01:00
|
|
|
areas=areas,
|
2020-10-28 11:26:38 +00:00
|
|
|
sent=broadcast_event.sent_at_as_cap_datetime_string,
|
|
|
|
|
expires=broadcast_event.transmitted_finishes_at_as_cap_datetime_string,
|
2020-10-20 11:57:26 +01:00
|
|
|
)
|
2020-10-23 17:54:33 +01:00
|
|
|
elif broadcast_event.message_type == BroadcastEventMessageType.UPDATE:
|
2020-11-17 12:35:22 +00:00
|
|
|
cbc_proxy_provider_client.update_and_send_broadcast(
|
2020-11-16 18:48:00 +00:00
|
|
|
identifier=str(broadcast_provider_message.id),
|
2020-10-23 17:54:33 +01:00
|
|
|
headline="GOV.UK Notify Broadcast",
|
|
|
|
|
description=broadcast_event.transmitted_content['body'],
|
|
|
|
|
areas=areas,
|
2020-11-17 12:35:10 +00:00
|
|
|
previous_provider_messages=broadcast_event.get_earlier_provider_messages(provider),
|
2020-10-28 11:26:38 +00:00
|
|
|
sent=broadcast_event.sent_at_as_cap_datetime_string,
|
|
|
|
|
expires=broadcast_event.transmitted_finishes_at_as_cap_datetime_string,
|
2020-10-23 17:54:33 +01:00
|
|
|
)
|
|
|
|
|
elif broadcast_event.message_type == BroadcastEventMessageType.CANCEL:
|
2020-11-17 12:35:22 +00:00
|
|
|
cbc_proxy_provider_client.cancel_broadcast(
|
2020-11-16 18:48:00 +00:00
|
|
|
identifier=str(broadcast_provider_message.id),
|
2020-10-23 17:54:33 +01:00
|
|
|
headline="GOV.UK Notify Broadcast",
|
|
|
|
|
description=broadcast_event.transmitted_content['body'],
|
|
|
|
|
areas=areas,
|
2020-11-17 12:35:10 +00:00
|
|
|
previous_provider_messages=broadcast_event.get_earlier_provider_messages(provider),
|
2020-10-28 11:26:38 +00:00
|
|
|
sent=broadcast_event.sent_at_as_cap_datetime_string,
|
|
|
|
|
expires=broadcast_event.transmitted_finishes_at_as_cap_datetime_string,
|
2020-10-23 17:54:33 +01:00
|
|
|
)
|
2020-10-29 11:12:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='trigger-link-test')
|
|
|
|
|
def trigger_link_test(provider):
|
|
|
|
|
"""
|
|
|
|
|
Currently we only have one hardcoded CBC Proxy, which corresponds to one
|
|
|
|
|
CBC, and so currently we do not specify the CBC Proxy name
|
|
|
|
|
|
|
|
|
|
In future we will have multiple CBC proxies, each proxy corresponding to
|
|
|
|
|
one MNO's CBC
|
|
|
|
|
|
|
|
|
|
This task should invoke other tasks which do the actual link tests, eg:
|
|
|
|
|
for cbc_name in app.config.ENABLED_CBCS:
|
|
|
|
|
send_link_test_for_cbc(cbc_name)
|
|
|
|
|
|
|
|
|
|
Alternatively this task could be configured to be a Celery group
|
|
|
|
|
"""
|
|
|
|
|
identifier = str(uuid.uuid4())
|
|
|
|
|
message = f"Sending a link test to CBC proxy for provider {provider} with ID {identifier}"
|
|
|
|
|
current_app.logger.info(message)
|
2020-11-17 12:35:22 +00:00
|
|
|
cbc_proxy_client.get_proxy(provider).send_link_test(identifier)
|