Pārlūkot izejas kodu

处理时间和日志

twzydn20000928 1 gadu atpakaļ
vecāks
revīzija
da1a01ef8d

+ 3 - 1
pdaaphm-admin/src/main/java/com/pdaaphm/PdaaphmApplication.java

@@ -3,13 +3,15 @@ package com.pdaaphm;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.scheduling.annotation.EnableAsync;
 
 /**
  * 启动程序
- * 
+ *
  * @author Allen
  */
 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
+@EnableAsync
 public class PdaaphmApplication
 {
     public static void main(String[] args)

+ 2 - 1
pdaaphm-admin/src/main/java/com/pdaaphm/biz/controller/AlgorithmController.java

@@ -134,6 +134,7 @@ public class AlgorithmController extends BaseController
     @Log(title = "算法", businessType = BusinessType.UPDATE)
     @GetMapping(value = "/runAlgorithms/{id}")
     public AjaxResult runAlgorithms(@PathVariable("id") Long id) throws IOException {
-        return success(algorithmService.runAlgorithms(id));
+        algorithmService.runAlgorithms(id);
+        return success();
     }
 }

+ 11 - 9
pdaaphm-admin/src/main/java/com/pdaaphm/biz/domain/File.java

@@ -7,13 +7,15 @@ import com.pdaaphm.common.core.domain.BaseEntity;
 
 /**
  * 文件对象 t_file
- * 
+ *
  * @author xlk
  * @date 2023-07-26
  */
 public class File extends BaseEntity
 {
     private static final long serialVersionUID = 1L;
+    public static String inputFlag = "1";
+    public static String ouputFlag = "2";
 
     /** 编号 */
     private Long id;
@@ -30,39 +32,39 @@ public class File extends BaseEntity
     @Excel(name = "类型")
     private String type;
 
-    public void setId(Long id) 
+    public void setId(Long id)
     {
         this.id = id;
     }
 
-    public Long getId() 
+    public Long getId()
     {
         return id;
     }
-    public void setName(String name) 
+    public void setName(String name)
     {
         this.name = name;
     }
 
-    public String getName() 
+    public String getName()
     {
         return name;
     }
-    public void setPath(String path) 
+    public void setPath(String path)
     {
         this.path = path;
     }
 
-    public String getPath() 
+    public String getPath()
     {
         return path;
     }
-    public void setType(String type) 
+    public void setType(String type)
     {
         this.type = type;
     }
 
-    public String getType() 
+    public String getType()
     {
         return type;
     }

+ 8 - 0
pdaaphm-admin/src/main/java/com/pdaaphm/biz/mapper/AlgorithmMapper.java

@@ -1,10 +1,12 @@
 package com.pdaaphm.biz.mapper;
 
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 import com.pdaaphm.biz.domain.Algorithm;
 import com.pdaaphm.biz.dto.AlgorithmDTO;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * 算法Mapper接口
@@ -67,4 +69,10 @@ public interface AlgorithmMapper
     public int runAlgorithm(Long id);
 
     public List<Map> selectUrlAndAddressById(Long id);
+
+    public void updateStartTimeById(@Param("id") Long id, @Param("startTime") Date startTime);
+
+    public void updateCompletedTimeById(@Param("id") Long id, @Param("completedTime") Date completedTime);
+
+    public void updateCostSecondById(@Param("id") Long id, @Param("costSecond") Long costSecond);
 }

+ 2 - 1
pdaaphm-admin/src/main/java/com/pdaaphm/biz/service/IAlgorithmService.java

@@ -6,6 +6,7 @@ import java.util.Map;
 
 import com.pdaaphm.biz.domain.Algorithm;
 import com.pdaaphm.biz.dto.AlgorithmDTO;
+import org.springframework.scheduling.annotation.Async;
 
 /**
  * 算法Service接口
@@ -81,5 +82,5 @@ public interface IAlgorithmService
      */
     public AlgorithmDTO getAlgorithmDto(Long id);
 
-    public int runAlgorithms(Long id) throws IOException;
+    public void runAlgorithms(Long id) throws IOException;
 }

