提交 | 用户 | 时间
|
7fd198
|
1 |
package com.iailab.module.model.mdk.sample; |
潘 |
2 |
|
|
3 |
import com.iailab.module.model.mdk.sample.dto.SampleData; |
|
4 |
import com.iailab.module.model.mdk.sample.dto.SampleInfo; |
|
5 |
import com.iailab.module.model.mdk.vo.DataValueVO; |
|
6 |
import org.slf4j.Logger; |
|
7 |
import org.slf4j.LoggerFactory; |
|
8 |
import org.springframework.util.CollectionUtils; |
|
9 |
|
|
10 |
import java.util.*; |
|
11 |
|
|
12 |
abstract class SampleDataConstructor { |
|
13 |
|
|
14 |
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
15 |
|
|
16 |
/** |
|
17 |
* prepareSampleData |
|
18 |
* |
|
19 |
* @param sampleInfo |
|
20 |
* @return |
|
21 |
*/ |
|
22 |
public abstract List<SampleData> prepareSampleData(SampleInfo sampleInfo); |
|
23 |
|
|
24 |
/** |
|
25 |
* 补全数据 |
|
26 |
* |
|
27 |
* @param length |
|
28 |
* @param dataEntityList |
|
29 |
* @param startTime |
|
30 |
* @param endTime |
|
31 |
* @return |
|
32 |
*/ |
|
33 |
public List<DataValueVO> completionData(int length, List<DataValueVO> dataEntityList, Date startTime, Date endTime, int granularity) { |
|
34 |
if (CollectionUtils.isEmpty(dataEntityList) || length <= dataEntityList.size()) { |
|
35 |
return dataEntityList; |
|
36 |
} |
|
37 |
logger.info("补全数据, length =" + length + "; size = " + dataEntityList.size() + "; startTime = " + startTime.getTime() + "; endTime = " + endTime.getTime()); |
|
38 |
logger.info("补全前:" + dataEntityList); |
|
39 |
|
|
40 |
Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size()); |
|
41 |
for (DataValueVO dataEntity : dataEntityList) { |
|
42 |
sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue()); |
|
43 |
} |
|
44 |
|
|
45 |
//找出缺少项 |
|
46 |
long oneMin = 1000 * granularity; |
|
47 |
long start = startTime.getTime(); |
|
48 |
long end = endTime.getTime(); |
|
49 |
long mins = ((end - start) / oneMin) + 1; |
|
50 |
Map<Long, Double> dataMap = new LinkedHashMap<>(); |
|
51 |
for (int i = 0; i < mins; i++) { |
|
52 |
Long key = start + oneMin * i; |
|
53 |
Double value = sourceDataMap.get(key); |
|
54 |
dataMap.put(key, value); |
|
55 |
} |
|
56 |
|
|
57 |
//补充缺少项 |
|
58 |
int k = 0; |
|
59 |
Map.Entry<Long, Double> lastItem = null; |
|
60 |
List<DataValueVO> completionDataEntityList = new ArrayList<>(); |
|
61 |
for (Map.Entry<Long, Double> item : dataMap.entrySet()) { |
|
62 |
if (k == 0 && item.getValue() == null) { |
|
63 |
item.setValue(getFirstValue(dataMap)); |
|
64 |
} else if (item.getValue() == null) { |
|
65 |
item.setValue(lastItem.getValue()); |
|
66 |
} |
|
67 |
k++; |
|
68 |
lastItem = item; |
|
69 |
|
|
70 |
DataValueVO dataEntity = new DataValueVO(); |
|
71 |
dataEntity.setDataTime(new Date(item.getKey())); |
|
72 |
dataEntity.setDataValue(item.getValue()); |
|
73 |
completionDataEntityList.add(dataEntity); |
|
74 |
} |
|
75 |
|
|
76 |
logger.info("补全后:" + completionDataEntityList); |
|
77 |
return completionDataEntityList; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* getFirstValue |
|
82 |
* |
|
83 |
* @param dataMap |
|
84 |
* @return |
|
85 |
*/ |
|
86 |
private Double getFirstValue(Map<Long, Double> dataMap) { |
|
87 |
for (Map.Entry<Long, Double> item : dataMap.entrySet()) { |
|
88 |
if (item.getValue() != null) { |
|
89 |
return item.getValue(); |
|
90 |
} |
|
91 |
} |
|
92 |
return null; |
|
93 |
} |
|
94 |
|
|
95 |
} |