Przeglądaj źródła

定时job调用任务流程

ZhuDeKang 4 miesięcy temu
rodzic
commit
36a8465a47

+ 65 - 17
ruoyi-admin/src/test/java/com/ruoyi/TinyflowTest.java

@@ -11,6 +11,7 @@ import com.agentsflex.core.chain.listener.ChainEventListener;
 import com.agentsflex.core.chain.listener.ChainOutputListener;
 import com.agentsflex.llm.openai.OpenAILlm;
 import com.ruoyi.interfaces.tinyflow.parser.PrintNodeParser;
+import com.ruoyi.interfaces.tinyflow.parser.ServiceNodeParser;
 import dev.tinyflow.core.Tinyflow;
 
 import java.util.HashMap;
@@ -31,7 +32,7 @@ public class TinyflowTest {
             "                \"expand\": true,\n" +
             "                \"parameters\": [\n" +
             "                    {\n" +
-            "                        \"name\": \"inputParam1\",\n" +
+            "                        \"name\": \"year\",\n" +
             "                        \"dataType\": \"String\",\n" +
             "                        \"refType\": \"input\",\n" +
             "                        \"required\": true,\n" +
@@ -60,25 +61,71 @@ public class TinyflowTest {
             "        {\n" +
             "            \"id\": \"2\",\n" +
             "            \"data\": {\n" +
-            "                \"title\": \"打印节点\",\n" +
+            "                \"title\": \"服务节点\",\n" +
             "                \"parameters\": [\n" +
             "                    {\n" +
-            "                        \"name\": \"message\",\n" +
-            "                        \"value\": \"Hello value\",\n" +
+            "                        \"name\": \"year\",\n" +
             "                        \"dataType\": \"String\",\n" +
-            "                        \"defaultValue\": \"Hello defaultValue\",\n" +
-            "                        \"refType\": \"fixed\"\n" +
-            "                    },\n" +
-            "                    {\n" +
-            "                        \"name\": \"input1\",\n" +
-            "                        \"dataType\": \"String\",\n" +
-            "                        \"defaultValue\": \"Hello defaultValue\",\n" +
-            "                        \"ref\": \"1.inputParam1\",\n" +
+            "                        \"ref\": \"1.year\",\n" +
             "                        \"refType\": \"ref\"\n" +
             "                    }\n" +
-            "                ]\n" +
+            "                ],\n" +
+            "                \"service\": {\n" +
+            "                    \"srvId\": \"634ae1e0ee78e00bd609b85514961373\",\n" +
+            "                    \"mdid\": \"e4dc50dd-c4a9-4639-b43e-62e48314a7e0\",\n" +
+            "                    \"cateCode\": \"S\",\n" +
+            "                    \"name\": \"获取台风信息列表\",\n" +
+            "                    \"intro\": \"获取有台风的编号及名称\",\n" +
+            "                    \"serviceSource\": null,\n" +
+            "                    \"serviceDept\": null,\n" +
+            "                    \"manageName\": null,\n" +
+            "                    \"developUnit\": null,\n" +
+            "                    \"developContacter\": null,\n" +
+            "                    \"maintainUnit\": null,\n" +
+            "                    \"maintainContacer\": null,\n" +
+            "                    \"upCycl\": null,\n" +
+            "                    \"openCndtn\": null,\n" +
+            "                    \"adName\": null,\n" +
+            "                    \"dataField\": null,\n" +
+            "                    \"dataRange\": null,\n" +
+            "                    \"userId\": null,\n" +
+            "                    \"status\": null,\n" +
+            "                    \"note\": null,\n" +
+            "                    \"type\": \"HTTP\",\n" +
+            "                    \"url\": \"http://49.4.2.185:2111/RiverStrongAPI2.0/StormSurgeForecast/Typhoon/GetInfos\",\n" +
+            "                    \"rqtype\": \"POST\",\n" +
+            "                    \"rptype\": \"1\",\n" +
+            "                    \"rpcontent\": \"{\\n  \\\"data\\\": [\\n        {\\n            \\\"typhoonId\\\": \\\"202317\\\",\\n            \\\"typhoonName\\\": \\\"杰拉华\\\",\\n           \\\"test\\\": \\\"1123\\\"\\n        },\\n        {\\n            \\\"typhoonId\\\": \\\"202316\\\",\\n            \\\"typhoonName\\\": \\\"三巴\\\",\\n           \\\"test\\\": \\\"1123\\\"\\n           \\n        }\\n    ],\\n    \\\"succeeded\\\": true,\\n    \\\"statusCode\\\": 200,\\n    \\\"remark\\\": \\\"获取该年台风信息成功\\\"\\n}\",\n" +
+            "                    \"proxyAddress\": null,\n" +
+            "                    \"proxyPath\": null,\n" +
+            "                    \"example\": \"\",\n" +
+            "                    \"cnt\": null,\n" +
+            "                    \"viewNum\": 0,\n" +
+            "                    \"applyNum\": null,\n" +
+            "                    \"dataNum\": null,\n" +
+            "                    \"tm\": null,\n" +
+            "                    \"uptm\": null,\n" +
+            "                    \"rlstm\": null,\n" +
+            "                    \"releaseTime\": null,\n" +
+            "                    \"attentionId\": null,\n" +
+            "                    \"params\": [\n" +
+            "                        {\n" +
+            "                            \"srvId\": \"634ae1e0ee78e00bd609b85514961373\",\n" +
+            "                            \"paramCode\": \"year\",\n" +
+            "                            \"paramName\": \"年份\",\n" +
+            "                            \"paramType\": \"int\",\n" +
+            "                            \"paramValue\": \"2023\",\n" +
+            "                            \"paramFormat\": null,\n" +
+            "                            \"paramNote\": \"\",\n" +
+            "                            \"sort\": 0,\n" +
+            "                            \"paramObject\": 2023\n" +
+            "                        }\n" +
+            "                    ],\n" +
+            "                    \"sort\": null,\n" +
+            "                    \"audit\": null\n" +
+            "                }\n" +
             "            },\n" +
-            "            \"type\": \"printNode\",\n" +
+            "            \"type\": \"serviceNode\",\n" +
             "            \"position\": {\n" +
             "                \"x\": 600,\n" +
             "                \"y\": 50\n" +
@@ -99,8 +146,8 @@ public class TinyflowTest {
             "                \"outputDefs\": [\n" +
             "                    {\n" +
             "                        \"name\": \"data\",\n" +
-            "                        \"dataType\": \"String\",\n" +
-            "                        \"ref\": \"2.msg\",\n" +
+            "                        \"dataType\": \"Object\",\n" +
+            "                        \"ref\": \"2.result\",\n" +
             "                        \"refType\": \"ref\"\n" +
             "                    }\n" +
             "                ]\n" +
@@ -150,10 +197,11 @@ public class TinyflowTest {
         System.out.println(data1);
         Tinyflow tinyflow = new Tinyflow(data1);
         tinyflow.getChainParser().addNodeParser("printNode",new PrintNodeParser());
+        tinyflow.getChainParser().addNodeParser("serviceNode",new ServiceNodeParser());
 //        tinyflow.setLlmProvider(id -> OpenAILlm.of(""));
 
         Map<String, Object> variables = new HashMap<>();
-        variables.put("inputParam1", "开始节点引入1");
+        variables.put("year", "2024");
         variables.put("inputParam2", 2);
 
         Chain chain = tinyflow.toChain();

+ 1 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/snailJob/TestJob.java

@@ -10,6 +10,7 @@ import org.springframework.stereotype.Component;
 public class TestJob {
     @JobExecutor(name = "testJob")
     public ExecuteResult job(JobArgs jobArgs) {
+        System.out.println(jobArgs.toString());
         SnailJobLog.REMOTE.info("=======调用成功========");
         SnailJobLog.REMOTE.info("=======调用成功========");
         SnailJobLog.REMOTE.info("=======调用成功========");

+ 34 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/snailJob/flowJob.java

@@ -0,0 +1,34 @@
+package com.ruoyi.interfaces.snailJob;
+
+import com.agentsflex.core.chain.Chain;
+import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
+import com.aizuda.snailjob.client.job.core.dto.JobArgs;
+import com.aizuda.snailjob.model.dto.ExecuteResult;
+import com.ruoyi.common.utils.JsonUtils;
+import com.ruoyi.interfaces.domain.MdAppFlow;
+import com.ruoyi.interfaces.service.IMdAppFlowService;
+import com.ruoyi.interfaces.service.impl.MdAppFlowServiceImpl;
+import com.ruoyi.interfaces.tinyflow.TinyflowUtil;
+import dev.tinyflow.core.Tinyflow;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Component
+public class flowJob {
+    @Autowired
+    private IMdAppFlowService  mdAppFlowService;
+    @JobExecutor(name = "runFolwJob")
+    public ExecuteResult runJob(JobArgs jobArgs) {
+        System.out.println(jobArgs.getJobParams());
+        MdAppFlow flowPar = JsonUtils.jsonToPojo(jobArgs.getJobParams().toString(), MdAppFlow.class);
+        MdAppFlow mdAppFlow = mdAppFlowService.selectMdAppFlowByFlowId(flowPar.getFlowId());
+        Tinyflow tinyflow = TinyflowUtil.getTinyflow(mdAppFlow.getFlowGraph());
+        Chain chain = tinyflow.toChain();
+        Map<String, Object> stringObjectMap = chain.executeForResult(flowPar.getParams());
+        System.out.println(stringObjectMap);
+        //TinyflowUtil.getTinyflow()
+        return ExecuteResult.success(stringObjectMap);
+    }
+}

+ 14 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/tinyflow/TinyflowUtil.java

@@ -0,0 +1,14 @@
+package com.ruoyi.interfaces.tinyflow;
+
+import com.ruoyi.interfaces.tinyflow.parser.PrintNodeParser;
+import com.ruoyi.interfaces.tinyflow.parser.ServiceNodeParser;
+import dev.tinyflow.core.Tinyflow;
+
+public class TinyflowUtil {
+    public static Tinyflow  getTinyflow(String data) {
+        Tinyflow tinyflow = new Tinyflow(data);
+        tinyflow.getChainParser().addNodeParser("printNode",new PrintNodeParser());
+        tinyflow.getChainParser().addNodeParser("serviceNode",new ServiceNodeParser());
+        return tinyflow;
+    }
+}

+ 15 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/tinyflow/node/PrintNode.java

@@ -6,6 +6,7 @@ import com.agentsflex.core.chain.node.BaseNode;
 import com.ruoyi.common.utils.spring.SpringUtils;
 import com.ruoyi.interfaces.domain.PtService;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -85,7 +86,21 @@ public class PrintNode extends BaseNode {
         // 返回执行结果,可以为空,也可以包含输出数据
         Map<String, Object> result = new HashMap<>();
         //result.put("service", ptService);
+
+        ArrayList<Object> data = new ArrayList<>();
+        HashMap<String, String> dataMap = new HashMap<>();
+        HashMap<String, String> dataMap2 = new HashMap<>();
+        HashMap<String, String> dataMap3 = new HashMap<>();
+        dataMap.put("type","123");
+        dataMap2.put("type","456");
+        dataMap3.put("type","789");
+        data.add(dataMap);
+        data.add(dataMap2);
+        data.add(dataMap3);
         result.put("msg",input);
+        result.put("data",data);
+
+
 
         return result;
     }

+ 76 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/tinyflow/node/ServiceNode.java

@@ -0,0 +1,76 @@
+package com.ruoyi.interfaces.tinyflow.node;
+
+import com.agentsflex.core.chain.Chain;
+import com.agentsflex.core.chain.node.BaseNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.ruoyi.common.utils.JsonUtils;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.http.HttpUtils;
+import com.ruoyi.interfaces.domain.PtService;
+import com.ruoyi.interfaces.domain.PtServiceParam;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ServiceNode extends BaseNode {
+    private PtService ptService;
+
+
+    public PtService getPtService() {
+        return ptService;
+    }
+
+    public void setPtService(PtService ptService) {
+        this.ptService = ptService;
+    }
+
+    @Override
+    protected Map<String, Object> execute(Chain chain) {
+        Map<String,Object> map = new HashMap<>();
+
+        Map<String, Object> parameterValues = chain.getParameterValues(this); //获取参数
+        List<PtServiceParam> params = ptService.getParams();
+
+
+
+        HashMap<String, String> headers = null;
+        if (StringUtils.isNotNull(parameterValues.get("headers")))
+            headers = (HashMap<String, String>) parameterValues.get("headers");
+        switch (ptService.getRqtype()) {
+            case "POST":
+                HashMap<String, Object> paramMap = new HashMap<>();
+                for (PtServiceParam serviceParam : params) {
+                    Object paramValue = parameterValues.get(serviceParam.getParamCode());
+                    if (StringUtils.isNotNull(paramValue)) {
+                        paramMap.put(serviceParam.getParamCode(), paramValue);
+                    }
+                }
+                try {
+                     map.put("result",JsonUtils.jsonToPojo(HttpUtils.sendBodyPost(ptService.getUrl(), paramMap, headers), Map.class));
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
+                break;
+            case "GET":
+                String paramString ="";
+                for (PtServiceParam serviceParam : params) {
+                    Object paramValue = parameterValues.get(serviceParam.getParamCode());
+                    if (StringUtils.isNotNull(paramValue)) {
+                        paramString += (serviceParam.getParamCode() + "=" + paramValue) + "&";
+                    }
+                }
+                map.put("result",JsonUtils.jsonToPojo(HttpUtils.sendGet(ptService.getUrl(), paramString, headers), Map.class));
+                break;
+        }
+
+        /**
+         * 调用服务
+         */
+
+
+        return map;
+    }
+}

+ 22 - 0
ruoyi-api-patform/src/main/java/com/ruoyi/interfaces/tinyflow/parser/ServiceNodeParser.java

@@ -0,0 +1,22 @@
+package com.ruoyi.interfaces.tinyflow.parser;
+
+import com.agentsflex.core.chain.node.BaseNode;
+import com.alibaba.fastjson.JSONObject;
+import com.ruoyi.interfaces.domain.PtService;
+import com.ruoyi.interfaces.tinyflow.node.ServiceNode;
+import dev.tinyflow.core.Tinyflow;
+import dev.tinyflow.core.parser.BaseNodeParser;
+
+public class ServiceNodeParser extends BaseNodeParser {
+    @Override
+    protected BaseNode doParse(JSONObject root, JSONObject data, Tinyflow tinyflow) {
+        ServiceNode serviceNode = new ServiceNode();
+        if (data != null) {
+            serviceNode.setId(data.getString("id"));
+            serviceNode.setName(data.getString("name"));
+            serviceNode.setDescription(data.getString("description"));
+            serviceNode.setPtService(data.getObject("service", PtService.class));
+        }
+        return serviceNode;
+    }
+}

+ 1 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/JsonUtils.java

@@ -47,6 +47,7 @@ public class JsonUtils {
             T t = MAPPER.readValue(jsonData, beanType);
             return t;
         } catch (Exception e) {
+            System.out.println(ExceptionUtil.getRootErrorMessage(e));
             e.printStackTrace();
         }
         return null;

+ 0 - 18
ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java

@@ -134,24 +134,6 @@ public class HttpUtils {
     }
 
     public static String sendGet(String url, String param, HashMap<String, String> headers) {
-        System.out.println("=======================");
-        System.out.println("=======================");
-        System.out.println("=======================");
-        System.out.println("=======================");
-        System.out.println(url);
-        System.out.println(param);
-        param = param.substring(0,1);
-        System.out.println(param);
-        for (String s : headers.keySet()) {
-            System.out.println(s+":"+headers.get(s));
-        }
-
-        System.out.println("=======================");
-        System.out.println("=======================");
-        System.out.println("=======================");
-        System.out.println("=======================");
-
-
 
         StringBuilder result = new StringBuilder();
         BufferedReader in = null;

+ 2 - 2
ruoyi-ui/.env.development

@@ -10,8 +10,8 @@ VITE_APP_BASE_Title = '/sh'
 # 若依管理系统/生产环境
 VITE_APP_BASE_API = '/sh-api'
 
-# VITE_DEV_PATH = 'http://localhost:8082'
-VITE_DEV_PATH = 'http://39.98.38.2:18082/sh-api'
+VITE_DEV_PATH = 'http://localhost:8082'
+#VITE_DEV_PATH = 'http://39.98.38.2:18082/sh-api'
 
 # 是否在打包时开启压缩,支持 gzip 和 brotli
 VITE_BUILD_COMPRESS = gzip