From 82e98b24e0871366a5fc8bddab07bbabfc321eb3 Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期一, 17 三月 2025 14:54:29 +0800
Subject: [PATCH] 构造样本优化

---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java |   24 +++++++++++
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/MmItemResultService.java          |    2 +
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/PredictSampleDataConstructor.java      |   31 +++++++++------
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/sample/SampleDataConstructor.java             |   50 ++++++++----------------
 4 files changed, 60 insertions(+), 47 deletions(-)

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 bc61c47..609aa5e 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
@@ -16,5 +16,7 @@
 
     List<DataValueVO> getPredictValue(String outputid, Date startTime, Date endTime);
 
+    List<DataValueVO> getPredictValueLast(String outputid, Date startTime, int mins);
+
     List<Object[]> getData(String outputid, Date startTime, Date endTime, String timeFormat);
 }
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 4eac950..ce85a55 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
@@ -8,13 +8,13 @@
 import com.iailab.module.model.influxdb.pojo.InfluxModelResultSimPOJO;
 import com.iailab.module.model.influxdb.service.InfluxDBService;
 import com.iailab.module.model.influxdb.vo.InfluxModelResultVO;
-import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
 import com.iailab.module.model.mcs.pre.entity.MmItemResultJsonEntity;
 import com.iailab.module.model.mcs.pre.service.MmItemResultJsonService;
 import com.iailab.module.model.mcs.pre.service.MmItemResultService;
 import com.iailab.module.model.mdk.vo.DataValueVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
 import java.util.*;
@@ -103,6 +103,28 @@
     }
 
     @Override
+    public List<DataValueVO> getPredictValueLast(String outputid, Date startTime, int mins) {
+        List<DataValueVO> result = new ArrayList<>();
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(startTime);
+        calendar.add(Calendar.MINUTE, mins * -1);
+        Date startTimeNew = calendar.getTime();
+        Date endTimeNew = new Date();
+        InfluxModelResultPOJO pojo = new InfluxModelResultPOJO();
+        pojo.setType(DataTypeEnum.FLOAT.getCode());
+        pojo.setOutPutId(outputid);
+        List<InfluxModelResultVO> influxModelResultVOS = influxDBService.queryModelResults(pojo, startTimeNew, endTimeNew);
+        if (!CollectionUtils.isEmpty(influxModelResultVOS)) {
+            InfluxModelResultVO t = influxModelResultVOS.get(influxModelResultVOS.size() - 1);
+            DataValueVO dv = new DataValueVO();
+            dv.setDataTime(Date.from(t.getTimestamp()));
+            dv.setDataValue(Double.valueOf(t.getValue().toString()));
+            result.add(dv);
+        }
+        return result;
+    }
+
+    @Override
     public List<Object[]> getData(String outputid, Date startTime, Date endTime, String timeFormat) {
         List<Object[]> result = new ArrayList<>();
         InfluxModelResultPOJO pojo = new InfluxModelResultPOJO();
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 6bd9314..b08e5a7 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
@@ -140,19 +140,20 @@
                         ColumnItem columnItem = entry.getColumnItemList().get(i);
                         dataEntityList = super.completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime, columnItem.getParamType(), columnItem.getGranularity());
 
-
                         /** 如果数据取不满,把缺失的数据点放在后面 */
