Commit 4f3c8c66 by bergquist

fix(influxdb): add default operator

ref #6523
parent 71eb0f32
......@@ -5,9 +5,15 @@ import (
"strconv"
"strings"
"regexp"
"github.com/grafana/grafana/pkg/tsdb"
)
var (
regexpOperatorPattern *regexp.Regexp = regexp.MustCompile(`^\/.*\/$`)
)
type QueryBuilder struct{}
func (qb *QueryBuilder) Build(query *Query, queryContext *tsdb.QueryContext) (string, error) {
......@@ -43,18 +49,28 @@ func (qb *QueryBuilder) renderTags(query *Query) []string {
str += " "
}
value := tag.Value
nValue, err := strconv.ParseFloat(tag.Value, 64)
//If the operator is missing we fall back to sensible defaults
if tag.Operator == "" {
if regexpOperatorPattern.Match([]byte(tag.Value)) {
tag.Operator = "=~"
} else {
tag.Operator = "="
}
}
textValue := ""
numericValue, err := strconv.ParseFloat(tag.Value, 64)
// quote value unless regex or number
if tag.Operator == "=~" || tag.Operator == "!~" {
value = fmt.Sprintf("%s", value)
textValue = tag.Value
} else if err == nil {
value = fmt.Sprintf("%v", nValue)
textValue = fmt.Sprintf("%v", numericValue)
} else {
value = fmt.Sprintf("'%s'", value)
textValue = fmt.Sprintf("'%s'", tag.Value)
}
res = append(res, fmt.Sprintf(`%s"%s" %s %s`, str, tag.Key, tag.Operator, value))
res = append(res, fmt.Sprintf(`%s"%s" %s %s`, str, tag.Key, tag.Operator, textValue))
}
return res
......
......@@ -86,16 +86,34 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
So(rawQuery, ShouldEqual, `Raw query`)
})
Convey("can render normal tags without operator", func() {
query := &Query{Tags: []*Tag{&Tag{Operator: "", Value: `value`, Key: "key"}}}
So(strings.Join(builder.renderTags(query), ""), ShouldEqual, `"key" = 'value'`)
})
Convey("can render regex tags without operator", func() {
query := &Query{Tags: []*Tag{&Tag{Operator: "", Value: `/value/`, Key: "key"}}}
So(strings.Join(builder.renderTags(query), ""), ShouldEqual, `"key" =~ /value/`)
})
Convey("can render regex tags", func() {
query := &Query{Tags: []*Tag{&Tag{Operator: "=~", Value: "value", Key: "key"}}}
query := &Query{Tags: []*Tag{&Tag{Operator: "=~", Value: `/value/`, Key: "key"}}}
So(strings.Join(builder.renderTags(query), ""), ShouldEqual, `"key" =~ value`)
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"}}}
query := &Query{Tags: []*Tag{&Tag{Operator: "=", Value: "10001", Key: "key"}}}
So(strings.Join(builder.renderTags(query), ""), ShouldEqual, `"key" = 10001`)
})
Convey("can render number tags with decimals", func() {
query := &Query{Tags: []*Tag{&Tag{Operator: "=", Value: "10001.1", Key: "key"}}}
So(strings.Join(builder.renderTags(query), ""), ShouldEqual, `"key" = 1`)
So(strings.Join(builder.renderTags(query), ""), ShouldEqual, `"key" = 10001.1`)
})
Convey("can render string tags", 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