root 2 жил өмнө
parent
commit
2aac8a8136

+ 58 - 23
api-common/src/main/java/api/common/util/JgitUtil.java

@@ -1,14 +1,17 @@
 package api.common.util;
 
+import lombok.SneakyThrows;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
 
-import java.io.*;
-import java.security.KeyManagementException;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
+import java.io.File;
+import java.io.IOException;
+import java.sql.Timestamp;
+import java.util.Date;
+import java.util.Iterator;
 import java.util.Map;
 
 
@@ -41,29 +44,61 @@ public class JgitUtil {
         return result;
     }
 
+    @SneakyThrows
+    public static long getLastCommitTimestamp(String url, String username, String password, String directory) {
+        Git git = Git.cloneRepository()
+                .setURI(url)
+                .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
+                .setDirectory(new File(directory))
+                .call();
+        Iterable<RevCommit> logIterable = git.log().call();
+        Iterator<RevCommit> logIterator = logIterable.iterator();//获取所有版本号的迭代器
+        if (!logIterator.hasNext()) {
+            throw new RuntimeException("GitUtil.getLastCommitTimestamp() git 仓库 " + url + " 没有提交过数据。");
+        }
 
-    public static void main(String[] args) throws GitAPIException, IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
-       try {
-           Git git = clone("https://github.com/tensorflow/serving.git","E:\\simulation-cloud\\test","","");
-//           Iterable<RevCommit> iterable = git.log().call();
-//           for (RevCommit u : iterable) {
-//               String ID = String.valueOf(u.getId());
-//               System.out.println("cgfghdnhg");
-//               System.out.println(u.getFullMessage());
-//               System.out.println( ID);
-//               System.out.println("done!!");
-////               正则表达式 截取需要的部分
-////               versionID = getMatcher("commit\\s(\\w+)\\s?", ID);
-//               break;
-//           }
-           System.out.println("slfasjkfasfksd:");
-           System.out.println(git.log());
-       }catch (Exception e){
-           System.out.println("failllll:");
-       }
+        Date lastCommitDate = new Timestamp(1666331629);
+        int i = 0;
+        while (logIterator.hasNext()) {
+            RevCommit commit = logIterator.next();
+            Date commitDate = commit.getAuthorIdent().getWhen();     //提交时间
+            if (i == 0) {
+                lastCommitDate = commitDate;
+            } else {
+                if (commitDate.compareTo(lastCommitDate) > 0) {
+                    lastCommitDate = commitDate;
+                }
+            }
+            i++;
+        }
+        git.close();
+        FileUtil.rm(directory);
+        return TimeUtil.dateToTimestamp(lastCommitDate);
     }
 
 
+//    public static void main(String[] args) throws GitAPIException, IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
+//       try {
+//           Git git = clone("https://github.com/tensorflow/serving.git","E:\\simulation-cloud\\test","","");
+////           Iterable<RevCommit> iterable = git.log().call();
+////           for (RevCommit u : iterable) {
+////               String ID = String.valueOf(u.getId());
+////               System.out.println("cgfghdnhg");
+////               System.out.println(u.getFullMessage());
+////               System.out.println( ID);
+////               System.out.println("done!!");
+//////               正则表达式 截取需要的部分
+//////               versionID = getMatcher("commit\\s(\\w+)\\s?", ID);
+////               break;
+////           }
+//           System.out.println("slfasjkfasfksd:");
+//           System.out.println(git.log());
+//       }catch (Exception e){
+//           System.out.println("failllll:");
+//       }
+//    }
+
+
 
 
 }

+ 8 - 0
api-common/src/main/java/api/common/util/TimeUtil.java

