|
@@ -1,34 +1,116 @@
|
|
|
package com.css.simulation.resource.scheduler.service;
|
|
|
|
|
|
+import api.common.pojo.constants.DictConstants;
|
|
|
+import api.common.util.CollectionUtil;
|
|
|
+import api.common.util.StringUtil;
|
|
|
+import api.common.util.TimeUtil;
|
|
|
import com.css.simulation.resource.scheduler.feign.CommonService;
|
|
|
+import com.css.simulation.resource.scheduler.mapper.IndexMapper;
|
|
|
+import com.css.simulation.resource.scheduler.mapper.ProjectMapper;
|
|
|
+import com.css.simulation.resource.scheduler.mapper.TaskMapper;
|
|
|
+import com.css.simulation.resource.scheduler.pojo.po.IndexPO;
|
|
|
+import com.css.simulation.resource.scheduler.pojo.po.ProjectPO;
|
|
|
+import com.css.simulation.resource.scheduler.pojo.po.TaskPO;
|
|
|
+import com.css.simulation.resource.scheduler.util.ScoreUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
@Service
|
|
|
public class TaskService {
|
|
|
|
|
|
+
|
|
|
@Autowired
|
|
|
private CommonService commonService;
|
|
|
+ @Autowired
|
|
|
+ private ProjectMapper projectMapper;
|
|
|
+ @Autowired
|
|
|
+ private TaskMapper taskMapper;
|
|
|
+ @Autowired
|
|
|
+ private IndexMapper indexMapper;
|
|
|
|
|
|
|
|
|
public void taskTick(String taskId) {
|
|
|
- // 将 taskId 存储到 redis,并刷新过期时间
|
|
|
-// commonService.set(new RedisParameter(taskId));
|
|
|
+ // 刷新 mysql 心跳时间
|
|
|
+ taskMapper.updateTickTime(taskId, TimeUtil.getNowForMysql());
|
|
|
}
|
|
|
|
|
|
|
|
|
- public void taskState(String taskId, String taskState) {
|
|
|
+ public void taskState(String taskId, String state) {
|
|
|
//1 根据 taskId 修改任务状态 taskState。
|
|
|
- //2 如果 taskState 为完成状态,项目的已完成任务数+1。
|
|
|
- //3 如果已完成任务数等于所有任务数量,调用打分程序,对项目中每个指标进行打分,
|
|
|
- //4 根据每个指标的得分和权重算出 project 的总得分。
|
|
|
+ taskMapper.updateState(taskId, state);
|
|
|
+ //2 如果 taskState 为完成状态,校验一下项目的已完成数量。
|
|
|
+ ProjectPO projectPO = projectMapper.selectById(taskId);
|
|
|
+ String projectId = projectPO.getId();
|
|
|
+ String scenePackageId = projectPO.getScenePackageId();
|
|
|
+ int taskNum = projectMapper.selectTaskNumById(projectId);
|
|
|
+ int completedTaskNum = projectMapper.selectTaskNumByProjectIdAndState(projectId, DictConstants.TASK_COMPLETED);
|
|
|
+ if (taskNum != completedTaskNum) {
|
|
|
+ return;
|
|
|
+ } //3 如果已完成任务数等于所有任务数量,调用打分程序,对项目中每个指标进行打分,
|
|
|
+ List<TaskPO> taskList = taskMapper.selectTaskListByProjectId(projectId); // 所有任务信息
|
|
|
+ List<IndexPO> leafIndexList = indexMapper.selectLeafIndexWithRuleDetailsByPackageId(scenePackageId);
|
|
|
|
|
|
+ // 计算所有叶子节点指标的得分
|
|
|
+ for (IndexPO indexPO : leafIndexList) {
|
|
|
+ String ruleDetails = indexPO.getRuleDetails();
|
|
|
+ Set<String> sceneIdSet = new HashSet<>();
|
|
|
+ String naturalIds = indexPO.getSceneNaturalIds();
|
|
|
+ String standardIds = indexPO.getSceneStatueIds();
|
|
|
+ String accidentIds = indexPO.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();
|
|
|
+ double sum = taskList.stream()
|
|
|
+ .filter(task1 -> sceneIdSet.contains(task1.getSceneId()))
|
|
|
+ .mapToDouble(task2 -> {
|
|
|
+ // 计算每个任务的得分
|
|
|
+ double score = ScoreUtil.score(task2.getRunResult(), ruleDetails);
|
|
|
+ task2.setScore(score);
|
|
|
+ return score;
|
|
|
+ }).sum();
|
|
|
+ double indexScore = sum / resultNumberOfCurrentIndex;
|
|
|
+ indexPO.setScore(indexScore);
|
|
|
+ }
|
|
|
+ // 计算不合格的任务总数(不到100分就是不合格)
|
|
|
+ int unqualified = (int) taskList.stream().filter(taskPO -> taskPO.getScore() < 100).count();
|
|
|
+ //4 根据每个指标的得分和权重算出 project 的总得分。
|
|
|
+ double totalScore = compute(leafIndexList);
|
|
|
+ //5 保存指标的分数和总分数
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
public Boolean taskConfirm(String taskId) {
|
|
|
// 将 taskId 存储到 redis,并刷新过期时间
|
|
|
// commonService.set(new RedisParameter(taskId,));
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ public double compute(List<IndexPO> leaf) {
|
|
|
+ double result = 0.0;
|
|
|
+ Map<String, List<IndexPO>> groups = leaf.stream().collect(Collectors.groupingBy(IndexPO::getParentId));
|
|
|
+ Set<String> idSet = groups.keySet();
|
|
|
+ List<IndexPO> indexPOList = indexMapper.selectByIdList(CollectionUtil.setToList(idSet));
|
|
|
+ indexPOList.forEach(index1 -> {
|
|
|
+ double sum = groups.get(index1.getIndexId()).stream().mapToDouble(index2 -> index2.getScore() * Double.parseDouble(index2.getWeight())).sum();
|
|
|
+ index1.setScore(sum);
|
|
|
+ });
|
|
|
+ if (indexPOList.size() > 1) {
|
|
|
+ result = result + compute(indexPOList);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|