潘志宝
2024-12-23 b651cbfd94d8d636c01b61e483ed1cff98e1bcb9
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.mdk.sample;
2
50084d 3 import com.iailab.module.data.api.plan.PlanItemApi;
D 4 import com.iailab.module.data.api.plan.dto.ApiPlanItemDTO;
b2aca2 5 import com.iailab.module.data.api.point.DataPointApi;
D 6 import com.iailab.module.data.api.point.dto.ApiPointDTO;
7 import com.iailab.module.data.enums.DataPointFreqEnum;
50084d 8 import com.iailab.module.data.enums.TimeGranularitySecEnum;
b2aca2 9 import com.iailab.module.model.mdk.common.enums.ModelParamType;
7fd198 10 import com.iailab.module.model.mdk.sample.dto.SampleData;
11 import com.iailab.module.model.mdk.sample.dto.SampleInfo;
12 import com.iailab.module.model.mdk.vo.DataValueVO;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
b2aca2 15 import org.springframework.beans.factory.annotation.Autowired;
7fd198 16 import org.springframework.util.CollectionUtils;
17
18 import java.util.*;
19
20 abstract class SampleDataConstructor {
21
22     private Logger logger = LoggerFactory.getLogger(getClass());
b2aca2 23
D 24     @Autowired
25     private DataPointApi dataPointApi;
50084d 26     @Autowired
D 27     private PlanItemApi planItemApi;
7fd198 28
29     /**
30      * prepareSampleData
31      *
32      * @param sampleInfo
33      * @return
34      */
50084d 35     public abstract List<SampleData> prepareSampleData(SampleInfo sampleInfo) throws Exception;
7fd198 36
37     /**
38      * 补全数据
39      *
40      * @param length
41      * @param dataEntityList
42      * @param startTime
43      * @param endTime
8bf553 44      * @param planMap
7fd198 45      * @return
46      */
a4891a 47     public List<DataValueVO> completionData(int length, List<DataValueVO> dataEntityList, Date startTime, Date endTime,
8bf553 48                                             String paramId, String paramType, Map<String, ApiPointDTO> pointMap, Map<String, ApiPlanItemDTO> planMap) {
b2aca2 49         if (CollectionUtils.isEmpty(dataEntityList) || length == dataEntityList.size()) {
7fd198 50             return dataEntityList;
a4891a 51         } else if (length < dataEntityList.size()) {
52             return dataEntityList.subList(dataEntityList.size() - length, dataEntityList.size());
7fd198 53         }
54
b2aca2 55         List<DataValueVO> completionDataEntityList = new ArrayList<>();
D 56         long oneMin = 0L;
57
58         long start = startTime.getTime();
59         long end = endTime.getTime();
60         long mins = 0L;
61
62         switch (ModelParamType.getEumByCode(paramType)) {
19a4fb 63             case NORMALITEM:
D 64             case MERGEITEM:
b2aca2 65                 // 预测值
D 66                 Calendar calendar = Calendar.getInstance();
67                 calendar.setTime(startTime);
a4891a 68                 calendar.set(Calendar.HOUR_OF_DAY, 0);
69                 calendar.set(Calendar.MINUTE, 0);
70                 calendar.set(Calendar.SECOND, 0);
71                 calendar.add(Calendar.DAY_OF_YEAR, 1);
b2aca2 72                 startTime = calendar.getTime();
D 73                 start = startTime.getTime();
74
75                 calendar.setTime(endTime);
a4891a 76                 calendar.set(Calendar.HOUR_OF_DAY, 0);
77                 calendar.set(Calendar.MINUTE, 0);
78                 calendar.set(Calendar.SECOND, 0);
b2aca2 79                 endTime = calendar.getTime();
D 80                 end = endTime.getTime();
81
50084d 82                 oneMin = 60 * 1000L;
b2aca2 83                 mins = ((end - start) / oneMin);
D 84                 break;
85             case DATAPOINT:
86                 // 测点值
8bf553 87                 ApiPointDTO dataPoint = pointMap.get(paramId);
b2aca2 88                 oneMin = 1000L * DataPointFreqEnum.getEumByCode(dataPoint.getMinfreqid()).getValue();
D 89                 // 设置时间偏移量
90                 start = start - (start % oneMin) + oneMin;
91                 end = end - (end % oneMin) + oneMin;
92                 mins = ((end - start) / oneMin);
93                 break;
94             case IND:
95                 // 指标数据
96                 oneMin = 24 * 60 * 60 * 1000;
97                 Calendar calendar2 = Calendar.getInstance();
98                 calendar2.setTime(startTime);
a4891a 99                 calendar2.set(Calendar.HOUR_OF_DAY, 0);
100                 calendar2.set(Calendar.MINUTE, 0);
101                 calendar2.set(Calendar.SECOND, 0);
b2aca2 102                 start = calendar2.getTime().getTime();
D 103
104                 calendar2.setTime(endTime);
a4891a 105                 calendar2.set(Calendar.HOUR_OF_DAY, 0);
106                 calendar2.set(Calendar.MINUTE, 0);
107                 calendar2.set(Calendar.SECOND, 0);
b2aca2 108                 end = calendar2.getTime().getTime();
D 109                 mins = ((end - start) / oneMin);
110                 break;
50084d 111             case PLAN:
D 112                 // 计划数据
8bf553 113                 ApiPlanItemDTO planItem = planMap.get(paramId);
50084d 114                 oneMin = 1000L * TimeGranularitySecEnum.getEumByCode(planItem.getTimeGranularity()).getValue();
D 115                 // 设置时间偏移量
116                 start = start - (start % oneMin) + oneMin;
117                 end = end - (end % oneMin) + oneMin;
118                 mins = ((end - start) / oneMin);
119                 break;
b2aca2 120             default:
D 121                 break;
122         }
7fd198 123         Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size());
124         for (DataValueVO dataEntity : dataEntityList) {
125             sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue());
126         }
127
128         //找出缺少项
129         Map<Long, Double> dataMap = new LinkedHashMap<>();
a4891a 130         for (int i = 0; i < mins; i++) {
7fd198 131             Long key = start + oneMin * i;
132             Double value = sourceDataMap.get(key);
133             dataMap.put(key, value);
134         }
135
136         //补充缺少项
137         int k = 0;
138         Map.Entry<Long, Double> lastItem = null;
139         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
140             if (k == 0 && item.getValue() == null) {
141                 item.setValue(getFirstValue(dataMap));
142             } else if (item.getValue() == null) {
143                 item.setValue(lastItem.getValue());
144             }
a4891a 145             k++;
7fd198 146             lastItem = item;
147
148             DataValueVO dataEntity = new DataValueVO();
e0b86e 149             dataEntity.setDataTime(new Date(item.getKey()));
7fd198 150             dataEntity.setDataValue(item.getValue());
151             completionDataEntityList.add(dataEntity);
152         }
153         return completionDataEntityList;
154     }
155
156     /**
157      * getFirstValue
158      *
159      * @param dataMap
160      * @return
161      */
162     private Double getFirstValue(Map<Long, Double> dataMap) {
163         for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
164             if (item.getValue() != null) {
165                 return item.getValue();
166             }
167         }
168         return null;
169     }
170
171 }