Example #1
0
    def get(self,
            incident_type: str,
            incident_priority: str,
            incident_description: str,
            db_session=None):
        """Fetches participants from Dispatch."""
        route_in = {
            "text": incident_description,
            "context": {
                "incident_priorities": [incident_priority.__dict__],
                "incident_types": [incident_type.__dict__],
                "terms": [],
            },
        }

        route_in = RouteRequest(**route_in)
        recommendation = route_service.get(db_session=db_session,
                                           route_in=route_in)

        log.debug(f"Recommendation: {recommendation}")
        # we need to resolve our service contacts to individuals
        for s in recommendation.service_contacts:
            p = plugins.get(s.type)
            log.debug(f"Resolving service contact. ServiceContact: {s}")
            individual_email = p.get(s.external_id)

            individual = individual_service.get_or_create(
                db_session=db_session, email=individual_email)
            recommendation.individual_contacts.append(individual)

        db_session.commit()
        return list(recommendation.individual_contacts), list(
            recommendation.team_contacts)
Example #2
0
def add_participant(user_email: str,
                    incident_id: id,
                    db_session: SessionLocal,
                    role: ParticipantRoleType = None):
    """Adds a participant."""
    # We load the incident
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # We add the participant to the incident
    individual = individual_service.get_or_create(db_session=db_session,
                                                  email=user_email)

    participant_role = participant_role_service.create(db_session=db_session,
                                                       role=role)
    participant = get_or_create(
        db_session=db_session,
        incident_id=incident.id,
        individual_id=individual.id,
        role=participant_role,
    )

    individual.participant.append(participant)
    incident.participants.append(participant)

    # We add and commit the changes
    db_session.add(individual)
    db_session.add(incident)
    db_session.commit()

    log.debug(f"{individual.name} has been added to incident {incident.name}.")

    return True
Example #3
0
def add_participant(
    user_email: str,
    incident: Incident,
    db_session: SessionLocal,
    service_id: int = None,
    role: ParticipantRoleType = ParticipantRoleType.participant,
):
    """Adds a participant."""
    # we get or create a new individual
    individual = individual_service.get_or_create(db_session=db_session,
                                                  incident=incident,
                                                  email=user_email)

    # we get or create a new participant
    participant_role = ParticipantRoleCreate(role=role)
    participant = get_or_create(
        db_session=db_session,
        incident_id=incident.id,
        individual_id=individual.id,
        service_id=service_id,
        participant_roles=[participant_role],
    )

    individual.participant.append(participant)
    incident.participants.append(participant)

    # we update the commander, reporter, scribe, or liaison foreign key
    if role == ParticipantRoleType.incident_commander:
        incident.commander_id = participant.id
        incident.commanders_location = participant.location
    elif role == ParticipantRoleType.reporter:
        incident.reporter_id = participant.id
        incident.reporters_location = participant.location
    elif role == ParticipantRoleType.scribe:
        incident.scribe_id = participant.id
    elif role == ParticipantRoleType.liaison:
        incident.liaison_id = participant.id

    # we add and commit the changes
    db_session.add(participant)
    db_session.add(individual)
    db_session.add(incident)
    db_session.commit()

    event_service.log(
        db_session=db_session,
        source="Dispatch Core App",
        description=
        f"{individual.name} added to incident with {participant_role.role} role",
        incident_id=incident.id,
    )

    return participant
Example #4
0
    def get(
        self,
        incident_type: IncidentType,
        incident_priority: IncidentPriority,
        incident_description: str,
        project: Project,
        db_session=None,
    ):
        """Fetches participants from Dispatch."""
        route_in = {
            "text": incident_description,
            "context": {
                "incident_priorities": [incident_priority],
                "incident_types": [incident_type],
                "terms": [],
                "project": project,
            },
        }

        route_in = RouteRequest(**route_in)
        recommendation = route_service.get(db_session=db_session,
                                           route_in=route_in)

        log.debug(f"Recommendation: {recommendation}")
        individual_contacts = [(x, None)
                               for x in recommendation.individual_contacts]
        # we need to resolve our service contacts to individuals
        for s in recommendation.service_contacts:
            plugin_instance = plugin_service.get_active_instance_by_slug(
                db_session=db_session, slug=s.type, project_id=project.id)

            if plugin_instance:
                if plugin_instance.enabled:
                    log.debug(
                        f"Resolving service contact. ServiceContact: {s}")
                    individual_email = plugin_instance.instance.get(
                        s.external_id)

                    individual = individual_service.get_or_create(
                        db_session=db_session, email=individual_email)
                    individual_contacts.append((individual, s))
                    recommendation.individual_contacts.append(individual)
                else:
                    log.warning(
                        f"Skipping service contact. Service: {s.name} Reason: Associated service plugin not enabled."
                    )
            else:
                log.warning(
                    f"Skipping service contact. Service: {s.name} Reason: Associated service plugin not found."
                )

        db_session.commit()
        return list(individual_contacts), list(recommendation.team_contacts)
