From b2aca2d1f4d9d23d7d2ff018f60faa3b3706f9f0 Mon Sep 17 00:00:00 2001 From: dengzedong <dengzedong@email> Date: 星期一, 11 十一月 2024 10:50:48 +0800 Subject: [PATCH] model run --- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java | 7 +++ iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleDataConstructor.java | 5 +- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleInfoConstructor.java | 4 +- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java | 10 +++-- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleDataConstructor.java | 94 ++++++++++++++++++++++++++++++++++++++++------- 5 files changed, 97 insertions(+), 23 deletions(-) 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 a5d05a8..1fd8c7f 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 @@ -24,10 +24,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RestController; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; import static com.iailab.framework.common.pojo.CommonResult.error; @@ -81,6 +78,11 @@ if (reqDTO.getModuleType() == null) { throw new Exception("ModuleType不能为空"); } + Calendar calendar = Calendar.getInstance(); + calendar.setTime(reqDTO.getPredictTime()); + calendar.set(Calendar.MILLISECOND, 0); + calendar.set(Calendar.SECOND, 0); + reqDTO.setPredictTime(calendar.getTime()); log.info("预测参数:" + JSON.toJSONString(reqDTO)); MdkPredictModuleRespDTO result = new MdkPredictModuleRespDTO(); result.setPredictTime(reqDTO.getPredictTime()); 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 41246be..ef75d02 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 @@ -83,6 +83,11 @@ //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) { + throw new RuntimeException("模型结果异常:" + modelResult); + } + + modelResult = (HashMap<String, Object>) modelResult.get("result"); //打印结果 JSONObject jsonObjResult = new JSONObject(); jsonObjResult.put("result", modelResult); @@ -135,7 +140,7 @@ newModelBean.setParamsArray(paramsArray); HashMap<String, Object> dataMap = new HashMap<>(); HashMap<String, String> models = new HashMap<>(1); - models.put("paramFile", predictModel.getModelpath()); + models.put("model_path", predictModel.getModelpath()); dataMap.put("models", models); newModelBean.setDataMap(dataMap); return newModelBean; diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleDataConstructor.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleDataConstructor.java index 0828ae3..c9a6afa 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleDataConstructor.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleDataConstructor.java @@ -90,7 +90,8 @@ } //补全数据 ColumnItem columnItem = entry.getColumnItemList().get(i); - dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.getEndTime(), columnItem.granularity); +// dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.getEndTime(), columnItem.granularity); + dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime, columnItem.paramId,columnItem.getParamType()); /** 如果数据取不满,把缺失的数据点放在后面 */ if (dataEntityList != null && dataEntityList.size() != 0) { @@ -122,7 +123,7 @@ String paramType = columnItem.getParamType(); switch (paramType) { case "DATAPOINT": - ApiPointDTO point = dataPointApi.getInfoById(columnItem.getId()); + ApiPointDTO point = dataPointApi.getInfoById(columnItem.getParamId()); ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); queryDto.setPointNo(point.getPointNo()); queryDto.setStart(columnItem.getStartTime()); diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleDataConstructor.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleDataConstructor.java index bee81d5..34a5900 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleDataConstructor.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleDataConstructor.java @@ -1,17 +1,26 @@ package com.iailab.module.model.mdk.sample; +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.mdk.common.enums.ModelParamType; import com.iailab.module.model.mdk.sample.dto.SampleData; import com.iailab.module.model.mdk.sample.dto.SampleInfo; import com.iailab.module.model.mdk.vo.DataValueVO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; +import java.sql.Timestamp; import java.util.*; abstract class SampleDataConstructor { private Logger logger = LoggerFactory.getLogger(getClass()); + + @Autowired + private DataPointApi dataPointApi; /** * prepareSampleData @@ -30,25 +39,85 @@ * @param endTime * @return */ - public List<DataValueVO> completionData(int length, List<DataValueVO> dataEntityList, Date startTime, Date endTime, int granularity) { - if (CollectionUtils.isEmpty(dataEntityList) || length <= dataEntityList.size()) { + public List<DataValueVO> completionData(int length, List<DataValueVO> dataEntityList, Date startTime, Date endTime, String paramId,String paramType) { + if (CollectionUtils.isEmpty(dataEntityList) || length == dataEntityList.size()) { return dataEntityList; + }else if (length < dataEntityList.size()){ + return dataEntityList.subList(dataEntityList.size()-length,dataEntityList.size()); } - logger.info("补全数据, length =" + length + "; size = " + dataEntityList.size() + "; startTime = " + startTime.getTime() + "; endTime = " + endTime.getTime()); - logger.info("补全前:" + dataEntityList); + List<DataValueVO> completionDataEntityList = new ArrayList<>(); + long oneMin = 0L; + + long start = startTime.getTime(); + long end = endTime.getTime(); + long mins = 0L; + + switch (ModelParamType.getEumByCode(paramType)) { + case PREDICTITEM: + // 预测值 + Calendar calendar = Calendar.getInstance(); + calendar.setTime(startTime); + calendar.set(Calendar.HOUR_OF_DAY,0); + calendar.set(Calendar.MINUTE,0); + calendar.set(Calendar.SECOND,0); + calendar.add(Calendar.DAY_OF_YEAR,1); + startTime = calendar.getTime(); + start = startTime.getTime(); + + calendar.setTime(endTime); + calendar.set(Calendar.HOUR_OF_DAY,0); + calendar.set(Calendar.MINUTE,0); + calendar.set(Calendar.SECOND,0); + endTime = calendar.getTime(); + end = endTime.getTime(); + + + oneMin = 24 * 60 * 60 * 1000; + mins = ((end - start) / oneMin); + break; + case DATAPOINT: + // 测点值 + ApiPointDTO dataPoint = dataPointApi.getInfoById(paramId); + oneMin = 1000L * DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue(); + + // 设置时间偏移量 + start = start - (start % oneMin) + oneMin; + end = end - (end % oneMin) + oneMin; + + + mins = ((end - start) / oneMin); + break; + case IND: + // 指标数据 + oneMin = 24 * 60 * 60 * 1000; + Calendar calendar2 = Calendar.getInstance(); + calendar2.setTime(startTime); + calendar2.set(Calendar.HOUR_OF_DAY,0); + calendar2.set(Calendar.MINUTE,0); + calendar2.set(Calendar.SECOND,0); + start = calendar2.getTime().getTime(); + + calendar2.setTime(endTime); + calendar2.set(Calendar.HOUR_OF_DAY,0); + calendar2.set(Calendar.MINUTE,0); + calendar2.set(Calendar.SECOND,0); + end = calendar2.getTime().getTime(); + + + mins = ((end - start) / oneMin); + break; + default: + break; + } Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size()); for (DataValueVO dataEntity : dataEntityList) { sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue()); } //找出缺少项 - long oneMin = 1000 * granularity; - long start = startTime.getTime(); - long end = endTime.getTime(); - long mins = ((end - start) / oneMin) + 1; Map<Long, Double> dataMap = new LinkedHashMap<>(); - for (int i = 0; i < mins; i++) { + for (int i = 0; i < mins; i ++) { Long key = start + oneMin * i; Double value = sourceDataMap.get(key); dataMap.put(key, value); @@ -57,23 +126,20 @@ //补充缺少项 int k = 0; Map.Entry<Long, Double> lastItem = null; - List<DataValueVO> completionDataEntityList = new ArrayList<>(); for (Map.Entry<Long, Double> item : dataMap.entrySet()) { if (k == 0 && item.getValue() == null) { item.setValue(getFirstValue(dataMap)); } else if (item.getValue() == null) { item.setValue(lastItem.getValue()); } - k++; + k ++; lastItem = item; DataValueVO dataEntity = new DataValueVO(); - dataEntity.setDataTime(new Date(item.getKey())); + dataEntity.setDataTime(new Timestamp(item.getKey())); dataEntity.setDataValue(item.getValue()); completionDataEntityList.add(dataEntity); } - - logger.info("补全后:" + completionDataEntityList); return completionDataEntityList; } diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleInfoConstructor.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleInfoConstructor.java index 5ca85de..f32b9be 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleInfoConstructor.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleInfoConstructor.java @@ -112,10 +112,10 @@ if (dataPoint == null) { return null; } - calendar.add(Calendar.SECOND, DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue() * -1); + dateTime = calendar.getTime(); break; case PREDICTITEM: - dateTime = calculateTime(originalTime, false, columnItem.getDataLength(), 60); + dateTime = calendar.getTime(); break; case IND: dateTime = calendar.getTime(); -- Gitblit v1.9.3