From 585be5b57da60870edf6e555aab1f0e61c609745 Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期五, 23 八月 2024 14:36:43 +0800
Subject: [PATCH] 动态influxdb

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/echarts/BarLineDTO.java                |   25 ++++++++++++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java   |    9 +++-
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/IndexStatisticDTO.java                 |   16 ++++++++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/IndexQueryDTO.java                     |   20 ++++++++++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/controller/DataController.java             |    6 +-
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/service/impl/InfluxDBServiceImpl.java |   20 +++++-----
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/echarts/SeriesItem.java                |   18 +++++++++
 7 files changed, 98 insertions(+), 16 deletions(-)

diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/controller/DataController.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/controller/DataController.java
index 0c83c57..01273df 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/controller/DataController.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/controller/DataController.java
@@ -2,9 +2,9 @@
 
 import com.iailab.framework.common.pojo.CommonResult;
 import com.iailab.framework.common.util.date.DateUtils;
-import com.iailab.module.data.common.dto.IndexQueryDTO;
-import com.iailab.module.data.common.dto.echarts.BarLineDTO;
-import com.iailab.module.data.common.dto.echarts.SeriesItem;
+import com.iailab.module.data.api.dto.IndexQueryDTO;
+import com.iailab.module.data.api.dto.echarts.BarLineDTO;
+import com.iailab.module.data.api.dto.echarts.SeriesItem;
 import com.iailab.module.data.common.utils.R;
 import com.iailab.module.data.api.dto.ApiPointValueQueryDTO;
 import com.iailab.module.data.point.collection.PointCollector;
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/IndexQueryDTO.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/IndexQueryDTO.java
new file mode 100644
index 0000000..3014e37
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/IndexQueryDTO.java
@@ -0,0 +1,20 @@
+package com.iailab.module.data.api.dto;
+
+import lombok.Data;
+
+import java.util.Date;
+import java.util.List;
+
+@Data
+public class IndexQueryDTO {
+	 
+    private Date startDate;
+    
+    private Date endDate;
+    
+    private String code;
+
+    private Integer granularity;
+
+    private List<String> codes;
+}
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/IndexStatisticDTO.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/IndexStatisticDTO.java
new file mode 100644
index 0000000..39ef1a4
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/IndexStatisticDTO.java
@@ -0,0 +1,16 @@
+package com.iailab.module.data.api.dto;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+
+@Data
+public class IndexStatisticDTO implements Serializable {
+	private static final long serialVersionUID = 1L;
+
+	public Date dateTime;
+	public BigDecimal data;
+
+}
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/echarts/BarLineDTO.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/echarts/BarLineDTO.java
new file mode 100644
index 0000000..eeabac0
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/echarts/BarLineDTO.java
@@ -0,0 +1,25 @@
+package com.iailab.module.data.api.dto.echarts;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * @author lirm
+ * @Description
+ * @createTime 2023.05.11
+ */
+@Data
+public class BarLineDTO implements Serializable {
+    private static final long serialVersionUID = 1L;
+    
+    private String valueName;//表名称
+
+    private List<String> legend;//线的名称
+
+    private List<String> categories;//X轴数据
+
+    private List<SeriesItem> series;//Y轴数据集合
+
+}
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/echarts/SeriesItem.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/echarts/SeriesItem.java
new file mode 100644
index 0000000..e9116ba
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/api/dto/echarts/SeriesItem.java
@@ -0,0 +1,18 @@
+package com.iailab.module.data.api.dto.echarts;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @author lirm
+ * @Description
+ * @createTime 2023.05.11
+ */
+@Data
+public class SeriesItem {
+	
+	private String name;
+
+    private List<Object[]> data;
+}
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java
index fa23641..a46e8af 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/influxdb/common/config/InfluxDBInstance.java
@@ -1,5 +1,6 @@
 package com.iailab.module.data.influxdb.common.config;
 
+import com.iailab.framework.tenant.core.context.TenantContextHolder;
 import com.influxdb.client.InfluxDBClient;
 import com.influxdb.client.InfluxDBClientFactory;
 import org.slf4j.Logger;
