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