def timer_transition(ticket_id, state_id, date_time, transition_id): """ 定时器流转 :param ticket_id: :param state_id: :param date_time: :param transition_id: :return: """ # 需要满足工单此状态后续无其他操作才自动流转 # 查询该工单此状态所有操作 flow_log_set, msg = TicketBaseService().get_ticket_flow_log(ticket_id, 'loonrobot', per_page=1000) for flow_log in flow_log_set: if flow_log.get('state').get('state_id') == state_id and flow_log.get('gmt_created') > date_time: return True, '后续有操作,定时器失效' # 执行流转 handle_ticket_data = dict(transition_id=transition_id, username='******', suggestion='定时器流转') TicketBaseService().handle_ticket(ticket_id, handle_ticket_data, True)
def post(self, request, *args, **kwargs): """ 工单hook回调,用于hoot请求后,被请求方执行完任务后回调loonflow,以触发工单继续流转 :param request: :param args: :param kwargs: :return: """ ticket_id = kwargs.get('ticket_id') json_str = request.body.decode('utf-8') if not json_str: return api_response(-1, 'post参数为空', {}) request_data_dict = json.loads(json_str) # {"result":true, "msg":"", field_value:{"xx":1,"bb":2}} app_name = request.META.get('HTTP_APPNAME') result, msg = TicketBaseService().hook_call_back(ticket_id, app_name, request_data_dict) if result: code, msg, data = 0, 'add ticket comment successful', '' else: code, msg, data = -1, msg, '' return api_response(code, msg, data)
def flow_hook_task(ticket_id): """ hook 任务 :param ticket_id: :return: """ # 查询工单状态 ticket_obj = TicketRecord.objects.filter(id=ticket_id, is_deleted=0).first() state_id = ticket_obj.state_id state_obj = State.objects.filter(id=state_id, is_deleted=0).first() participant_type_id = state_obj.participant_type_id if participant_type_id != CONSTANT_SERVICE.PARTICIPANT_TYPE_HOOK: return False, '' hook_config = state_obj.participant hook_config_dict = json.loads(hook_config) hook_url = hook_config_dict.get('hook_url') hook_token = hook_config_dict.get('hook_token') wait = hook_config_dict.get('wait') flag, msg = CommonService().gen_hook_signature(hook_token) if not flag: return False, msg all_ticket_data, msg = TicketBaseService().get_ticket_all_field_value( ticket_id) r = requests.post(hook_url, headers=msg, json=all_ticket_data, timeout=10) result = r.json() if result.get('code') == 0: # 调用成功 if wait: # date等格式需要转换为str for key, value in all_ticket_data.items(): if type(value) not in [int, str, bool, float]: all_ticket_data[key] = str(all_ticket_data[key]) all_ticket_data_json = json.dumps(all_ticket_data) TicketBaseService().add_ticket_flow_log( dict( ticket_id=ticket_id, transition_id=0, suggestion=result.get('msg'), participant_type_id=CONSTANT_SERVICE.PARTICIPANT_TYPE_HOOK, participant='hook', state_id=state_id, ticket_data=all_ticket_data_json, creator='loonrobot')) return True, '' else: # 不等待hook目标回调,直接流转 transition_queryset, msg = WorkflowTransitionService( ).get_state_transition_queryset(state_id) transition_id = transition_queryset[0] # hook状态只支持一个流转 new_request_dict = {} new_request_dict.update({ 'transition_id': transition_id, 'suggestion': msg, 'username': '******' }) # 执行流转 flag, msg = TicketBaseService().handle_ticket(ticket_id, new_request_dict, by_hook=True) if not flag: return False, msg else: TicketBaseService().update_ticket_field_value( {'script_run_last_result': False}) all_ticket_data, msg = TicketBaseService().get_ticket_all_field_value( ticket_id) # date等格式需要转换为str for key, value in all_ticket_data.items(): if type(value) not in [int, str, bool, float]: all_ticket_data[key] = str(all_ticket_data[key]) all_ticket_data_json = json.dumps(all_ticket_data) TicketBaseService().add_ticket_flow_log( dict(ticket_id=ticket_id, transition_id=0, suggestion=result.get('msg'), participant_type_id=CONSTANT_SERVICE.PARTICIPANT_TYPE_HOOK, participant='hook', state_id=state_id, ticket_data=all_ticket_data_json, creator='loonrobot'))