@@ -18,14 +19,13 @@
     @Value("${influx-db.org}")
     public String org;
 
-    @Value("${influx-db.bucket}")
-    public String bucket;
-
     @Value("${influx-db.token}")
     public String token;
 
     @Value("${influx-db.url}")
     public String url;
+
+    private final static String BUCKET_NAME = "tenant_";
 
     private Logger logger = LoggerFactory.getLogger(getClass());
 
@@ -43,5 +43,8 @@
         return client;
     }
 
+    public String getBucket() {
+        return BUCKET_NAME + TenantContextHolder.getRequiredTenantId();
+    }
 
 }
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 f7c0075..5470286 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
@@ -66,7 +66,7 @@
         point.addTag("point", pointNo);
         point.addField("value", Double.parseDouble(dataValue));
         point.time(time, WritePrecision.MS);
-        writeApiBlocking.writePoint(influxDBInstance.bucket, influxDBInstance.org, point);
+        writeApiBlocking.writePoint(influxDBInstance.getBucket(), influxDBInstance.org, point);
     }
 
     @Override
@@ -78,7 +78,7 @@
         point.addTag("point", pointNo);
         point.addField("value", Integer.parseInt(dataValue));
         point.time(time, WritePrecision.MS);
-        writeApiBlocking.writePoint(influxDBInstance.bucket, influxDBInstance.org, point);
+        writeApiBlocking.writePoint(influxDBInstance.getBucket(), influxDBInstance.org, point);
     }
 
     @Override
@@ -90,7 +90,7 @@
         point.addTag("point", pointNo);
         point.addField("value", Boolean.parseBoolean(dataValue));
         point.time(time, WritePrecision.MS);
-        writeApiBlocking.writePoint(influxDBInstance.bucket, influxDBInstance.org, point);
+        writeApiBlocking.writePoint(influxDBInstance.getBucket(), influxDBInstance.org, point);
     }
 
     @Override
@@ -100,7 +100,7 @@
         }
         if (!CollectionUtils.isEmpty(pointValues)) {
             pointValues.forEach(item -> {
-                writeApi.writeMeasurement(influxDBInstance.bucket, influxDBInstance.org, WritePrecision.MS, item);
+                writeApi.writeMeasurement(influxDBInstance.getBucket(), influxDBInstance.org, WritePrecision.MS, item);
             });
         }
         writeApi.flush();
@@ -113,7 +113,7 @@
         }
         if (!CollectionUtils.isEmpty(tagValues)) {
             tagValues.forEach(item -> {
-                writeApi.writeMeasurement(influxDBInstance.bucket, influxDBInstance.org, WritePrecision.MS, item);
+                writeApi.writeMeasurement(influxDBInstance.getBucket(), influxDBInstance.org, WritePrecision.MS, item);
             });
         }
         writeApi.flush();
@@ -142,7 +142,7 @@
             InfluxTagValuePOJO tag = influxParams.get(i);
             String measurement = TagValueUtils.getMeasurement(tag.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\")");
@@ -179,7 +179,7 @@
 
         String measurement = TagValueUtils.getMeasurement(tag.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\")");
@@ -232,7 +232,7 @@
             sql.append(" WHERE point = '");
             sql.append(point.getPoint());
             sql.append("'");
-            InfluxQLQueryResult data = influxQLQueryApi.query(new InfluxQLQuery(sql.toString(), influxDBInstance.bucket));
+            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);
         }
@@ -254,7 +254,7 @@
         sql.append(" WHERE point = '");
         sql.append(point.getPoint());
         sql.append("' AND time >= '" + utsStart +"'");
-        InfluxQLQueryResult data = influxQLQueryApi.query(new InfluxQLQuery(sql.toString(), influxDBInstance.bucket));
+        InfluxQLQueryResult data = influxQLQueryApi.query(new InfluxQLQuery(sql.toString(), influxDBInstance.getBucket()));
         if (data == null) {
             return null;
         }
@@ -291,7 +291,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\")");

--
Gitblit v1.9.3