Esempio n. 1
0
def send_incident_created_notifications(incident: Incident, db_session: SessionLocal):
    """Sends incident created notifications."""
    notification_template = INCIDENT_NOTIFICATION.copy()

    if incident.status != IncidentStatus.closed:
        notification_template.insert(0, INCIDENT_NAME_WITH_ENGAGEMENT)
    else:
        notification_template.insert(0, INCIDENT_NAME)

    notification_kwargs = {
        "name": incident.name,
        "title": incident.title,
        "status": incident.status,
        "type": incident.incident_type.name,
        "type_description": incident.incident_type.description,
        "priority": incident.incident_priority.name,
        "priority_description": incident.incident_priority.description,
        "commander_fullname": incident.commander.individual.name,
        "commander_weblink": incident.commander.individual.weblink,
        "document_weblink": resolve_attr(incident, "incident_document.weblink"),
        "storage_weblink": resolve_attr(incident, "storage.weblink"),
        "ticket_weblink": resolve_attr(incident, "ticket.weblink"),
        "conference_weblink": resolve_attr(incident, "conference.weblink"),
        "conference_challenge": resolve_attr(incident, "conference.conference_challenge"),
        "contact_fullname": incident.commander.individual.name,
        "contact_weblink": incident.commander.individual.weblink,
        "incident_id": incident.id,
    }

    faq_doc = document_service.get_incident_faq_document(db_session=db_session)
    if faq_doc:
        notification_kwargs.update({"faq_weblink": faq_doc.weblink})

    notification_params = {
        "text": "Incident Notification",
        "type": MessageType.incident_notification,
        "template": notification_template,
        "kwargs": notification_kwargs,
    }

    notification_service.send(
        db_session=db_session, class_instance=incident, notification_params=notification_params
    )

    log.debug("Incident created notifications sent.")
Esempio n. 2
0
def daily_report(db_session=None):
    """
    Creates and sends incident daily reports based on notifications.
    """
    # we fetch all active, stable and closed incidents
    active_incidents = get_all_by_status(db_session=db_session, status=IncidentStatus.active.value)
    stable_incidents = get_all_last_x_hours_by_status(
        db_session=db_session, status=IncidentStatus.stable.value, hours=24
    )
    closed_incidents = get_all_last_x_hours_by_status(
        db_session=db_session, status=IncidentStatus.closed.value, hours=24
    )
    incidents = active_incidents + stable_incidents + closed_incidents

    # we map incidents to notification filters
    incidents_notification_filters_mapping = defaultdict(lambda: defaultdict(lambda: []))
    notifications = notification_service.get_all_enabled(db_session=db_session)
    for incident in incidents:
        for notification in notifications:
            for search_filter in notification.filters:
                match = search_service.match(
                    db_session=db_session,
                    filter_spec=search_filter.expression,
                    class_instance=incident,
                )
                if match:
                    incidents_notification_filters_mapping[notification.id][
                        search_filter.id
                    ].append(incident)

            if not notification.filters:
                incidents_notification_filters_mapping[notification.id][0].append(incident)

    # we create and send an incidents daily report for each notification filter
    for notification_id, search_filter_dict in incidents_notification_filters_mapping.items():
        for search_filter_id, incidents in search_filter_dict.items():
            items_grouped = []
            items_grouped_template = INCIDENT

            for idx, incident in enumerate(incidents):
                try:
                    item = {
                        "commander_fullname": incident.commander.individual.name,
                        "commander_team": incident.commander.team,
                        "commander_weblink": incident.commander.individual.weblink,
                        "incident_id": incident.id,
                        "name": incident.name,
                        "priority": incident.incident_priority.name,
                        "priority_description": incident.incident_priority.description,
                        "status": incident.status,
                        "ticket_weblink": resolve_attr(incident, "ticket.weblink"),
                        "title": incident.title,
                        "type": incident.incident_type.name,
                        "type_description": incident.incident_type.description,
                    }

                    if incident.status != IncidentStatus.closed.value:
                        item.update(
                            {
                                "button_text": "Join Incident",
                                "button_value": str(incident.id),
                                "button_action": f"{ConversationButtonActions.invite_user.value}-{incident.status}-{idx}",
                            }
                        )

                    items_grouped.append(item)
                except Exception as e:
                    log.exception(e)

            notification_kwargs = {
                "contact_fullname": DISPATCH_HELP_EMAIL,
                "contact_weblink": DISPATCH_HELP_EMAIL,
                "items_grouped": items_grouped,
                "items_grouped_template": items_grouped_template,
            }

            notification_params = {
                "text": INCIDENT_DAILY_REPORT_TITLE,
                "type": MessageType.incident_daily_report,
                "template": INCIDENT_DAILY_REPORT,
                "kwargs": notification_kwargs,
            }

            notification = notification_service.get(
                db_session=db_session, notification_id=notification_id
            )

            notification_service.send(
                db_session=db_session,
                notification=notification,
                notification_params=notification_params,
            )
