Skip to content

Commit

Permalink
fix(influxdb): fixes bug when using regexps/numbers in tags
Browse files Browse the repository at this point in the history
closes #6488
  • Loading branch information
bergquist committed Nov 7, 2016
1 parent 16e7bfc commit 56340bf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/tsdb/influxdb/query_builder.go
Expand Up @@ -2,6 +2,7 @@ package influxdb

import (
"fmt"
"strconv"
"strings"

"github.com/grafana/grafana/pkg/tsdb"
Expand Down Expand Up @@ -42,7 +43,18 @@ func (qb *QueryBuilder) renderTags(query *Query) []string {
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
Expand Down
20 changes: 20 additions & 0 deletions pkg/tsdb/influxdb/query_builder_test.go
Expand Up @@ -3,6 +3,8 @@ package influxdb
import (
"testing"

"strings"

"github.com/grafana/grafana/pkg/tsdb"
. "github.com/smartystreets/goconvey/convey"
)
Expand Down Expand Up @@ -83,5 +85,23 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
So(err, ShouldBeNil)
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'`)
})
})
}

0 comments on commit 56340bf

Please sign in to comment.