andy 1 year ago
parent
commit
b50b6904dd

+ 11 - 0
src/main/java/com/scbfkj/uni/library/UniReturnUtil.java

@@ -1,8 +1,10 @@
 package com.scbfkj.uni.library;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
+import java.util.stream.Collectors;
 
 public class UniReturnUtil {
 
@@ -37,6 +39,10 @@ public class UniReturnUtil {
         return result;
     }
 
+    public static Map<String, Object> fail(Exception e) {
+        return fail(getMessage(e));
+    }
+
     public static Map<String, Object> fail(String code, String message, Map<String, Object> other) {
         Map<String, Object> result = fail(code, message);
         if (Objects.nonNull(other)) {
@@ -51,4 +57,9 @@ public class UniReturnUtil {
         result.put("code", code);
         return result;
     }
+
+    public static String getMessage(Exception e){
+        String detailMessage = Arrays.stream(e.getStackTrace()).limit(5).map(it -> "%s.%s:%s".formatted(it.getClassName(), it.getMethodName(), it.getLineNumber())).collect(Collectors.joining("\n"));
+        return "异常消息:%s\n 错误详情:%s".formatted(e.getMessage(),detailMessage);
+    }
 }

+ 18 - 6
src/main/java/com/scbfkj/uni/library/script/JavaScriptEngineUtil.java

@@ -2,6 +2,8 @@ package com.scbfkj.uni.library.script;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.scbfkj.uni.library.DataFormatUtil;
+import com.scbfkj.uni.library.UniReturnUtil;
+import com.scbfkj.uni.service.LoggerService;
 import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
 import org.apache.commons.pool2.KeyedObjectPool;
 import org.apache.commons.pool2.PooledObject;
@@ -13,6 +15,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.time.LocalDateTime;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -122,15 +125,24 @@ public class JavaScriptEngineUtil {
         }
     });
 
