def sync_tags(db_session=None): """Syncs tags from external sources.""" for p in plugins.all(plugin_type="tag"): log.debug(f"Getting tags via: {p.slug}") for t in p.get(): log.debug(f"Adding Tag. Tag: {t}") tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))
def update(*, db_session, incident: Incident, incident_in: IncidentUpdate) -> Incident: """Updates an existing incident.""" incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_in.incident_priority.name) incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_in.incident_type.name) tags = [] for t in incident_in.tags: tags.append( tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) terms = [] for t in incident_in.terms: terms.append( term_service.get_or_create(db_session=db_session, term_in=TermUpdate(**t))) duplicates = [] for d in incident_in.duplicates: duplicates.append(get(db_session=db_session, incident_id=d.id)) update_data = incident_in.dict( skip_defaults=True, exclude={ "incident_type", "incident_priority", "commander", "reporter", "status", "visibility", "tags", "terms", "duplicates", }, ) for field in update_data.keys(): setattr(incident, field, update_data[field]) incident.terms = terms incident.tags = tags incident.duplicates = duplicates incident.status = incident_in.status incident.visibility = incident_in.visibility incident.incident_priority = incident_priority incident.incident_type = incident_type db_session.add(incident) db_session.commit() return incident
def sync_tags(db_session=None): """Syncs tags from external sources.""" for p in plugin_service.get_all(db_session=db_session): if p.type != "tag": continue log.debug(f"Getting tags via: {p.slug}") for t in p.instance.get(): log.debug(f"Adding Tag. Tag: {t}") # we always use the plugin project when syncing project = p.project.__dict__ t["tag_type"].update({"project": project}) tag_in = TagCreate(**t, project=project) tag_service.get_or_create(db_session=db_session, tag_in=tag_in)
def sync_tags(db_session=None): """Syncs tags from external sources.""" for p in plugins.all(plugin_type="tag"): log.debug(f"Getting tags via: {p.slug}") for app in p.get(): log.debug(f"Adding Tag. Tag: {app}") tag_in = TagCreate(**app) tag = tag_service.get_by_name(db_session=db_session, name=tag_in.name) if tag: tag_service.update(db_session=db_session, tag=tag, tag_in=tag_in) else: tag_service.create(db_session=db_session, tag_in=tag_in)
def sync_tags(db_session=None): """Syncs tags from external sources.""" for project in project_service.get_all(db_session=db_session): plugin = plugin_service.get_active_instance( db_session=db_session, plugin_type="tag", project_id=project.id ) if not plugin: continue log.debug(f"Getting tags via: {plugin.plugin.slug}") for t in plugin.instance.get(): log.debug(f"Adding Tag. Tag: {t}") # we always use the plugin project when syncing t["tag_type"].update({"project": project}) tag_in = TagCreate(**t, project=project) tag_service.get_or_create(db_session=db_session, tag_in=tag_in)
def sync_tags(db_session: SessionLocal, project: Project): """Syncs tags from external sources.""" plugin = plugin_service.get_active_instance(db_session=db_session, plugin_type="tag", project_id=project.id) if not plugin: log.debug( f"No active plugins were found. PluginType: 'tag' ProjectId: {project.id}" ) return log.debug(f"Getting tags via: {plugin.plugin.slug}") for t in plugin.instance.get(): log.debug(f"Adding Tag. Tag: {t}") # we always use the plugin project when syncing t["tag_type"].update({"project": project}) tag_in = TagCreate(**t, project=project) tag_service.get_or_create(db_session=db_session, tag_in=tag_in)
def test_create(session, tag_type, project): from dispatch.tag.service import create from dispatch.tag.models import TagCreate name = "name" description = "description" uri = "https://www.example.com/" source = "dispatch" discoverable = True tag_in = TagCreate( name=name, description=description, uri=uri, source=source, discoverable=discoverable, tag_type=tag_type, project=project, ) tag = create(db_session=session, tag_in=tag_in) assert tag
def create( *, db_session, incident_priority: str, incident_type: str, reporter_email: str, title: str, status: str, description: str, tags: List[dict], visibility: str = None, ) -> Incident: """Creates a new incident.""" # We get the incident type by name if not incident_type: incident_type = incident_type_service.get_default(db_session=db_session) if not incident_type: raise Exception("No incident type specified and no default has been defined.") else: incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_type["name"] ) # We get the incident priority by name if not incident_priority: incident_priority = incident_priority_service.get_default(db_session=db_session) if not incident_priority: raise Exception("No incident priority specified and no default has been defined.") else: incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_priority["name"] ) if not visibility: visibility = incident_type.visibility tag_objs = [] for t in tags: tag_objs.append(tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) # We create the incident incident = Incident( title=title, description=description, status=status, incident_type=incident_type, incident_priority=incident_priority, visibility=visibility, tags=tag_objs, ) db_session.add(incident) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description="Incident created", incident_id=incident.id, ) # Add other incident roles (e.g. commander and liaison) assign_incident_role(db_session, incident, reporter_email, ParticipantRoleType.reporter) assign_incident_role( db_session, incident, reporter_email, ParticipantRoleType.incident_commander ) assign_incident_role(db_session, incident, reporter_email, ParticipantRoleType.liaison) return incident
def create( *, db_session, incident_priority: str, incident_type: str, reporter_email: str, title: str, status: str, description: str, tags: List[dict], visibility: str = None, ) -> Incident: """Creates a new incident.""" # We get the incident type by name incident_type = incident_type_service.get_by_name( db_session=db_session, name=incident_type["name"]) # We get the incident priority by name incident_priority = incident_priority_service.get_by_name( db_session=db_session, name=incident_priority["name"]) if not visibility: visibility = incident_type.visibility tag_objs = [] for t in tags: tag_objs.append( tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) # We create the incident incident = Incident( title=title, description=description, status=status, incident_type=incident_type, incident_priority=incident_priority, visibility=visibility, tags=tag_objs, ) db_session.add(incident) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description="Incident created", incident_id=incident.id, ) # We add the reporter to the incident reporter_participant = participant_flows.add_participant( reporter_email, incident.id, db_session, ParticipantRoleType.reporter) # We resolve the incident commander email incident_commander_email = resolve_incident_commander_email( db_session, reporter_email, incident_type.name, "", title, description, incident_priority.page_commander, ) if reporter_email == incident_commander_email: # We add the role of incident commander the reporter participant_role_service.add_role( participant_id=reporter_participant.id, participant_role=ParticipantRoleType.incident_commander, db_session=db_session, ) else: # We create a new participant for the incident commander and we add it to the incident participant_flows.add_participant( incident_commander_email, incident.id, db_session, ParticipantRoleType.incident_commander, ) return incident
def create(*, db_session, incident_in: IncidentCreate) -> Incident: """Creates a new incident.""" project = project_service.get_by_name_or_default( db_session=db_session, project_in=incident_in.project) incident_type = incident_type_service.get_by_name_or_default( db_session=db_session, project_id=project.id, incident_type_in=incident_in.incident_type) incident_priority = incident_priority_service.get_by_name_or_default( db_session=db_session, project_id=project.id, incident_priority_in=incident_in.incident_priority, ) if not incident_in.visibility: visibility = incident_type.visibility else: visibility = incident_in.visibility tag_objs = [] for t in incident_in.tags: tag_objs.append( tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) # We create the incident incident = Incident( title=incident_in.title, description=incident_in.description, status=incident_in.status, incident_type=incident_type, incident_priority=incident_priority, visibility=visibility, tags=tag_objs, project=project, ) db_session.add(incident) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description="Incident created", incident_id=incident.id, ) # Add other incident roles (e.g. commander and liaison) assign_incident_role(db_session, incident, incident_in.reporter.individual.email, ParticipantRoleType.reporter) assign_incident_role( db_session, incident, incident_in.reporter.individual.email, ParticipantRoleType.incident_commander, ) assign_incident_role(db_session, incident, incident_in.reporter.individual.email, ParticipantRoleType.liaison) return incident
def create( *, db_session, # job_priority: str = None, # job_type: str, code: str, job_type: str = "visit", org_id: str = None, org_code: str = None, name: str = None, planning_status: str, tags: List[dict] = [], description: str = None, team: TeamCreate, location: LocationCreate, flex_form_data: dict = None, requested_primary_worker: WorkerCreate = None, requested_start_datetime: datetime = None, requested_duration_minutes: float = None, scheduled_primary_worker: WorkerCreate = None, scheduled_secondary_workers: List[WorkerCreate] = [], scheduled_start_datetime: datetime = None, scheduled_duration_minutes: float = None, auto_planning: bool = True, requested_skills: List[str] = [], requested_items: List[str] = [], life_cycle_status: JobLifeCycleUpdate = None ) -> Job: """Creates a new job.""" tag_objs = [] for t in tags: tag_objs.append(tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) team_obj = team_service.get_by_code(db_session=db_session, code=team["code"]) location_obj = location_service.get_by_location_code( db_session=db_session, location_code=location["location_code"]) if location_obj is None: loc_to_create = LocationCreate(**location) loc_to_create.org_id = org_id if loc_to_create.geo_address_text and not loc_to_create.geo_latitude: try: nid = SHORTUUID.random(length=9) location_config = { "url": config.LOCATION_SERVICE_URL, "token": config.LOCATION_SERVICE_TOKEN, "request_method": config.LOCATION_SERVICE_REQUEST_METHOD, } payload = {"address_id": nid, "input_address": loc_to_create.geo_address_text } # get location service location_plug = service_plugin_service.get_by_service_id_and_type( db_session=db_session, service_id=team_obj.service_id, service_plugin_type=KandboxPlannerPluginType.kandbox_location_service, ).all() if location_plug: location_plugin = plugins.get(location_plug[0].plugin.slug) location_adapter_service = location_plugin(config=location_config) status, _location_ret, msg = location_adapter_service.get_pldt_location(payload) if status: if isinstance(_location_ret['latitude'], float): if _check_location(_location_ret, team_obj.flex_form_data): loc_to_create.geo_latitude = _location_ret['latitude'] loc_to_create.geo_longitude = _location_ret['longitude'] loc_to_create.id = _location_ret['location_id'] loc_to_create.location_code = _location_ret['location_code'] else: logService.create(db_session=db_session, log_in=LogCreate( title='Location Response Data OutSide', category='Location', content=f"location outside,job code:{code},input_address:{payload['input_address']}, msg:{str(_location_ret)}", org_id=int(org_id), team_id=team_obj.id)) log.error( f"Location Response Data OutSide ,{msg} :{payload['input_address']}") else: logService.create(db_session=db_session, log_in=LogCreate( title='Location Response Data NUll', category='Location', content=f"job code:{code},input_address:{payload['input_address']},msg:{str(_location_ret)}", org_id=int(org_id), team_id=team_obj.id)) log.error( f"Location Response Data NUll ,{msg} :{payload['input_address']}") else: logService.create(db_session=db_session, log_in=LogCreate( title=msg['type'], category='Location', content=f"job code:{code},input_address:{payload['input_address']},msg:{str(msg['msg'])}", org_id=int(org_id), team_id=team_obj.id)) log.error( f"Location Response failed ,{msg} :{payload['input_address']}") else: log.error( f"not find location plug,service:{team_obj.service_id},{KandboxPlannerPluginType.kandbox_location_service}") except Exception as e: print(traceback.format_exc()) log.error(f"address request error:{loc_to_create.geo_address_text},{ e} ") location_obj = location_service.get_or_create_by_code( db_session=db_session, location_in=loc_to_create) # if location_obj.geo_longitude < 1: # location_obj.geo_longitude = loc_to_create.geo_longitude # location_obj.geo_latitude = loc_to_create.geo_latitude db_session.add(location_obj) # location_obj = location_service.update( # db_session=db_session, location=location_obj, location_in=LocationUpdate(**location) # ) if requested_primary_worker: requested_primary_worker = worker_service.get_by_code( db_session=db_session, code=requested_primary_worker["code"]) if scheduled_primary_worker: scheduled_primary_worker = worker_service.get_by_code( db_session=db_session, code=scheduled_primary_worker["code"]) scheduled_secondary_workers_list = [] if scheduled_secondary_workers is not None: for w in scheduled_secondary_workers: scheduled_secondary_workers_list.append( worker_service.get_by_code(db_session=db_session, code=w['code'])) # We create the job if requested_skills: flex_form_data['requested_skills'] = requested_skills if requested_items: flex_form_data['requested_items'] = requested_items job = Job( code=code, name=name, org_id=org_id, job_type=job_type, description=description, planning_status=planning_status, tags=tag_objs, flex_form_data=flex_form_data, location=location_obj, team=team_obj, requested_start_datetime=requested_start_datetime, requested_duration_minutes=requested_duration_minutes, requested_primary_worker=requested_primary_worker, scheduled_start_datetime=scheduled_start_datetime, scheduled_duration_minutes=scheduled_duration_minutes, scheduled_primary_worker=scheduled_primary_worker, scheduled_secondary_workers=scheduled_secondary_workers_list, auto_planning=auto_planning, requested_skills=requested_skills, requested_items=requested_items, ) db_session.add(job) if job.job_type == JobType.REPLENISH: depot_code = flex_form_data["depot_code"] depot = depot_service.get_by_code(db_session=db_session, code=depot_code) for item_str in flex_form_data["requested_items"]: item_list = parse_item_str(item_str) item = item_service.get_by_code(db_session=db_session, code=item_list[0]) inv = inventory_service.get_by_item_depot( db_session=db_session, item_id=item.id, depot_id=depot.id, org_id=team_obj.org_id ).one_or_none() inv.curr_qty -= item_list[1] inv.allocated_qty += item_list[1] if inv.curr_qty < 0: log.error( f" Not enough inventory for item: {item_list[0]}, depot: {depot_code}, org.id: {team_obj.org_id}") continue db_session.add(inv) inventory_event_service.log( db_session=db_session, source="Env_Replenish", description=f"Allocated {item_list[1]} {item_list[0]} from depot: {depot_code}", item_code=item_list[0], depot_code=depot_code, item_id=item.id, depot_id=depot.id ) db_session.commit() print(f"\033[37;46m\t1:add job succeed,{code}\033[0m") log.info(f"1:add job succeed,{code}") event_service.log( db_session=db_session, source="Dispatch Core App", description=f"Job ({code}) is created, planning_status={planning_status}, requested_start_datetime={requested_start_datetime}", job_id=job.id, ) post_job_to_kafka(job=job, message_type=KafkaMessageType.CREATE_JOB, db_session=db_session, org_code=org_code) print(f"\033[37;46m\t2:job post kafka in succeed,{code}\033[0m") log.info(f"2:job post kafka in succeed,{code}") # zulip send message if job.planning_status != JobPlanningStatus.UNPLANNED: zulip_dict = get_zulip_client_by_org_id(job.org_id) if zulip_dict: zulip_core = zulip_dict['client'] zulip_core.update_job_send_message( job, [job.scheduled_primary_worker] + job.scheduled_secondary_workers) return job
def create( *, db_session, # job_priority: str = None, # job_type: str, code: str, name: str = None, planning_status: str, tags: List[dict], description: str = None, team: TeamCreate, location: LocationCreate, flex_form_data: dict = None, requested_primary_worker: WorkerCreate = None, requested_start_datetime: datetime = None, requested_duration_minutes: float = None, scheduled_primary_worker: WorkerCreate = None, scheduled_secondary_workers: List[WorkerCreate], scheduled_start_datetime: datetime = None, scheduled_duration_minutes: float = None, auto_planning: bool = True, ) -> Job: """Creates a new job.""" tag_objs = [] for t in tags: tag_objs.append( tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))) team_obj = team_service.get_by_code(db_session=db_session, code=team["code"]) loc_to_create = LocationCreate(**location) location_obj = location_service.get_or_create_by_code( db_session=db_session, location_in=loc_to_create) # if location_obj.geo_longitude < 1: # location_obj.geo_longitude = loc_to_create.geo_longitude # location_obj.geo_latitude = loc_to_create.geo_latitude db_session.add(location_obj) # location_obj = location_service.update( # db_session=db_session, location=location_obj, location_in=LocationUpdate(**location) # ) if requested_primary_worker: requested_primary_worker = worker_service.get_by_code( db_session=db_session, code=requested_primary_worker["code"]) if scheduled_primary_worker: scheduled_primary_worker = worker_service.get_by_code( db_session=db_session, code=scheduled_primary_worker["code"]) if scheduled_secondary_workers is None: scheduled_secondary_workers = [] # We create the job job = Job( code=code, name=name, description=description, planning_status=planning_status, tags=tag_objs, flex_form_data=flex_form_data, location=location_obj, team=team_obj, requested_start_datetime=requested_start_datetime, requested_duration_minutes=requested_duration_minutes, requested_primary_worker=requested_primary_worker, scheduled_start_datetime=scheduled_start_datetime, scheduled_duration_minutes=scheduled_duration_minutes, scheduled_primary_worker=scheduled_primary_worker, scheduled_secondary_workers=scheduled_secondary_workers, auto_planning=auto_planning, ) db_session.add(job) db_session.commit() event_service.log( db_session=db_session, source="Dispatch Core App", description= f"Job ({code}) is created, planning_status={planning_status}, scheduled_start_datetime={scheduled_start_datetime}", job_id=job.id, ) post_job_to_kafka(job=job, message_type=KafkaMessageType.CREATE_JOB, db_session=db_session) return job