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) 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=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, )
def send(*, db_session, class_instance: Type[Base], notification_params: dict): """Sends notifications.""" notifications = get_all_enabled(db_session=db_session) 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=class_instance, ) if match: plugin = plugin_service.get_active( db_session=db_session, plugin_type=notification.type) if plugin: plugin.instance.send( notification.target, notification_params["text"], notification_params["template"], notification_params["type"], **notification_params["kwargs"], ) else: log.warning( f"Notification {notification.name} not sent. No {notification.type} plugin is active." )
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, )