-                        if (dataEntityList != null && dataEntityList.size() != 0) {
-                            logger.info("设置matrix, i = " + i + ", size = " + dataEntityList.size());
-                            // 调整值
-                            double adjustVal = SampleInfo.getAdjustValueFromDeviation(portIdx, i + 1, sampleInfo.getDeviation());
-                            for (int k = 0; k < dataEntityList.size(); k++) {
-                                Double dataValue = dataEntityList.get(k).getDataValue();
-                                if (null != dataValue) {
-                                    // 用BigDecimal计算,解决double精度问题
-                                    matrix[k][i] = BigDecimal.valueOf(dataValue).add(BigDecimal.valueOf(adjustVal)).doubleValue();
-                                }
+                        if (CollectionUtils.isEmpty(dataEntityList)) {
+                            continue;
+                        }
+                        logger.info("设置matrix, i = " + i + ", size = " + dataEntityList.size());
+                        // 调整值
+                        double adjustVal = SampleInfo.getAdjustValueFromDeviation(portIdx, i + 1, sampleInfo.getDeviation());
+                        for (int k = 0; k < dataEntityList.size(); k++) {
+                            Double dataValue = dataEntityList.get(k).getDataValue();
+                            if (null == dataValue) {
+                                continue;
                             }
+                            // 用BigDecimal计算,解决double精度问题
+                            matrix[k][i] = BigDecimal.valueOf(dataValue).add(BigDecimal.valueOf(adjustVal)).doubleValue();
                         }
                     } catch (Exception e) {
                         e.printStackTrace();
@@ -201,8 +202,12 @@
                 break;
             case NORMALITEM:
             case MERGEITEM:
-                List<DataValueVO> predictValue = mmItemResultService.getPredictValue(columnItem.getParamId(), columnItem.getStartTime(), columnItem.getEndTime());
-
+                List<DataValueVO> predictValue = new ArrayList<>();
+                if (columnItem.getStartTime().getTime() == columnItem.getEndTime().getTime()) {
+                    predictValue = mmItemResultService.getPredictValueLast(columnItem.getParamId(), columnItem.getStartTime(), 2);
+                } else {
+                    predictValue = mmItemResultService.getPredictValue(columnItem.getParamId(), columnItem.getStartTime(), columnItem.getEndTime());
+                }
                 if (CollectionUtils.isEmpty(predictValue)) {
                     break;
                 }
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 c01e92a..6d12d13 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
@@ -61,48 +61,18 @@
         switch (ModelParamType.getEumByCode(paramType)) {
             case NORMALITEM:
             case MERGEITEM:
-                // 预测值
-                oneMin = granularity * 1000L;
                 // 设置时间偏移量
-                if (granularity >= 24*60*60) {
-                    // 如果时间粒度大于等于一天,因为时区问题,特殊处理,否则时间为8:00,而非0:00
-                    start = start - ((start+8*60*60*1000) % oneMin);
-                    end = end - ((end+8*60*60*1000) % oneMin);
-                } else {
-                    start = start - (start % oneMin);
-                    end = end - (end % oneMin);
-                }
-                mins = ((end - start) / oneMin);
+                mins = calMins(start, end, granularity);
                 break;
             case DATAPOINT:
             case PLAN:
-                // 测点值
-                oneMin = 1000L * granularity;
                 // 设置时间偏移量
-                if (granularity >= 24*60*60) {
-                    // 如果时间粒度大于等于一天,因为时区问题,特殊处理,否则时间为8:00,而非0:00
-                    start = start - ((start+8*60*60*1000) % oneMin);
-                    end = end - ((end+8*60*60*1000) % oneMin);
-                } else {
-                    start = start - (start % oneMin);
-                    end = end - (end % oneMin);
-                }
-                mins = ((end - start) / oneMin);
+                mins = calMins(start, end, granularity);
                 break;
             case IND:
             case IND_ASCII:
-                // 测点值
-                oneMin = 1000L * granularity;
                 // 设置时间偏移量
-                if (granularity >= 24*60*60) {
-                    // 如果时间粒度大于等于一天,因为时区问题,特殊处理,否则时间为8:00,而非0:00
-                    start = start - ((start+8*60*60*1000) % oneMin);
-                    end = end - ((end+8*60*60*1000) % oneMin);
-                } else {
-                    start = start - (start % oneMin);
-                    end = end - (end % oneMin);
-                }
-                mins = ((end - start) / oneMin) + 1;
+                mins = calMins(start, end, granularity) + 1;
                 break;
             default:
                 break;
@@ -142,6 +112,20 @@
         return completionDataEntityList;
     }
 
+    private long calMins(long start, long end, Integer granularity) {
+        long oneMin = granularity * 1000L;
+        // 设置时间偏移量
+        if (granularity >= 24 * 60 * 60) {
+            // 如果时间粒度大于等于一天,因为时区问题,特殊处理,否则时间为8:00,而非0:00
+            start = start - ((start + 8 * 60 * 60 * 1000) % oneMin);
+            end = end - ((end + 8 * 60 * 60 * 1000) % oneMin);
+        } else {
+            start = start - (start % oneMin);
+            end = end - (end % oneMin);
+        }
+        return ((end - start) / oneMin);
+    }
+
     /**
      * getFirstValue
      *

--
Gitblit v1.9.3