Merge pull request #3497 from alphagov/broadcast-alert-earlier-181423293

Alert earlier when a broadcast is approved
This commit is contained in:
Ben Thorner
2022-04-07 10:34:23 +01:00
committed by GitHub
7 changed files with 184 additions and 197 deletions

View File

@@ -2,14 +2,12 @@ import iso8601
from flask import Blueprint, jsonify, request
from notifications_utils.template import BroadcastMessageTemplate
from app.broadcast_message import utils as broadcast_utils
from app.broadcast_message.broadcast_message_schema import (
create_broadcast_message_schema,
update_broadcast_message_schema,
update_broadcast_message_status_schema,
)
from app.broadcast_message.utils import (
validate_and_update_broadcast_message_status,
)
from app.dao.broadcast_message_dao import (
dao_get_broadcast_message_by_id_and_service_id,
dao_get_broadcast_messages_for_service,
@@ -162,6 +160,6 @@ def update_broadcast_message_status(service_id, broadcast_message_id):
status_code=400
)
validate_and_update_broadcast_message_status(broadcast_message, new_status, updating_user)
broadcast_utils.update_broadcast_message_status(broadcast_message, new_status, updating_user)
return jsonify(broadcast_message.serialize()), 200

View File

@@ -1,7 +1,12 @@
import inspect
from datetime import datetime
from flask import current_app
from notifications_utils.clients.zendesk.zendesk_client import (
NotifySupportTicket,
)
from app import zendesk_client
from app.celery.broadcast_message_tasks import send_broadcast_event
from app.config import QueueNames
from app.dao.dao_utils import dao_save_object
@@ -13,7 +18,31 @@ from app.models import (
)
def validate_and_update_broadcast_message_status(broadcast_message, new_status, updating_user=None, api_key_id=None):
def update_broadcast_message_status(broadcast_message, new_status, updating_user=None, api_key_id=None):
_validate_broadcast_update(broadcast_message, new_status, updating_user)
if new_status == BroadcastStatusType.BROADCASTING:
broadcast_message.approved_at = datetime.utcnow()
broadcast_message.approved_by = updating_user
if new_status == BroadcastStatusType.CANCELLED:
broadcast_message.cancelled_at = datetime.utcnow()
broadcast_message.cancelled_by = updating_user
broadcast_message.cancelled_by_api_key_id = api_key_id
current_app.logger.info(
f'broadcast_message {broadcast_message.id} moving from {broadcast_message.status} to {new_status}'
)
broadcast_message.status = new_status
dao_save_object(broadcast_message)
_create_p1_zendesk_alert(broadcast_message)
if new_status in {BroadcastStatusType.BROADCASTING, BroadcastStatusType.CANCELLED}:
_create_broadcast_event(broadcast_message)
def _validate_broadcast_update(broadcast_message, new_status, updating_user):
if new_status not in BroadcastStatusType.ALLOWED_STATUS_TRANSITIONS[broadcast_message.status]:
raise InvalidRequest(
f'Cannot move broadcast_message {broadcast_message.id} from {broadcast_message.status} to {new_status}',
@@ -32,24 +61,39 @@ def validate_and_update_broadcast_message_status(broadcast_message, new_status,
f'broadcast_message {broadcast_message.id} has no selected areas and so cannot be broadcasted.',
status_code=400
)
else:
broadcast_message.approved_at = datetime.utcnow()
broadcast_message.approved_by = updating_user
if new_status == BroadcastStatusType.CANCELLED:
broadcast_message.cancelled_at = datetime.utcnow()
broadcast_message.cancelled_by = updating_user
broadcast_message.cancelled_by_api_key_id = api_key_id
current_app.logger.info(
f'broadcast_message {broadcast_message.id} moving from {broadcast_message.status} to {new_status}'
def _create_p1_zendesk_alert(broadcast_message):
if current_app.config['NOTIFY_ENVIRONMENT'] != 'live':
return
if broadcast_message.status != BroadcastStatusType.BROADCASTING:
return
message = inspect.cleandoc(f"""
Broadcast Sent
https://www.notifications.service.gov.uk/services/{broadcast_message.service_id}/current-alerts/{broadcast_message.id}
Sent on channel {broadcast_message.service.broadcast_channel} to {broadcast_message.areas["names"]}.
Content starts "{broadcast_message.content[:100]}".
Follow the runbook to check the broadcast went out OK:
https://docs.google.com/document/d/1J99yOlfp4nQz6et0w5oJVqi-KywtIXkxrEIyq_g2XUs/edit#heading=h.lzr9aq5b4wg
""")
ticket = NotifySupportTicket(
subject='Live broadcast sent',
message=message,
ticket_type=NotifySupportTicket.TYPE_INCIDENT,
technical_ticket=True,
org_id=current_app.config['BROADCAST_ORGANISATION_ID'],
org_type='central',
service_id=str(broadcast_message.service_id),
p1=True
)
broadcast_message.status = new_status
dao_save_object(broadcast_message)
if new_status in {BroadcastStatusType.BROADCASTING, BroadcastStatusType.CANCELLED}:
_create_broadcast_event(broadcast_message)
zendesk_client.send_ticket_to_zendesk(ticket)
def _create_broadcast_event(broadcast_message):

View File

@@ -1,11 +1,8 @@
from datetime import datetime
from flask import current_app
from notifications_utils.clients.zendesk.zendesk_client import (
NotifySupportTicket,
)
from app import cbc_proxy_client, notify_celery, zendesk_client
from app import cbc_proxy_client, notify_celery
from app.clients.cbc_proxy import CBCProxyRetryableException
from app.config import QueueNames, TaskNames
from app.dao.broadcast_message_dao import (
@@ -126,37 +123,6 @@ def check_event_makes_sense_in_sequence(broadcast_event, provider):
def send_broadcast_event(broadcast_event_id):
broadcast_event = dao_get_broadcast_event_by_id(broadcast_event_id)
if (
current_app.config['NOTIFY_ENVIRONMENT'] == 'live' and
broadcast_event.message_type == BroadcastEventMessageType.ALERT
):
broadcast_message = broadcast_event.broadcast_message
# raise a zendesk ticket to alert team that broadcast is going out.
message = '\n'.join([
'Broadcast Sent',
'',
f'https://www.notifications.service.gov.uk/services/{broadcast_message.service_id}/current-alerts/{broadcast_message.id}', # noqa
'',
f'This broacast has been sent on channel {broadcast_message.service.broadcast_channel}.',
f'This broadcast is targeted at areas {broadcast_message.areas.get("names", [])}.', # noqa
''
f'This broadcast\'s content starts "{broadcast_message.content[:100]}"'
'',
'If this alert is not expected refer to the runbook for instructions.',
'https://docs.google.com/document/d/1J99yOlfp4nQz6et0w5oJVqi-KywtIXkxrEIyq_g2XUs',
])
ticket = NotifySupportTicket(
subject='Live broadcast sent',
message=message,
ticket_type=NotifySupportTicket.TYPE_INCIDENT,
technical_ticket=True,
org_id=current_app.config['BROADCAST_ORGANISATION_ID'],
org_type='central',
service_id=str(broadcast_message.service_id)
)
zendesk_client.send_ticket_to_zendesk(ticket)
current_app.logger.error(message)
notify_celery.send_task(
name=TaskNames.PUBLISH_GOVUK_ALERTS,
queue=QueueNames.GOVUK_ALERTS

View File

@@ -6,10 +6,8 @@ from notifications_utils.template import BroadcastMessageTemplate
from sqlalchemy.orm.exc import MultipleResultsFound
from app import api_user, authenticated_service, redis_store
from app.broadcast_message import utils as broadcast_utils
from app.broadcast_message.translators import cap_xml_to_dict
from app.broadcast_message.utils import (
validate_and_update_broadcast_message_status,
)
from app.dao.broadcast_message_dao import (
dao_get_broadcast_message_by_references_and_service_id,
)
@@ -121,7 +119,7 @@ def _cancel_or_reject_broadcast(references_to_original_broadcast, service_id):
new_status = BroadcastStatusType.REJECTED
else:
new_status = BroadcastStatusType.CANCELLED
validate_and_update_broadcast_message_status(
broadcast_utils.update_broadcast_message_status(
broadcast_message,
new_status,
api_key_id=api_user.id