+ 15 - 12
pdaaphm-admin/src/main/java/com/pdaaphm/biz/service/impl/AlgorithmServiceImpl.java

@@ -2,8 +2,10 @@ package com.pdaaphm.biz.service.impl;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 import com.pdaaphm.biz.domain.AlgorithmSubType;
 import com.pdaaphm.biz.domain.SubAlgorithm;
@@ -20,6 +22,7 @@ import com.pdaaphm.common.utils.DateUtils;
 import com.pdaaphm.common.utils.file.FileUploadUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import com.pdaaphm.biz.mapper.AlgorithmMapper;
 import com.pdaaphm.biz.domain.Algorithm;
@@ -102,8 +105,6 @@ public class AlgorithmServiceImpl implements IAlgorithmService
 
     @Override
     public int addOrUpdateDto(AlgorithmDTO algorithmDto) {
-        // todo dto 2 domain, save doman
-        // get ioSubList, save SubAlgorithm
         int res;
         Algorithm algorithm = new Algorithm();
         BeanUtils.copyProperties(algorithmDto,algorithm);
@@ -167,19 +168,17 @@ public class AlgorithmServiceImpl implements IAlgorithmService
     @Override
     public List<Map> getOption() { return algorithmMapper.getOption(); }
 
+    @Async
     @Override
-    public int runAlgorithms(Long id) throws IOException {
-        int res = 0;
-        //1. update Algorithm.startTime to now
-        //2. run algorithmImpl
+    public void runAlgorithms(Long id) throws IOException {
+        Date startTime = DateUtils.getNowDate();
+        algorithmMapper.updateStartTimeById(id, startTime);
         List<Map> list = algorithmMapper.selectUrlAndAddressById(id);
         Algorithm algorithm = this.selectAlgorithmById(id);
         AlgorithmSubType algorithmSubType = algorithmSubTypeMapper.selectAlgorithmSubTypeById(algorithm.getSubTypeId());
         RunAlgorithmService runAlgorithmService = algorithmFactory.createRun(algorithmSubType.getRunType());
-        // todo remove this code
-        PadaphmConfig.getProfile();
         if(CollectionUtils.isEmpty(list)){
-            return res;
+            return;
         }
         List<String> docAddress = new ArrayList<>(list.size());
         List<String> resultAddress = new ArrayList<>(list.size());
@@ -187,8 +186,7 @@ public class AlgorithmServiceImpl implements IAlgorithmService
         String prefix2 = PadaphmConfig.getResultPath();
         int j = 1;
         for (int i = 0; i < list.size(); i++) {
-            // todo "1".equals(list.get(i).get("type"))
-            if (list.get(i).get("type").equals("1")) {
+            if ("1".equals(list.get(i).get("type"))) {
                 String s = (String)list.get(i).get("path");
                 if (s.startsWith("/profile")) {
                     s = s.replace("/profile", prefix);
@@ -211,6 +209,11 @@ public class AlgorithmServiceImpl implements IAlgorithmService
         requestDto.setResultList(resultAddress);
         runAlgorithmService.runAlgorithm(id, (String)list.get(0).get("url"), requestDto);
         //3. todo update Algorithm.completedTime and Algorithm.costSecond
-        return res;
+        Date completedTime = DateUtils.getNowDate();
+        algorithmMapper.updateCompletedTimeById(id, completedTime);
+        Long diffInMillies = completedTime.getTime() - startTime.getTime();
+        Long costSecond = TimeUnit.MILLISECONDS.toSeconds(diffInMillies);
+        algorithmMapper.updateCostSecondById(id, costSecond);
+        return;
     }
 }

+ 9 - 12
pdaaphm-admin/src/main/java/com/pdaaphm/biz/service/impl/RunPythonImpl.java

@@ -28,6 +28,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeoutException;
 
+
 @Service
 public class RunPythonImpl implements RunAlgorithmService {
 
@@ -71,7 +72,9 @@ public class RunPythonImpl implements RunAlgorithmService {
         // save result path to subAlgorithm
         List<String> resultList = requestDto.getResultList();
         List<Map> subAlgorithmOutputList = subAlgorithmMapper.selectSubAlgorithmOutputList(id);
-        // todo subAlgorithmOutputList == null || subAlgorithmOutputList.size == 0?
+        if (resultList == null) {
+            return res;
+        }
         for (int i = 0; i < resultList.size(); i++) {
             String resultPath = resultList.get(i);
             Map subAlgorithmOutput = subAlgorithmOutputList.get(i);
@@ -89,8 +92,7 @@ public class RunPythonImpl implements RunAlgorithmService {
                 File resultFile = new File();
                 resultFile.setName(map.get("name"));
                 resultFile.setPath(map.get("path"));
-                // todo 魔鬼数字,创建静态变量在File中 xxx=1,yyy=2,这里引用
-                resultFile.setType("2");
+                resultFile.setType(File.ouputFlag);
                 resultFile.setCreateTime(DateUtils.getNowDate());
                 Long fileId = fileMapper.selectOutputFile(id);
                 if (fileId == null) {
@@ -106,11 +108,10 @@ public class RunPythonImpl implements RunAlgorithmService {
                 subAlgorithm.setFieldId((Long) subAlgorithmOutput.get("field_id"));
                 subAlgorithm.setUploadId(resultFile.getId());
                 subAlgorithm.setCreateTime(DateUtils.getNowDate());
-                // todo 只有update?啥时候insert?
                 subAlgorithmMapper.updateSubAlgorithm(subAlgorithm);
             }
             else {
-                logger.error("存在未生成文件!");
+                logger.error("输出文件未生成!");
                 // todo 对程序结果产生负面影响的分子,应反馈给前端
                 // update algorithm
             }
@@ -123,12 +124,8 @@ public class RunPythonImpl implements RunAlgorithmService {
         // 指定目录路径
         java.io.File directory = new java.io.File(dirs);
 
-        // 已知的文件名前缀(不带扩展名)
-        // todo variable is redundant
-        String knownFileName = prefixName;
-
         // 使用FilenameFilter筛选与已知文件名开头的文件
-        String[] matchingFiles = directory.list((dir, name) -> name.startsWith(knownFileName));
+        String[] matchingFiles = directory.list((dir, name) -> name.startsWith(prefixName));
 
         Map<String,String> map = new HashMap<>();
 
@@ -139,11 +136,11 @@ public class RunPythonImpl implements RunAlgorithmService {
             return map;
         } else if (matchingFiles != null && matchingFiles.length > 1) {
             // todo 工程内禁止使用System.out.print 日志统一使用log
-            System.out.println("找到多个匹配的文件,这不应该发生。");
+            logger.error("找到多个匹配的文件,这不应该发生。");
             return null;
         } else {
             // todo 工程内禁止使用System.out.print 日志统一使用log
-            System.out.println("没有找到匹配的文件。");
+            logger.error("没有找到匹配的文件。");
             return null;
         }
     }

+ 24 - 0
pdaaphm-admin/src/main/resources/mapper/algoManager/AlgorithmMapper.xml

@@ -118,4 +118,28 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         WHERE
             ta.id = #{id}
     </select>
+
+    <update id="updateStartTimeById" parameterType="map">
+        update t_algorithm
+        <trim prefix="SET" suffixOverrides=",">
+            <if test="startTime != null">start_time = #{startTime},</if>
+        </trim>
+        where id = #{id}
+    </update>
+
+    <update id="updateCompletedTimeById" parameterType="map">
+        update t_algorithm
+        <trim prefix="SET" suffixOverrides=",">
+            <if test="completedTime != null">completed_time = #{completedTime},</if>
+        </trim>
+        where id = #{id}
+    </update>
+
+    <update id="updateCostSecondById" parameterType="map">
+        update t_algorithm
+        <trim prefix="SET" suffixOverrides=",">
+            <if test="costSecond != null">cost_second = #{costSecond},</if>
+        </trim>
+        where id = #{id}
+    </update>
 </mapper>