def update_instance(*, db_session, plugin_instance: PluginInstance, plugin_instance_in: PluginInstanceUpdate) -> Plugin: """Updates a plugin instance.""" plugin_data = jsonable_encoder(plugin_instance) update_data = plugin_instance_in.dict(skip_defaults=True) if plugin_instance_in.enabled: # user wants to enable the plugin if not plugin_instance.plugin.multiple: # we can't have multiple plugins of this type disable the currently enabled one enabled_plugin_instances = get_enabled_instances_by_type( db_session=db_session, plugin_type=plugin_instance.plugin.type) if enabled_plugin_instances: enabled_plugin_instances[0].enabled = False db_session.add(enabled_plugin_instances[0]) if not plugin_instance_in.enabled: # user wants to disable the plugin if plugin_instance.plugin.type == OncallPlugin.type: oncall_services = service_service.get_all_by_type_and_status( db_session=db_session, service_type=plugin_instance.plugin.slug, is_active=True) if oncall_services: raise InvalidConfiguration( f"Cannot disable plugin instance: {plugin_instance.plugin.title}. One or more oncall services depend on it. " ) for field in plugin_data: if field in update_data: setattr(plugin_instance, field, update_data[field]) db_session.add(plugin_instance) db_session.commit() return plugin_instance
def update(*, db_session, plugin: Plugin, plugin_in: PluginUpdate) -> Plugin: """Updates a plugin.""" plugin_data = jsonable_encoder(plugin) update_data = plugin_in.dict(skip_defaults=True) if plugin_in.enabled: # user wants to enable the plugin if not plugin.multiple: # we can't have multiple plugins of this type disable the currently enabled one enabled_plugins = get_enabled_by_type(db_session=db_session, plugin_type=plugin.type) if enabled_plugins: enabled_plugins[0].enabled = False db_session.add(enabled_plugins[0]) if not plugin_in.enabled: # user wants to disable the plugin if plugin.required: enabled_plugins = get_enabled_by_type(db_session=db_session, plugin_type=plugin.type) if len(enabled_plugins) == 1: raise InvalidConfiguration( f"Cannot disable plugin: {plugin.title}. It is required and no other plugins of type {plugin.type} are enabled." ) for field in plugin_data: if field in update_data: setattr(plugin, field, update_data[field]) db_session.add(plugin) db_session.commit() return plugin
def update(*, db_session, service: Service, service_in: ServiceUpdate) -> Service: """Updates an existing service.""" service_data = jsonable_encoder(service) terms = [term_service.get_or_create(db_session=db_session, term_in=t) for t in service_in.terms] incident_priorities = [ incident_priority_service.get_by_name(db_session=db_session, name=n.name) for n in service_in.incident_priorities ] incident_types = [ incident_type_service.get_by_name(db_session=db_session, name=n.name) for n in service_in.incident_types ] update_data = service_in.dict( skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"} ) if service_in.is_active: # user wants to enable the service oncall_plugin = plugin_service.get_by_slug(db_session=db_session, slug=service_in.type) if not oncall_plugin.enabled: raise InvalidConfiguration( f"Cannot enable service: {service.name}. Its associated plugin {oncall_plugin.title} is not enabled." ) for field in service_data: if field in update_data: setattr(service, field, update_data[field]) service.terms = terms service.incident_priorities = incident_priorities service.incident_types = incident_types db_session.add(service) db_session.commit() return service
def update(*, db_session, service: Service, service_in: ServiceUpdate) -> Service: """Updates an existing service.""" service_data = jsonable_encoder(service) update_data = service_in.dict(skip_defaults=True, exclude={"filters"}) filters = [ search_filter_service.get(db_session=db_session, search_filter_id=f.id) for f in service_in.filters ] if service_in.is_active: # user wants to enable the service oncall_plugin_instance = plugin_service.get_active_instance_by_slug( db_session=db_session, slug=service_in.type, project_id=service.project.id) if not oncall_plugin_instance.enabled: raise InvalidConfiguration( f"Cannot enable service: {service.name}. Its associated plugin {oncall_plugin_instance.plugin.title} is not enabled." ) for field in service_data: if field in update_data: setattr(service, field, update_data[field]) service.filters = filters db_session.add(service) db_session.commit() return service
def create_executive_report( user_email: str, incident_id: int, executive_report_in: ExecutiveReportCreate, db_session=None, ): """Creates an executive report.""" report_template = document_service.get_executive_report_template( db_session=db_session) current_date = date.today().strftime("%B %d, %Y") current_status = executive_report_in.current_status overview = executive_report_in.overview next_steps = executive_report_in.next_steps # we load the incident instance incident = incident_service.get(db_session=db_session, incident_id=incident_id) if not report_template: raise InvalidConfiguration("No executive report template defined.") # we fetch all previous executive reports executive_reports = get_all_by_incident_id_and_type( db_session=db_session, incident_id=incident_id, report_type=ReportTypes.executive_report) previous_executive_reports = [] for executive_report in executive_reports: previous_executive_reports.append( f"{executive_report.document.name} - {executive_report.document.weblink}\n" ) # we create a new executive report details = { "current_status": current_status, "overview": overview, "next_steps": next_steps } executive_report_in = ReportCreate( details=details, type=ReportTypes.executive_report, ) executive_report = create(db_session=db_session, report_in=executive_report_in) # we load the participant participant = participant_service.get_by_incident_id_and_email( db_session=db_session, incident_id=incident_id, email=user_email) # we save the executive report participant.reports.append(executive_report) incident.reports.append(executive_report) db_session.add(participant) db_session.add(incident) db_session.commit() event_service.log( db_session=db_session, source="Incident Participant", description= f"{participant.individual.name} created a new executive report", details={ "current_status": current_status, "overview": overview, "next_steps": next_steps }, incident_id=incident_id, individual_id=participant.individual.id, ) # we create a new document for the executive report storage_plugin = plugin_service.get_active_instance( db_session=db_session, project_id=incident.project.id, plugin_type="storage") executive_report_document_name = f"{incident.name} - Executive Report - {current_date}" executive_report_document = storage_plugin.instance.copy_file( folder_id=incident.storage.resource_id, file_id=report_template.resource_id, name=executive_report_document_name, ) executive_report_document.update({ "name": executive_report_document_name, "resource_type": INCIDENT_RESOURCE_EXECUTIVE_REPORT_DOCUMENT, }) storage_plugin.instance.move_file( new_folder_id=incident.storage.resource_id, file_id=executive_report_document["id"]) event_service.log( db_session=db_session, source=storage_plugin.plugin.title, description="Executive report document added to storage", incident_id=incident.id, ) document_in = DocumentCreate( name=executive_report_document["name"], resource_id=executive_report_document["id"], resource_type=executive_report_document["resource_type"], project=incident.project, weblink=executive_report_document["weblink"], ) executive_report.document = document_service.create( db_session=db_session, document_in=document_in) incident.documents.append(executive_report.document) db_session.add(executive_report) db_session.add(incident) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description="Executive report document added to incident", incident_id=incident.id, ) # we update the incident update document document_plugin = plugin_service.get_active_instance( db_session=db_session, project_id=incident.project.id, plugin_type="document") document_plugin.instance.update( executive_report_document["id"], name=incident.name, title=incident.title, current_date=current_date, current_status=current_status, overview=overview, next_steps=next_steps, previous_reports="\n".join(previous_executive_reports), commander_fullname=incident.commander.individual.name, commander_team=incident.commander.team, commander_weblink=incident.commander.individual.weblink, ) # we send the executive report to the notifications group send_executive_report_to_notifications_group(incident.id, executive_report, db_session) return executive_report