瀏覽代碼

adjust metric prefix code to sql engine refactor

Sven Klemm 7 年之前
父節點
當前提交
7905c29875
共有 2 個文件被更改,包括 18 次插入2 次删除
  1. 14 1
      pkg/tsdb/sql_engine.go
  2. 4 1
      public/app/plugins/datasource/postgres/partials/query.editor.html

+ 14 - 1
pkg/tsdb/sql_engine.go

@@ -229,6 +229,8 @@ func (e *sqlQueryEndpoint) transformToTimeSeries(query *Query, rows *core.Rows,
 	rowCount := 0
 	rowCount := 0
 	timeIndex := -1
 	timeIndex := -1
 	metricIndex := -1
 	metricIndex := -1
+	metricPrefix := false
+	var metricPrefixValue string
 
 
 	// check columns of resultset: a column named time is mandatory
 	// check columns of resultset: a column named time is mandatory
 	// the first text column is treated as metric name unless a column named metric is present
 	// the first text column is treated as metric name unless a column named metric is present
@@ -256,6 +258,11 @@ func (e *sqlQueryEndpoint) transformToTimeSeries(query *Query, rows *core.Rows,
 		}
 		}
 	}
 	}
 
 
+	// use metric column as prefix with multiple value columns
+	if metricIndex != -1 && len(columnNames) > 3 {
+		metricPrefix = true
+	}
+
 	if timeIndex == -1 {
 	if timeIndex == -1 {
 		return fmt.Errorf("Found no column named %s", strings.Join(e.timeColumnNames, " or "))
 		return fmt.Errorf("Found no column named %s", strings.Join(e.timeColumnNames, " or "))
 	}
 	}
@@ -301,7 +308,11 @@ func (e *sqlQueryEndpoint) transformToTimeSeries(query *Query, rows *core.Rows,
 
 
 		if metricIndex >= 0 {
 		if metricIndex >= 0 {
 			if columnValue, ok := values[metricIndex].(string); ok {
 			if columnValue, ok := values[metricIndex].(string); ok {
-				metric = columnValue
+				if metricPrefix {
+					metricPrefixValue = columnValue
+				} else {
+					metric = columnValue
+				}
 			} else {
 			} else {
 				return fmt.Errorf("Column metric must be of type %s. metric column name: %s type: %s but datatype is %T", strings.Join(e.metricColumnTypes, ", "), columnNames[metricIndex], columnTypes[metricIndex].DatabaseTypeName(), values[metricIndex])
 				return fmt.Errorf("Column metric must be of type %s. metric column name: %s type: %s but datatype is %T", strings.Join(e.metricColumnTypes, ", "), columnNames[metricIndex], columnTypes[metricIndex].DatabaseTypeName(), values[metricIndex])
 			}
 			}
@@ -318,6 +329,8 @@ func (e *sqlQueryEndpoint) transformToTimeSeries(query *Query, rows *core.Rows,
 
 
 			if metricIndex == -1 {
 			if metricIndex == -1 {
 				metric = col
 				metric = col
+			} else if metricPrefix {
+				metric = metricPrefixValue + " " + col
 			}
 			}
 
 
 			series, exist := pointsBySeries[metric]
 			series, exist := pointsBySeries[metric]

+ 4 - 1
public/app/plugins/datasource/postgres/partials/query.editor.html

@@ -40,7 +40,10 @@
 		<pre class="gf-form-pre alert alert-info">Time series:
 		<pre class="gf-form-pre alert alert-info">Time series:
 - return column named <i>time</i> (UTC in seconds or timestamp)
 - return column named <i>time</i> (UTC in seconds or timestamp)
 - return column(s) with numeric datatype as values
 - return column(s) with numeric datatype as values
-- (Optional: return column named <i>metric</i> to represent the series name. If no column named metric is found the column name of the value column is used as series name)
+Optional: 
+  - return column named <i>metric</i> to represent the series name. 
+  - If multiple value columns are returned the metric column is used as prefix. 
+  - If no column named metric is found the column name of the value column is used as series name
 
 
 Table:
 Table:
 - return any set of columns
 - return any set of columns