提交 | 用户 | 时间
|
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)); |
a955f1
|
80 |
resultJsonList.add(resultJson); |
7fd198
|
81 |
} |
a955f1
|
82 |
// json结果存入mysql |
807efb
|
83 |
mmItemResultJsonService.insert(resultJsonList); |
a955f1
|
84 |
// double结果存入influxdb |
D |
85 |
influxDBService.asyncWriteModelResults(importList); |
|
86 |
influxDBService.asyncWriteModelResults(lastList); |
7fd198
|
87 |
} |
潘 |
88 |
|
|
89 |
@Override |
|
90 |
public List<DataValueVO> getPredictValue(String outputid, Date startTime, Date endTime) { |
a955f1
|
91 |
InfluxModelResultPOJO pojo = new InfluxModelResultPOJO(); |
D |
92 |
pojo.setType(DataTypeEnum.FLOAT.getCode()); |
|
93 |
pojo.setOutPutId(outputid); |
|
94 |
List<InfluxModelResultVO> influxModelResultVOS = influxDBService.queryModelResults(pojo, startTime, endTime); |
|
95 |
List<DataValueVO> result = influxModelResultVOS.stream().map(t -> { |
7fd198
|
96 |
DataValueVO dv = new DataValueVO(); |
a955f1
|
97 |
dv.setDataTime(Date.from(t.getTimestamp())); |
D |
98 |
dv.setDataValue(Double.valueOf(t.getValue().toString())); |
7fd198
|
99 |
return dv; |
潘 |
100 |
}).collect(Collectors.toList()); |
|
101 |
return result; |
|
102 |
} |
b368e6
|
103 |
|
潘 |
104 |
@Override |
977edc
|
105 |
public List<Object[]> getData(String outputid, Date startTime, Date endTime, String timeFormat) { |
b368e6
|
106 |
List<Object[]> result = new ArrayList<>(); |
a955f1
|
107 |
InfluxModelResultPOJO pojo = new InfluxModelResultPOJO(); |
D |
108 |
pojo.setType(DataTypeEnum.FLOAT.getCode()); |
|
109 |
pojo.setOutPutId(outputid); |
|
110 |
List<InfluxModelResultVO> influxModelResultVOS = influxDBService.queryModelResults(pojo, startTime, endTime); |
|
111 |
influxModelResultVOS.forEach(item -> { |
b368e6
|
112 |
Object[] dataItem = new Object[2]; |
a955f1
|
113 |
dataItem[0] = DateUtils.format(Date.from(item.getTimestamp()), timeFormat); |
D |
114 |
dataItem[1] = BigDecimal.valueOf(Double.valueOf(item.getValue().toString())).setScale(2, BigDecimal.ROUND_HALF_UP); |
b368e6
|
115 |
result.add(dataItem); |
潘 |
116 |
}); |
|
117 |
return result; |
|
118 |
} |
a6e46f
|
119 |
|
D |
120 |
@Override |
|
121 |
public void savePredictValue(Map<MmItemOutputEntity, Double> predictDoubleValues, Date predictTime) { |
a955f1
|
122 |
List<InfluxModelResultPOJO> list = new ArrayList<>(); |
a6e46f
|
123 |
for (Map.Entry<MmItemOutputEntity, Double> entry : predictDoubleValues.entrySet()) { |
a955f1
|
124 |
InfluxModelResultSimPOJO pojo = new InfluxModelResultSimPOJO(); |
D |
125 |
pojo.setValue(entry.getValue()); |
|
126 |
pojo.setTimestamp(predictTime.toInstant()); |
|
127 |
pojo.setOutPutId(entry.getKey().getId()); |
|
128 |
pojo.setType(DataTypeEnum.FLOAT.getCode()); |
|
129 |
list.add(pojo); |
a6e46f
|
130 |
} |
a955f1
|
131 |
influxDBService.asyncWriteModelResults(list); |
a6e46f
|
132 |
} |
7fd198
|
133 |
} |