def test_delete(session, notification): from dispatch.notification.service import delete, get delete(db_session=session, notification_id=notification.id) assert not get(db_session=session, notification_id=notification.id)
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, )
def test_get(session, notification): from dispatch.notification.service import get t_notification = get(db_session=session, notification_id=notification.id) assert t_notification.id == notification.id