From af7bd200a95b9fc6b8b3f3fc603d612221e21fc7 Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期三, 08 一月 2025 08:23:48 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java | 134 +++++++++++++++++++++----------------------- 1 files changed, 63 insertions(+), 71 deletions(-) diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java index 777b60c..2eb99cf 100644 --- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java +++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java @@ -1,21 +1,22 @@ package com.iailab.module.model.mcs.pre.service.impl; import com.alibaba.fastjson.JSONArray; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.iailab.framework.common.util.date.DateUtils; -import com.iailab.module.model.mcs.pre.dao.MmItemResultDao; -import com.iailab.module.model.mcs.pre.entity.MmItemResultEntity; +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 org.springframework.util.CollectionUtils; import java.math.BigDecimal; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @@ -24,28 +25,27 @@ * @date 2021年05月28日 10:34 */ @Service -public class MmItemResultServiceImpl extends ServiceImpl<MmItemResultDao, MmItemResultEntity> implements MmItemResultService { - - private final int max_group_count = 100; - - private final String T_MM_ITEM_RESULT = "T_MM_ITEM_RESULT"; +public class MmItemResultServiceImpl implements MmItemResultService { @Autowired - private MmItemResultDao mmItemResultDao; + private MmItemResultJsonService mmItemResultJsonService; + @Autowired + private InfluxDBService influxDBService; @Override public void savePredictValue(Map<String, List<DataValueVO>> predictValueMap, int t, String nIndex, Date predictTime) { - List<MmItemResultEntity> importList = new ArrayList<>(); - List<MmItemResultEntity> lastList = new ArrayList<>(); + 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()) { - MmItemResultEntity importData = new MmItemResultEntity(); - importData.setId(String.valueOf(UUID.randomUUID())); - importData.setOutputid(entry.getKey()); - importData.setDatatime(dataVo.getDataTime()); - importData.setDatavalue(new BigDecimal(dataVo.getDataValue())); - importList.add(importData); + 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<>(); @@ -63,79 +63,71 @@ } for (DataValueVO dataVo : lastVoList) { - MmItemResultEntity importData = new MmItemResultEntity(); - importData.setId(String.valueOf(UUID.randomUUID())); - importData.setOutputid(entry.getKey()); - importData.setDatatime(dataVo.getDataTime()); - importData.setDatavalue(new BigDecimal(dataVo.getDataValue())); - lastList.add(importData); + 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 -> { - return valueVO.getDataValue(); - }).collect(Collectors.toList()); + List<Double> jsonValueList = entry.getValue().stream().map(valueVO -> valueVO.getDataValue()).collect(Collectors.toList()); resultJson.setJsonvalue(JSONArray.toJSONString(jsonValueList)); - Map<String, Object> map4 = new HashMap(2); - map4.put("TABLENAME", "T_MM_ITEM_RESULT_JSON"); - map4.put("entity", resultJson); - mmItemResultDao.savePredictJsonValue(map4); - - Map<String, Object> params = new HashMap(4); - params.put("TABLENAME", T_MM_ITEM_RESULT); - params.put("OUTPUTID", entry.getKey()); - params.put("STARTTIME", importList.get(0).getDatatime()); - params.put("ENDTIME", importList.get(importList.size() - 1).getDatatime()); - mmItemResultDao.deletePredictValue(params); + resultJsonList.add(resultJson); } - mmItemResultDao.insertBatch(importList,max_group_count); - - Map<String, Object> map3 = new HashMap<>(2); - map3.put("TABLENAME", "T_MM_ITEM_RESULT_LAST_POINT"); - map3.put("list", lastList); - mmItemResultDao.savePredictValue(map3); + // json结果存入mysql + mmItemResultJsonService.insert(resultJsonList); + // double结果存入influxdb + influxDBService.asyncWriteModelResults(importList); + influxDBService.asyncWriteModelResults(lastList); } @Override public List<DataValueVO> getPredictValue(String outputid, Date startTime, Date endTime) { - List<DataValueVO> result = new ArrayList<>(); - QueryWrapper<MmItemResultEntity> queryWrapper = new QueryWrapper<MmItemResultEntity>() - .eq("outputid", outputid) - .between("datatime", startTime, endTime) - .orderByAsc("datatime"); - List<MmItemResultEntity> list = mmItemResultDao.selectList(queryWrapper); - if (CollectionUtils.isEmpty(list)) { - return result; - } - result = list.stream().map(t -> { + 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(t.getDatatime()); - dv.setDataValue(t.getDatavalue().doubleValue()); + 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) { + public List<Object[]> getData(String outputid, Date startTime, Date endTime, String timeFormat) { List<Object[]> result = new ArrayList<>(); - QueryWrapper<MmItemResultEntity> queryWrapper = new QueryWrapper<MmItemResultEntity>() - .eq("outputid", outputid) - .between("datatime", startTime, endTime) - .orderByAsc("datatime"); - List<MmItemResultEntity> list = mmItemResultDao.selectList(queryWrapper); - if (CollectionUtils.isEmpty(list)) { - return result; - } - list.forEach(item -> { + 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(item.getDatatime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); - dataItem[1] = item.getDatavalue().setScale(2, BigDecimal.ROUND_HALF_UP); + 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; } + + @Override + public void savePredictValue(Map<MmItemOutputEntity, Double> predictDoubleValues, Date predictTime) { + List<InfluxModelResultPOJO> list = new ArrayList<>(); + for (Map.Entry<MmItemOutputEntity, Double> entry : predictDoubleValues.entrySet()) { + InfluxModelResultSimPOJO pojo = new InfluxModelResultSimPOJO(); + pojo.setValue(entry.getValue()); + pojo.setTimestamp(predictTime.toInstant()); + pojo.setOutPutId(entry.getKey().getId()); + pojo.setType(DataTypeEnum.FLOAT.getCode()); + list.add(pojo); + } + influxDBService.asyncWriteModelResults(list); + } } -- Gitblit v1.9.3