Esempio n. 1
0
def filter_and_send(*, db_session, incident: Incident,
                    class_instance: Type[Base], notification_params: dict):
    """Sends notifications."""
    notifications = get_all_enabled(db_session=db_session,
                                    project_id=incident.project.id)
    for notification in notifications:
        for search_filter in notification.filters:
            match = search_filter_service.match(
                db_session=db_session,
                filter_spec=search_filter.expression,
                class_instance=class_instance,
            )
            if match:
                send(
                    db_session=db_session,
                    project_id=incident.project.id,
                    notification=notification,
                    notification_params=notification_params,
                )

        if not notification.filters:
            send(
                db_session=db_session,
                project_id=incident.project.id,
                notification=notification,
                notification_params=notification_params,
            )
Esempio n. 2
0
def get_resource_matches(*, db_session, incident: Incident,
                         model: Any) -> List[RecommendationMatch]:
    """Fetches all matching model entities for the given incident."""
    # get all entities with an associated filter
    model_cls, model_state = model
    resources = (db_session.query(model_cls).filter(
        model_cls.project_id == incident.project_id).filter(
            model_cls.filters.any()).all())

    matched_resources = []
    for resource in resources:
        for f in resource.filters:
            match = search_filter_service.match(
                db_session=db_session,
                filter_spec=f.expression,
                class_instance=incident,
            )

            if match:
                matched_resources.append(
                    RecommendationMatch(
                        resource_state=json.loads(
                            model_state(**resource.__dict__).json()),
                        resource_type=model_cls.__name__,
                    ))
                break

    return matched_resources
Esempio n. 3
0
def daily_report(db_session=None):
    """
    Creates and sends incident daily reports based on notifications.
    """
    for project in project_service.get_all(db_session=db_session):
        # we fetch all active, stable and closed incidents
        active_incidents = get_all_by_status(
            db_session=db_session,
            project_id=project.id,
            status=IncidentStatus.active.value)
        stable_incidents = get_all_last_x_hours_by_status(
            db_session=db_session,
            project_id=project.id,
            status=IncidentStatus.stable.value,
            hours=24,
        )
        closed_incidents = get_all_last_x_hours_by_status(
            db_session=db_session,
            project_id=project.id,
            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, project_id=project.id)
        for incident in incidents:
            for notification in notifications:
                for search_filter in notification.filters:
                    match = search_filter_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,
                    project_id=notification.project.id,
                    notification=notification,
                    notification_params=notification_params,
                )