martin 3 년 전
부모
커밋
8a28c0b710

+ 7 - 7
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/controller/TaskController.java

@@ -65,13 +65,13 @@ public class TaskController {
         taskService.taskState(taskId, state, podName);
     }
 
-    /**
-     * 修改任务状态
-     */
-    @GetMapping("/stateTest")
-    public void taskStateTest(@RequestParam("taskId") String taskId, @RequestParam("state") String state, @RequestParam("podName") String podName) {
-        taskService.taskStateTest(taskId, state, podName);
-    }
+//    /**
+//     * 修改任务状态
+//     */
+//    @GetMapping("/stateTest")
+//    public void taskStateTest(@RequestParam("taskId") String taskId, @RequestParam("state") String state, @RequestParam("podName") String podName) {
+//        taskService.taskStateTest(taskId, state, podName);
+//    }
 
     /**
      * 任务执行前调用该接口,确定该任务没有被终止

+ 1 - 146
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/service/TaskService.java

@@ -158,6 +158,7 @@ public class TaskService {
                         ScoreTO score;
                         String runResultMinio = task2.getRunResultFilePath() + "/Ego.csv";
                         String runResultLinux = linuxTempPath + runResultMinio;
+
 //                        python3 /home/ubuntu/test/Evaluate/main.py /home/ubuntu/test/test_data.csv 4 AEB_1-2
 //                        String command = "python3 " + pyPath + " " + runResultLinux + " " + task2.getSceneType();  // 默认使用场景名称找打分脚本
                         String command = "python3 " + pyPath + "main.py " + runResultLinux + " " + task2.getSceneType() + " " + ruleName; // 指定打分脚本
@@ -246,153 +247,7 @@ public class TaskService {
     }
 
 
-    @SneakyThrows
-    public void taskStateTest(String taskId, String state, String podName) {
-        log.info("------- /state 修改任务 " + taskId + "的状态:" + state + ",pod 名称为:" + podName);
-        taskMapper.updateState(taskId, state);
 
-        ProjectPO projectPO = projectMapper.selectById(taskId);
-        String projectId = projectPO.getId();
-        String scenePackageId = projectPO.getScenePackageId();  // 场景测试包 id,指标根 id
-        log.info("------- /state 任务 " + taskId + " 的父项目为:" + projectId);
-        int taskNum = projectMapper.selectTaskNumById(projectId);
-        int endTaskNum = projectMapper.selectEndTaskNum(projectId);    // 查询已结束的任务 'Aborted', 'PendingAnalysis', 'Terminated'
-        projectMapper.updateTaskCompleted(projectId, endTaskNum);
-        log.info("------- /state 项目 " + projectId + " 完成进度为:" + endTaskNum + "/" + taskNum);
-        if (taskNum != endTaskNum) {  // 已结束任务数等于所有任务数量,才会准备打分;否则退出。
-            return;
-        }
-        projectMapper.updateProjectState(projectId, DictConstants.PROJECT_COMPLETED);   // 修改该 project 的状态为已完成
-        List<TaskPO> taskList = taskMapper.selectTaskListByProjectId(projectId);  // 所有任务信息
-        // -------------------------------- 查询叶子指标 --------------------------------
-        List<IndexTemplatePO> leafIndexTemplateList = indexTemplateMapper.selectLeafIndexWithRuleDetailsByPackageId(scenePackageId);
-        List<TaskIndexPO> leafTaskIndexList = new ArrayList<>();
-        log.info("------- /state 共有 " + leafIndexTemplateList.size() + "个叶子节点!");
-
-        ClientSession session = SshUtil.getSession(hostname, username, password);
-        for (int i = 0; i < leafIndexTemplateList.size(); i++) {
-            IndexTemplatePO indexTemplatePO = leafIndexTemplateList.get(i);
-            String indexId = indexTemplatePO.getIndexId();
-            log.info("------- /state 开始执行对第 " + (i+1) + " 个叶子节点 " + indexId + " 进行打分!");
-            String ruleName = indexTemplatePO.getRuleName();    // 打分脚本名称,例如 AEB_1-1
-            String ruleDetails = indexTemplatePO.getRuleDetails();    // 打分脚本内容
-            String ruleFilePath = pyPath + "script/" + ruleName.split("_")[0] + "/" + ruleName + ".py";
-            log.info("------- /state 将叶子节点 " + indexId + " 对应的打分规则保存到临时目录:" + ruleFilePath);
-            FileUtil.writeInputStreamToLocalFile(IoUtil.stringToInputStream(ruleDetails), ruleFilePath);
-            Set<String> sceneIdSet = new HashSet<>();
-            String naturalIds = indexTemplatePO.getSceneNaturalIds();
-            String standardIds = indexTemplatePO.getSceneStatueIds();
-            String accidentIds = indexTemplatePO.getSceneTrafficIds();
-            if (StringUtil.isNotEmpty(naturalIds)) {
-                String[] naturalIdArray = naturalIds.split(",");
-                sceneIdSet.addAll(Arrays.asList(naturalIdArray));
-            }
-            if (StringUtil.isNotEmpty(standardIds)) {
-                String[] standardArray = standardIds.split(",");
-                sceneIdSet.addAll(Arrays.asList(standardArray));
-            }
-            if (StringUtil.isNotEmpty(accidentIds)) {
-                String[] accidentIdArray = accidentIds.split(",");
-                sceneIdSet.addAll(Arrays.asList(accidentIdArray));
-            }
-            int resultNumberOfCurrentIndex = sceneIdSet.size();
-            log.info("------- /state 叶子节点 " + indexId + " 包括 " + resultNumberOfCurrentIndex + " 个场景!");
-            log.info("------- /state 计算叶子节点 " + indexId + " 的得分!");
-            double sum = taskList.stream()
-                    .filter(task1 -> sceneIdSet.contains(task1.getSceneId()))
-                    .mapToDouble(task2 -> {
-                        String task2Id = task2.getId();
-                        taskMapper.updateState(task2Id, DictConstants.TASK_ANALYSING);
-                        // 计算每个任务的得分
-                        ScoreTO score;
-                        String runResultMinio = task2.getRunResultFilePath() + "/Ego.csv";
-                        String runResultLinux = "D:\\css-project\\仿真云平台\\Ego.csv";
-//                        python3 /home/ubuntu/test/Evaluate/main.py /home/ubuntu/test/test_data.csv 4 AEB_1-2
-//                        String command = "python3 " + pyPath + " " + runResultLinux + " " + task2.getSceneType();  // 默认使用场景名称找打分脚本
-                        String command = "python3 " + pyPath + "main.py " + runResultLinux + " " + task2.getSceneType() + " " + ruleName; // 指定打分脚本
-                        try {
-                            try {
-                                log.info("------- /state 下载 minio 上的结果文件 " + runResultMinio + " 到本地:" + runResultLinux);
-                                MinioUtil.downloadToFile(minioClient, bucketName, runResultMinio, runResultLinux);
-                            } catch (Exception e) {
-                                throw new RuntimeException("------- /state 下载 minio 上的结果文件出错:" + e.getMessage());
-                            }
-                            try {
-                                log.info("------- /state 开始执行打分命令:" + command);
-                                score = JsonUtil.jsonToBean(SshUtil.execute(session, command), ScoreTO.class);
-                                log.info("------- /state 打分结束,结果为:" + score);
-                            } catch (IOException e) {
-                                throw new RuntimeException("------- /state 任务 " + task2Id + " 打分出错,命令为:" + command + " 修改状态为:" + DictConstants.TASK_ABORTED + "\n" + e.getMessage());
-                            }
-                        } catch (Exception e) {
-                            taskMapper.updateState(task2Id, DictConstants.TASK_ABORTED);
-                            throw new RuntimeException(e.getMessage());
-                        }
-                        task2.setReturnSceneId(score.getUnit_scene_ID());
-                        task2.setScore(score.getUnit_scene_score());
-                        task2.setTargetEvaluate(score.getEvaluate_item());
-                        task2.setScoreExplain(score.getScore_description());
-                        task2.setModifyUserId(USER_ID);
-                        task2.setModifyTime(TimeUtil.getNowForMysql());
-                        return score.getUnit_scene_score();
-                    }).sum();
-            // 计算不合格的任务数(不到100分就是不合格)
-            long notStandardSceneNum = taskList.stream().filter(task1 -> sceneIdSet.contains(task1.getSceneId()) && task1.getScore() < 100).count();
-            // 叶子指标的得分(叶子指标下所有场景得分的平均值)
-            double leafIndexScore = sum / resultNumberOfCurrentIndex;
-            // -------------------------------- 保存叶子指标得分 --------------------------------
-            indexTemplatePO.setTempScore(leafIndexScore);
-            TaskIndexPO leafTaskIndex = TaskIndexPO.builder()
-                    .id(StringUtil.getRandomUUID())
-                    .pId(projectId)
-                    .target(indexTemplatePO.getIndexId())
-                    .notStandardSceneNum((int) notStandardSceneNum)
-                    .score(leafIndexScore)
-                    .build();
-            leafTaskIndex.setCreateUserId(USER_ID);
-            leafTaskIndex.setCreateTime(TimeUtil.getNowForMysql());
-            leafTaskIndex.setModifyUserId(USER_ID);
-            leafTaskIndex.setModifyTime(TimeUtil.getNowForMysql());
-            leafTaskIndex.setIsDeleted("0");
-            leafTaskIndexList.add(leafTaskIndex);
-        }
-        SshUtil.stop(session);
-
-        // 根据每个指标的得分和权重算出 project 的总得分。
-        double totalScore = compute(leafIndexTemplateList);
-        // 保存分数
-        // 保存任务分数
-        taskManager.batchUpdateByScoreResult(taskList);
-        // 保存指标分数
-        taskIndexManager.batchInsertLeafIndex(leafTaskIndexList);
-        // 保存总分数
-        TaskIndexPO totalTaskIndex = TaskIndexPO.builder()
-                .id(StringUtil.getRandomUUID())
-                .pId(projectId)
-                .target(scenePackageId)
-                .score(totalScore)
-                .build();
-        totalTaskIndex.setCreateUserId(USER_ID);
-        totalTaskIndex.setCreateTime(TimeUtil.getNowForMysql());
-        totalTaskIndex.setModifyUserId(USER_ID);
-        totalTaskIndex.setModifyTime(TimeUtil.getNowForMysql());
-        totalTaskIndex.setIsDeleted("0");
-        taskIndexMapper.insertTotalIndex(totalTaskIndex);
-
-        // 调用 server 的接口,计算评价等级
-        String tokenUrl = tokenUri + "?grant_type=client_credentials"
-                + "&client_id=" + clientId
-                + "client_secret" + clientSecret;
-        String response = HttpUtil.get(closeableHttpClient, requestConfig, tokenUrl);
-        ObjectMapper objectMapper = new ObjectMapper();
-        JsonNode jsonNode = objectMapper.readTree(response);
-        String accessToken = jsonNode.path("access_token").asText();
-        Map<String, String> headers = new HashMap<>();
-        headers.put("Authorization", "Bearer " + accessToken);
-        HttpUtil.post(closeableHttpClient, requestConfig, evaluationLevelUri, headers, null);
-
-
-    }
 
 
     public Boolean taskConfirm(String taskId) {

+ 6 - 1
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/util/MinioUtil.java

@@ -6,6 +6,7 @@ import io.minio.http.Method;
 import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.security.InvalidKeyException;
@@ -109,13 +110,17 @@ public class MinioUtil {
             String objectName,
             String targetFilePath
     ) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, io.minio.errors.InternalException {
+
+        File file = new File(targetFilePath);
+        if (!file.getParentFile().exists()) {
+            boolean mkdir = file.getParentFile().mkdirs();
+        }
         minioClient.downloadObject(DownloadObjectArgs.builder()
                 .bucket(bucketName)
                 .object(objectName)
                 .filename(targetFilePath)
                 .build());
     }
-
     /**
      * 下载文件
      */