提交 | 用户 | 时间
|
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; |
c9dd12
|
9 |
import com.iailab.module.model.mcs.pre.enums.PredGranularityEnum; |
b2aca2
|
10 |
import com.iailab.module.model.mdk.common.enums.ModelParamType; |
7fd198
|
11 |
import com.iailab.module.model.mdk.sample.dto.SampleData; |
潘 |
12 |
import com.iailab.module.model.mdk.sample.dto.SampleInfo; |
|
13 |
import com.iailab.module.model.mdk.vo.DataValueVO; |
|
14 |
import org.slf4j.Logger; |
|
15 |
import org.slf4j.LoggerFactory; |
b2aca2
|
16 |
import org.springframework.beans.factory.annotation.Autowired; |
7fd198
|
17 |
import org.springframework.util.CollectionUtils; |
潘 |
18 |
|
|
19 |
import java.util.*; |
|
20 |
|
|
21 |
abstract class SampleDataConstructor { |
|
22 |
|
|
23 |
private Logger logger = LoggerFactory.getLogger(getClass()); |
b2aca2
|
24 |
|
D |
25 |
@Autowired |
|
26 |
private DataPointApi dataPointApi; |
50084d
|
27 |
@Autowired |
D |
28 |
private PlanItemApi planItemApi; |
7fd198
|
29 |
|
潘 |
30 |
/** |
|
31 |
* prepareSampleData |
|
32 |
* |
|
33 |
* @param sampleInfo |
|
34 |
* @return |
|
35 |
*/ |
50084d
|
36 |
public abstract List<SampleData> prepareSampleData(SampleInfo sampleInfo) throws Exception; |
7fd198
|
37 |
|
潘 |
38 |
/** |
|
39 |
* 补全数据 |
|
40 |
* |
|
41 |
* @param length |
|
42 |
* @param dataEntityList |
|
43 |
* @param startTime |
|
44 |
* @param endTime |
|
45 |
* @return |
|
46 |
*/ |
e691b9
|
47 |
public List<DataValueVO> completionData(int length, List<DataValueVO> dataEntityList, Date startTime, Date endTime, String paramType, Integer granularity) { |
b2aca2
|
48 |
if (CollectionUtils.isEmpty(dataEntityList) || length == dataEntityList.size()) { |
7fd198
|
49 |
return dataEntityList; |
a4891a
|
50 |
} else if (length < dataEntityList.size()) { |
潘 |
51 |
return dataEntityList.subList(dataEntityList.size() - length, dataEntityList.size()); |
7fd198
|
52 |
} |
潘 |
53 |
|
b2aca2
|
54 |
List<DataValueVO> completionDataEntityList = new ArrayList<>(); |
D |
55 |
long oneMin = 0L; |
|
56 |
|
|
57 |
long start = startTime.getTime(); |
|
58 |
long end = endTime.getTime(); |
|
59 |
long mins = 0L; |
|
60 |
|
|
61 |
switch (ModelParamType.getEumByCode(paramType)) { |
19a4fb
|
62 |
case NORMALITEM: |
D |
63 |
case MERGEITEM: |
b2aca2
|
64 |
// 预测值 |
e691b9
|
65 |
oneMin = granularity * 1000L; |
e45a3b
|
66 |
// 设置时间偏移量 |
D |
67 |
if (granularity >= 24*60*60) { |
|
68 |
// 如果时间粒度大于等于一天,因为时区问题,特殊处理,否则时间为8:00,而非0:00 |
|
69 |
start = start - ((start+8*60*60*1000) % oneMin); |
|
70 |
end = end - ((end+8*60*60*1000) % oneMin); |
|
71 |
} else { |
|
72 |
start = start - (start % oneMin); |
|
73 |
end = end - (end % oneMin); |
|
74 |
} |
2e0a3c
|
75 |
mins = ((end - start) / oneMin); |
b2aca2
|
76 |
break; |
D |
77 |
case DATAPOINT: |
e691b9
|
78 |
case PLAN: |
b2aca2
|
79 |
// 测点值 |
e691b9
|
80 |
oneMin = 1000L * granularity; |
b2aca2
|
81 |
// 设置时间偏移量 |
e45a3b
|
82 |
if (granularity >= 24*60*60) { |
D |
83 |
// 如果时间粒度大于等于一天,因为时区问题,特殊处理,否则时间为8:00,而非0:00 |
|
84 |
start = start - ((start+8*60*60*1000) % oneMin); |
|
85 |
end = end - ((end+8*60*60*1000) % oneMin); |
|
86 |
} else { |
|
87 |
start = start - (start % oneMin); |
|
88 |
end = end - (end % oneMin); |
|
89 |
} |
b2aca2
|
90 |
mins = ((end - start) / oneMin); |
D |
91 |
break; |
|
92 |
case IND: |
1ab119
|
93 |
case IND_ASCII: |
75a848
|
94 |
// 测点值 |
D |
95 |
oneMin = 1000L * granularity; |
|
96 |
// 设置时间偏移量 |
|
97 |
if (granularity >= 24*60*60) { |
|
98 |
// 如果时间粒度大于等于一天,因为时区问题,特殊处理,否则时间为8:00,而非0:00 |
|
99 |
start = start - ((start+8*60*60*1000) % oneMin); |
|
100 |
end = end - ((end+8*60*60*1000) % oneMin); |
|
101 |
} else { |
|
102 |
start = start - (start % oneMin); |
|
103 |
end = end - (end % oneMin); |
|
104 |
} |
|
105 |
mins = ((end - start) / oneMin) + 1; |
50084d
|
106 |
break; |
b2aca2
|
107 |
default: |
D |
108 |
break; |
|
109 |
} |
7fd198
|
110 |
Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size()); |
潘 |
111 |
for (DataValueVO dataEntity : dataEntityList) { |
75a848
|
112 |
if (null != dataEntity.getDataTime()) { |
D |
113 |
sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue()); |
|
114 |
} |
7fd198
|
115 |
} |
潘 |
116 |
|
|
117 |
//找出缺少项 |
|
118 |
Map<Long, Double> dataMap = new LinkedHashMap<>(); |
a4891a
|
119 |
for (int i = 0; i < mins; i++) { |
7fd198
|
120 |
Long key = start + oneMin * i; |
潘 |
121 |
Double value = sourceDataMap.get(key); |
|
122 |
dataMap.put(key, value); |
|
123 |
} |
|
124 |
|
|
125 |
//补充缺少项 |
|
126 |
int k = 0; |
|
127 |
Map.Entry<Long, Double> lastItem = null; |
|
128 |
for (Map.Entry<Long, Double> item : dataMap.entrySet()) { |
|
129 |
if (k == 0 && item.getValue() == null) { |
|
130 |
item.setValue(getFirstValue(dataMap)); |
|
131 |
} else if (item.getValue() == null) { |
|
132 |
item.setValue(lastItem.getValue()); |
|
133 |
} |
a4891a
|
134 |
k++; |
7fd198
|
135 |
lastItem = item; |
潘 |
136 |
|
|
137 |
DataValueVO dataEntity = new DataValueVO(); |
e0b86e
|
138 |
dataEntity.setDataTime(new Date(item.getKey())); |
7fd198
|
139 |
dataEntity.setDataValue(item.getValue()); |
潘 |
140 |
completionDataEntityList.add(dataEntity); |
|
141 |
} |
|
142 |
return completionDataEntityList; |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* getFirstValue |
|
147 |
* |
|
148 |
* @param dataMap |
|
149 |
* @return |
|
150 |
*/ |
|
151 |
private Double getFirstValue(Map<Long, Double> dataMap) { |
|
152 |
for (Map.Entry<Long, Double> item : dataMap.entrySet()) { |
|
153 |
if (item.getValue() != null) { |
|
154 |
return item.getValue(); |
|
155 |
} |
|
156 |
} |
|
157 |
return null; |
|
158 |
} |
|
159 |
|
|
160 |
} |