Esempio n. 3
0
def send_incident_update_notifications(
    incident: Incident, previous_incident: IncidentRead, db_session: SessionLocal
):
    """Sends notifications about incident changes."""
    notification_text = "Incident Notification"
    notification_type = MessageType.incident_notification
    notification_template = INCIDENT_NOTIFICATION_COMMON.copy()

    change = False
    if previous_incident.status != incident.status:
        change = True
        notification_template.append(INCIDENT_STATUS_CHANGE)

    if previous_incident.incident_type.name != incident.incident_type.name:
        change = True
        notification_template.append(INCIDENT_TYPE_CHANGE)

    if previous_incident.incident_priority.name != incident.incident_priority.name:
        change = True
        notification_template.append(INCIDENT_PRIORITY_CHANGE)

    if not change:
        # we don't need to notify
        log.debug("Incident updated notifications not sent.")
        return

    notification_template.append(INCIDENT_COMMANDER)

    # we send an update to the incident conversation if the incident is active or stable
    if incident.status != IncidentStatus.closed:
        incident_conversation_notification_template = notification_template.copy()
        incident_conversation_notification_template.insert(0, INCIDENT_NAME)

        convo_plugin = plugin_service.get_active(db_session=db_session, plugin_type="conversation")
        if convo_plugin:
            convo_plugin.instance.send(
                incident.conversation.channel_id,
                notification_text,
                incident_conversation_notification_template,
                notification_type,
                commander_fullname=incident.commander.individual.name,
                commander_weblink=incident.commander.individual.weblink,
                incident_priority_new=incident.incident_priority.name,
                incident_priority_old=previous_incident.incident_priority.name,
                incident_status_new=incident.status,
                incident_status_old=previous_incident.status.value,
                incident_type_new=incident.incident_type.name,
                incident_type_old=previous_incident.incident_type.name,
                name=incident.name,
                ticket_weblink=incident.ticket.weblink,
                title=incident.title,
            )
        else:
            log.debug(
                "Incident updated notification not sent to incident conversation. No conversation plugin enabled."
            )

    # we only send notifications about incident updates to fyi conversations
    # and distribution lists if the incident's visibility is open
    if incident.visibility == Visibility.open:
        fyi_notification_template = notification_template.copy()
        if incident.status != IncidentStatus.closed:
            fyi_notification_template.insert(0, INCIDENT_NAME_WITH_ENGAGEMENT)
        else:
            fyi_notification_template.insert(0, INCIDENT_NAME)

        notification_kwargs = {
            "commander_fullname": incident.commander.individual.name,
            "commander_weblink": incident.commander.individual.weblink,
            "contact_fullname": incident.commander.individual.name,
            "contact_weblink": incident.commander.individual.weblink,
            "incident_id": incident.id,
            "incident_priority_new": incident.incident_priority.name,
            "incident_priority_old": previous_incident.incident_priority.name,
            "incident_status_new": incident.status,
            "incident_status_old": previous_incident.status.value,
            "incident_type_new": incident.incident_type.name,
            "incident_type_old": previous_incident.incident_type.name,
            "name": incident.name,
            "ticket_weblink": resolve_attr(incident, "ticket.weblink"),
            "title": incident.title,
        }

        notification_params = {
            "text": notification_text,
            "type": notification_type,
            "template": fyi_notification_template,
            "kwargs": notification_kwargs,
        }

        notification_service.send(
            db_session=db_session, class_instance=incident, notification_params=notification_params
        )

    log.debug("Incident updated notifications sent.")