提交 | 用户 | 时间
|
7fd198
|
1 |
package com.iailab.module.model.api; |
潘 |
2 |
|
|
3 |
import com.alibaba.fastjson.JSON; |
9162d9
|
4 |
import com.iailab.framework.common.util.object.ConvertUtils; |
7fd198
|
5 |
import com.iailab.module.model.api.mdk.MdkApi; |
潘 |
6 |
import com.iailab.module.model.api.mdk.dto.*; |
|
7 |
import com.iailab.module.model.mcs.pre.entity.DmModuleEntity; |
|
8 |
import com.iailab.module.model.mcs.pre.service.DmModuleService; |
|
9 |
import com.iailab.module.model.mcs.pre.service.MmPredictItemService; |
9162d9
|
10 |
import com.iailab.module.model.mdk.factory.ItemEntityFactory; |
潘 |
11 |
import com.iailab.module.model.mdk.factory.PredictItemFactory; |
|
12 |
import com.iailab.module.model.mdk.predict.PredictItemHandler; |
7fd198
|
13 |
import com.iailab.module.model.mdk.predict.PredictModuleHandler; |
9162d9
|
14 |
import com.iailab.module.model.mdk.predict.PredictResultHandler; |
054fb9
|
15 |
import com.iailab.module.model.mdk.schedule.ScheduleModelHandler; |
9162d9
|
16 |
import com.iailab.module.model.mdk.vo.DataValueVO; |
7fd198
|
17 |
import com.iailab.module.model.mdk.vo.ItemVO; |
潘 |
18 |
import com.iailab.module.model.mdk.vo.PredictResultVO; |
054fb9
|
19 |
import com.iailab.module.model.mdk.vo.ScheduleResultVO; |
7fd198
|
20 |
import lombok.extern.slf4j.Slf4j; |
潘 |
21 |
import org.springframework.beans.factory.annotation.Autowired; |
9162d9
|
22 |
import org.springframework.util.CollectionUtils; |
7fd198
|
23 |
import org.springframework.validation.annotation.Validated; |
潘 |
24 |
import org.springframework.web.bind.annotation.RestController; |
|
25 |
|
b2aca2
|
26 |
import java.util.*; |
7fd198
|
27 |
import java.util.stream.Collectors; |
潘 |
28 |
|
|
29 |
import static com.iailab.framework.common.pojo.CommonResult.error; |
|
30 |
import static com.iailab.framework.common.pojo.CommonResult.success; |
|
31 |
|
|
32 |
/** |
|
33 |
* @author PanZhibao |
|
34 |
* @Description |
|
35 |
* @createTime 2024年08月26日 |
|
36 |
*/ |
|
37 |
@Slf4j |
|
38 |
@RestController |
|
39 |
@Validated |
|
40 |
public class MdkApiImpl implements MdkApi { |
|
41 |
|
|
42 |
@Autowired |
|
43 |
private DmModuleService dmModuleService; |
|
44 |
|
|
45 |
@Autowired |
|
46 |
private MmPredictItemService mmPredictItemService; |
|
47 |
|
|
48 |
@Autowired |
|
49 |
private PredictModuleHandler predictModuleHandler; |
9162d9
|
50 |
|
潘 |
51 |
@Autowired |
|
52 |
private ItemEntityFactory itemEntityFactory; |
|
53 |
|
|
54 |
@Autowired |
|
55 |
private PredictItemFactory predictItemFactory; |
|
56 |
|
|
57 |
@Autowired |
|
58 |
private PredictResultHandler predictResultHandler; |
054fb9
|
59 |
|
潘 |
60 |
@Autowired |
|
61 |
private ScheduleModelHandler scheduleModelHandler; |
7fd198
|
62 |
|
潘 |
63 |
/** |
|
64 |
* 按模块预测 |
|
65 |
* |
|
66 |
* @param reqDTO |
|
67 |
* @return |
|
68 |
*/ |
|
69 |
@Override |
148842
|
70 |
public MdkPredictModuleRespDTO predictModule(MdkPredictReqDTO reqDTO) { |
7fd198
|
71 |
MdkPredictModuleRespDTO resp = new MdkPredictModuleRespDTO(); |
69bd5e
|
72 |
resp.setPredictTime(reqDTO.getPredictTime()); |
D |
73 |
resp.setModuleType(reqDTO.getModuleType()); |
|
74 |
|
7fd198
|
75 |
Map<String, MdkPredictItemRespDTO> predictItemRespMap = new HashMap<>(); |
潘 |
76 |
try { |
|
77 |
if (reqDTO.getPredictTime() == null) { |
|
78 |
throw new Exception("PredictTime不能为空"); |
|
79 |
} |
|
80 |
if (reqDTO.getModuleType() == null) { |
|
81 |
throw new Exception("ModuleType不能为空"); |
|
82 |
} |
b2aca2
|
83 |
Calendar calendar = Calendar.getInstance(); |
D |
84 |
calendar.setTime(reqDTO.getPredictTime()); |
|
85 |
calendar.set(Calendar.MILLISECOND, 0); |
|
86 |
calendar.set(Calendar.SECOND, 0); |
|
87 |
reqDTO.setPredictTime(calendar.getTime()); |
7fd198
|
88 |
log.info("预测参数:" + JSON.toJSONString(reqDTO)); |
潘 |
89 |
MdkPredictModuleRespDTO result = new MdkPredictModuleRespDTO(); |
|
90 |
result.setPredictTime(reqDTO.getPredictTime()); |
|
91 |
result.setModuleType(reqDTO.getModuleType()); |
|
92 |
List<DmModuleEntity> moduleList = dmModuleService.getModuleByModuleType(reqDTO.getModuleType()); |
|
93 |
log.info("预测计算开始: " + System.currentTimeMillis()); |
|
94 |
for (DmModuleEntity module : moduleList) { |
|
95 |
int intervalTime = 0; |
|
96 |
if (module.getPredicttime() != null) { |
|
97 |
intervalTime = (int) (reqDTO.getPredictTime().getTime() - module.getPredicttime().getTime()) / (1000 * 60); |
|
98 |
} |
|
99 |
List<ItemVO> predictItemList = mmPredictItemService.getByModuleId(module.getId()); |
07890e
|
100 |
Map<String, PredictResultVO> predictResultMap = new HashMap<>(predictItemList.size()); |
D |
101 |
// 分组,先运行normal预测项,再将结果传递给merge预测项 |
|
102 |
List<ItemVO> normalItems = predictItemList.stream().filter(e -> e.getItemType().equals("NormalItem")).collect(Collectors.toList()); |
|
103 |
if (!CollectionUtils.isEmpty(normalItems)) { |
|
104 |
predictModuleHandler.predict(normalItems, reqDTO.getPredictTime(), intervalTime,predictResultMap); |
|
105 |
List<ItemVO> mergeItem = predictItemList.stream().filter(e -> e.getItemType().equals("MergeItem")).collect(Collectors.toList()); |
|
106 |
if (!CollectionUtils.isEmpty(mergeItem)) { |
|
107 |
predictModuleHandler.predict(mergeItem, reqDTO.getPredictTime(), intervalTime,predictResultMap); |
|
108 |
} |
|
109 |
} |
4f1717
|
110 |
// 更新Module时间 |
潘 |
111 |
dmModuleService.updatePredictTime(module.getId(), reqDTO.getPredictTime()); |
|
112 |
if (reqDTO.getIsResult() == null || !reqDTO.getIsResult()) { |
|
113 |
return resp; |
|
114 |
} |
|
115 |
for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) { |
|
116 |
MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO(); |
|
117 |
itemResp.setItemId(entry.getKey()); |
|
118 |
itemResp.setPredictTime(reqDTO.getPredictTime()); |
|
119 |
Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>(); |
|
120 |
|
|
121 |
Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(entry.getValue()); |
|
122 |
for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) { |
|
123 |
List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> { |
|
124 |
MdkPredictDataDTO dto1 = new MdkPredictDataDTO(); |
|
125 |
dto1.setDataTime(t.getDataTime()); |
|
126 |
dto1.setDataValue(t.getDataValue()); |
|
127 |
return dto1; |
|
128 |
}).collect(Collectors.toList()); |
|
129 |
itemPredictData.put(dataListEntry.getKey(), predictData); |
|
130 |
} |
|
131 |
itemResp.setPredictData(itemPredictData); |
|
132 |
predictItemRespMap.put(entry.getKey(), itemResp); |
|
133 |
} |
7fd198
|
134 |
} |
潘 |
135 |
log.info("预测计算结束: " + System.currentTimeMillis()); |
|
136 |
} catch (Exception ex) { |
148842
|
137 |
ex.printStackTrace(); |
潘 |
138 |
return resp; |
7fd198
|
139 |
} |
潘 |
140 |
resp.setPredictItemRespMap(predictItemRespMap); |
148842
|
141 |
return resp; |
7fd198
|
142 |
} |
潘 |
143 |
|
|
144 |
/** |
|
145 |
* 单个预测 |
|
146 |
* |
|
147 |
* @param reqDTO |
|
148 |
* @return |
|
149 |
*/ |
|
150 |
@Override |
148842
|
151 |
public MdkPredictItemRespDTO predictItem(MdkPredictReqDTO reqDTO) { |
7fd198
|
152 |
MdkPredictItemRespDTO resp = new MdkPredictItemRespDTO(); |
潘 |
153 |
|
9162d9
|
154 |
try { |
潘 |
155 |
log.info("预测计算开始: " + System.currentTimeMillis()); |
4f1717
|
156 |
Map<String, List<MdkPredictDataDTO>> predictData = new HashMap<>(); |
9162d9
|
157 |
ItemVO predictItem = itemEntityFactory.getItemByItemNo(reqDTO.getItemNo()); |
4f1717
|
158 |
PredictItemHandler predictItemHandler = predictItemFactory.create(predictItem.getId()); |
7f0bcd
|
159 |
PredictResultVO predictResult = predictItemHandler.predict(reqDTO.getPredictTime(), predictItem,null,null); |
9162d9
|
160 |
Map<String, List<DataValueVO>> resultMap = predictResultHandler.convertToPredictData(predictResult); |
潘 |
161 |
if (!CollectionUtils.isEmpty(resultMap)) { |
|
162 |
for (Map.Entry<String, List<DataValueVO>> entry : resultMap.entrySet()) { |
4f1717
|
163 |
List<MdkPredictDataDTO> data = ConvertUtils.sourceToTarget(entry.getValue(), MdkPredictDataDTO.class); |
潘 |
164 |
predictData.put(entry.getKey(), data); |
9162d9
|
165 |
} |
潘 |
166 |
} |
|
167 |
resp.setPredictData(predictData); |
|
168 |
resp.setItemId(predictItem.getId()); |
|
169 |
resp.setPredictTime(reqDTO.getPredictTime()); |
|
170 |
log.info("预测计算结束: " + System.currentTimeMillis()); |
|
171 |
} catch (Exception ex) { |
4f1717
|
172 |
log.info("预测计算异常: " + System.currentTimeMillis(), ex); |
148842
|
173 |
return resp; |
9162d9
|
174 |
} |
潘 |
175 |
|
148842
|
176 |
return resp; |
7fd198
|
177 |
} |
潘 |
178 |
|
|
179 |
/** |
|
180 |
* 预测调整 |
|
181 |
* |
|
182 |
* @param reqDTO |
|
183 |
* @return |
|
184 |
*/ |
|
185 |
@Override |
148842
|
186 |
public Boolean predictAutoAdjust(MdkPredictReqDTO reqDTO) { |
7fd198
|
187 |
|
潘 |
188 |
|
148842
|
189 |
return true; |
7fd198
|
190 |
} |
潘 |
191 |
|
|
192 |
/** |
|
193 |
* 执行调度模型 |
|
194 |
* |
|
195 |
* @param reqDTO |
|
196 |
* @return |
|
197 |
*/ |
|
198 |
@Override |
148842
|
199 |
public MdkScheduleRespDTO doSchedule(MdkScheduleReqDTO reqDTO) { |
7fd198
|
200 |
MdkScheduleRespDTO resp = new MdkScheduleRespDTO(); |
054fb9
|
201 |
resp.setScheduleCode(reqDTO.getScheduleCode()); |
潘 |
202 |
resp.setScheduleTime(reqDTO.getScheduleTime()); |
|
203 |
try { |
|
204 |
log.info("调度计算开始: " + System.currentTimeMillis()); |
51c1c2
|
205 |
ScheduleResultVO scheduleResult = scheduleModelHandler.doSchedule(reqDTO.getScheduleCode(), reqDTO.getScheduleTime()); |
054fb9
|
206 |
resp.setResult(scheduleResult.getResult()); |
潘 |
207 |
log.info("预测计算结束: " + System.currentTimeMillis()); |
|
208 |
} catch (Exception ex) { |
|
209 |
log.info("调度计算异常: " + System.currentTimeMillis()); |
07890e
|
210 |
// ex.printStackTrace(); |
148842
|
211 |
return resp; |
054fb9
|
212 |
} |
148842
|
213 |
return resp; |
7fd198
|
214 |
} |
潘 |
215 |
} |