async def get_app_data(self, uuid, app_uuid, app_info=None): key_result = app_uuid + "&&" + self.only_id + "&&result" key_status = app_uuid + "&&" + self.only_id + "&&status" if self.input_app == app_uuid or self.webhook_app == app_uuid or self.timer_app == app_uuid: if redis.exists(key_result) == 0: result_data = await self.execute(app_uuid=app_uuid) result = str(result_data["result"]) redis.set(key_result, result, ex=60 * 1) redis.set(key_status, result_data["status"], ex=60 * 1) if self.input_app == app_uuid: await self.add_execute_logs(uuid=uuid, app_uuid=app_uuid, app_name="用户输入", result=result, status=result_data["status"], html=result) elif self.webhook_app == app_uuid: await self.add_execute_logs(uuid=uuid, app_uuid=app_uuid, app_name="WebHook", result=result, status=result_data["status"], html=result) elif self.timer_app == app_uuid: await self.add_execute_logs(uuid=uuid, app_uuid=app_uuid, app_name="定时器", result=result, status=result_data["status"], html=result) return result_data["status"], result else: return int(redis.get(key_status).decode()), redis.get(key_result).decode() else: if redis.exists(key_result) == 0: result_data = await self.execute(app_uuid=app_uuid, app_dir=app_info["app_dir"], data=app_info["data"]) result = str(result_data["result"]) redis.set(key_result, result, ex=60 * 1) redis.set(key_status, result_data["status"], ex=60 * 1) if str(result_data["html"]) == "": html_data = result else: html_data = result_data["html"] await self.add_execute_logs( uuid=uuid, app_uuid=app_uuid, app_name=app_info["data"]["node_name"], result=result, status=result_data["status"], html=html_data, args=result_data["args"] ) return result_data["status"], result else: return int(redis.get(key_status).decode()), redis.get(key_result).decode()
async def find_next_app(self, edges, next_app=None): num = 0 for r in edges: if next_app: edge_info_action = "" edge_info_if_else = "" edge_info_switch = False if r["id"] in self.flow_data: edge_info = self.flow_data[r["id"]] edge_info_action = edge_info["action"] edge_info_if_else = edge_info["ifelse"] edge_info_switch = edge_info["switch"] key = next_app + "&&" + self.only_id + "&&sum" if redis.exists(key) == 1: sum = redis.get(key) if str(r["source"]) == next_app: if num != int(sum): num = num + 1 else: return str(r["label"]), r["source"], r[ "target"], edge_info_switch, str(edge_info_action), str(edge_info_if_else) else: if str(r["source"]) == next_app: return str(r["label"]), r["source"], r[ "target"], edge_info_switch, str(edge_info_action), str(edge_info_if_else)
def _get_login_auth_code(phone): """ 使用手机号获得登录验证码 :param phone: :return: """ key = "%s:%d" % (app.config.get("MANAGER_LOGIN_MSG_CODE"), int(phone)) auth_count_key = "AUTH_COUNT_{0}:{1}".format( app.config.get("MANAGER_LOGIN_MSG_CODE"), int(phone)) if redis.exists(key): code = int(redis.get(key)) else: code = int(random.uniform(1000, 9999)) redis.set(key, code) redis.expire(key, 15 * 60) redis.set(auth_count_key, 0) redis.expire(auth_count_key, 15 * 60) return code
def before(): g.start_time = time.time() if request.content_type == 'application/json;charset=UTF-8': g.payload = request.json else: payload = {} for k, v in request.values.to_dict().items(): payload[k] = v g.payload = json.dumps(payload, ensure_ascii=False) if "api/v1" in request.path and request.path not in no_path: if request.method == "POST": token = request.headers.get("token") if token: from core import redis if redis.exists(token) == 0: return Response.re(ErrToken) else: return Response.re(ErrToken)
async def run(self, uuid): redis.incr("exec_sum") redis.incr(uuid + "&&exec_sum") workflow_info = Workflow.select( 'uuid', 'name', 'remarks', 'start_app', 'end_app', 'input_app', 'webhook_app', 'timer_app', 'flow_json', 'flow_data', 'controller_data', 'local_var_data', 'status' ).where( "uuid", uuid ).first() if workflow_info: if str(workflow_info.status) == "1": return False self.workflow_name = workflow_info.name self.workflow_remarks = workflow_info.remarks self.start_app = workflow_info.start_app self.end_app = workflow_info.end_app self.input_app = workflow_info.input_app self.webhook_app = workflow_info.webhook_app self.timer_app = workflow_info.timer_app self.flow_json = json.loads(workflow_info.flow_json) self.flow_data = json.loads(workflow_info.flow_data) await self.make_var(workflow_info.local_var_data, workflow_info.controller_data) target_app = await self.find_start_app(edges=self.flow_json["edges"], start_app=self.start_app) await self.add_execute_logs(uuid=uuid, app_uuid=self.start_app, app_name="开始", result="剧本开始执行", status=0, html="<span>剧本开始执行</span>") is_while = True while is_while: try: edge_name, source_app, next_app, is_switch, edge_action, edge_if_else = await self.find_next_app( edges=self.flow_json["edges"], next_app=target_app ) except Exception as e: await self.add_execute_logs(uuid=uuid, app_uuid="", app_name="", result="当前剧本不具有可执行条件", status=1, html="<span>当前剧本不具有可执行条件</span>") is_while = False break key = target_app + "&&" + self.only_id + "&&sum" if redis.exists(key) == 1: sum = redis.get(key) redis.set(key, int(sum) + 1, ex=60 * 1) else: redis.set(key, 1, ex=60 * 1) if self.input_app == source_app or self.webhook_app == source_app or self.timer_app == source_app: is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app) else: source_info = self.flow_data[source_app] is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app, app_info=source_info) if is_status == 0: if is_switch: if str(edge_action) == "1": is_arr = re.findall(r'\[\w*.+\]', edge_if_else) if len(is_arr) > 0: edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",") if if_else_result in edge_if_else_arr: target_app = next_app else: pass else: if edge_if_else == if_else_result: target_app = next_app else: pass elif str(edge_action) == "2": is_arr = re.findall(r'\[\w*.+\]', edge_if_else) if len(is_arr) > 0: edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",") if if_else_result not in edge_if_else_arr: target_app = next_app else: pass else: if edge_if_else != if_else_result: target_app = next_app else: pass elif str(edge_action) == "3": is_arr = re.findall(r'{0}'.format(edge_if_else), if_else_result) if len(is_arr) > 0: target_app = next_app else: pass else: target_app = next_app else: is_while = False if next_app == self.end_app: await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束", status=0, html="<span>剧本执行结束</span>") await self.add_report() is_while = False if int(redis.decr("exec_sum")) < 0: redis.set("exec_sum", "0") if int(redis.decr(uuid + "&&exec_sum")) < 0: redis.set(uuid + "&&exec_sum", "0") break
async def run(self, uuid): redis.incr("exec_sum") redis.incr(uuid + "&&exec_sum") workflow_info = Workflow.select( 'uuid', 'name', 'remarks', 'start_app', 'end_app', 'input_app', 'webhook_app', 'timer_app', 'flow_json', 'flow_data', 'controller_data', 'local_var_data', 'status' ).where( "uuid", uuid ).first() if workflow_info: if str(workflow_info.status) == "1": return False self.workflow_name = workflow_info.name self.workflow_remarks = workflow_info.remarks self.start_app = workflow_info.start_app self.end_app = workflow_info.end_app self.input_app = workflow_info.input_app self.webhook_app = workflow_info.webhook_app self.timer_app = workflow_info.timer_app self.flow_json = json.loads(workflow_info.flow_json) self.flow_data = json.loads(workflow_info.flow_data) await self.make_var(workflow_info.local_var_data, workflow_info.controller_data) target_app = await self.find_start_app(edges=self.flow_json["edges"], start_app=self.start_app) await self.add_execute_logs(uuid=uuid, app_uuid=self.start_app, app_name="开始", result="剧本开始执行", status=0, html="<span>剧本开始执行</span>") is_while = True while is_while: try: edge_name, source_app, next_app, is_switch, edge_action, edge_if_else = await self.find_next_app( edges=self.flow_json["edges"], next_app=target_app ) except Exception as e: await self.add_execute_logs(uuid=uuid, app_uuid="", app_name="", result="当前剧本不具有可执行条件", status=1, html="<span>当前剧本不具有可执行条件</span>") await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束", status=0, html="<span>剧本执行结束</span>") await self.decr_sum(uuid=uuid) is_while = False break key = target_app + "&&" + self.only_id + "&&sum" if redis.exists(key) == 1: sum = redis.get(key) redis.set(key, int(sum) + 1, ex=lose_time) else: redis.set(key, 1, ex=lose_time) if self.input_app == source_app or self.webhook_app == source_app or self.timer_app == source_app: is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app) else: source_info = self.flow_data[source_app] is_status, if_else_result = await self.get_app_data(uuid=uuid, app_uuid=source_app, app_info=source_info) if str(is_status) == "0": if is_switch: if str(edge_action) == "1": is_arr = re.findall(r'\[\w*.+\]', edge_if_else) if len(is_arr) > 0: edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",") if if_else_result in edge_if_else_arr: target_app = next_app else: pass else: if str(edge_if_else) == str(if_else_result): target_app = next_app else: pass elif str(edge_action) == "2": is_arr = re.findall(r'\[\w*.+\]', edge_if_else) if len(is_arr) > 0: edge_if_else_arr = str(is_arr[0]).replace("[", "").replace("]", "").split(",") if if_else_result not in edge_if_else_arr: target_app = next_app else: pass else: if str(edge_if_else) != str(if_else_result): target_app = next_app else: pass elif str(edge_action) == "3": is_arr = re.findall(r'{0}'.format(edge_if_else), if_else_result) if len(is_arr) > 0: target_app = next_app else: pass elif str(edge_action) == "4": is_json_arr = re.findall(r'\{\w.*\}!=\w.*', edge_if_else) is_json_arr_equal = re.findall(r'\{\w.*\}=\w.*', edge_if_else) if len(is_json_arr) == 1: is_equal = False is_json_arr = is_json_arr[0].split("!=") json_key = str(is_json_arr[0]) json_val = str(is_json_arr[1]) else: is_equal = True is_json_arr = is_json_arr_equal[0].split("=") json_key = str(is_json_arr[0]) json_val = str(is_json_arr[1]) if len(is_json_arr) > 0: edge_if_else_key = json_key.replace("{", "").replace("}", "") edge_if_else_arr = edge_if_else_key.split(".") is_json = self.is_json(json_text=if_else_result) if is_json is False: await self.add_execute_logs( uuid=uuid, app_uuid="", app_name="", result="非 JSON 格式变量", status=1, html="<span>非 JSON 格式变量</span>") await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束", status=0, html="<span>剧本执行结束</span>") await self.decr_sum(uuid=uuid) is_while = False break try: if_else_result = json.loads(json.loads(if_else_result)) except: if_else_result = json.loads(if_else_result) key_all = "if_else_result" key_front = "['" key_after = "']" key_number_front = "[" key_number_after = "]" for k in edge_if_else_arr: try: int(k) key_all += key_number_front + k + key_number_after except ValueError: k = str(k).replace("!!!", "") key_all += key_front + k + key_after try: if_else_result = eval(key_all) if is_equal: if str(if_else_result) == json_val: target_app = next_app else: pass else: if str(if_else_result) != json_val: target_app = next_app else: pass except IndexError: await self.add_execute_logs( uuid=uuid, app_uuid="", app_name="", result="未找到 JSON Index : {key}".format(key=edge_if_else_key), status=1, html="<span>未找到 JSON Index : {key}</span>".format(key=edge_if_else_key)) await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束", status=0, html="<span>剧本执行结束</span>") await self.decr_sum(uuid=uuid) is_while = False break except KeyError: await self.add_execute_logs( uuid=uuid, app_uuid="", app_name="", result="未找到 JSON KEY : {key}".format(key=edge_if_else_key), status=1, html="<span>未找到 JSON KEY : {key}</span>".format(key=edge_if_else_key)) await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束", status=0, html="<span>剧本执行结束</span>") await self.decr_sum(uuid=uuid) is_while = False break except TypeError: await self.add_execute_logs( uuid=uuid, app_uuid="", app_name="", result="JSON 格式不存在 : {key}".format(key=edge_if_else_key), status=1, html="<span>JSON 格式不存在 : {key}</span>".format(key=edge_if_else_key)) await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束", status=0, html="<span>剧本执行结束</span>") await self.decr_sum(uuid=uuid) is_while = False break else: target_app = next_app else: await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束", status=0, html="<span>剧本执行结束</span>") await self.decr_sum(uuid=uuid) is_while = False break if next_app == self.end_app: await self.add_execute_logs(uuid=uuid, app_uuid=self.end_app, app_name="结束", result="剧本执行结束", status=0, html="<span>剧本执行结束</span>") await self.add_report() await self.decr_sum(uuid=uuid) # redis.delete(*redis.keys(pattern='*{key}*'.format(key=self.only_id))) is_while = False break