dengzedong
2025-02-27 6205c22a959fbb3b69735f34af8d3316c4082a5d
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
package com.iailab.module.model.mcs.pre.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.module.model.common.enums.DataTypeEnum;
import com.iailab.module.model.influxdb.pojo.InfluxModelResultLastSimPOJO;
import com.iailab.module.model.influxdb.pojo.InfluxModelResultPOJO;
import com.iailab.module.model.influxdb.pojo.InfluxModelResultSimPOJO;
import com.iailab.module.model.influxdb.service.InfluxDBService;
import com.iailab.module.model.influxdb.vo.InfluxModelResultVO;
import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity;
import com.iailab.module.model.mcs.pre.entity.MmItemResultJsonEntity;
import com.iailab.module.model.mcs.pre.service.MmItemResultJsonService;
import com.iailab.module.model.mcs.pre.service.MmItemResultService;
import com.iailab.module.model.mdk.vo.DataValueVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author PanZhibao
 * @date 2021年05月28日 10:34
 */
@Service
public class MmItemResultServiceImpl implements MmItemResultService {
 
    @Autowired
    private MmItemResultJsonService mmItemResultJsonService;
    @Autowired
    private InfluxDBService influxDBService;
 
    @Override
    public void savePredictValue(Map<String, List<DataValueVO>> predictValueMap, int t, String nIndex, Date predictTime) {
        List<InfluxModelResultPOJO> importList = new ArrayList<>();
        List<InfluxModelResultPOJO> lastList = new ArrayList<>();
        List<MmItemResultJsonEntity> resultJsonList = new ArrayList<>();
 
        for (Map.Entry<String, List<DataValueVO>> entry : predictValueMap.entrySet()) {
            for (DataValueVO dataVo : entry.getValue()) {
                InfluxModelResultSimPOJO pojo = new InfluxModelResultSimPOJO();
                pojo.setValue(dataVo.getDataValue());
                pojo.setTimestamp(dataVo.getDataTime().toInstant());
                pojo.setOutPutId(entry.getKey());
                pojo.setType(DataTypeEnum.FLOAT.getCode());
                importList.add(pojo);
            }
 
            List<DataValueVO> lastVoList = new ArrayList<>();
            int size = entry.getValue().size();
            t = Math.max(t, 0);
            int n = "n".equals(nIndex) ? size : Integer.parseInt(nIndex);
            int length = Math.max((n - t), 0); //预测完不变的数据长度
            if (size >= n) {
                for (int i = 0; i < (size - length); i ++) {
                    int index = length + i;
                    lastVoList.add(entry.getValue().get(index));
                }
            } else {
                lastVoList = entry.getValue();
            }
 
            for (DataValueVO dataVo : lastVoList) {
                InfluxModelResultLastSimPOJO pojo = new InfluxModelResultLastSimPOJO();
                pojo.setValue(dataVo.getDataValue());
                pojo.setTimestamp(dataVo.getDataTime().toInstant());
                pojo.setOutPutId(entry.getKey());
                pojo.setType(DataTypeEnum.FLOAT.getCode());
                lastList.add(pojo);
            }
 
            MmItemResultJsonEntity resultJson = new MmItemResultJsonEntity();
            resultJson.setId(UUID.randomUUID().toString());
            resultJson.setOutputid(entry.getKey());
            resultJson.setPredicttime(predictTime);
            List<Double> jsonValueList = entry.getValue().stream().map(valueVO -> valueVO.getDataValue()).collect(Collectors.toList());
            resultJson.setJsonvalue(JSONArray.toJSONString(jsonValueList));
            resultJson.setCumulant("");
            resultJsonList.add(resultJson);
        }
        // json结果存入mysql
        mmItemResultJsonService.insert(resultJsonList);
        // double结果存入influxdb
        influxDBService.asyncWriteModelResults(importList);
        influxDBService.asyncWriteModelResults(lastList);
    }
 
    @Override
    public List<DataValueVO> getPredictValue(String outputid, Date startTime, Date endTime) {
        InfluxModelResultPOJO pojo = new InfluxModelResultPOJO();
        pojo.setType(DataTypeEnum.FLOAT.getCode());
        pojo.setOutPutId(outputid);
        List<InfluxModelResultVO> influxModelResultVOS = influxDBService.queryModelResults(pojo, startTime, endTime);
        List<DataValueVO> result = influxModelResultVOS.stream().map(t -> {
            DataValueVO dv = new DataValueVO();
            dv.setDataTime(Date.from(t.getTimestamp()));
            dv.setDataValue(Double.valueOf(t.getValue().toString()));
            return dv;
        }).collect(Collectors.toList());
        return result;
    }
 
    @Override
    public List<Object[]> getData(String outputid, Date startTime, Date endTime, String timeFormat) {
        List<Object[]> result = new ArrayList<>();
        InfluxModelResultPOJO pojo = new InfluxModelResultPOJO();
        pojo.setType(DataTypeEnum.FLOAT.getCode());
        pojo.setOutPutId(outputid);
        List<InfluxModelResultVO> influxModelResultVOS = influxDBService.queryModelResults(pojo, startTime, endTime);
        influxModelResultVOS.forEach(item -> {
            Object[] dataItem = new Object[2];
            dataItem[0] = DateUtils.format(Date.from(item.getTimestamp()), timeFormat);
            dataItem[1] = BigDecimal.valueOf(Double.valueOf(item.getValue().toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
            result.add(dataItem);
        });
        return result;
    }
}