Esempio n. 1
0
def incident_closed_flow(incident_id: int,
                         command: Optional[dict] = None,
                         db_session=None):
    """Runs the incident closed flow."""
    # we load the incident instance
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # we set the closed time
    incident.closed_at = datetime.utcnow()

    # we update the incident cost
    incident_cost = incident_service.calculate_cost(incident_id, db_session)

    # we archive the conversation
    convo_plugin = plugins.get(INCIDENT_PLUGIN_CONVERSATION_SLUG)
    convo_plugin.archive(incident.conversation.channel_id)

    # we update the external ticket
    update_incident_ticket(incident.ticket.resource_id,
                           status=IncidentStatus.closed.lower(),
                           cost=incident_cost)

    if incident.visibility == Visibility.open:
        # we archive the artifacts in the storage
        archive_incident_artifacts(incident, db_session)

        # we delete the tactical and notification groups
        delete_participant_groups(incident, db_session)

    # we delete the conference
    delete_conference(incident, db_session)

    db_session.add(incident)
    db_session.commit()

    event_service.log(
        db_session=db_session,
        source="Dispatch Core App",
        description=f"Incident marked as {incident.status}",
        incident_id=incident.id,
    )
Esempio n. 2
0
def incident_stable_flow(incident_id: int,
                         command: Optional[dict] = None,
                         db_session=None):
    """Runs the incident stable flow."""
    # we load the incident instance
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # we set the stable time
    incident.stable_at = datetime.utcnow()

    # we remind the incident commander to write a status report
    send_incident_status_report_reminder(incident)

    # we update the incident cost
    incident_cost = incident_service.calculate_cost(incident_id, db_session)

    # we update the external ticket
    update_incident_ticket(incident.ticket.resource_id,
                           status=IncidentStatus.stable.lower(),
                           cost=incident_cost)

    incident_review_document = get_document(
        db_session=db_session,
        incident_id=incident.id,
        resource_type=INCIDENT_RESOURCE_INCIDENT_REVIEW_DOCUMENT,
    )

    if not incident_review_document:
        storage_plugin = plugins.get(INCIDENT_PLUGIN_STORAGE_SLUG)

        # we create a copy of the incident review document template and we move it to the incident storage
        incident_review_document_name = f"{incident.name} - Post Incident Review Document"
        incident_review_document = storage_plugin.copy_file(
            team_drive_id=incident.storage.resource_id,
            file_id=INCIDENT_STORAGE_INCIDENT_REVIEW_FILE_ID,
            name=incident_review_document_name,
        )

        incident_review_document.update({
            "name":
            incident_review_document_name,
            "resource_type":
            INCIDENT_RESOURCE_INCIDENT_REVIEW_DOCUMENT,
        })

        storage_plugin.move_file(
            new_team_drive_id=incident.storage.resource_id,
            file_id=incident_review_document["id"])

        event_service.log(
            db_session=db_session,
            source=storage_plugin.title,
            description="Incident review document added to storage",
            incident_id=incident.id,
        )

        document_in = DocumentCreate(
            name=incident_review_document["name"],
            resource_id=incident_review_document["id"],
            resource_type=incident_review_document["resource_type"],
            weblink=incident_review_document["weblink"],
        )
        incident.documents.append(
            document_service.create(db_session=db_session,
                                    document_in=document_in))

        event_service.log(
            db_session=db_session,
            source="Dispatch Core App",
            description="Incident review document added to incident",
            incident_id=incident.id,
        )

        # we get the incident investigation and faq documents
        incident_document = get_document(
            db_session=db_session,
            incident_id=incident_id,
            resource_type=INCIDENT_RESOURCE_INVESTIGATION_DOCUMENT,
        )

        # we update the incident review document
        update_document(
            incident_review_document["id"],
            incident.name,
            incident.incident_priority.name,
            incident.status,
            incident.incident_type.name,
            incident.title,
            incident.description,
            incident.commander.name,
            incident.conversation.weblink,
            incident_document.weblink,
            incident.storage.weblink,
            incident.ticket.weblink,
        )

        # we send a notification about the incident review document to the conversation
        send_incident_review_document_notification(
            incident.conversation.channel_id,
            incident_review_document["weblink"])

    db_session.add(incident)
    db_session.commit()

    event_service.log(
        db_session=db_session,
        source="Dispatch Core App",
        description=f"Incident marked as {incident.status}",
        incident_id=incident.id,
    )
