From 4f171763ce5ee065a704e5d08fcbd5a3379dec85 Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期三, 13 十一月 2024 14:22:31 +0800 Subject: [PATCH] 返回预测结果 --- iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictReqDTO.java | 5 + iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java | 42 ++++---- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/common/enums/CommonConstant.java | 6 + iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictModuleRespDTO.java | 3 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemNormalHandlerImpl.java | 28 ++--- iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictItemRespDTO.java | 6 + iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java | 59 +++++------ iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemMergeHandlerImpl.java | 8 + iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java | 3 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java | 38 ------- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModelHandler.java | 8 + iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmItemResultService.java | 1 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictItemHandler.java | 8 + iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java | 9 + iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModuleHandler.java | 13 ++ iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java | 35 +++++- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/vo/PredictResultVO.java | 9 + 17 files changed, 164 insertions(+), 117 deletions(-) diff --git a/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictItemRespDTO.java b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictItemRespDTO.java index d67c117..424af2c 100644 --- a/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictItemRespDTO.java +++ b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictItemRespDTO.java @@ -5,8 +5,11 @@ import java.util.Date; import java.util.List; +import java.util.Map; /** + * 单个预测项预测结果 + * * @author PanZhibao * @Description * @createTime 2024年08月26日 @@ -19,5 +22,6 @@ private Date predictTime; - private List<MdkPredictDataDTO> predictData; + @Schema(description = "单个预测项预测结果,KEY为预测项目编码") + private Map<String, List<MdkPredictDataDTO>> predictData; } \ No newline at end of file diff --git a/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictModuleRespDTO.java b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictModuleRespDTO.java index 987fb79..185709f 100644 --- a/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictModuleRespDTO.java +++ b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictModuleRespDTO.java @@ -1,5 +1,6 @@ package com.iailab.module.model.api.mdk.dto; +import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; @@ -15,9 +16,11 @@ @Data public class MdkPredictModuleRespDTO { + @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8") private Date predictTime; private String moduleType; + @Schema(description = "模块预测结果,KEY为预测项目编码") private Map<String, MdkPredictItemRespDTO> predictItemRespMap; } \ No newline at end of file diff --git a/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictReqDTO.java b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictReqDTO.java index 6b44ced..f039812 100644 --- a/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictReqDTO.java +++ b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mdk/dto/MdkPredictReqDTO.java @@ -1,5 +1,6 @@ package com.iailab.module.model.api.mdk.dto; +import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; @@ -17,6 +18,7 @@ @Schema(description = "预测时间") @NotNull(message="预测时间不能为空") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8") private Date predictTime; @Schema(description = "预测模块(管网类型)") @@ -24,4 +26,7 @@ @Schema(description = "预测项编号") private String itemNo; + + @Schema(description = "是否返回预测结果") + private Boolean isResult; } \ No newline at end of file diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java index 89994ce..eb7267e 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java @@ -99,34 +99,32 @@ } List<ItemVO> predictItemList = mmPredictItemService.getByModuleId(module.getId()); Map<String, PredictResultVO> predictResultMap = predictModuleHandler.predict(predictItemList, reqDTO.getPredictTime(), intervalTime); + // 更新Module时间 + dmModuleService.updatePredictTime(module.getId(), reqDTO.getPredictTime()); -// for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) { -// for (Map.Entry<String, List<DataValueVO>> dataListEntry : entry.getValue().getPredictLists().entrySet()) { -// List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> { -// MdkPredictDataDTO dto1 = new MdkPredictDataDTO(); -// dto1.setDataTime(t.getDataTime()); -// dto1.setDataValue(t.getDataValue()); -// return dto1; -// }).collect(Collectors.toList()); -// MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO(); -// itemResp.setItemId(dataListEntry.getKey()); -// itemResp.setPredictData(predictData); -// predictItemRespMap.put(entry.getKey(), itemResp); -// } -// } + if (reqDTO.getIsResult() == null || !reqDTO.getIsResult()) { + return resp; + } -// for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) { -// List<MdkPredictDataDTO> predictData = entry.getValue().getPredictList().stream().map(t-> { -// MdkPredictDataDTO dto1 = new MdkPredictDataDTO(); -// dto1.setDataTime(t.getDataTime()); -// dto1.setDataValue(t.getDataValue()); -// return dto1; -// }).collect(Collectors.toList()); -// MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO(); -// itemResp.setItemId(entry.getValue().getPredictId()); -// itemResp.setPredictData(predictData); -// predictItemRespMap.put(entry.getKey(), itemResp); -// } + for (Map.Entry<String, PredictResultVO> entry : predictResultMap.entrySet()) { + MdkPredictItemRespDTO itemResp = new MdkPredictItemRespDTO(); + itemResp.setItemId(entry.getKey()); + itemResp.setPredictTime(reqDTO.getPredictTime()); + Map<String, List<MdkPredictDataDTO>> itemPredictData = new HashMap<>(); + + Map<String, List<DataValueVO>> predictLists = predictResultHandler.convertToPredictData2(entry.getValue()); + for (Map.Entry<String, List<DataValueVO>> dataListEntry : predictLists.entrySet()) { + List<MdkPredictDataDTO> predictData = dataListEntry.getValue().stream().map(t -> { + MdkPredictDataDTO dto1 = new MdkPredictDataDTO(); + dto1.setDataTime(t.getDataTime()); + dto1.setDataValue(t.getDataValue()); + return dto1; + }).collect(Collectors.toList()); + itemPredictData.put(dataListEntry.getKey(), predictData); + } + itemResp.setPredictData(itemPredictData); + predictItemRespMap.put(entry.getKey(), itemResp); + } } log.info("预测计算结束: " + System.currentTimeMillis()); } catch (Exception ex) { @@ -149,14 +147,15 @@ try { log.info("预测计算开始: " + System.currentTimeMillis()); - List<MdkPredictDataDTO> predictData = new ArrayList<>(); + Map<String, List<MdkPredictDataDTO>> predictData = new HashMap<>(); ItemVO predictItem = itemEntityFactory.getItemByItemNo(reqDTO.getItemNo()); - PredictItemHandler predictItemHandler = (PredictItemHandler)predictItemFactory.create(predictItem.getId()); + PredictItemHandler predictItemHandler = predictItemFactory.create(predictItem.getId()); PredictResultVO predictResult = predictItemHandler.predict(reqDTO.getPredictTime(), predictItem); Map<String, List<DataValueVO>> resultMap = predictResultHandler.convertToPredictData(predictResult); if (!CollectionUtils.isEmpty(resultMap)) { for (Map.Entry<String, List<DataValueVO>> entry : resultMap.entrySet()) { - predictData = ConvertUtils.sourceToTarget(entry.getValue(), MdkPredictDataDTO.class); + List<MdkPredictDataDTO> data = ConvertUtils.sourceToTarget(entry.getValue(), MdkPredictDataDTO.class); + predictData.put(entry.getKey(), data); } } resp.setPredictData(predictData); @@ -164,7 +163,7 @@ resp.setPredictTime(reqDTO.getPredictTime()); log.info("预测计算结束: " + System.currentTimeMillis()); } catch (Exception ex) { - log.info("预测计算异常: " + System.currentTimeMillis(),ex); + log.info("预测计算异常: " + System.currentTimeMillis(), ex); return resp; } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/common/enums/CommonConstant.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/common/enums/CommonConstant.java index 8c80215..0594498 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/common/enums/CommonConstant.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/common/enums/CommonConstant.java @@ -15,4 +15,10 @@ BigDecimal ZERO_VALUE = new BigDecimal("0"); String MDK_SUFFIX = ".miail"; + + String MDK_RESULT = "result"; + + String MDK_STATUS_CODE = "status_code"; + + String MDK_STATUS_100 = "100"; } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java index ea7d041..fe83757 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/DmModuleService.java @@ -4,6 +4,7 @@ import com.iailab.module.model.mcs.pre.entity.DmModuleEntity; import com.iailab.module.model.mcs.pre.vo.DmModulePageReqVO; +import java.util.Date; import java.util.List; import java.util.Map; @@ -30,4 +31,6 @@ DmModuleEntity info(String id); DmModuleEntity getModuleByItemId(String itemId); + + void updatePredictTime(String id, Date predictTime); } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmItemResultService.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmItemResultService.java index a5d28b6..f4d57d4 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmItemResultService.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmItemResultService.java @@ -1,6 +1,5 @@ package com.iailab.module.model.mcs.pre.service; -import com.iailab.module.data.api.point.dto.ApiPointDTO; import com.iailab.module.model.mcs.pre.entity.MmItemResultEntity; import com.iailab.module.model.mdk.vo.DataValueVO; diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java index 962f7e8..2ed8c90 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/DmModuleServiceImpl.java @@ -86,7 +86,16 @@ params.put("moduletype", moduletype); QueryWrapper<DmModuleEntity> wrapper = getWrapper(params); return dmModuleDao.selectList(wrapper); + } + @Override + public void updatePredictTime(String id, Date predictTime) { + DmModuleEntity entity = dmModuleDao.selectById(id); + if (entity == null) { + return; + } + entity.setPredicttime(predictTime); + dmModuleDao.updateById(entity); } @Override diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java index b69168a..03e3c52 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java @@ -75,9 +75,9 @@ List<DataValueVO> lastVoList = new ArrayList<>(); int size = entry.getValue().size(); - t = t > 0 ? t : 0; + t = Math.max(t, 0); int n = "n".equals(nIndex) ? size : Integer.parseInt(nIndex); - int length = (n - t) > 0 ? (n - t) : 0; //预测完不变的数据长度 + int length = Math.max((n - t), 0); //预测完不变的数据长度 if (size >= n) { for (int i = 0; i < (size - length); i ++) { int index = length + i; @@ -112,41 +112,7 @@ params.put("STARTTIME", importList.get(0).getDatatime()); params.put("ENDTIME", importList.get(importList.size() - 1).getDatatime()); mmItemResultDao.deletePredictValue(params); - - } - -// Map<String, Object> params = new HashMap(4); -// params.put("TABLENAME", T_MM_ITEM_RESULT); -// params.put("OUTPUTID", importList.get(0).getOutputid()); -// params.put("STARTTIME", importList.get(0).getDatatime()); -// params.put("ENDTIME", importList.get(importList.size() - 1).getDatatime()); -// mmItemResultDao.deletePredictValue(params); - -// int num1 = importList.size() / max_group_count; -// int num2 = importList.size() % max_group_count; -// if (num2 != 0) { -// num1++; -// } -// -// List<MmItemResultEntity> tempList; -// //先删除已经存在的数据,再插入新数据 -// for (int i = 0; i < num1; i++) { -// int startIndex = max_group_count * i; -// int count = max_group_count; -// if (num2!=0 && i == num1 - 1) { -// count = num2; -// } -// tempList = new ArrayList<>(); -// //获取某个索引范围内的对象集合 -// for (int j = startIndex; j < startIndex + count; j++) { -// tempList.add(importList.get(j)); -// } -// Map<String, Object> map2 = new HashMap<>(2); -// map2.put("TABLENAME", T_MM_ITEM_RESULT); -// map2.put("list", tempList); -// mmItemResultDao.savePredictValue(map2); -// } mmItemResultDao.insertBatch(importList,max_group_count); Map<String, Object> map3 = new HashMap<>(2); diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictItemHandler.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictItemHandler.java index ec51294..c0d15dc 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictItemHandler.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictItemHandler.java @@ -13,5 +13,13 @@ */ public interface PredictItemHandler { + /** + * 单个预测项预测 + * + * @param predictTime + * @param predictItemDto + * @return + * @throws ItemInvokeException + */ PredictResultVO predict(Date predictTime, ItemVO predictItemDto) throws ItemInvokeException; } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModelHandler.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModelHandler.java index 5253a3c..9379a02 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModelHandler.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModelHandler.java @@ -13,5 +13,13 @@ */ public interface PredictModelHandler { + /** + * 根据模型预测 + * + * @param predictTime + * @param predictModel + * @return + * @throws ModelInvokeException + */ PredictResultVO predictByModel(Date predictTime, MmPredictModelEntity predictModel) throws ModelInvokeException; } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModuleHandler.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModuleHandler.java index 2b07c1d..ecf8a08 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModuleHandler.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictModuleHandler.java @@ -1,6 +1,5 @@ package com.iailab.module.model.mdk.predict; -import com.iailab.module.model.mcs.pre.entity.MmItemStatusEntity; 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; @@ -39,6 +38,14 @@ private MmItemStatusService mmItemStatusService; + /** + * 预测处理 + * + * @param predictItemList + * @param predictTime + * @param intervalTime + * @return + */ public Map<String, PredictResultVO> predict(List<ItemVO> predictItemList, Date predictTime, int intervalTime) { Map<String, PredictResultVO> result = new HashMap<>(); @@ -53,6 +60,7 @@ PredictItemHandler predictItemHandler = predictItemFactory.create(predictItem.getId()); Instant start = Instant.now(); try { + // 预测项开始预测 predictResult = predictItemHandler.predict(predictTime, predictItem); } catch (Exception e) { e.printStackTrace(); @@ -63,9 +71,12 @@ log.info(MessageFormat.format("预测项:{0},预测时间:{1}秒", predictItem.getItemName(), drtPre)); totalDur = totalDur + drtPre; + predictResult.setGranularity(predictItem.getGranularity()); predictResult.setT(intervalTime); predictResult.setSaveIndex(predictItem.getSaveIndex()); predictResult.setLt(1); + + // 保存预测结果 predictResultHandler.savePredictResult(predictResult); Instant endSave = Instant.now(); Long drtSave = Duration.between(end, endSave).getSeconds(); diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java index 8bc9517..86b5052 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java @@ -1,9 +1,6 @@ package com.iailab.module.model.mdk.predict; import com.baomidou.dynamic.datasource.annotation.DSTransactional; -import com.iailab.module.data.api.point.DataPointApi; -import com.iailab.module.data.api.point.dto.ApiPointDTO; -import com.iailab.module.data.enums.DataPointFreqEnum; import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; import com.iailab.module.model.mcs.pre.service.MmItemResultService; import com.iailab.module.model.mdk.factory.ItemEntityFactory; @@ -25,9 +22,6 @@ @Autowired private ItemEntityFactory itemEntityFactory; - @Autowired - private DataPointApi dataPointApi; - /** * convertToPredictData * @@ -45,8 +39,6 @@ Map<MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs(); HashMap<String,List<DataValueVO>> predictLists = new HashMap<>(); for (Map.Entry<MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) { - ApiPointDTO point = dataPointApi.getInfoById(entry.getKey().getPointid()); - Integer pointMinfreq = DataPointFreqEnum.getEumByCode(point.getMinfreqid()).getValue(); Integer rows = entry.getValue().length; List<DataValueVO> predictDataList = new ArrayList<>(); Calendar calendar = Calendar.getInstance(); @@ -57,14 +49,39 @@ predictData.setDataValue(Double.valueOf(entry.getValue()[i])); predictDataList.add(predictData); - calendar.add(Calendar.SECOND, pointMinfreq); + calendar.add(Calendar.SECOND, predictResult.getGranularity()); } resultMap.put(entry.getKey().getId(), predictDataList); + predictLists.put(entry.getKey().getResultstr(), predictDataList); } predictResult.setPredictLists(predictLists); return resultMap; } + public Map<String, List<DataValueVO>> convertToPredictData2(PredictResultVO predictResult) { + Map<String, List<DataValueVO>> predictLists = new HashMap<>(); + if (!CollectionUtils.isEmpty(predictResult.getPredictList())) { + return predictLists; + } + Map<MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs(); + for (Map.Entry<MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) { + Integer rows = entry.getValue().length; + List<DataValueVO> predictDataList = new ArrayList<>(); + Calendar calendar = Calendar.getInstance(); + calendar.setTime(predictResult.getPredictTime()); + for (Integer i = 0; i < rows; i++) { + DataValueVO predictData = new DataValueVO(); + predictData.setDataTime(calendar.getTime()); + predictData.setDataValue(Double.valueOf(entry.getValue()[i])); + predictDataList.add(predictData); + + calendar.add(Calendar.SECOND, predictResult.getGranularity()); + } + predictLists.put(entry.getKey().getResultstr(), predictDataList); + } + return predictLists; + } + /** * savePredictResult * diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemMergeHandlerImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemMergeHandlerImpl.java index 5e4e599..2445bef 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemMergeHandlerImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemMergeHandlerImpl.java @@ -41,6 +41,14 @@ @Autowired private PredictResultHandler predictResultHandler; + /** + * MergeItem预测 + * + * @param predictTime + * @param predictItemDto + * @return + * @throws ItemInvokeException + */ @Override public PredictResultVO predict(Date predictTime, ItemVO predictItemDto) throws ItemInvokeException { diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemNormalHandlerImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemNormalHandlerImpl.java index 5131d2a..d32884b 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemNormalHandlerImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemNormalHandlerImpl.java @@ -34,13 +34,20 @@ @Autowired private PredictModelHandler predictModelHandler; + /** + * NormalItem预测 + * + * @param predictTime + * @param predictItemDto + * @return + * @throws ItemInvokeException + */ @Override public PredictResultVO predict(Date predictTime, ItemVO predictItemDto) throws ItemInvokeException{ - String itemId = predictItemDto.getId(); - PredictResultVO finalResult = new PredictResultVO(); PredictResultVO predictResult = new PredictResultVO(); - List<PredictResultVO> predictResultList = new ArrayList<>(); + String itemId = predictItemDto.getId(); + predictResult.setPredictId(itemId); try { // 获取预测项模型 List<MmPredictModelEntity> predictModelList = mmPredictModelService.getActiveModelByItemId(itemId); @@ -48,17 +55,8 @@ throw new ModelInvokeException(MessageFormat.format("{0},itemId={1}", ModelInvokeException.errorGetModelEntity, itemId)); } - for (MmPredictModelEntity predictModel : predictModelList) { - predictResult = predictModelHandler.predictByModel(predictTime, predictModel); - predictResult.setPredictId(itemId); - predictResultList.add(predictResult); - } - /*Calendar calendar = Calendar.getInstance(); - calendar.setTime(predictTime); - calendar.add(Calendar.MINUTE, predictResult.getPredictMatrix().length - 1); - Timestamp endTime = new Timestamp(calendar.getTime().getTime());*/ - finalResult = predictResultList.get(0); - + MmPredictModelEntity predictModel = predictModelList.get(0); + predictResult = predictModelHandler.predictByModel(predictTime, predictModel); } catch (Exception ex) { ex.printStackTrace(); //预测项预测失败的状态 @@ -66,6 +64,6 @@ ItemInvokeException.errorItemFailed, itemId)); } - return finalResult; + return predictResult; } } \ No newline at end of file diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java index 0f05ef4..5a107da 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java @@ -2,16 +2,13 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; -import com.iail.IAILMDK; import com.iail.model.IAILModel; -import com.iailab.module.model.mcs.pre.controller.admin.MmItemOutputController; +import com.iailab.module.model.common.enums.CommonConstant; import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; import com.iailab.module.model.mcs.pre.entity.MmModelArithSettingsEntity; -import com.iailab.module.model.mcs.pre.entity.MmModelResultstrEntity; import com.iailab.module.model.mcs.pre.entity.MmPredictModelEntity; import com.iailab.module.model.mcs.pre.service.MmItemOutputService; import com.iailab.module.model.mcs.pre.service.MmModelArithSettingsService; -import com.iailab.module.model.mcs.pre.service.MmModelResultstrService; import com.iailab.module.model.mdk.common.enums.TypeA; import com.iailab.module.model.mdk.common.exceptions.ModelInvokeException; import com.iailab.module.model.mdk.predict.PredictModelHandler; @@ -21,7 +18,6 @@ import com.iailab.module.model.mpk.common.utils.DllUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.parameters.P; import org.springframework.stereotype.Component; import java.util.*; @@ -39,15 +35,21 @@ private MmModelArithSettingsService mmModelArithSettingsService; @Autowired - private MmModelResultstrService mmModelResultstrService; - @Autowired private MmItemOutputService mmItemOutputService; @Autowired private SampleConstructor sampleConstructor; + /** + * 根据模型预测,返回预测结果 + * + * @param predictTime + * @param predictModel + * @return + * @throws ModelInvokeException + */ @Override - public PredictResultVO predictByModel(Date predictTime, MmPredictModelEntity predictModel) throws ModelInvokeException { + public synchronized PredictResultVO predictByModel(Date predictTime, MmPredictModelEntity predictModel) throws ModelInvokeException { PredictResultVO result = new PredictResultVO(); if (predictModel == null) { throw new ModelInvokeException("modelEntity is null"); @@ -70,10 +72,10 @@ int portLength = sampleDataList.size(); Object[] param2Values = new Object[portLength + 2]; for (int i = 0; i < portLength; i++) { - param2Values[i]=sampleDataList.get(i).getMatrix(); + param2Values[i] = sampleDataList.get(i).getMatrix(); } param2Values[portLength] = newModelBean.getDataMap().get("models"); - param2Values[portLength+1] = settings; + param2Values[portLength + 1] = settings; log.info("#######################预测模型 " + predictModel.getItemid() + " ##########################"); JSONObject jsonObjNewModelBean = new JSONObject(); @@ -84,13 +86,12 @@ log.info(String.valueOf(jsonObjParam2Values)); //IAILMDK.run -// HashMap<String, Object> modelResult = IAILMDK.run(newModelBean, param2Values); HashMap<String, Object> modelResult = DllUtils.run(newModelBean, param2Values, predictModel.getMpkprojectid()); - if(!modelResult.containsKey("status_code") || !modelResult.containsKey("result") || Integer.parseInt(modelResult.get("status_code").toString()) != 100) { + if (!modelResult.containsKey(CommonConstant.MDK_STATUS_CODE) || !modelResult.containsKey(CommonConstant.MDK_RESULT) || + !modelResult.get(CommonConstant.MDK_STATUS_CODE).toString().equals(CommonConstant.MDK_STATUS_100)) { throw new RuntimeException("模型结果异常:" + modelResult); } - - modelResult = (HashMap<String, Object>) modelResult.get("result"); + modelResult = (HashMap<String, Object>) modelResult.get(CommonConstant.MDK_RESULT); //打印结果 JSONObject jsonObjResult = new JSONObject(); jsonObjResult.put("result", modelResult); @@ -99,7 +100,7 @@ List<MmItemOutputEntity> ItemOutputList = mmItemOutputService.getByItemid(predictModel.getItemid()); log.info("模型计算完成:modelId=" + modelId + modelResult); - Map<MmItemOutputEntity,double[]> predictMatrixs = new HashMap<>(ItemOutputList.size()); + Map<MmItemOutputEntity, double[]> predictMatrixs = new HashMap<>(ItemOutputList.size()); for (MmItemOutputEntity outputEntity : ItemOutputList) { String resultStr = outputEntity.getResultstr(); @@ -111,8 +112,8 @@ for (int i = 0; i < temp.length; i++) { temp1[i] = temp[i].doubleValue(); } - predictMatrixs.put(outputEntity,temp1); - }else if (outputEntity.getResultType() == 2) { + predictMatrixs.put(outputEntity, temp1); + } else if (outputEntity.getResultType() == 2) { // 二维数组 Double[][] temp = (Double[][]) modelResult.get(resultStr); Double[] temp2 = temp[outputEntity.getResultIndex()]; @@ -120,20 +121,17 @@ for (int i = 0; i < temp2.length; i++) { temp1[i] = temp2[i].doubleValue(); } - predictMatrixs.put(outputEntity,temp1); + predictMatrixs.put(outputEntity, temp1); } } } - result.setPredictMatrixs(predictMatrixs); result.setModelResult(modelResult); result.setPredictTime(predictTime); } catch (Exception ex) { - log.error("IAILModel对象构造失败,modelId=" + modelId); + log.error("调用发生异常,异常信息为:{}", ex); log.error(ex.getMessage()); - log.error("调用发生异常,异常信息为:{}" , ex); ex.printStackTrace(); - } return result; } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/vo/PredictResultVO.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/vo/PredictResultVO.java index b7d334a..be560fe 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/vo/PredictResultVO.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/vo/PredictResultVO.java @@ -35,7 +35,7 @@ /** * 统一预测入口的预测类型(循环调用、手动调用) Map<MmItemOutputEntity,double[]> */ - private Map<MmItemOutputEntity,double[]> predictMatrixs; + private Map<MmItemOutputEntity, double[]> predictMatrixs; /** * 时间间隔 (当前预测时间 与 上一次预测时间 相差的分钟数;系统计算得出) @@ -53,13 +53,18 @@ private String saveIndex; /** + * 粒度 + */ + private Integer granularity; + + /** * 预测集合 */ private List<DataValueVO> predictList; /** * 预测集合 */ - private HashMap<String,List<DataValueVO>> predictLists; + private HashMap<String, List<DataValueVO>> predictLists; /** * 模型结果 -- Gitblit v1.9.3