@@ -7,6 +7,14 @@ import java.util.*;
 
 public class TimeUtil {
 
+    /**
+     * @param date
+     * @return 13 位时间戳
+     */
+    public static long dateToTimestamp(Date date) {
+        return date.getTime();
+    }
+
     private static String[] dateFmtArr = new String[]{"yyyyMMdd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HHmmssSS", "HH:mm:ss", "yyyyMMddHHmmss"};
 
     public static long getNow() {

+ 1 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/configuration/git/GitConfiguration.java

@@ -10,4 +10,5 @@ import org.springframework.context.annotation.Configuration;
 public class GitConfiguration {
     private String name;
     private String url;
+    private String tempDirectory;
 }

+ 2 - 2
simulation-resource-server/src/main/java/com/css/simulation/resource/algorithm/ctrl/AlgorithmCtrl.java

@@ -78,8 +78,8 @@ public class AlgorithmCtrl {
      */
     @RequestMapping("deleteByid")
     @ResponseBody
-    public ResponseBodyVO deleteByid(@RequestBody AlgorithmParameter param){
-        return service.deleteByid(param);
+    public ResponseBodyVO<String> deleteById(@RequestBody AlgorithmParameter param){
+        return service.deleteById(param);
     }
 
     /**

+ 1 - 1
simulation-resource-server/src/main/java/com/css/simulation/resource/algorithm/service/AlgorithmService.java

@@ -15,7 +15,7 @@ public interface AlgorithmService {
 
     ResponseBodyVO selectAlgoPlatformList(AlgorithmParameter param);
 
-    ResponseBodyVO deleteByid(AlgorithmParameter param);
+    ResponseBodyVO<String> deleteById(AlgorithmParameter param);
 
     ResponseBodyVO<Map<String,String>> deleteCheck(AlgorithmParameter param);
 

+ 50 - 38
simulation-resource-server/src/main/java/com/css/simulation/resource/algorithm/serviceImpl/AlgorithmServiceImpl.java

@@ -229,44 +229,56 @@ public class AlgorithmServiceImpl implements AlgorithmService {
         }
     }
 
+//    @Override
+//    public String getGitVersion(String algorithmId) {
+//        AlgorithmPO algorithmPO = algorithmMapper.selectDetailsById(algorithmId);
+//        String gitUrl = algorithmPO.getGitUrl();
+//        String gitUserName = algorithmPO.getGitUserName();
+//        String gitPassword = algorithmPO.getGitPassword();
+//        if (StringUtil.isEmpty(gitUrl)) {
+//            return "";
+//        }
+//        //拼接get请求地址
+////        String prefix = "https://api.github.com/repos/";
+//        String prefix = "https://api.github.com/repos/";
+//        String suffix = "/commits?page=1&per_page=1";
+//        String gitInfo = gitUrl.substring(19, gitUrl.length() - 4);
+//        String requestUrl = prefix + gitInfo + suffix;
+//        try {
+//            String result = HttpUtil.get(HttpUtil.getHttpClient(), HttpUtil.getRequestConfig(), requestUrl);
+//            List<HashMap> hashMaps = JsonUtil.jsonToList(result, HashMap.class);
+//            if (hashMaps.size() > 0) {
+//                HashMap hashMap = hashMaps.get(0);
+//                String version = hashMap.get("sha").toString();
+//                return version;
+//            }
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//        return StringUtil.getRandomCode();
+//    }
+
     @Override
     public String getGitVersion(String algorithmId) {
-        AlgorithmPO po = algorithmMapper.selectDetailsById(algorithmId);
-        String gitUrl = po.getGitUrl();
-        if (StringUtil.isEmpty(gitUrl)) {
-            return "";
-        }
-        //拼接get请求地址
-        String prefix = "https://api.github.com/repos/";
-        String suffix = "/commits?page=1&per_page=1";
-        String gitInfo = gitUrl.substring(19, gitUrl.length() - 4);
-        String requestUrl = prefix + gitInfo + suffix;
-        try {
-            String result = HttpUtil.get(HttpUtil.getHttpClient(), HttpUtil.getRequestConfig(), requestUrl);
-            List<HashMap> hashMaps = JsonUtil.jsonToList(result, HashMap.class);
-            if (hashMaps.size() > 0) {
-                HashMap hashMap = hashMaps.get(0);
-                String version = hashMap.get("sha").toString();
-                return version;
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return StringUtil.getRandomCode();
+        AlgorithmPO algorithmPO = algorithmMapper.selectDetailsById(algorithmId);
+        String gitUrl = algorithmPO.getGitUrl();
+        String gitUserName = algorithmPO.getGitUserName();
+        String gitPassword = algorithmPO.getGitPassword();
+        return JgitUtil.getLastCommitTimestamp(gitUrl, gitUserName, gitPassword,
+                gitConfiguration.getTempDirectory() + StringUtil.getRandomEightBitUUID()) + "";
     }
 
-
     @Override
-    public ResponseBodyVO deleteByid(AlgorithmParameter param) {
+    public ResponseBodyVO<String> deleteById(AlgorithmParameter param) {
         if (isEmpty(param.getId())) {
-            return new ResponseBodyVO(ResponseBodyVO.Response.CLIENT_FAILURE);
+            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE);
         } else {
             String algorithmId = param.getId();
             List<RunningProjectVO> runningProjectVos = algorithmMapper.selectRunningProject(algorithmId);
             if (runningProjectVos != null && runningProjectVos.size() > 0) {
-                StringBuffer stringBuffer = new StringBuffer("");
+                StringBuilder stringBuilder = new StringBuilder();
                 for (RunningProjectVO vo : runningProjectVos) {
-                    //2022/10/08,删除算法将停止所有使用该算法的正在运行中的工作项目
+                    // 删除算法将停止所有使用该算法的正在运行中的工作项目
                     //子项目
                     SimulationManualProjectParam voparam = new SimulationManualProjectParam();
                     voparam.setId(vo.getId());
@@ -280,33 +292,33 @@ public class AlgorithmServiceImpl implements AlgorithmService {
 
                     for (RunningProjectVO voParent : runningParentProjectVos) {
                         SimulationManualProjectParam voparamParent = new SimulationManualProjectParam();
-                        stringBuffer.append(voParent.getProjectId() + ",");
+                        stringBuilder.append(voParent.getProjectId()).append(",");
                         voparamParent.setId(voParent.getId());
                         voparamParent.setAutomaticRunState("1");
                         simulationProjectService.updateAutomaticRunState(voparamParent);
                     }
 
                 }
-                String substring = stringBuffer.substring(0, stringBuffer.lastIndexOf(","));
-                return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS, "该算法已绑定ID:" + substring + " 自动运行项目,删除后该项目将停止自动运行");
+                String substring = stringBuilder.substring(0, stringBuilder.lastIndexOf(","));
+                return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "该算法已绑定ID:" + substring + " 自动运行项目,删除后该项目将停止自动运行");
             } else {
                 int i = algorithmMapper.deleteByid(param);
                 if (i > 0) {
                     LogUtil.delete();
-                    return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS);
+                    return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
                 }
-                return new ResponseBodyVO(ResponseBodyVO.Response.SERVER_FAILURE, "删除失败");
+                return new ResponseBodyVO<>(ResponseBodyVO.Response.SERVER_FAILURE, "删除失败");
             }
         }
 
     }
 
     @Override
-    public ResponseBodyVO<Map<String,String>> deleteCheck(AlgorithmParameter param) {
+    public ResponseBodyVO<Map<String, String>> deleteCheck(AlgorithmParameter param) {
         if (isEmpty(param.getId())) {
             return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE);
         } else {
-            Map<String,String> map =new HashMap<>();
+            Map<String, String> map = new HashMap<>();
             String algorithmId = param.getId();
             List<RunningProjectVO> runningParentProjectVos = algorithmMapper.selectAutoProjectIdByAlgorithmId(algorithmId);
             if (runningParentProjectVos != null && runningParentProjectVos.size() > 0) {
@@ -315,11 +327,11 @@ public class AlgorithmServiceImpl implements AlgorithmService {
                     stringBuilder.append(voParent.getProjectId()).append(",");
                 }
                 String substring = stringBuilder.substring(0, stringBuilder.lastIndexOf(","));
-                map.put("substring",substring);
-                return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "该算法已绑定 ID 为 " + substring + " 的自动运行项目,删除后该项目将停止自动运行。",map);
+                map.put("substring", substring);
+                return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "该算法已绑定 ID 为 " + substring + " 的自动运行项目,删除后该项目将停止自动运行。", map);
             } else {
-                map.put("substring","");
-                return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS,"该算法未绑定自动运行项目。",map);
+                map.put("substring", "");
+                return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "该算法未绑定自动运行项目。", map);
             }
         }
     }

+ 1 - 0
simulation-resource-server/src/main/java/com/css/simulation/resource/configuration/git/GitConfiguration.java

@@ -10,4 +10,5 @@ import org.springframework.context.annotation.Configuration;
 public class GitConfiguration {
     private String name;
     private String url;
+    private String tempDirectory;
 }

+ 5 - 5
simulation-resource-server/src/main/java/com/css/simulation/resource/project/impl/SimulationProjectServiceImpl.java

@@ -23,13 +23,13 @@ import com.css.simulation.resource.algorithm.mapper.AlgorithmMapper;
 import com.css.simulation.resource.algorithm.service.AlgorithmService;
 import com.css.simulation.resource.common.utils.AuthUtil;
 import com.css.simulation.resource.common.utils.PageUtil;
+import com.css.simulation.resource.configuration.git.GitConfiguration;
 import com.css.simulation.resource.feign.AlgoPlatformService;
 import com.css.simulation.resource.feign.FileDownService;
 import com.css.simulation.resource.feign.MonitorService;
 import com.css.simulation.resource.project.constants.ProjectConstants;
 import com.css.simulation.resource.project.enums.ProjectRunStateEnum;
 import com.css.simulation.resource.project.enums.SceneTypeEnum;
-import com.css.simulation.resource.project.feign.KafkaService;
 import com.css.simulation.resource.project.mapper.*;
 import com.css.simulation.resource.project.service.SimulationProjectService;
 import com.css.simulation.resource.system.service.DictService;
@@ -89,8 +89,7 @@ public class SimulationProjectServiceImpl implements SimulationProjectService {
     SimulationMptLastTargetScoreMapper simulationMptLastTargetScoreMapper;
     @Resource
     AlgorithmMapper algorithmMapper;
-    @Resource
-    KafkaService kafkaService;
+
     @Resource
     KafkaTemplate<String, String> kafkaTemplate;
     @Resource
@@ -4156,8 +4155,9 @@ public class SimulationProjectServiceImpl implements SimulationProjectService {
 
         SimulationManualProjectParam p = new SimulationManualProjectParam();
         p.setProjectId(po.getId());
-        ResponseBodyVO start = monitorService.stop(p);
-        boolean status = start.isStatus();
+        // 停止任务
+        ResponseBodyVO stop = monitorService.stop(p);
+        boolean status = stop.isStatus();
         if (!status) {
             //定时任务启动失败,抛出异常
             log.info("自动运行项目停止失败:" + po.getId());