From f5072463229ec111266c6781d75f7906a8769dc9 Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期一, 17 三月 2025 23:46:40 +0800 Subject: [PATCH] 修复查询不到开始时间数据问题 --- iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java | 111 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 92 insertions(+), 19 deletions(-) diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java index 5470286..efff0f2 100644 --- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java +++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java @@ -1,5 +1,6 @@ package com.iailab.module.data.influxdb.service.impl; +import com.iailab.module.data.api.dto.ApiPointValueQueryDTO; import com.iailab.module.data.common.utils.ExcelUtil; import com.iailab.framework.common.util.date.DateUtils; import com.iailab.module.data.api.dto.ApiExportValueDTO; @@ -11,6 +12,9 @@ import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; import com.iailab.module.data.influxdb.pojo.InfluxTagValuePOJO; import com.iailab.module.data.influxdb.service.InfluxDBService; +import com.iailab.module.data.point.dto.DaPointDTO; +import com.iailab.module.data.point.service.DaPointService; +import com.iailab.module.data.point.vo.PointValueExportVO; import com.influxdb.client.InfluxQLQueryApi; import com.influxdb.client.QueryApi; import com.influxdb.client.WriteApi; @@ -22,7 +26,10 @@ import com.influxdb.query.FluxTable; import com.influxdb.query.InfluxQLQueryResult; import lombok.extern.slf4j.Slf4j; + import javax.annotation.Resource; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -30,6 +37,7 @@ import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; import java.util.*; +import java.util.stream.Collectors; /** * InfluxDB操作类 @@ -43,15 +51,18 @@ private WriteApi writeApi; + @Autowired + private DaPointService daPointService; + private WriteApiBlocking writeApiBlocking; private QueryApi queryApi; private InfluxQLQueryApi influxQLQueryApi; - private String VALUE = "value"; + public static final String VALUE = "value"; - private String TIME = "time"; + public static final String TIME = "time"; private int rawOffset = TimeZone.getDefault().getRawOffset(); @@ -100,7 +111,8 @@ } if (!CollectionUtils.isEmpty(pointValues)) { pointValues.forEach(item -> { - writeApi.writeMeasurement(influxDBInstance.getBucket(), influxDBInstance.org, WritePrecision.MS, item); + String bucket = influxDBInstance.getBucket(); + writeApi.writeMeasurement(bucket, influxDBInstance.org, WritePrecision.MS, item); }); } writeApi.flush(); @@ -113,6 +125,7 @@ } if (!CollectionUtils.isEmpty(tagValues)) { tagValues.forEach(item -> { + log.info("bucket===" + influxDBInstance.getBucket() + ",org===" + influxDBInstance.org); writeApi.writeMeasurement(influxDBInstance.getBucket(), influxDBInstance.org, WritePrecision.MS, item); }); } @@ -132,8 +145,8 @@ calendar.set(Calendar.MILLISECOND, 0); // String start = startTime.getTime() - calendar.getTime().getTime() + "ms"; String start = startTime.getTime() - calendar.getTime().getTime() - pas_ms + "ms"; - if (endTime==null){ - endTime= new Date(); + if (endTime == null) { + endTime = new Date(); } String stop = endTime.getTime() - calendar.getTime().getTime() + "ms"; @@ -201,21 +214,79 @@ } @Override - public boolean exportTagValue(HttpServletResponse response, HttpServletRequest request, ApiTagValueQueryDTO params) { - InfluxTagValuePOJO tag = new InfluxTagValuePOJO(); - tag.setId(params.getTagId()); - tag.setType(params.getDataType()); - List<ApiExportValueDTO> valueList = getExportValue(tag, params.getStart(), params.getEnd()); - try{ + public boolean exportTagValue(HttpServletResponse response, HttpServletRequest request, ApiTagValueQueryDTO queryDto) { + //构建参数 + Map<String, Object> params = new HashMap<>(1); + params.put("pointNos", queryDto.getTagIds()); + + //查询point列表 + List<DaPointDTO> pointList = daPointService.list(params); + if (CollectionUtils.isEmpty(pointList)) { + return true; + } + + //插入pointType + List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> { + InfluxPointValuePOJO pojo = new InfluxPointValuePOJO(); + pojo.setPoint(item.getTagNo()); + pojo.setType(item.getDataType()); + return pojo; + }).collect(Collectors.toList()); + + //查询 + Map<String, List<Map<String, Object>>> data = queryPointsValues(influxParams, queryDto.getStart(), queryDto.getEnd()); + + //提取list + List<Map<String, Object>> list = data.get(queryDto.getTagIds().get(0)); + + //导出 + try { String sheetTitle = "采集数据"; String[] title = new String[]{"值", "时间"}; - ExcelUtil.exportPointValue(sheetTitle, title, new ArrayList<>(valueList), response); + ExcelUtil.exportPointValue(sheetTitle, title, new ArrayList<>(list), response); } catch (Exception ex) { return false; } return true; } + @Override + public List<PointValueExportVO> exportPointValue(ApiPointValueQueryDTO queryDto) { + + List<PointValueExportVO> pointValueExportList = new ArrayList<>(); + + //构建参数 + Map<String, Object> params = new HashMap<>(1); + params.put("pointNos", queryDto.getPointNos()); + + //查询point列表 + List<DaPointDTO> pointList = daPointService.list(params); + if (CollectionUtils.isEmpty(pointList)) { + return pointValueExportList; + } + + //插入pointType + List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> { + InfluxPointValuePOJO pojo = new InfluxPointValuePOJO(); + pojo.setPoint(item.getPointNo()); + pojo.setType(item.getDataType()); + return pojo; + }).collect(Collectors.toList()); + + //查询 + Map<String, List<Map<String, Object>>> data = queryPointsValues(influxParams, queryDto.getStart(), queryDto.getEnd()); + + //提取list + List<Map<String, Object>> list = data.get(queryDto.getPointNos().get(0)); + for (Map<String, Object> map : list) { + PointValueExportVO dto = new PointValueExportVO(); + dto.setDatatime(map.get("time").toString()); + dto.setDatavalue(map.get("value").toString()); + pointValueExportList.add(dto); + } + + return pointValueExportList; + } @Override public Map<String, Object> queryPointsLastValue(List<InfluxPointValuePOJO> influxParams) { @@ -234,7 +305,9 @@ sql.append("'"); InfluxQLQueryResult data = influxQLQueryApi.query(new InfluxQLQuery(sql.toString(), influxDBInstance.getBucket())); Object value = data.getResults().get(0).getSeries().get(0).getValues().get(0).getValueByKey("last"); - result.put(point.getPoint(), value); + if (value != null) { + result.put(point.getPoint(), value); + } } return result; } @@ -244,7 +317,7 @@ if (influxQLQueryApi == null) { influxQLQueryApi = influxDBInstance.getClient().getInfluxQLQueryApi(); } - long utcMillis = startTime.getTime() -rawOffset; + long utcMillis = startTime.getTime() - rawOffset; String utsStart = DateUtils.format(new Date(utcMillis), DateUtils.FORMAT_YEAR_MONTH_DAY); log.info("utsStart=" + utsStart); String measurement = PointValueUtils.getMeasurement(point.getType()); @@ -253,7 +326,7 @@ sql.append(measurement); sql.append(" WHERE point = '"); sql.append(point.getPoint()); - sql.append("' AND time >= '" + utsStart +"'"); + sql.append("' AND time >= '" + utsStart + "'"); InfluxQLQueryResult data = influxQLQueryApi.query(new InfluxQLQuery(sql.toString(), influxDBInstance.getBucket())); if (data == null) { return null; @@ -280,9 +353,9 @@ Map<String, List<Map<String, Object>>> result = new HashMap<>(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); - String start = startTime.getTime() - calendar.getTime().getTime() + "ms"; - if (endTime==null){ - endTime= new Date(); + String start = (startTime.getTime() - calendar.getTime().getTime() - pas_ms) + "ms"; + if (endTime == null) { + endTime = new Date(); } String stop = endTime.getTime() - calendar.getTime().getTime() + "ms"; @@ -306,7 +379,7 @@ for (FluxRecord record : records) { Map<String, Object> dataIem = new HashMap<>(2); dataIem.put(VALUE, record.getValueByKey("_value")); - dataIem.put(TIME, Date.from(record.getTime())); + dataIem.put(TIME, DateUtils.format(Date.from(record.getTime()), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); dataList.add(dataIem); } } -- Gitblit v1.9.3