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