ソースを参照

feat: 继续优化函数结构,进一步封装程序

WANGKANG 5 ヶ月 前
コミット
524958a1e4

+ 52 - 58
taais-modules/taais-biz/src/main/java/com/taais/biz/service/impl/ToInfraredServiceImpl.java

@@ -160,52 +160,46 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
     /**
      * 新增可见光转红外
      *
-     * @param toInfraredBo 可见光转红外Bo
+     * @param entityBo 可见光转红外Bo
      * @return 结果:true 操作成功,false 操作失败
      */
     @Override
-    public boolean insert(ToInfraredBo toInfraredBo) {
+    @Transactional
+    public boolean insert(ToInfraredBo entityBo) {
         // 检查input_oss_id是否存在
-        if (ObjectUtil.isNull(toInfraredBo.getInputOssId())) {
+        if (ObjectUtil.isNull(entityBo.getInputOssId())) {
             return false;
         }
 
-        SysOssVo ossEntity = ossService.getById(toInfraredBo.getInputOssId());
+        SysOssVo ossEntity = ossService.getById(entityBo.getInputOssId());
         if (ObjectUtil.isNull(ossEntity)) {
             return false;
         }
 
         ToInfrared entity = new ToInfrared();
-        entity.setInputOssId(toInfraredBo.getInputOssId());
+        entity.setInputOssId(entityBo.getInputOssId());
         entity.setUrl(ossEntity.getUrl());
         entity.setZipFilePath(ossEntity.getFileName());
-        entity.setName(toInfraredBo.getName());
+        entity.setName(entityBo.getName());
         entity.setStatus(NOT_START);
-        entity.setRemarks(toInfraredBo.getRemarks());
-        entity.setAlgorithmModelId(toInfraredBo.getAlgorithmModelId());
-        entity.setAlgorithmId(toInfraredBo.getAlgorithmId());
+        entity.setRemarks(entityBo.getRemarks());
+        entity.setAlgorithmModelId(entityBo.getAlgorithmModelId());
+        entity.setAlgorithmId(entityBo.getAlgorithmId());
         boolean flag = this.save(entity);
 
         if (!flag) {
             return false;
         }
 
-        entity = updateEntity(entity, toInfraredBo, ossEntity);
+        entity = updateEntity(entity, entityBo, ossEntity);
 
         // 步骤 6. 保存算法参数到数据库
         return this.updateById(entity);// 使用全局配置的雪花算法主键生成器生成ID值
     }
 
-    private ToInfrared updateEntity(ToInfrared entity, ToInfraredBo toInfraredBo, SysOssVo ossEntity) {
-        // 从这里开始,配置任务的algorithm_parameters参数
-        // 步骤 1. 首先根据算法id获取算法配置
-        AlgorithmConfigTrack algorithmConfig = algorithmConfigTrackService.getById(entity.getAlgorithmId());
-        if (ObjectUtil.isNull(algorithmConfig)) {
-            throw new RuntimeException("算法配置参数为空");
-        }
-
+    public static Map<String, Object> getAlgorithmParameters(String algorithmConfigStr, Map<String, Object> otherParams) {
         Map<String, Object> algorithmParameters = new HashMap<>();
-        List<Dict> config_list = JsonUtils.parseArrayMap(algorithmConfig.getParameters());
+        List<Dict> config_list = JsonUtils.parseArrayMap(algorithmConfigStr);
 
         // 步骤 2. 根据算法配置,生成默认参数的map数据结构,同时需要解析default_value字段,生成对应的结果
         if (ObjectUtil.isEmpty(config_list)) {
@@ -220,47 +214,37 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
         }
         // 步骤 3. 然后根据前端传入的参数,覆盖默认参数
         for (String key : algorithmParameters.keySet()) {
-            if (toInfraredBo.getOtherParams().containsKey(key) && ObjectUtil.isNotEmpty(toInfraredBo.getOtherParams().get(key))) {
-                algorithmParameters.put(key, toInfraredBo.getOtherParams().get(key));
+            if (otherParams.containsKey(key) && ObjectUtil.isNotEmpty(otherParams.get(key))) {
+                algorithmParameters.put(key, otherParams.get(key));
             }
         }
-        // 步骤4. 构造可以直接传给前端的map数据结构
-        Map<String, Object> result = new HashMap<>();
+        return algorithmParameters;
+    }
 
-        if (algorithmConfig.getType().equals(BizConstant.AlgorithmType.TRAIN)) {
-            String source_dir = getTrainInputPath(ossEntity);
-            String result_dir = getTrainOutputPath(entity, ossEntity);
+    private ToInfrared updateEntity(ToInfrared entity, ToInfraredBo entityBo, SysOssVo ossEntity) {
+        // 从这里开始,配置任务的algorithm_parameters参数
+        // 步骤 1. 首先根据算法id获取算法配置
+        AlgorithmConfigTrack algorithmConfig = algorithmConfigTrackService.getById(entity.getAlgorithmId());
+        if (ObjectUtil.isNull(algorithmConfig)) {
+            throw new RuntimeException("算法配置参数为空");
+        }
 
-            entity.setInputPath(source_dir);
-            entity.setOutputPath(result_dir);
+        Map<String, Object> algorithmParameters = getAlgorithmParameters(algorithmConfig.getParameters(), entityBo.getOtherParams());
 
-            String log_path = getLogFilePath(entity);
-            entity.setLogPath(log_path);
+        // 步骤4. 构造可以直接传给前端的map数据结构
 
-            result.put("source_dir", source_dir);
-            result.put("result_dir", result_dir);
-            result.put("log_path", log_path);
+        Map<String, Object> result = getCommonResultParams(entity.getId(), ossEntity);
 
-        } else if (algorithmConfig.getType().equals(BizConstant.AlgorithmType.REASONING)) {
-            String source_dir = getPredictInputPath(ossEntity);
-            String result_dir = getPredictOutputPath(entity, ossEntity);
+        if (algorithmConfig.getType().equals(BizConstant.AlgorithmType.REASONING)) {
             String model_path = getModelPath(entity);
-
-            entity.setInputPath(source_dir);
-            entity.setOutputPath(result_dir);
-
-            String log_path = getLogFilePath(entity);
-            entity.setLogPath(log_path);
-
-            result.put("result_dir", result_dir);
-            result.put("source_dir", source_dir);
             result.put("model_path", model_path);
-            result.put("log_path", log_path);
-
-        } else {
-            throw new RuntimeException("算法类型不支持");
         }
         result.put("otherParams", algorithmParameters);
+
+        entity.setInputPath((String) result.get("source_dir"));
+        entity.setOutputPath((String) result.get("result_dir"));
+        entity.setLogPath((String) result.get("log_path"));
+
         // 步骤 5. 将算法参数map序列化为json字符串,保存到数据库中
         entity.setAlgorithmParameters(JsonUtils.toJsonString(result));
 
@@ -276,6 +260,20 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
         return entity;
     }
 
+    private Map<String, Object> getCommonResultParams(Long id, SysOssVo ossEntity) {
+        Map<String, Object> result = new HashMap<>();
+        String source_dir = getTrainInputPath(ossEntity);
+        String result_dir = getTrainOutputPath(id, ossEntity);
+        String log_path = getLogFilePath(result_dir, id, BizConstant.TO_INFRARED_SUFFIX);
+
+        result.put("biz_id", id);
+        result.put("biz_type", BizConstant.BizType.TO_INFRARED);
+        result.put("source_dir", source_dir);
+        result.put("result_dir", result_dir);
+        result.put("log_path", log_path);
+        return result;
+    }
+
     /**
      * 修改可见光转红外
      *
@@ -333,10 +331,10 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
         return getUnZipDirPath(ossEntity);
     }
 
-    public String getTrainOutputPath(ToInfrared entity, SysOssVo ossEntity) {
+    public String getTrainOutputPath(Long id, SysOssVo ossEntity) {
         String resourcePath = getResourcePath(ossEntity);
         Path path = Paths.get(resourcePath);
-        Path outputPath = path.resolveSibling(entity.getId().toString() + BizConstant.TO_INFRARED_SUFFIX);
+        Path outputPath = path.resolveSibling(id.toString() + BizConstant.TO_INFRARED_SUFFIX);
         return outputPath.toString();
     }
 
@@ -348,12 +346,9 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
         return getUnZipDirPath(ossEntity) + File.separator + BizConstant.TO_INFRARED_PREDICT_OUTPUT_DIR;
     }
 
-    private String getLogFileName(ToInfrared entity) {
-        return entity.getId() + BizConstant.TO_INFRARED_SUFFIX + ".log";
-    }
 
-    public String getLogFilePath(ToInfrared entity) {
-        return entity.getOutputPath() + File.separator + getLogFileName(entity);
+    public static String getLogFilePath(String outputPath, Long id, String suffix) {
+        return outputPath + File.separator + id.toString() + suffix + ".log";
     }
 
     public String getStatisticsResultPath(SysOssVo ossEntity) {
@@ -447,8 +442,7 @@ public class ToInfraredServiceImpl extends BaseServiceImpl<ToInfraredMapper, ToI
     @Override
     public CommonResult getLog(Long id) {
         ToInfrared entity = getById(id);
-        String logPath = getLogFilePath(entity);
-        log.info("logPath: {}", logPath);
+        String logPath = entity.getLogPath();
         File file = new File(logPath);
         if (!file.exists()) {
             return CommonResult.fail("日志文件不存在!");