Example #5
0
def contact_load_csv_command(input, first_row_is_header):
    """Load contacts via CSV."""
    import csv
    from pydantic import ValidationError
    from dispatch.individual import service as individual_service
    from dispatch.team import service as team_service
    from dispatch.database import SessionLocal

    db_session = SessionLocal()

    individual_contacts = []
    team_contacts = []
    if first_row_is_header:
        reader = csv.DictReader(input)
        for row in reader:
            row = {k.lower(): v for k, v in row.items()}
            if not row.get("email"):
                continue

            individual_contacts.append(row)

    for i in individual_contacts:
        i["is_external"] = True
        try:
            click.secho(f"Adding new individual contact. Email: {i['email']}",
                        fg="blue")
            individual_service.get_or_create(db_session=db_session, **i)
        except ValidationError as e:
            click.secho(f"Failed to add individual contact. {e} {row}",
                        fg="red")

    for t in team_contacts:
        i["is_external"] = True
        try:
            click.secho(f"Adding new team contact. Email: {t['email']}",
                        fg="blue")
            team_service.get_or_create(db_session=db_session, **t)
        except ValidationError as e:
            click.secho(f"Failed to add team contact. {e} {row}", fg="red")
Example #6
0
def add_participant(
    user_email: str,
    incident_id: id,
    db_session: SessionLocal,
    service: Service = None,
    role: ParticipantRoleType = None,
):
    """Adds a participant."""
    # We load the incident
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # We get or create a new individual
    individual = individual_service.get_or_create(db_session=db_session,
                                                  email=user_email)

    # We create a role for the participant
    participant_role_in = ParticipantRoleCreate(role=role)

    participant_role = participant_role_service.create(
        db_session=db_session, participant_role_in=participant_role_in)

    # We get or create a new participant
    participant = get_or_create(
        db_session=db_session,
        incident_id=incident.id,
        individual_id=individual.id,
        service=service,
        participant_roles=[participant_role],
    )

    individual.participant.append(participant)
    incident.participants.append(participant)

    # We add and commit the changes
    db_session.add(individual)
    db_session.add(incident)
    db_session.commit()

    event_service.log(
        db_session=db_session,
        source="Dispatch Core App",
        description=
        f"{individual.name} added to incident with {participant_role.role} role",
        incident_id=incident_id,
    )

    return participant
Example #7
0
def add_participant(user_email: str,
                    incident_id: id,
                    db_session: SessionLocal,
                    role: ParticipantRoleType = None):
    """Adds a participant."""
    # We load the incident
    incident = incident_service.get(db_session=db_session,
                                    incident_id=incident_id)

    # We get or create a new individual
    individual = individual_service.get_or_create(db_session=db_session,
                                                  email=user_email)

    # We create a role for the participant
    participant_role_in = ParticipantRoleCreate(role=role)
    participant_role = participant_role_service.create(
        db_session=db_session, participant_role_in=participant_role_in)

    # We get or create a new participant
    participant = get_or_create(
        db_session=db_session,
        incident_id=incident.id,
        individual_id=individual.id,
        participant_roles=[participant_role],
    )

    individual.participant.append(participant)
    incident.participants.append(participant)

    # We add and commit the changes
    db_session.add(individual)
    db_session.add(incident)
    db_session.commit()

    log.debug(
        f"{individual.name} with email address {individual.email} has been added to incident id {incident.id} with role {participant_role.role}."
    )

    return participant