-    public static Object invoke(Map<String, Object> datasource, String expression, List<Object> args) throws Exception {
+    public static Map<String, Object> invoke(Map<String, Object> datasource, String expression, List<Object> args) {
         datasource.put("methodName", expression);
         String key = DataFormatUtil.toString(datasource);
 
-        JavaApply javaApply = javaRefPool.borrowObject(key);
-        try{
-            return javaApply.invokeApply(args.toArray());
-        }finally {
-            javaRefPool.returnObject(key,javaApply);
+        JavaApply javaApply = null;
+        try {
+            javaApply = javaRefPool.borrowObject(key);
+            return UniReturnUtil.success(javaApply.invokeApply(args.toArray()));
+        } catch (Exception e) {
+            return UniReturnUtil.fail(e);
+        } finally {
+            if (Objects.nonNull(javaApply)) {
+                try {
+                    javaRefPool.returnObject(key, javaApply);
+                } catch (Exception e) {
+                    LoggerService.logSystemError(LocalDateTime.now(), null, UniReturnUtil.getMessage(e));
+                }
+            }
         }
     }
 }

+ 20 - 5
src/main/java/com/scbfkj/uni/library/script/JsScriptEngineUtil.java

@@ -1,5 +1,7 @@
 package com.scbfkj.uni.library.script;
 
+import com.scbfkj.uni.library.UniReturnUtil;
+import com.scbfkj.uni.service.LoggerService;
 import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
 import org.apache.commons.pool2.KeyedObjectPool;
 import org.apache.commons.pool2.PooledObject;
@@ -10,6 +12,10 @@ import org.graalvm.polyglot.Context;
 import org.graalvm.polyglot.Source;
 import org.graalvm.polyglot.Value;
 
+import java.time.LocalDateTime;
+import java.util.Map;
+import java.util.Objects;
+
 public final class JsScriptEngineUtil {
 
     private final static String TO_STRING_SCRIPT = """
@@ -38,17 +44,26 @@ public final class JsScriptEngineUtil {
     }, new GenericKeyedObjectPoolConfig<>());
 
 
-    public static String eval(String script, Object... args) throws Exception {
-        Value function = scriptPool.borrowObject(script);
+    public static Map<String,Object> eval(String script, Object... args) {
+        Value function = null;
         try {
+        function = scriptPool.borrowObject(script);
             if (function.canExecute()) {
                 Value result = function.execute(args);
-                return toString(result);
+                return UniReturnUtil.success(toString(result));
             } else {
-                return toString(function);
+                return UniReturnUtil.success(toString(function));
             }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
         } finally {
-            scriptPool.returnObject(script, function);
+            if (Objects.nonNull(function)) {
+                try {
+                    scriptPool.returnObject(script, function);
+                } catch (Exception e) {
+                    LoggerService.logSystemError(LocalDateTime.now(), null, UniReturnUtil.getMessage(e));
+                }
+            }
         }
     }
 

+ 4 - 11
src/main/java/com/scbfkj/uni/process/DataBase.java

@@ -19,7 +19,7 @@ import java.util.stream.Collectors;
 public class DataBase {
 
     private final static Map<String, Map<String, Object>> cacheDatas = new HashMap<>();
-    private final static List<Map<String, Object>> cacheConfigList = new ArrayList<>();
+    private  static List<Map<String, Object>> cacheConfigList = new ArrayList<>();
     public final static Map<String, HikariPool> dataSourcePools = new HashMap<>();
 
 
@@ -55,16 +55,9 @@ public class DataBase {
             }
         }).toList();
 
-        if (cacheConfigList.isEmpty()) {
-            HikariPool sourcePool = getDataSourcePool(Config.centerConnectionStr);
-            try (Connection connection = sourcePool.getConnection();
-                 PreparedStatement preparedStatement = connection.prepareStatement("select * from datacache")
-            ) {
-                ResultSet resultSet = preparedStatement.executeQuery();
-                cacheConfigList.addAll(getResult(connectionStr, sql, resultSet));
-            } catch (Exception exception) {
-                throw new RuntimeException("数据异常: %s\n sql: select * from datacache ;\n args: null ".formatted(exception.getMessage()));
-            }
+            String querySql = "select * from datacache where 1=?";
+        if ( !Objects.equals(querySql,sql)) {
+            cacheConfigList=  query(Config.centerConnectionStr,querySql,Collections.singletonList(new Object[]{1}));
         }
         cacheConfigList.stream().filter(it -> Objects.equals(it.get("querysql"), sql) && Objects.equals(it.get("connectset"), connectionStr)).findFirst().ifPresent(it -> {
             Object o = it.get("effectiveduration");

+ 72 - 140
src/main/java/com/scbfkj/uni/service/DataProcessService.java

@@ -20,126 +20,81 @@ public class DataProcessService {
 
     public static Map<String, Object> process(Map<String, Object> inData) {
         String lifecycleid = null;
-        Map<String, Object> result;
         List<Map<String, Object>> resource = new ArrayList<>();
+        List<Map<String, Object>> preResource = new ArrayList<>();
         resource.add(inData);
         String serviceId = null;
-        Object algorithmlibraryid = null;
-        Optional<String> serviceIdOpt = null;
         LocalDateTime startDateTime = LocalDateTime.now();
+        Optional<String> serviceIdOpt;
+        String message = null;
+        Object algorithmlibraryid;
+
         try {
             serviceIdOpt = DataAliasGetUtil.getValue("serviceid", inData);
-        } catch (Exception e) {
-            LoggerService.logService(
-                    startDateTime,
-                    serviceId,
-                    DataFormatUtil.toString(resource),
-                    null,
-                    LocalDateTime.now(),
-                    null,
-                    "-1",
-                    e.getMessage(),
-                    lifecycleid
-            );
-            return UniReturnUtil.fail(e.getMessage());
-        }
-        if (Objects.isNull(inData) || inData.isEmpty() || serviceIdOpt.isEmpty())
-            return UniReturnUtil.fail("服务编号不能为空");
-        serviceId = serviceIdOpt.get();
+            if (Objects.isNull(inData) || inData.isEmpty() || serviceIdOpt.isEmpty())
+                throw new RuntimeException("服务编号不能为空");
+            serviceId = serviceIdOpt.get();
 
 //        熔断
 
-        List<Map<String, Object>> algorithmLibraries;
-        try {
+            List<Map<String, Object>> algorithmLibraries;
             if (serviceId.matches("^\\d+$")) {
                 algorithmLibraries = DataBase.query(Config.centerConnectionStr, "select * from algorithmlibrary where serviceid=?", Collections.singletonList(new Object[]{serviceId}));
             } else {
                 algorithmLibraries = DataBase.query(Config.localCenterConnectionStr, "select * from algorithmlibrary where serviceid=?", Collections.singletonList(new Object[]{serviceId}));
             }
-        } catch (Exception e) {
-//            记录异常
-            LoggerService.logService(
-                    startDateTime,
-                    serviceId,
-                    DataFormatUtil.toString(resource),
-                    null,
-                    LocalDateTime.now(),
-                    null,
-                    "-1",
-                    e.getMessage(),
-                    lifecycleid
-            );
-            return UniReturnUtil.fail(e.getMessage());
-        }
 
-        Optional<String> lifecycleidOpt = null;
-        try {
+            Optional<String> lifecycleidOpt;
             lifecycleidOpt = DataAliasGetUtil.getValue("lifecycleid", inData);
-        } catch (Exception e) {
-            LoggerService.logService(
-                    startDateTime,
-                    serviceId,
-                    DataFormatUtil.toString(resource),
-                    null,
-                    LocalDateTime.now(),
-                    null,
-                    "-1",
-                    e.getMessage(),
-                    lifecycleid
-            );
-            return UniReturnUtil.fail(e.getMessage());
-        }
-        if (lifecycleidOpt.isEmpty()) {
-            lifecycleid = DataAliasGetUtil.createLifeCycleCol(Config.containerCode, serviceId);
-            inData.put("lifecycleid", lifecycleid);
-        } else {
-            lifecycleid = lifecycleidOpt.get();
-        }
 
-        for (Map<String, Object> algorithmLibrary : algorithmLibraries) {
+            if (lifecycleidOpt.isEmpty()) {
+                lifecycleid = DataAliasGetUtil.createLifeCycleCol(Config.containerCode, serviceId);
+                inData.put("lifecycleid", lifecycleid);
+            } else {
+                lifecycleid = lifecycleidOpt.get();
+            }
+
+            for (Map<String, Object> algorithmLibrary : algorithmLibraries) {
 
-            Map<String, Object> algorithmResult = null;
+                Map<String, Object> algorithmResult = null;
 
-            long execTime;
-            String code = null;
-            String message = null;
+                long execTime;
 
-            startDateTime = LocalDateTime.now();
-            long startTime = System.currentTimeMillis();
-            List<Object> parameters = null;
-            algorithmlibraryid = algorithmLibrary.get("algorithmlibraryid");
-            Object preConditions = algorithmLibrary.get("preconditions");
-            Object preparameterset = algorithmLibrary.get("preparameterset");
-            try {
+                startDateTime = LocalDateTime.now();
+                long startTime = System.currentTimeMillis();
+                List<Object> parameters = null;
+                algorithmlibraryid = algorithmLibrary.get("algorithmlibraryid");
+                Object preConditions = algorithmLibrary.get("preconditions");
+                Object preparameterset = algorithmLibrary.get("preparameterset");
                 HashMap<String, Object> data = new HashMap<>();
                 resource.add(data);
 //                记录生命周期ID
                 data.put("lifecycleid", lifecycleid);
+                data.put("algorithmlibraryid", algorithmlibraryid);
                 if (Objects.nonNull(preConditions)) {
+                    HashMap<String, Object> preData = new HashMap<>();
+                    preResource.add(preData);
+
+                    preData.put("algorithmlibraryid", algorithmlibraryid);
+
 
                     String preCode;
-                    if (Objects.isNull(preparameterset)) {
-                        preCode = JsScriptEngineUtil.eval(DataFormatUtil.toString(preConditions), resource);
-                    } else {
-                        List<Object> params = getParams(Objects.requireNonNull(DataFormatUtil.toString(preparameterset)), resource);
-                        preCode = JsScriptEngineUtil.eval(DataFormatUtil.toString(preConditions), params.toArray());
-                    }
+                    List<Object> params = getParams(Optional.ofNullable(preparameterset).map(DataFormatUtil::toString).orElse(null), resource);
+                    Map<String, Object> eval = JsScriptEngineUtil.eval(DataFormatUtil.toString(preConditions), params.toArray());
+
+                    preData.put("preResult", eval);
+                    if (!Objects.equals(eval.get("code"), "0"))
+                        throw new RuntimeException(eval.get("message").toString());
+                    preCode = eval.get("returnData").toString();
+
+
+                    preData.put("preCode", preCode);
 //                    记录前置条件结果
-                    data.put("preResult", preCode);
+                    preData.put("preParameters", params);
                     if (Objects.equals("2", preCode)) {
-                        LoggerService.logService(
-                                startDateTime,
-                                serviceId,
-                                DataFormatUtil.toString(resource),
-                                DataFormatUtil.toString(parameters),
-                                LocalDateTime.now(),
-                                DataFormatUtil.toString(algorithmResult),
-                                "0",
-                                message,
-                                lifecycleid
-                        );
-                        return UniReturnUtil.success(resource.size() - 1);
+                        break;
                     } else if (Objects.equals("1", preCode)) {
+                        resource.add(null);
                         continue;
                     }
                 }
@@ -161,55 +116,48 @@ public class DataProcessService {
                     data.put("parameters", parameters);
 
                 }
-                List<String> filterColumns = Optional.of(resource).map(it -> it.get(0)).map(it -> it.get("filterColumns")).map(it -> ((List<String>) it)).orElse(new ArrayList<>());
-                List<Map<String, Object>> filterLines = Optional.of(resource).map(it -> it.get(0)).map(it -> it.get("filterLines")).map(it -> ((List<Map<String, Object>>) it)).orElse(new ArrayList<>());
-
-                algorithmResult = processByAlgorithm(algorithmLibrary, datasource, parameters, filterColumns, filterLines);
+                algorithmResult = processByAlgorithm(algorithmLibrary, parameters);
 //                    算法执行结果
                 data.put("result", algorithmResult);
                 execTime = System.currentTimeMillis() - startTime;
 //                    执行时长
                 data.put("execTime", execTime);
 //                    执行成功与否
-                code = Optional.ofNullable(algorithmResult.get("code")).map(DataFormatUtil::toString).orElse(null);
+                Object code = Optional.ofNullable(algorithmResult.get("code")).map(DataFormatUtil::toString).orElse(null);
                 message = Optional.ofNullable(algorithmResult.get("message")).map(DataFormatUtil::toString).orElse(null);
                 data.put("resultCode", code);
-            } catch (Exception e) {
-                message = e.getMessage();
-//                    把错误细化后往上一层抛出统一记录日志
-                throw new RuntimeException("算法id: %s 异常信息:%s ".formatted(algorithmlibraryid, e.getMessage()));
-            } finally {
-//                    不管成功还是失败都记录日志
-                LoggerService.logService(
-                        startDateTime,
-                        serviceId,
-                        DataFormatUtil.toString(resource),
-                        DataFormatUtil.toString(parameters),
-                        LocalDateTime.now(),
-                        DataFormatUtil.toString(algorithmResult),
-                        Optional.ofNullable(code).orElse("-1"),
-                        message,
-                        lifecycleid
-                );
-            }
-            if (!Objects.equals(code, "0")) {
-                return resource.get(resource.size() - 1);
+
+                if (!Objects.equals(code, "0")) {
+                    throw new RuntimeException(message);
+                }
             }
+            return resource.get(resource.size() - 1);
+        } catch (Exception e) {
+            message = e.getMessage();
+            return UniReturnUtil.fail(e);
+        } finally {
+//                    不管成功还是失败都记录日志
+            LoggerService.logService(
+                    startDateTime,
+                    serviceId,
+                    DataFormatUtil.toString(resource),
+                    DataFormatUtil.toString(preResource),
+                    LocalDateTime.now(),
+                    Objects.nonNull(message) ? "-1" : "0",
+                    message,
+                    lifecycleid
+            );
         }
-        return resource.get(resource.size() - 1);
 
     }
 
     /**
      * @param algorithmLibrary 算法配置
-     * @param datasource
      * @param parameters       算法参数
-     * @param filterColumns
-     * @param filterLines
      * @return
      * @throws Exception
      */
-    public static Map<String, Object> processByAlgorithm(Map<String, Object> algorithmLibrary, Map<String, Object> datasource, List<Object> parameters, List<String> filterColumns, List<Map<String, Object>> filterLines) throws Exception {
+    public static Map<String, Object> processByAlgorithm(Map<String, Object> algorithmLibrary, List<Object> parameters) throws Exception {
 
         Object type = algorithmLibrary.get("algorithmtype");
         Object expression = algorithmLibrary.get("computingexpression");
@@ -218,39 +166,22 @@ public class DataProcessService {
         switch (type.toString()) {
 //            java反射
             case "1" -> {
-                Object invoke = JavaScriptEngineUtil.invoke(datasource, expression.toString(), parameters);
-                return UniReturnUtil.success(invoke);
+                return JavaScriptEngineUtil.invoke((Map<String, Object>) parameters.get(0), expression.toString(), parameters.subList(1,parameters.size()));
             }
 //            JS表达式
             case "2" -> {
-                return UniReturnUtil.success(JsScriptEngineUtil.eval(expression.toString(), parameters));
+                return JsScriptEngineUtil.eval(expression.toString(), parameters);
             }
 //            数据库
             case "3" -> {
 //                下放到Database中处理数据
 //                参数表达式顺序是 数据源连接字符串(String.$.datasource.connectset),sql表达式(String.$.algorithm.computingexpression),需要操作的值(List.$.args[1].returnData),执行编号(String.$.algorithm.executionnumber)
-                return DataBase.exec(parameters.get(0).toString(), parameters.get(1).toString(), ((List<Map<String, Object>>) parameters.get(2)), parameters.get(3), filterColumns, filterLines);
+                return DataBase.exec(parameters.get(0).toString(), parameters.get(1).toString(), ((List<Map<String, Object>>) parameters.get(2)), parameters.get(3), (List<String>) parameters.get(4), (List<Map<String, Object>>) parameters.get(5));
             }
         }
         return UniReturnUtil.fail("算法类型不支持");
     }
 
-    /**
-     * @param script 前置表达式 不能为空
-     * @param args   表达式参数
-     * @return
-     * @throws Exception
-     */
-    private static String preConditionScript(String script, List<Map<String, Object>> args, String parameterSet) throws Exception {
-//        todo 兼容之前版本的写法的
-//        判断算法中是否存在书名号
-        if (Objects.isNull(parameterSet)) {
-            return JsScriptEngineUtil.eval(script, args);
-        } else {
-            List<Object> params = getParams(parameterSet, args);
-            return JsScriptEngineUtil.eval(script, params.toArray());
-        }
-    }
 
     /**
      * @param parameterSet 使用非标准的jsonPath表达式 String.$.a.b[0].c,String.abcc 多个参数之间使用;;分隔
@@ -258,6 +189,7 @@ public class DataProcessService {
      * @return
      */
     public static List<Object> getParams(String parameterSet, Object source) {
+        if (Objects.isNull(parameterSet)) return Collections.emptyList();
         String[] paths = parameterSet.split(";;");
         String json = DataFormatUtil.toString(source);
         List<Object> result = new ArrayList<>();

+ 4 - 5
src/main/java/com/scbfkj/uni/service/LoggerService.java

@@ -50,16 +50,15 @@ public class LoggerService {
      * @param beginTime     算法开始时间
      * @param serviceID     服务id
      * @param resource      算法数据历史
-     * @param requestData   入参
+     * @param preResource   前置算法
      * @param endTime       算法结束时间
-     * @param returnData    算法执行结果
      * @param returnCode    算法结果成功失败编号
      * @param returnMessage 算法失败错误消息
      * @param dataObjectID  数据声明周期
      */
     //服务日志
-    public static void logService(LocalDateTime beginTime, String serviceID, String resource, String requestData,
-                                  LocalDateTime endTime, String returnData,
+    public static void logService(LocalDateTime beginTime, String serviceID, String resource, String preResource,
+                                  LocalDateTime endTime,
                                   String returnCode, String returnMessage
             , String dataObjectID) {
         //调用工具类的获取雪花编号getUniqueNumber(时间戳+当前容器编号+0)
@@ -72,7 +71,7 @@ public class LoggerService {
             if (!Objects.equals(returnCode, "0")) {
                 tableName = "serviceerrlog";
             }
-            insertLog(connectionStr, "insert into %s ('begintime','endtime','serviceid','resource','requestdata','returndata','returncode','returnmessage','lifecycleid') values (?,?,?,?,?,?,?,?,?)".formatted(tableName), List.of(beginTime, endTime, serviceID, resource, requestData, requestData, returnCode, returnMessage, dataObjectID));
+            insertLog(connectionStr, "insert into %s ('begintime','endtime','serviceid','resource','preresource','returncode','returnmessage','lifecycleid') values (?,?,?,?,?,?,?,?,?)".formatted(tableName), List.of(beginTime, endTime, serviceID, resource, preResource, returnCode, returnMessage, dataObjectID));
         } catch (Exception e) {
             throw new RuntimeException(e);
         }