You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ha...@apache.org on 2022/08/24 06:58:53 UTC

[iotdb] branch master updated: [IOTDB-4183] Display the data of BOOL dataType in grafana plugin (#7076)

This is an automated email from the ASF dual-hosted git repository.

haonan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new d747ee33a0 [IOTDB-4183] Display the data of BOOL dataType in grafana plugin (#7076)
d747ee33a0 is described below

commit d747ee33a0b01402349ff51cfcae07bc0ed73e9e
Author: Summer <43...@users.noreply.github.com>
AuthorDate: Wed Aug 24 14:58:46 2022 +0800

    [IOTDB-4183] Display the data of BOOL dataType in grafana plugin (#7076)
---
 .../Ecosystem Integration/Grafana Plugin.md        |  1 +
 .../Ecosystem Integration/Grafana Plugin.md        |  2 +
 grafana-plugin/pkg/plugin/plugin.go                | 43 ++++++++++++++++++++--
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/docs/UserGuide/Ecosystem Integration/Grafana Plugin.md b/docs/UserGuide/Ecosystem Integration/Grafana Plugin.md
index 6696a02722..394d492d0a 100644
--- a/docs/UserGuide/Ecosystem Integration/Grafana Plugin.md	
+++ b/docs/UserGuide/Ecosystem Integration/Grafana Plugin.md	
@@ -296,6 +296,7 @@ In addition to the examples above, the following statements are supported:
 *  `show devices`
 *  `select xx from root.xxx limit xx 等sql 查询`
 
+Tip: If the query field contains Boolean data, the result value will be converted to 1 by true and 0 by false.
 
 #### Grafana alert function
 
diff --git a/docs/zh/UserGuide/Ecosystem Integration/Grafana Plugin.md b/docs/zh/UserGuide/Ecosystem Integration/Grafana Plugin.md
index 876875555f..9f91fc2980 100644
--- a/docs/zh/UserGuide/Ecosystem Integration/Grafana Plugin.md	
+++ b/docs/zh/UserGuide/Ecosystem Integration/Grafana Plugin.md	
@@ -302,6 +302,8 @@ Type下拉中有Query、Custom、Text box、Constant、DataSource、Interval、A
 *  `show devices`
 *  `select xx from root.xxx limit xx 等sql 查询`
 
+* 提示:如果查询的字段中有布尔类型的数据,会将true转化成1,false转化成0结果值进行显示。
+
 #### 告警功能
 本插件支持 Grafana alert功能。
 
diff --git a/grafana-plugin/pkg/plugin/plugin.go b/grafana-plugin/pkg/plugin/plugin.go
index 932f0e1fc0..a0ab87a2c9 100644
--- a/grafana-plugin/pkg/plugin/plugin.go
+++ b/grafana-plugin/pkg/plugin/plugin.go
@@ -133,7 +133,7 @@ type QueryDataReq struct {
 type QueryDataResponse struct {
 	Expressions []string    `json:"expressions"`
 	Timestamps  []int64     `json:"timestamps"`
-	Values      [][]float32 `json:"values"`
+	Values      [][]interface{} `json:"values"`
 	ColumnNames interface{} `json:"columnNames"`
 	Code        int32       `json:"code"`
 	Message     string      `json:"message"`
@@ -221,11 +221,11 @@ func (d *IoTDBDataSource) query(cxt context.Context, pCtx backend.PluginContext,
 	frame := data.NewFrame("response")
 	for i := 0; i < len(queryDataResp.Expressions); i++ {
 		times := make([]time.Time, len(queryDataResp.Timestamps))
-		values := make([]float32, len(queryDataResp.Timestamps))
 		for c := 0; c < len(queryDataResp.Timestamps); c++ {
 			times[c] = time.Unix(queryDataResp.Timestamps[c]/1000, 0)
-			values[c] = queryDataResp.Values[i][c]
 		}
+		values :=  recoverType(queryDataResp.Values[i])
+
 		frame.Fields = append(frame.Fields,
 			data.NewField("time", nil, times),
 			data.NewField(queryDataResp.Expressions[i], nil, values),
@@ -236,6 +236,43 @@ func (d *IoTDBDataSource) query(cxt context.Context, pCtx backend.PluginContext,
 	return response
 }
 
+func recoverType(m []interface{}) interface{} {
+    if len(m) > 0 {
+        switch m[0].(type) {
+        case float64:
+            tmp := make([]float64, len(m))
+            for i := range m {
+                tmp[i] = m[i].(float64)
+            }
+            return tmp
+        case string:
+            tmp := make([]string, len(m))
+            for i := range m {
+                tmp[i] = m[i].(string)
+            }
+            return tmp
+        case bool:
+            tmp := make([]float64, len(m))
+            for i := range m {
+                if m[i].(bool) {
+                    tmp[i] = 1
+                } else {
+                    tmp[i] = 0
+                }
+            }
+            return tmp
+        default:
+            tmp := make([]float64, len(m))
+            for i := range m {
+                tmp[i] = 0
+            }
+            return tmp
+        }
+    } else {
+        return make([]float64, 0)
+    }
+}
+
 // Whether the last character of the URL for processing datasource configuration is "/"
 func DataSourceUrlHandler(url string) string {
 	var lastCharacter = url[len(url)-1 : len(url)]