dengzedong
3 天以前 7f0bcd00c556889ba890e5e68c681f1b5d4267e1
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.mdk.predict;
2
07890e 3 import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
4eb113 4 import com.iailab.module.model.mcs.pre.enums.ItemRunStatusEnum;
7fd198 5 import com.iailab.module.model.mcs.pre.enums.ItemStatus;
4eb113 6 import com.iailab.module.model.mcs.pre.service.MmItemStatusService;
7fd198 7 import com.iailab.module.model.mdk.factory.PredictItemFactory;
8 import com.iailab.module.model.mdk.vo.ItemVO;
9 import com.iailab.module.model.mdk.vo.PredictResultVO;
10 import lombok.extern.slf4j.Slf4j;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.stereotype.Component;
07890e 13 import org.springframework.util.CollectionUtils;
7fd198 14
15 import java.text.MessageFormat;
16 import java.time.Duration;
17 import java.time.Instant;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 /**
24  * @author PanZhibao
25  * @Description
26  * @createTime 2024年08月30日
27  */
28 @Slf4j
29 @Component
30 public class PredictModuleHandler {
31
32
33     @Autowired
34     private PredictItemFactory predictItemFactory;
35
36     @Autowired
37     private PredictResultHandler predictResultHandler;
38
4eb113 39     @Autowired
40     private MmItemStatusService mmItemStatusService;
41
7fd198 42
4f1717 43     /**
44      * 预测处理
45      *
46      * @param predictItemList
47      * @param predictTime
48      * @param intervalTime
49      * @return
50      */
07890e 51     public void predict(List<ItemVO> predictItemList, Date predictTime, int intervalTime,Map<String, PredictResultVO> predictResultMap) {
D 52         PredictResultVO predictResult;
53         Map<String, double[]> predictValueMap = null;
54         if (!CollectionUtils.isEmpty(predictResultMap)) {
55             // 将predictResultMap处理成Map<outPutId, double[]>
56             predictValueMap = new HashMap<>();
57             for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) {
58                 for (Map.Entry<MmItemOutputEntity, double[]> mmItemOutputEntityEntry : entry.getValue().getPredictMatrixs().entrySet()) {
59                     predictValueMap.put(mmItemOutputEntityEntry.getKey().getId(),mmItemOutputEntityEntry.getValue());
60                 }
61             }
62         }
7fd198 63         for (ItemVO predictItem : predictItemList) {
1a2b62 64             if (!predictItem.getStatus().equals(ItemStatus.STATUS1.getCode())) {
7fd198 65                 continue;
66             }
4eb113 67             Long totalDur = 0L;
7f0bcd 68             ItemRunStatusEnum itemRunStatusEnum = ItemRunStatusEnum.PROCESSING;
7fd198 69             try {
7f0bcd 70                 mmItemStatusService.recordStatus(predictItem.getId(), itemRunStatusEnum, totalDur, predictTime);
cf2287 71                 PredictItemHandler predictItemHandler = predictItemFactory.create(predictItem.getId());
a4fdfb 72                 long start = System.currentTimeMillis();
7fd198 73                 try {
4f1717 74                     // 预测项开始预测
7f0bcd 75                     predictResult = predictItemHandler.predict(predictTime, predictItem, predictValueMap,itemRunStatusEnum);
7fd198 76                 } catch (Exception e) {
ead005 77                     continue;
7fd198 78                 }
a4fdfb 79                 long end = System.currentTimeMillis();
80                 Long drtPre = end - start;
81                 log.info(MessageFormat.format("预测项:{0},预测时间:{1}ms", predictItem.getItemName(), drtPre));
4eb113 82                 totalDur = totalDur + drtPre;
7fd198 83
4f1717 84                 predictResult.setGranularity(predictItem.getGranularity());
7fd198 85                 predictResult.setT(intervalTime);
86                 predictResult.setSaveIndex(predictItem.getSaveIndex());
87                 predictResult.setLt(1);
4f1717 88
89                 // 保存预测结果
7fd198 90                 predictResultHandler.savePredictResult(predictResult);
a4fdfb 91                 long endSave = System.currentTimeMillis();
92                 Long drtSave = endSave - end;
93                 log.info(MessageFormat.format("预测项:{0},保存时间:{1}ms", predictItem.getItemName(),
7fd198 94                         drtSave));
4eb113 95                 totalDur = totalDur + drtSave;
07890e 96                 predictResultMap.put(predictItem.getItemNo(), predictResult);
7fd198 97             } catch (Exception e) {
98                 e.printStackTrace();
99                 log.error(MessageFormat.format("预测项编号:{0},预测项名称:{1},预测失败:{2} 预测时刻:{3}",
100                         predictItem.getId(), predictItem.getItemName(), e.getMessage(), predictTime));
7f0bcd 101                 itemRunStatusEnum = ItemRunStatusEnum.FAIL;
D 102             } finally {
103                 mmItemStatusService.recordStatus(predictItem.getId(), itemRunStatusEnum, totalDur, predictTime);
7fd198 104             }
105         }
106     }
107 }