package com.iailab.module.model.sample.constructor;
|
import com.iailab.module.data.dto.ApiDataDTO;
|
import com.iailab.module.data.api.IFeignDataApi;
|
import com.iailab.module.data.dto.ApiDataPointDTO;
|
import com.iailab.module.data.dto.FeignQueryPointDTO;
|
import com.iailab.module.device.service.DeviceMainService;
|
import com.iailab.module.event.service.EventInfoService;
|
import com.iailab.module.mcs.service.StModelResultService;
|
import com.iailab.module.model.sample.dto.ColumnItem;
|
import com.iailab.module.model.sample.dto.ColumnItemPort;
|
import com.iailab.module.model.sample.dto.SampleData;
|
import com.iailab.module.model.sample.dto.SampleInfo;
|
import com.iailab.module.model.sample.entity.DataEntity;
|
import com.iailab.module.prod.service.IndexEvaluateSystemService;
|
import lombok.extern.slf4j.Slf4j;
|
import javax.annotation.Resource;
|
import org.springframework.stereotype.Component;
|
import org.springframework.util.CollectionUtils;
|
|
import java.sql.Timestamp;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author PanZhibao
|
* @Description
|
* @createTime 2023年05月16日 16:24:00
|
*/
|
@Slf4j
|
@Component
|
public class SampleDataConstructor {
|
|
@Resource
|
private IFeignDataApi iFeignDataApi;
|
|
@Resource
|
private StModelResultService modelResultService;
|
|
@Resource
|
private EventInfoService eventInfoService;
|
|
@Resource
|
private DeviceMainService deviceMainService;
|
|
@Resource
|
private IndexEvaluateSystemService indexEvaluateSystemService;
|
|
@Resource
|
private IFeignDataApi feignDataController;
|
|
public List<SampleData> prepareSampleData(SampleInfo sampleInfo) {
|
List<SampleData> sampleDataList = new ArrayList<>();
|
for (ColumnItemPort entry : sampleInfo.getColumnInfo()) {
|
double[][] matrix = new double[entry.getDataLength()][entry.getColumnItemList().size()];
|
for (int i = 0; i < entry.getColumnItemList().size(); i++) {
|
ColumnItem columnItem = entry.getColumnItemList().get(i);
|
List<DataEntity> dataEntityList = getColumnData(columnItem);
|
|
//补全数据
|
dataEntityList = completionData(matrix.length, dataEntityList, columnItem.startTime, columnItem.endTime, columnItem.paramId);
|
|
if (CollectionUtils.isEmpty(dataEntityList)) {
|
continue;
|
}
|
for (int k = 0; k < entry.getDataLength(); k++) {
|
matrix[k][i] = dataEntityList.get(k).getDataValue();
|
}
|
}
|
SampleData sampleData = new SampleData();
|
sampleData.setMatrix(matrix);
|
sampleDataList.add(sampleData);
|
}
|
return sampleDataList;
|
}
|
|
public List<DataEntity> getColumnData(ColumnItem columnItem) {
|
List<DataEntity> dataEntityList = new ArrayList<>();
|
switch (columnItem.getParamType()) {
|
case "predict":
|
// 预测值
|
dataEntityList = modelResultService.getValueList(columnItem.getParamId(), columnItem.startTime, columnItem.endTime)
|
.stream().collect(Collectors.toList());
|
break;
|
case "point":
|
// 测点值
|
FeignQueryPointDTO dto = new FeignQueryPointDTO();
|
dto.setEndTime(columnItem.endTime);
|
dto.setStartTime(columnItem.startTime);
|
dto.setPointCode(columnItem.getParamId());
|
List<ApiDataDTO> pointValues = iFeignDataApi.queryPointValues(dto);
|
|
List<DataEntity> tempList = new ArrayList<>();
|
pointValues.forEach(item -> {
|
DataEntity dataEntity = new DataEntity();
|
dataEntity.setDataValue(item.getDataValue());
|
dataEntity.setTimeStamp(item.getTimeStamp());
|
tempList.add(dataEntity);
|
});
|
dataEntityList = tempList;
|
break;
|
case "wz":
|
FeignQueryPointDTO queryTag = new FeignQueryPointDTO();
|
queryTag.setEndTime(columnItem.endTime);
|
queryTag.setStartTime(columnItem.startTime);
|
queryTag.setPointCode(columnItem.getParamId());
|
List<ApiDataDTO> tagValues = iFeignDataApi.querySimTagValues(queryTag);
|
|
List<DataEntity> tempListTag = new ArrayList<>();
|
tagValues.forEach(item -> {
|
DataEntity dataEntity = new DataEntity();
|
dataEntity.setDataValue(item.getDataValue());
|
dataEntity.setTimeStamp(item.getTimeStamp());
|
tempListTag.add(dataEntity);
|
});
|
dataEntityList = tempListTag;
|
break;
|
case "real":
|
// 实时值
|
List<String> pointNos = new ArrayList<>();
|
pointNos.add(columnItem.getParamId());
|
Map<String, Object> data = iFeignDataApi.getCurrentValue(pointNos);
|
DataEntity realData = new DataEntity();
|
realData.setTimeStamp(new Date());
|
realData.setDataValue(Double.parseDouble(data.get(columnItem.getParamId()).toString()));
|
dataEntityList.add(realData);
|
break;
|
case "ind":
|
// 指标数据
|
FeignQueryPointDTO dto1 = new FeignQueryPointDTO();
|
dto1.setEndTime(columnItem.endTime);
|
dto1.setStartTime(columnItem.startTime);
|
dto1.setPointCode(columnItem.getParamId());
|
List<ApiDataDTO> indItemValues = iFeignDataApi.queryIndItemValues(dto1);
|
List<DataEntity> tempList1 = new ArrayList<>();
|
indItemValues.forEach(item -> {
|
DataEntity dataEntity = new DataEntity();
|
dataEntity.setDataValue(item.getDataValue());
|
dataEntity.setTimeStamp(item.getTimeStamp());
|
tempList1.add(dataEntity);
|
});
|
dataEntityList = tempList1;
|
break;
|
case "device-main":
|
// 检修记录
|
Integer mainTimes = deviceMainService.getCount(columnItem.getParamId(), columnItem.startTime, columnItem.endTime);
|
DataEntity mainData = new DataEntity();
|
mainData.setTimeStamp(columnItem.endTime);
|
mainData.setDataValue(mainTimes.doubleValue());
|
dataEntityList.add(mainData);
|
break;
|
case "event":
|
// 报警事件
|
Long eventTimes = eventInfoService.getCount(columnItem.getParamId(), columnItem.startTime, columnItem.endTime);
|
DataEntity eventData = new DataEntity();
|
eventData.setTimeStamp(columnItem.endTime);
|
eventData.setDataValue(eventTimes.doubleValue());
|
dataEntityList.add(eventData);
|
break;
|
case "evaluate_weight":
|
// 指标权重
|
List<Double> weightList = indexEvaluateSystemService.getWeight();
|
List<DataEntity> weightDataList = new ArrayList<>();
|
weightList.forEach(item -> {
|
DataEntity weightData = new DataEntity();
|
weightData.setTimeStamp(columnItem.endTime);
|
weightData.setDataValue(item);
|
weightDataList.add(weightData);
|
});
|
dataEntityList = weightDataList;
|
break;
|
default:
|
break;
|
}
|
return dataEntityList;
|
}
|
|
/**
|
* 补全数据
|
*
|
* @param length
|
* @param dataEntityList
|
* @param startTime
|
* @param endTime
|
* @return
|
*/
|
public List<DataEntity> completionData(int length, List<DataEntity> dataEntityList, Date startTime, Date endTime, String paramId) {
|
if (CollectionUtils.isEmpty(dataEntityList) || length <= dataEntityList.size()) {
|
return dataEntityList;
|
}
|
// log.info("补全数据, length =" + length + "; size = " + dataEntityList.size() + "; startTime = " + startTime + "; endTime = " + endTime);
|
// log.info("补全前:" + dataEntityList);
|
|
ApiDataPointDTO dataPoint = feignDataController.getPoint(paramId);
|
|
Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size());
|
for (DataEntity dataEntity : dataEntityList) {
|
sourceDataMap.put(dataEntity.getTimeStamp().getTime(), dataEntity.getDataValue());
|
}
|
|
//找出缺少项
|
long oneMin = 1000 * SampleInfoConstructor.minFreqMap.get(dataPoint.getMinfreqid());
|
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<DataEntity> 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;
|
|
DataEntity dataEntity = new DataEntity();
|
dataEntity.setTimeStamp(new Timestamp(item.getKey()));
|
dataEntity.setDataValue(item.getValue());
|
completionDataEntityList.add(dataEntity);
|
}
|
|
// log.info("补全后:" + completionDataEntityList);
|
return completionDataEntityList;
|
}
|
|
public List<DataEntity> completionData(int length, List<DataEntity> dataEntityList, Date startTime, Date endTime, int s) {
|
if (CollectionUtils.isEmpty(dataEntityList) || length <= dataEntityList.size()) {
|
return dataEntityList;
|
}
|
Map<Long, Double> sourceDataMap = new HashMap<>(dataEntityList.size());
|
for (DataEntity dataEntity : dataEntityList) {
|
sourceDataMap.put(dataEntity.getTimeStamp().getTime(), dataEntity.getDataValue());
|
}
|
|
//找出缺少项
|
long oneMin = 1000 * s;
|
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<DataEntity> 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;
|
|
DataEntity dataEntity = new DataEntity();
|
dataEntity.setTimeStamp(new Timestamp(item.getKey()));
|
dataEntity.setDataValue(item.getValue());
|
completionDataEntityList.add(dataEntity);
|
}
|
|
// log.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;
|
}
|
}
|