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 prepareSampleData(SampleInfo sampleInfo); /** * 补全数据 * * @param length * @param dataEntityList * @param startTime * @param endTime * @return */ public List completionData(int length, List 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 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 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 lastItem = null; List completionDataEntityList = new ArrayList<>(); for (Map.Entry 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 dataMap) { for (Map.Entry item : dataMap.entrySet()) { if (item.getValue() != null) { return item.getValue(); } } return null; } }