dengzedong
2025-02-27 6205c22a959fbb3b69735f34af8d3316c4082a5d
提交 | 用户 | 时间
7fd198 1 package com.iailab.module.model.mcs.pre.service.impl;
2
3 import com.alibaba.fastjson.JSONArray;
b368e6 4 import com.iailab.framework.common.util.date.DateUtils;
a955f1 5 import com.iailab.module.model.common.enums.DataTypeEnum;
D 6 import com.iailab.module.model.influxdb.pojo.InfluxModelResultLastSimPOJO;
7 import com.iailab.module.model.influxdb.pojo.InfluxModelResultPOJO;
8 import com.iailab.module.model.influxdb.pojo.InfluxModelResultSimPOJO;
9 import com.iailab.module.model.influxdb.service.InfluxDBService;
10 import com.iailab.module.model.influxdb.vo.InfluxModelResultVO;
a6e46f 11 import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
7fd198 12 import com.iailab.module.model.mcs.pre.entity.MmItemResultJsonEntity;
a955f1 13 import com.iailab.module.model.mcs.pre.service.MmItemResultJsonService;
7fd198 14 import com.iailab.module.model.mcs.pre.service.MmItemResultService;
15 import com.iailab.module.model.mdk.vo.DataValueVO;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.stereotype.Service;
18
19 import java.math.BigDecimal;
20 import java.util.*;
21 import java.util.stream.Collectors;
22
23 /**
24  * @author PanZhibao
25  * @date 2021年05月28日 10:34
26  */
5c6007 27 @Service
a955f1 28 public class MmItemResultServiceImpl implements MmItemResultService {
7fd198 29
30     @Autowired
a955f1 31     private MmItemResultJsonService mmItemResultJsonService;
D 32     @Autowired
33     private InfluxDBService influxDBService;
7fd198 34
35     @Override
36     public void savePredictValue(Map<String, List<DataValueVO>> predictValueMap, int t, String nIndex, Date predictTime) {
a955f1 37         List<InfluxModelResultPOJO> importList = new ArrayList<>();
D 38         List<InfluxModelResultPOJO> lastList = new ArrayList<>();
39         List<MmItemResultJsonEntity> resultJsonList = new ArrayList<>();
69bd5e 40
7fd198 41         for (Map.Entry<String, List<DataValueVO>> entry : predictValueMap.entrySet()) {
42             for (DataValueVO dataVo : entry.getValue()) {
a955f1 43                 InfluxModelResultSimPOJO pojo = new InfluxModelResultSimPOJO();
D 44                 pojo.setValue(dataVo.getDataValue());
45                 pojo.setTimestamp(dataVo.getDataTime().toInstant());
46                 pojo.setOutPutId(entry.getKey());
47                 pojo.setType(DataTypeEnum.FLOAT.getCode());
48                 importList.add(pojo);
7fd198 49             }
50
51             List<DataValueVO> lastVoList = new ArrayList<>();
52             int size = entry.getValue().size();
4f1717 53             t = Math.max(t, 0);
7fd198 54             int n = "n".equals(nIndex) ? size : Integer.parseInt(nIndex);
4f1717 55             int length = Math.max((n - t), 0); //预测完不变的数据长度
7fd198 56             if (size >= n) {
57                 for (int i = 0; i < (size - length); i ++) {
58                     int index = length + i;
59                     lastVoList.add(entry.getValue().get(index));
60                 }
61             } else {
62                 lastVoList = entry.getValue();
63             }
64
65             for (DataValueVO dataVo : lastVoList) {
a955f1 66                 InfluxModelResultLastSimPOJO pojo = new InfluxModelResultLastSimPOJO();
D 67                 pojo.setValue(dataVo.getDataValue());
68                 pojo.setTimestamp(dataVo.getDataTime().toInstant());
69                 pojo.setOutPutId(entry.getKey());
70                 pojo.setType(DataTypeEnum.FLOAT.getCode());
71                 lastList.add(pojo);
7fd198 72             }
73
74             MmItemResultJsonEntity resultJson = new MmItemResultJsonEntity();
75             resultJson.setId(UUID.randomUUID().toString());
76             resultJson.setOutputid(entry.getKey());
77             resultJson.setPredicttime(predictTime);
a955f1 78             List<Double> jsonValueList = entry.getValue().stream().map(valueVO -> valueVO.getDataValue()).collect(Collectors.toList());
91343d 79             resultJson.setJsonvalue(JSONArray.toJSONString(jsonValueList));
5131f1 80             resultJson.setCumulant("");
a955f1 81             resultJsonList.add(resultJson);
7fd198 82         }
a955f1 83         // json结果存入mysql
807efb 84         mmItemResultJsonService.insert(resultJsonList);
a955f1 85         // double结果存入influxdb
D 86         influxDBService.asyncWriteModelResults(importList);
87         influxDBService.asyncWriteModelResults(lastList);
7fd198 88     }
89
90     @Override
91     public List<DataValueVO> getPredictValue(String outputid, Date startTime, Date endTime) {
a955f1 92         InfluxModelResultPOJO pojo = new InfluxModelResultPOJO();
D 93         pojo.setType(DataTypeEnum.FLOAT.getCode());
94         pojo.setOutPutId(outputid);
95         List<InfluxModelResultVO> influxModelResultVOS = influxDBService.queryModelResults(pojo, startTime, endTime);
96         List<DataValueVO> result = influxModelResultVOS.stream().map(t -> {
7fd198 97             DataValueVO dv = new DataValueVO();
a955f1 98             dv.setDataTime(Date.from(t.getTimestamp()));
D 99             dv.setDataValue(Double.valueOf(t.getValue().toString()));
7fd198 100             return dv;
101         }).collect(Collectors.toList());
102         return result;
103     }
b368e6 104
105     @Override
977edc 106     public List<Object[]> getData(String outputid, Date startTime, Date endTime, String timeFormat) {
b368e6 107         List<Object[]> result = new ArrayList<>();
a955f1 108         InfluxModelResultPOJO pojo = new InfluxModelResultPOJO();
D 109         pojo.setType(DataTypeEnum.FLOAT.getCode());
110         pojo.setOutPutId(outputid);
111         List<InfluxModelResultVO> influxModelResultVOS = influxDBService.queryModelResults(pojo, startTime, endTime);
112         influxModelResultVOS.forEach(item -> {
b368e6 113             Object[] dataItem = new Object[2];
a955f1 114             dataItem[0] = DateUtils.format(Date.from(item.getTimestamp()), timeFormat);
D 115             dataItem[1] = BigDecimal.valueOf(Double.valueOf(item.getValue().toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
b368e6 116             result.add(dataItem);
117         });
118         return result;
119     }
7fd198 120 }