houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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;
    }
}