Commit f0a0e647 by bergquist

feat(influxdb): backend support for alias by segment

ref #6510
parent 97271514
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp" "regexp"
"strconv"
"strings" "strings"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
...@@ -61,6 +62,8 @@ func (rp *ResponseParser) formatSerieName(row Row, column string, query *Query) ...@@ -61,6 +62,8 @@ func (rp *ResponseParser) formatSerieName(row Row, column string, query *Query)
return rp.buildSerieNameFromQuery(row, column) return rp.buildSerieNameFromQuery(row, column)
} }
nameSegment := strings.Split(row.Name, ".")
result := legendFormat.ReplaceAllFunc([]byte(query.Alias), func(in []byte) []byte { result := legendFormat.ReplaceAllFunc([]byte(query.Alias), func(in []byte) []byte {
aliasFormat := string(in) aliasFormat := string(in)
aliasFormat = strings.Replace(aliasFormat, "[[", "", 1) aliasFormat = strings.Replace(aliasFormat, "[[", "", 1)
...@@ -74,6 +77,11 @@ func (rp *ResponseParser) formatSerieName(row Row, column string, query *Query) ...@@ -74,6 +77,11 @@ func (rp *ResponseParser) formatSerieName(row Row, column string, query *Query)
return []byte(column) return []byte(column)
} }
pos, err := strconv.Atoi(aliasFormat)
if err == nil && len(nameSegment) >= pos {
return []byte(nameSegment[pos])
}
if !strings.HasPrefix(aliasFormat, "tag_") { if !strings.HasPrefix(aliasFormat, "tag_") {
return in return in
} }
......
...@@ -72,7 +72,7 @@ func TestInfluxdbResponseParser(t *testing.T) { ...@@ -72,7 +72,7 @@ func TestInfluxdbResponseParser(t *testing.T) {
Result{ Result{
Series: []Row{ Series: []Row{
{ {
Name: "cpu", Name: "cpu.upc",
Columns: []string{"time", "mean", "sum"}, Columns: []string{"time", "mean", "sum"},
Tags: map[string]string{"datacenter": "America"}, Tags: map[string]string{"datacenter": "America"},
Values: [][]interface{}{ Values: [][]interface{}{
...@@ -113,6 +113,20 @@ func TestInfluxdbResponseParser(t *testing.T) { ...@@ -113,6 +113,20 @@ func TestInfluxdbResponseParser(t *testing.T) {
So(result.Series[0].Name, ShouldEqual, "alias America") So(result.Series[0].Name, ShouldEqual, "alias America")
}) })
Convey("segment alias", func() {
query := &Query{Alias: "alias $1"}
result := parser.Parse(response, query)
So(result.Series[0].Name, ShouldEqual, "alias upc")
})
Convey("segment position out of bound", func() {
query := &Query{Alias: "alias $5"}
result := parser.Parse(response, query)
So(result.Series[0].Name, ShouldEqual, "alias $5")
})
}) })
Convey("[[]] alias", func() { Convey("[[]] alias", func() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment