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 | 41 ++++++++++++++++++----------------------- 1 files changed, 18 insertions(+), 23 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 e609842..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 @@ -26,6 +26,7 @@ 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; @@ -35,11 +36,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; -import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; - -import static com.iailab.framework.common.pojo.CommonResult.success; /** * InfluxDB操作类 @@ -62,18 +60,13 @@ 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(); private int pas_ms = 1000; - - private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - @Resource - private InfluxDBService influxDBService; @Override public void syncWriteFloatValue(String pointNo, String dataValue, long time) { @@ -132,7 +125,7 @@ } if (!CollectionUtils.isEmpty(tagValues)) { tagValues.forEach(item -> { - log.info("bucket==="+ influxDBInstance.getBucket() + ",org===" + influxDBInstance.org); + log.info("bucket===" + influxDBInstance.getBucket() + ",org===" + influxDBInstance.org); writeApi.writeMeasurement(influxDBInstance.getBucket(), influxDBInstance.org, WritePrecision.MS, item); }); } @@ -152,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"; @@ -247,7 +240,7 @@ List<Map<String, Object>> list = data.get(queryDto.getTagIds().get(0)); //导出 - try{ + try { String sheetTitle = "采集数据"; String[] title = new String[]{"值", "时间"}; ExcelUtil.exportPointValue(sheetTitle, title, new ArrayList<>(list), response); @@ -285,7 +278,7 @@ //提取list List<Map<String, Object>> list = data.get(queryDto.getPointNos().get(0)); - for(Map<String, Object> map : list){ + for (Map<String, Object> map : list) { PointValueExportVO dto = new PointValueExportVO(); dto.setDatatime(map.get("time").toString()); dto.setDatavalue(map.get("value").toString()); @@ -312,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; } @@ -322,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()); @@ -331,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; @@ -358,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"; @@ -369,7 +364,7 @@ InfluxPointValuePOJO point = influxParams.get(i); String measurement = PointValueUtils.getMeasurement(point.getType()); StringBuilder sb = new StringBuilder(); - sb.append("from(bucket:\"" + influxDBInstance.bucket + "\") "); + sb.append("from(bucket:\"" + influxDBInstance.getBucket() + "\") "); sb.append("|> range(start: ").append(start).append(", stop: ").append(stop).append(") "); sb.append("|> filter(fn: (r) => r[\"_measurement\"] == \"" + measurement + "\")"); sb.append("|> filter(fn: (r) => r[\"_field\"] == \"value\")"); @@ -384,7 +379,7 @@ for (FluxRecord record : records) { Map<String, Object> dataIem = new HashMap<>(2); dataIem.put(VALUE, record.getValueByKey("_value")); - dataIem.put(TIME, sdf.format(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