Commit f0a0e647 by bergquist

feat(influxdb): backend support for alias by segment

ref #6510
parent 97271514
......@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/grafana/grafana/pkg/tsdb"
......@@ -61,6 +62,8 @@ func (rp *ResponseParser) formatSerieName(row Row, column string, query *Query)
return rp.buildSerieNameFromQuery(row, column)
}
nameSegment := strings.Split(row.Name, ".")
result := legendFormat.ReplaceAllFunc([]byte(query.Alias), func(in []byte) []byte {
aliasFormat := string(in)
aliasFormat = strings.Replace(aliasFormat, "[[", "", 1)
......@@ -74,6 +77,11 @@ func (rp *ResponseParser) formatSerieName(row Row, column string, query *Query)
return []byte(column)
}
pos, err := strconv.Atoi(aliasFormat)
if err == nil && len(nameSegment) >= pos {
return []byte(nameSegment[pos])
}
if !strings.HasPrefix(aliasFormat, "tag_") {
return in
}
......
......@@ -72,7 +72,7 @@ func TestInfluxdbResponseParser(t *testing.T) {
Result{
Series: []Row{
{
Name: "cpu",
Name: "cpu.upc",
Columns: []string{"time", "mean", "sum"},
Tags: map[string]string{"datacenter": "America"},
Values: [][]interface{}{
......@@ -113,6 +113,20 @@ func TestInfluxdbResponseParser(t *testing.T) {
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() {
......
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