Example #8
0
def create(
    *,
    db_session,
    incident_priority: str,
    incident_type: str,
    reporter_email: str,
    title: str,
    status: str,
    description: str,
) -> Incident:
    participants = []

    # TODO should some of this logic be in the incident_create_flow_instead? (kglisson)
    incident_priority = incident_priority_service.get_by_name(
        db_session=db_session, name=incident_priority["name"])

    incident_type = incident_type_service.get_by_name(
        db_session=db_session, name=incident_type["name"])

    commander_email = resolve_incident_commander_email(
        db_session,
        reporter_email,
        incident_type.name,
        incident_priority.name,
        "",
        title,
        description,
    )

    commander_info = individual_service.resolve_user_by_email(commander_email)

    incident_commander_role = participant_role_service.create(
        db_session=db_session, role=ParticipantRoleType.incident_commander)

    commander_participant = participant_service.create(
        db_session=db_session, participant_role=[incident_commander_role])

    commander = individual_service.get_or_create(
        db_session=db_session,
        email=commander_info["email"],
        name=commander_info["fullname"],
        weblink=commander_info["weblink"],
    )

    incident_reporter_role = participant_role_service.create(
        db_session=db_session, role=ParticipantRoleType.reporter)

    if reporter_email == commander_email:
        commander_participant.participant_role.append(incident_reporter_role)
    else:
        reporter_participant = participant_service.create(
            db_session=db_session, participant_role=[incident_reporter_role])
        reporter_info = individual_service.resolve_user_by_email(
            reporter_email)
        reporter = individual_service.get_or_create(
            db_session=db_session,
            email=reporter_info["email"],
            name=reporter_info["fullname"],
            weblink=commander_info["weblink"],
        )
        reporter.participant.append(reporter_participant)
        db_session.add(reporter)
        participants.append(reporter_participant)

    participants.append(commander_participant)
    incident = Incident(
        title=title,
        description=description,
        status=status,
        incident_priority=incident_priority,
        incident_type=incident_type,
        participants=participants,
    )

    commander.participant.append(commander_participant)
    db_session.add(commander)
    db_session.add(incident)
    db_session.commit()
    return incident
Example #9
0
    def get(
        self,
        incident: Incident,
        db_session=None,
    ):
        """Fetches participants from Dispatch."""
        models = [
            (IndividualContact, IndividualContactRead),
            (Service, ServiceRead),
            (TeamContact, TeamContactRead),
        ]
        recommendation = route_service.get(db_session=db_session,
                                           incident=incident,
                                           models=models)

        log.debug(f"Recommendation: {recommendation}")

        individual_contacts = []
        team_contacts = []
        for match in recommendation.matches:
            if match.resource_type == TeamContact.__name__:
                team = team_service.get_or_create(
                    db_session=db_session,
                    email=match.resource_state["email"],
                    incident=incident)
                team_contacts.append(team)

            if match.resource_type == IndividualContact.__name__:
                individual = individual_service.get_or_create(
                    db_session=db_session,
                    email=match.resource_state["email"],
                    incident=incident)

                individual_contacts.append((individual, None))

            # we need to do more work when we have a service
            if match.resource_type == Service.__name__:
                plugin_instance = plugin_service.get_active_instance_by_slug(
                    db_session=db_session,
                    slug=match.resource_state["type"],
                    project_id=incident.project.id,
                )

                if plugin_instance:
                    if plugin_instance.enabled:
                        log.debug(
                            f"Resolving service contact. ServiceContact: {match.resource_state}"
                        )
                        # ensure that service is enabled
                        service = service_service.get_by_external_id_and_project_id(
                            db_session=db_session,
                            external_id=match.resource_state["external_id"],
                            project_id=incident.project_id,
                        )
                        if service.is_active:
                            individual_email = plugin_instance.instance.get(
                                match.resource_state["external_id"])

                            individual = individual_service.get_or_create(
                                db_session=db_session,
                                email=individual_email,
                                incident=incident)

                            individual_contacts.append(
                                (individual, match.resource_state["id"]))
                    else:
                        log.warning(
                            f"Skipping service contact. Service: {match.resource_state['name']} Reason: Associated service plugin not enabled."
                        )
                else:
                    log.warning(
                        f"Skipping service contact. Service: {match.resource_state['name']} Reason: Associated service plugin not found."
                    )

        db_session.commit()
        return individual_contacts, team_contacts