Commit 56340bfd by bergquist

fix(influxdb): fixes bug when using regexps/numbers in tags

closes #6488
parent 16e7bfc6
...@@ -2,6 +2,7 @@ package influxdb ...@@ -2,6 +2,7 @@ package influxdb
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
...@@ -42,7 +43,18 @@ func (qb *QueryBuilder) renderTags(query *Query) []string { ...@@ -42,7 +43,18 @@ func (qb *QueryBuilder) renderTags(query *Query) []string {
str += " " str += " "
} }
res = append(res, fmt.Sprintf(`%s"%s" %s '%s'`, str, tag.Key, tag.Operator, tag.Value)) value := tag.Value
nValue, err := strconv.ParseFloat(tag.Value, 64)
if tag.Operator == "=~" || tag.Operator == "!~" {
value = fmt.Sprintf("%s", value)
} else if err == nil {
value = fmt.Sprintf("%v", nValue)
} else {
value = fmt.Sprintf("'%s'", value)
}
res = append(res, fmt.Sprintf(`%s"%s" %s %s`, str, tag.Key, tag.Operator, value))
} }
return res return res
......
...@@ -3,6 +3,8 @@ package influxdb ...@@ -3,6 +3,8 @@ package influxdb
import ( import (
"testing" "testing"
"strings"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
...@@ -83,5 +85,23 @@ func TestInfluxdbQueryBuilder(t *testing.T) { ...@@ -83,5 +85,23 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(rawQuery, ShouldEqual, `Raw query`) So(rawQuery, ShouldEqual, `Raw query`)
}) })
Convey("can render regex tags", func() {
query := &Query{Tags: []*Tag{&Tag{Operator: "=~", Value: "value", Key: "key"}}}
So(strings.Join(builder.renderTags(query), ""), ShouldEqual, `"key" =~ value`)
})
Convey("can render number tags", func() {
query := &Query{Tags: []*Tag{&Tag{Operator: "=", Value: "1", Key: "key"}}}
So(strings.Join(builder.renderTags(query), ""), ShouldEqual, `"key" = 1`)
})
Convey("can render string tags", func() {
query := &Query{Tags: []*Tag{&Tag{Operator: "=", Value: "value", Key: "key"}}}
So(strings.Join(builder.renderTags(query), ""), ShouldEqual, `"key" = 'value'`)
})
}) })
} }
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