Esempio n. 3
0
def incident_stable_flow(incident_id: int,
                         command: Optional[dict] = None,
                         db_session=None):
    """Runs the incident stable flow."""
    # we load the incident instance
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    if incident.status == IncidentStatus.stable:
        if command:
            convo_plugin = plugins.get(INCIDENT_PLUGIN_CONVERSATION_SLUG)
            convo_plugin.send_ephemeral(
                command["channel_id"],
                command["user_id"],
                "Incident Already Stable Notification",
                blocks=[{
                    "type": "section",
                    "text": {
                        "type":
                        "plain_text",
                        "text":
                        "The incident is already stable. Aborting command...",
                    },
                }],
            )
            return

    # we update the status of the incident
    update_incident_status(db_session=db_session,
                           incident=incident,
                           status=IncidentStatus.stable)

    log.debug(
        f"We have updated the status of the incident to {IncidentStatus.stable}."
    )

    # we update the incident cost
    incident_cost = incident_service.calculate_cost(incident_id, db_session)

    log.debug(f"We have updated the cost of the incident.")

    # we update the external ticket
    update_incident_ticket(
        incident.ticket.resource_id,
        incident_type=incident.incident_type.name,
        status=IncidentStatus.stable.lower(),
        cost=incident_cost,
    )

    log.debug(
        f"We have updated the status of the external ticket to {IncidentStatus.stable}."
    )

    # we update the conversation topic
    set_conversation_topic(incident)

    incident_review_document = get_document(
        db_session=db_session,
        incident_id=incident.id,
        resource_type=INCIDENT_RESOURCE_INCIDENT_REVIEW_DOCUMENT,
    )

    if not incident_review_document:
        storage_plugin = plugins.get(INCIDENT_PLUGIN_STORAGE_SLUG)

        # we create a copy of the incident review document template and we move it to the incident storage
        incident_review_document_name = f"{incident.name} - Post Incident Review Document"
        incident_review_document = storage_plugin.copy_file(
            team_drive_id=incident.storage.resource_id,
            file_id=INCIDENT_STORAGE_INCIDENT_REVIEW_FILE_ID,
            name=incident_review_document_name,
        )

        incident_review_document.update({
            "name":
            incident_review_document_name,
            "resource_type":
            INCIDENT_RESOURCE_INCIDENT_REVIEW_DOCUMENT,
        })

        storage_plugin.move_file(
            new_team_drive_id=incident.storage.resource_id,
            file_id=incident_review_document["id"])

        log.debug(
            "We have added the incident review document in the incident storage."
        )

        document_in = DocumentCreate(
            name=incident_review_document["name"],
            resource_id=incident_review_document["id"],
            resource_type=incident_review_document["resource_type"],
            weblink=incident_review_document["weblink"],
        )
        incident.documents.append(
            document_service.create(db_session=db_session,
                                    document_in=document_in))

        db_session.add(incident)
        db_session.commit()

        log.debug(
            "We have added the incident review document to the incident.")

        # we get the incident investigation and faq documents
        incident_document = get_document(
            db_session=db_session,
            incident_id=incident_id,
            resource_type=INCIDENT_RESOURCE_INVESTIGATION_DOCUMENT,
        )

        # we update the incident review document
        update_document(
            incident_review_document["id"],
            incident.name,
            incident.incident_priority.name,
            incident.status,
            incident.title,
            incident.description,
            incident.commander.name,
            incident.conversation.weblink,
            incident_document.weblink,
            incident.storage.weblink,
            incident.ticket.weblink,
        )

        log.debug("We have updated the incident review document.")

        # we send a notification about the incident review document to the conversation
        send_incident_review_document_notification(
            incident.conversation.channel_id,
            incident_review_document["weblink"])

        log.debug(
            "We have sent a notification about the incident review document to the conversation."
        )

    # we send the stable notifications
    send_incident_status_notifications(incident, db_session)

    log.debug("We have sent the incident stable notifications.")
