dengzedong
3 天以前 07890ec0dcb75572740f25a29c80f6d7bddb39e0
提交 | 用户 | 时间
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;
7fd198 68             try {
ead005 69                 mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.PROCESSING, totalDur, predictTime);
cf2287 70                 PredictItemHandler predictItemHandler = predictItemFactory.create(predictItem.getId());
a4fdfb 71                 long start = System.currentTimeMillis();
7fd198 72                 try {
4f1717 73                     // 预测项开始预测
07890e 74                     predictResult = predictItemHandler.predict(predictTime, predictItem, predictValueMap);
7fd198 75                 } catch (Exception e) {
07890e 76 //                    e.printStackTrace();
D 77 //                    log.error(String.valueOf(e));
ead005 78                     mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.FAIL, totalDur, predictTime);
79                     continue;
7fd198 80                 }
a4fdfb 81                 long end = System.currentTimeMillis();
82                 Long drtPre = end - start;
83                 log.info(MessageFormat.format("预测项:{0},预测时间:{1}ms", predictItem.getItemName(), drtPre));
4eb113 84                 totalDur = totalDur + drtPre;
7fd198 85
4f1717 86                 predictResult.setGranularity(predictItem.getGranularity());
7fd198 87                 predictResult.setT(intervalTime);
88                 predictResult.setSaveIndex(predictItem.getSaveIndex());
89                 predictResult.setLt(1);
4f1717 90
91                 // 保存预测结果
7fd198 92                 predictResultHandler.savePredictResult(predictResult);
a4fdfb 93                 long endSave = System.currentTimeMillis();
94                 Long drtSave = endSave - end;
95                 log.info(MessageFormat.format("预测项:{0},保存时间:{1}ms", predictItem.getItemName(),
7fd198 96                         drtSave));
4eb113 97                 totalDur = totalDur + drtSave;
ead005 98                 mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.SUCCESS, totalDur, predictTime);
07890e 99                 predictResultMap.put(predictItem.getItemNo(), predictResult);
7fd198 100             } catch (Exception e) {
101                 e.printStackTrace();
102                 log.error(MessageFormat.format("预测项编号:{0},预测项名称:{1},预测失败:{2} 预测时刻:{3}",
103                         predictItem.getId(), predictItem.getItemName(), e.getMessage(), predictTime));
ead005 104                 mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.FAIL, totalDur, predictTime);
7fd198 105             }
106         }
107     }
108 }