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