Esempio n. 4
0
def incident_closed_flow(incident_id: int,
                         command: Optional[dict] = None,
                         db_session=None):
    """Runs the incident closed flow."""
    # we load the incident instance
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    if incident.status == IncidentStatus.active:
        # we run the stable flow and let the user know
        if command:
            convo_plugin = plugins.get(INCIDENT_PLUGIN_CONVERSATION_SLUG)
            convo_plugin.send_ephemeral(
                command["channel_id"],
                command["user_id"],
                "Mark Incident Stable Notification",
                blocks=[{
                    "type": "section",
                    "text": {
                        "type": "plain_text",
                        "text": "Marking the incident as stable..."
                    },
                }],
            )
        incident_stable_flow(incident_id,
                             command=command,
                             db_session=db_session)

    # we update the status of the incident
    update_incident_status(db_session=db_session,
                           incident=incident,
                           status=IncidentStatus.closed)
    log.debug(
        f"We have updated the status of the incident to {IncidentStatus.closed}."
    )

    # we update the incident cost
    incident_cost = incident_service.calculate_cost(incident_id, db_session)
    log.debug(f"We have updated the cost of the incident.")

    # we archive the conversation
    convo_plugin = plugins.get(INCIDENT_PLUGIN_CONVERSATION_SLUG)
    convo_plugin.archive(incident.conversation.channel_id)
    log.debug("We have archived the incident conversation.")

    # we send the closed notifications
    send_incident_status_notifications(incident, db_session)
    log.debug("We have sent the incident closed notifications.")

    # we update the external ticket
    update_incident_ticket(
        incident.ticket.resource_id,
        incident_type=incident.incident_type.name,
        status=IncidentStatus.closed.lower(),
        cost=incident_cost,
    )
    log.debug(
        f"We have updated the status of the external ticket to {IncidentStatus.closed}."
    )

    # we archive the artifacts in the storage
    storage_plugin = plugins.get(INCIDENT_PLUGIN_STORAGE_SLUG)
    storage_plugin.archive(
        source_team_drive_id=incident.storage.resource_id,
        dest_team_drive_id=INCIDENT_STORAGE_ARCHIVAL_FOLDER_ID,
        folder_name=incident.name,
    )
    log.debug(
        "We have archived the incident artifacts in the archival folder and re-applied permissions and deleted the source."
    )

    # we get the tactical group
    tactical_group = get_group(
        db_session=db_session,
        incident_id=incident_id,
        resource_type=INCIDENT_RESOURCE_TACTICAL_GROUP,
    )

    # we get the notifications group
    notifications_group = get_group(
        db_session=db_session,
        incident_id=incident_id,
        resource_type=INCIDENT_RESOURCE_NOTIFICATIONS_GROUP,
    )

    group_plugin = plugins.get(INCIDENT_PLUGIN_GROUP_SLUG)
    group_plugin.delete(email=tactical_group.email)
    group_plugin.delete(email=notifications_group.email)
    log.debug("We have deleted the notification and tactical groups.")
Esempio n. 5
0
def incident_closed_flow(incident_id: int,
                         command: Optional[dict] = None,
                         db_session=None):
    """Runs the incident closed flow."""
    # we load the incident instance
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # we set the closed time
    incident.closed_at = datetime.utcnow()
    log.debug(f"We have set the closed time.")

    # we update the incident cost
    incident_cost = incident_service.calculate_cost(incident_id, db_session)
    log.debug(f"We have updated the cost of the incident.")

    # we archive the conversation
    convo_plugin = plugins.get(INCIDENT_PLUGIN_CONVERSATION_SLUG)
    convo_plugin.archive(incident.conversation.channel_id)
    log.debug("We have archived the incident conversation.")

    # we update the external ticket
    update_incident_ticket(incident.ticket.resource_id,
                           status=IncidentStatus.closed.lower(),
                           cost=incident_cost)
    log.debug(
        f"We have updated the status of the external ticket to {IncidentStatus.closed}."
    )

    if incident.visibility == Visibility.open:
        # we archive the artifacts in the storage
        storage_plugin = plugins.get(INCIDENT_PLUGIN_STORAGE_SLUG)
        storage_plugin.archive(
            source_team_drive_id=incident.storage.resource_id,
            dest_team_drive_id=INCIDENT_STORAGE_ARCHIVAL_FOLDER_ID,
            folder_name=incident.name,
            visibility=incident.visibility,
        )
        log.debug(
            "We have archived the incident artifacts in the archival folder and re-applied permissions and deleted the source."
        )

        # we get the tactical group
        tactical_group = group_service.get_by_incident_id_and_resource_type(
            db_session=db_session,
            incident_id=incident_id,
            resource_type=INCIDENT_RESOURCE_TACTICAL_GROUP,
        )

        # we get the notifications group
        notifications_group = group_service.get_by_incident_id_and_resource_type(
            db_session=db_session,
            incident_id=incident_id,
            resource_type=INCIDENT_RESOURCE_NOTIFICATIONS_GROUP,
        )

        group_plugin = plugins.get(INCIDENT_PLUGIN_GROUP_SLUG)
        group_plugin.delete(email=tactical_group.email)
        group_plugin.delete(email=notifications_group.email)
        log.debug("We have deleted the notification and tactical groups.")

    db_session.add(incident)
    db_session.commit()