iailab-module-data/iailab-module-data-api/src/main/java/com/iailab/module/data/api/point/DataPointApi.java
@@ -7,9 +7,9 @@ import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.*; import javax.annotation.security.PermitAll; import java.util.List; import java.util.Map; import java.util.Set; /** * @author PanZhibao @@ -32,7 +32,7 @@ @PostMapping(PREFIX + "/info/ids") @Operation(summary = "根据多个测点ID查询测点信息") List<ApiPointDTO> getInfoByIds(@RequestParam("pointNos") List<String> pointIds); List<ApiPointDTO> getInfoByIds(@RequestParam("pointNos") Set<String> pointIds); @PostMapping(PREFIX + "/query-points/real-value") @Operation(summary = "查询多个测点当前值") iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/point/DataPointApiImpl.java
@@ -42,7 +42,7 @@ } @Override public List<ApiPointDTO> getInfoByIds(List<String> pointIds) { public List<ApiPointDTO> getInfoByIds(Set<String> pointIds) { List<ApiPointDTO> result = new ArrayList<>(pointIds.size()); for (String pointId : pointIds) { result.add(daPointService.getSimpleInfoById(pointId)); iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/PredictResultHandler.java
@@ -29,12 +29,12 @@ */ public Map<String, List<DataValueVO>> convertToPredictData(PredictResultVO predictResult) { Map<String, List<DataValueVO>> resultMap = new HashMap<>(); List<MmItemOutputEntity> itemOutPutList = itemEntityFactory.getOutPutByItemId(predictResult.getPredictId()); if (!CollectionUtils.isEmpty(predictResult.getPredictList())) { resultMap.put(itemOutPutList.get(0).getId(), predictResult.getPredictList()); return resultMap; } // List<MmItemOutputEntity> itemOutPutList = itemEntityFactory.getOutPutByItemId(predictResult.getPredictId()); // // if (!CollectionUtils.isEmpty(predictResult.getPredictList())) { // resultMap.put(itemOutPutList.get(0).getId(), predictResult.getPredictList()); // return resultMap; // } Map<com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity, double[]> predictMatrixs = predictResult.getPredictMatrixs(); HashMap<String,List<DataValueVO>> predictLists = new HashMap<>(); for (Map.Entry<com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity, double[]> entry : predictMatrixs.entrySet()) { iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictItemNormalHandlerImpl.java
@@ -1,5 +1,6 @@ package com.iailab.module.model.mdk.predict.impl; import com.iailab.module.model.common.exception.ModelResultErrorException; import com.iailab.module.model.mcs.pre.entity.MmPredictModelEntity; import com.iailab.module.model.mcs.pre.enums.ItemRunStatusEnum; import com.iailab.module.model.mcs.pre.service.MmPredictModelService; @@ -39,7 +40,7 @@ * @throws ItemInvokeException */ @Override public PredictResultVO predict(Date predictTime, ItemVO predictItemDto, Map<String, double[]> predictValueMap) throws ItemInvokeException { public PredictResultVO predict(Date predictTime, ItemVO predictItemDto, Map<String, double[]> predictValueMap) throws ItemInvokeException,ModelResultErrorException { PredictResultVO predictResult = new PredictResultVO(); String itemId = predictItemDto.getId(); try { @@ -50,6 +51,8 @@ } predictResult = predictModelHandler.predictByModel(predictTime, predictModel,predictItemDto.getItemName()); predictResult.setPredictId(itemId); } catch (ModelResultErrorException ex) { throw ex; } catch (Exception ex) { throw new ItemInvokeException(MessageFormat.format("{0},itemId={1}", ItemInvokeException.errorItemFailed, itemId)); iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/predict/impl/PredictModelHandlerImpl.java
@@ -64,7 +64,7 @@ } String modelId = predictModel.getId(); try { List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Predict.name(), modelId, predictTime); List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Predict.name(), modelId, predictTime, itemName); String modelPath = predictModel.getModelpath(); if (modelPath == null) { log.info("模型路径不存在,modelId=" + modelId); @@ -143,9 +143,12 @@ result.setPredictDoubleValues(predictDoubleValues); result.setModelResult(modelResult); result.setPredictTime(predictTime); } catch (Exception ex) { log.error("调用发生异常,异常信息为:{}", ex); } catch (ModelResultErrorException ex) { ex.printStackTrace(); throw ex; } catch (Exception ex) { log.error("调用发生异常,异常信息为:{1}", ex); // ex.printStackTrace(); throw new ModelInvokeException(ex.getMessage()); } return result; iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleDataConstructor.java
@@ -1,9 +1,12 @@ package com.iailab.module.model.mdk.sample; import com.iailab.module.data.api.plan.PlanItemApi; import com.iailab.module.data.api.point.DataPointApi; import com.iailab.module.data.api.point.dto.ApiPointDTO; import com.iailab.module.data.api.point.dto.ApiPointValueDTO; import com.iailab.module.data.api.point.dto.ApiPointValueQueryDTO; import com.iailab.module.data.common.ApiDataQueryDTO; import com.iailab.module.data.common.ApiDataValueDTO; import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; import com.iailab.module.model.mcs.pre.service.MmItemOutputService; import com.iailab.module.model.mcs.pre.service.MmItemResultService; @@ -14,18 +17,20 @@ 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 lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; /** * 预测样本数据构造 */ @Slf4j @Component public class PredictSampleDataConstructor extends SampleDataConstructor { @@ -33,6 +38,9 @@ @Autowired private DataPointApi dataPointApi; @Autowired private PlanItemApi planItemApi; @Autowired private MmItemResultService mmItemResultService; @@ -51,8 +59,9 @@ * @return */ @Override public List<SampleData> prepareSampleData(SampleInfo sampleInfo) { public List<SampleData> prepareSampleData(SampleInfo sampleInfo) throws Exception { List<SampleData> sampleDataList = new ArrayList<>(); Map<String, ApiPointDTO> pointMap = sampleInfo.getPointMap(); //对每个爪分别进行计算 for (ColumnItemPort entry : sampleInfo.getColumnInfo()) { //先依据爪内数据项的modelParamOrder进行排序——重写comparator匿名函数 @@ -74,7 +83,7 @@ //对每一项依次进行数据查询,然后将查询出的值赋给matrix对应的位置 for (int i = 0; i < entry.getColumnItemList().size(); i++) { try { List<DataValueVO> dataEntityList = getData(entry.getColumnItemList().get(i)); List<DataValueVO> dataEntityList = getData(entry.getColumnItemList().get(i),pointMap); //补全数据 ColumnItem columnItem = entry.getColumnItemList().get(i); dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime, @@ -89,6 +98,7 @@ } } catch (Exception e) { e.printStackTrace(); throw e; } } SampleData sampleData = new SampleData(); @@ -102,20 +112,23 @@ * getData * * @param columnItem * @param pointMap * @return * @throws Exception */ private List<DataValueVO> getData(ColumnItem columnItem) throws Exception { private List<DataValueVO> getData(ColumnItem columnItem, Map<String, ApiPointDTO> pointMap) throws Exception { List<DataValueVO> dataList = new ArrayList<>(); String paramType = columnItem.getParamType(); switch (ModelParamType.getEumByCode(paramType)) { case DATAPOINT: ApiPointDTO point = dataPointApi.getInfoById(columnItem.getParamId()); ApiPointValueQueryDTO queryDto = new ApiPointValueQueryDTO(); queryDto.setPointNo(point.getPointNo()); queryDto.setPointNo(pointMap.get(columnItem.getParamId()).getPointNo()); queryDto.setStart(columnItem.getStartTime()); queryDto.setEnd(columnItem.getEndTime()); List<ApiPointValueDTO> pointValueList = dataPointApi.queryPointHistoryValue(queryDto); if (CollectionUtils.isEmpty(pointValueList)) { break; } dataList = pointValueList.stream().map(t -> { DataValueVO vo = new DataValueVO(); vo.setDataTime(t.getT()); @@ -126,15 +139,31 @@ case NORMALITEM: case MERGEITEM: MmItemOutputEntity outPut = mmItemOutputService.getOutPutById(columnItem.getParamId()); dataList = mmItemResultService.getPredictValue(outPut.getId(), columnItem.getStartTime(), columnItem.getEndTime()); if (dataList == null) { throw new Exception("没有预测值"); List<DataValueVO> predictValue = mmItemResultService.getPredictValue(outPut.getId(), columnItem.getStartTime(), columnItem.getEndTime()); if (CollectionUtils.isEmpty(predictValue)) { break; } dataList = predictValue; break; case PLAN: ApiDataQueryDTO queryPlanItemDto = new ApiDataQueryDTO(); queryPlanItemDto.setItemNo(columnItem.getParamId()); queryPlanItemDto.setStart(columnItem.getStartTime()); queryPlanItemDto.setEnd(columnItem.getEndTime()); List<ApiDataValueDTO> planValueList = planItemApi.queryPlanItemHistoryValue(queryPlanItemDto); if (CollectionUtils.isEmpty(planValueList)) { break; } dataList = planValueList.stream().map(t -> { DataValueVO vo = new DataValueVO(); vo.setDataTime(t.getDataTime()); vo.setDataValue(t.getDataValue()); return vo; }).collect(Collectors.toList()); default: break; } log.info("数据获取,columnItem:" + columnItem + ",dataList" + dataList); return dataList; } } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleInfoConstructor.java
@@ -9,14 +9,12 @@ import com.iailab.module.model.mdk.common.enums.ModelParamType; import com.iailab.module.model.mdk.sample.dto.ColumnItem; import com.iailab.module.model.mdk.sample.dto.ColumnItemPort; import com.iailab.module.model.mdk.sample.dto.SampleInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -59,7 +57,8 @@ * @return */ @Override protected List<ColumnItemPort> getColumnInfo(String modelId, Date predictTime) { protected SampleInfo getColumnInfo(String modelId, Date predictTime) { SampleInfo sampleInfo = new SampleInfo(); List<ColumnItemPort> resultList = new ArrayList<>(); List<ColumnItem> columnItemList = new ArrayList<>(); ColumnItem columnInfo = new ColumnItem(); @@ -73,9 +72,9 @@ //设置当前查询数据长度,初始值为最小端口数据长度 int curDataLength = modelInputParamEntityList.get(0).getDatalength(); // 统一获取测点的信息 List<String> pointIds = modelInputParamEntityList.stream().filter(e -> ModelParamType.getEumByCode(e.getModelparamtype()).equals(ModelParamType.DATAPOINT)).map(MmModelParamEntity::getModelparamid).collect(Collectors.toList()); Set<String> pointIds = modelInputParamEntityList.stream().filter(e -> ModelParamType.getEumByCode(e.getModelparamtype()).equals(ModelParamType.DATAPOINT)).map(MmModelParamEntity::getModelparamid).collect(Collectors.toSet()); List<ApiPointDTO> points = dataPointApi.getInfoByIds(pointIds); Map<String, ApiPointDTO> pointMap = points.stream().collect(Collectors.toMap(ApiPointDTO::getId, Function.identity())); Map<String, ApiPointDTO> pointMap = points.stream().collect(Collectors.toMap(ApiPointDTO::getId, Function.identity(), (e1,e2) -> e1)); for (MmModelParamEntity entry : modelInputParamEntityList) { columnInfo.setParamType(entry.getModelparamtype()); @@ -108,7 +107,9 @@ curPort.setDataLength(curDataLength); curPort.setPortOrder(curPortOrder); resultList.add(curPort); return resultList; sampleInfo.setColumnInfo(resultList); sampleInfo.setPointMap(pointMap); return sampleInfo; } /** iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleConstructor.java
@@ -1,6 +1,7 @@ package com.iailab.module.model.mdk.sample; import com.iailab.module.model.mdk.common.exceptions.DataAccessException; import com.iailab.module.model.mdk.common.exceptions.ModelInvokeException; import com.iailab.module.model.mdk.sample.dto.SampleData; import com.iailab.module.model.mdk.sample.dto.SampleInfo; import org.springframework.beans.factory.annotation.Autowired; @@ -24,18 +25,15 @@ @Autowired private SampleFactory sampleFactory; public List<SampleData> constructSample(String typeA, String modelId, Date runTime) { public List<SampleData> constructSample(String typeA, String modelId, Date runTime,String itemName) throws ModelInvokeException { try { SampleInfoConstructor sampleInfoConstructor = sampleFactory.createSampleInfo(typeA, modelId); SampleInfo sampleInfo = sampleInfoConstructor.prepareSampleInfo(modelId, runTime); SampleDataConstructor sampleDataConstructor = sampleFactory.createSampelData(typeA); return sampleDataConstructor.prepareSampleData(sampleInfo); } catch (Exception e) { DataAccessException exception = new DataAccessException(MessageFormat.format("{0},类名为{1}", DataAccessException.errorDataAccess, Thread.currentThread().getStackTrace()[1].getClassName())); System.out.println(exception); e.printStackTrace(); return null; throw new ModelInvokeException(MessageFormat.format("{0},Name:{1}", ModelInvokeException.errorGetModelArithParam, itemName)); } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleDataConstructor.java
@@ -1,8 +1,11 @@ package com.iailab.module.model.mdk.sample; import com.iailab.module.data.api.plan.PlanItemApi; import com.iailab.module.data.api.plan.dto.ApiPlanItemDTO; 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.data.enums.TimeGranularitySecEnum; 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; @@ -21,6 +24,8 @@ @Autowired private DataPointApi dataPointApi; @Autowired private PlanItemApi planItemApi; /** * prepareSampleData @@ -28,7 +33,7 @@ * @param sampleInfo * @return */ public abstract List<SampleData> prepareSampleData(SampleInfo sampleInfo); public abstract List<SampleData> prepareSampleData(SampleInfo sampleInfo) throws Exception; /** * 补全数据 @@ -74,7 +79,7 @@ endTime = calendar.getTime(); end = endTime.getTime(); oneMin = 24 * 60 * 60 * 1000; oneMin = 60 * 1000L; mins = ((end - start) / oneMin); break; case DATAPOINT: @@ -103,6 +108,15 @@ end = calendar2.getTime().getTime(); mins = ((end - start) / oneMin); break; case PLAN: // 计划数据 ApiPlanItemDTO planItem = planItemApi.getInfoById(paramId); oneMin = 1000L * TimeGranularitySecEnum.getEumByCode(planItem.getTimeGranularity()).getValue(); // 设置时间偏移量 start = start - (start % oneMin) + oneMin; end = end - (end % oneMin) + oneMin; mins = ((end - start) / oneMin); break; default: break; } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleInfoConstructor.java
@@ -48,14 +48,8 @@ * @return */ protected SampleInfo prepareSampleInfo(String modelId, Date predictTime) { SampleInfo sampleInfo = new SampleInfo(); //调用样本列数的方法 // sampleInfo.setSampleColumn(getSampleColumn(modelId)); //样本的列信息 sampleInfo.setColumnInfo(getColumnInfo(modelId, predictTime)); //样本的采样周期 // sampleInfo.setSampleCycle(getSampleCycle(modelId)); return sampleInfo; return getColumnInfo(modelId, predictTime); } /** @@ -74,7 +68,7 @@ * @param predictTime * @return */ protected abstract List<ColumnItemPort> getColumnInfo(String modelId, Date predictTime); protected abstract SampleInfo getColumnInfo(String modelId, Date predictTime); /** * 样本的采样周期 @@ -180,7 +174,7 @@ granularity = mmPredictItemService.getItemByOutPutId(columnItem.getParamId()).getGranularity(); break; case MERGEITEM: granularity = mmPredictItemService.getItemByIdFromCache(columnItem.getParamId()).getGranularity(); granularity = mmPredictItemService.getItemByOutPutId(columnItem.getParamId()).getGranularity(); break; case IND: ApiIndItemDTO indItemDTO = indItemApi.getInfoById(columnItem.getParamId()); iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/ScheduleSampleInfoConstructor.java
@@ -8,14 +8,12 @@ import com.iailab.module.model.mdk.common.enums.ModelParamType; import com.iailab.module.model.mdk.sample.dto.ColumnItem; import com.iailab.module.model.mdk.sample.dto.ColumnItemPort; import com.iailab.module.model.mdk.sample.dto.SampleInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -34,7 +32,8 @@ } @Override protected List<ColumnItemPort> getColumnInfo(String modelId, Date predictTime) { protected SampleInfo getColumnInfo(String modelId, Date predictTime) { SampleInfo sampleInfo = new SampleInfo(); List<ColumnItemPort> resultList = new ArrayList<>(); List<ColumnItem> columnItemList = new ArrayList<>(); ColumnItem columnInfo = new ColumnItem(); @@ -48,7 +47,7 @@ //设置当前查询数据长度,初始值为最小端口数据长度 int curDataLength = modelInputParamEntityList.get(0).getDatalength(); // 统一获取测点的信息 List<String> pointIds = modelInputParamEntityList.stream().filter(e -> ModelParamType.getEumByCode(e.getModelparamtype()).equals(ModelParamType.DATAPOINT)).map(StScheduleModelParamEntity::getModelparamid).collect(Collectors.toList()); Set<String> pointIds = modelInputParamEntityList.stream().filter(e -> ModelParamType.getEumByCode(e.getModelparamtype()).equals(ModelParamType.DATAPOINT)).map(StScheduleModelParamEntity::getModelparamid).collect(Collectors.toSet()); List<ApiPointDTO> points = dataPointApi.getInfoByIds(pointIds); Map<String, ApiPointDTO> pointMap = points.stream().collect(Collectors.toMap(ApiPointDTO::getId, Function.identity())); @@ -83,7 +82,9 @@ curPort.setDataLength(curDataLength); curPort.setPortOrder(curPortOrder); resultList.add(curPort); return resultList; sampleInfo.setColumnInfo(resultList); sampleInfo.setPointMap(pointMap); return sampleInfo; } @Override iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/dto/SampleInfo.java
@@ -1,5 +1,6 @@ package com.iailab.module.model.mdk.sample.dto; import com.iailab.module.data.api.point.dto.ApiPointDTO; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -8,6 +9,7 @@ import java.math.BigDecimal; import java.sql.Timestamp; import java.util.List; import java.util.Map; @Data @AllArgsConstructor @@ -30,4 +32,6 @@ private Integer sampleCycle; private BigDecimal[][] deviation; // 所有测点信息,避免重复查询 private Map<String, ApiPointDTO> pointMap; } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/schedule/impl/ScheduleModelHandlerImpl.java
@@ -63,7 +63,7 @@ try { //1.根据模型id构造模型输入样本 long now = System.currentTimeMillis(); List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Schedule.name(), modelId, scheduleTime); List<SampleData> sampleDataList = sampleConstructor.constructSample(TypeA.Schedule.name(), modelId, scheduleTime,scheduleScheme.getName()); log.info("构造模型输入样本消耗时长:" + (System.currentTimeMillis() - now) / 1000 + "秒"); if (CollectionUtils.isEmpty(sampleDataList)) { log.info("调度模型构造样本失败,schemeCode=" + schemeCode);