package com.iailab.module.model.mdk.sample;
|
|
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 org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.*;
|
|
abstract class SampleDataConstructor {
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
/**
|
* prepareSampleData
|
*
|
* @param sampleInfo
|
* @return
|
*/
|
public abstract List<SampleData> prepareSampleData(SampleInfo sampleInfo);
|
|
/**
|
* 补全数据
|
*
|
* @param length
|
* @param dataEntityList
|
* @param startTime
|
* @param endTime
|
* @return
|
*/
|
public List<DataValueVO> completionData(int length, List<DataValueVO> dataEntityList, Date startTime, Date endTime, int granularity) {
|
if (CollectionUtils.isEmpty(dataEntityList) || length <= dataEntityList.size()) {
|
return dataEntityList;
|
}
|
logger.info("补全数据, length =" + length + "; size = " + dataEntityList.size() + "; startTime = " + startTime.getTime() + "; endTime = " + endTime.getTime());
|
logger.info("补全前:" + dataEntityList);
|
|
Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size());
|
for (DataValueVO dataEntity : dataEntityList) {
|
sourceDataMap.put(dataEntity.getDataTime().getTime(), dataEntity.getDataValue());
|
}
|
|
//找出缺少项
|
long oneMin = 1000 * granularity;
|
long start = startTime.getTime();
|
long end = endTime.getTime();
|
long mins = ((end - start) / oneMin) + 1;
|
Map<Long, Double> dataMap = new LinkedHashMap<>();
|
for (int i = 0; i < mins; i++) {
|
Long key = start + oneMin * i;
|
Double value = sourceDataMap.get(key);
|
dataMap.put(key, value);
|
}
|
|
//补充缺少项
|
int k = 0;
|
Map.Entry<Long, Double> lastItem = null;
|
List<DataValueVO> completionDataEntityList = new ArrayList<>();
|
for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
|
if (k == 0 && item.getValue() == null) {
|
item.setValue(getFirstValue(dataMap));
|
} else if (item.getValue() == null) {
|
item.setValue(lastItem.getValue());
|
}
|
k++;
|
lastItem = item;
|
|
DataValueVO dataEntity = new DataValueVO();
|
dataEntity.setDataTime(new Date(item.getKey()));
|
dataEntity.setDataValue(item.getValue());
|
completionDataEntityList.add(dataEntity);
|
}
|
|
logger.info("补全后:" + completionDataEntityList);
|
return completionDataEntityList;
|
}
|
|
/**
|
* getFirstValue
|
*
|
* @param dataMap
|
* @return
|
*/
|
private Double getFirstValue(Map<Long, Double> dataMap) {
|
for (Map.Entry<Long, Double> item : dataMap.entrySet()) {
|
if (item.getValue() != null) {
|
return item.getValue();
|
}
|
}
|
return null;
|
}
|
|
}
|