Commit e499e885 by bergquist

fix(influxdb): fix for timerange

parent 0633e2c0
package influxdb package influxdb
import "github.com/grafana/grafana/pkg/tsdb"
type Query struct { type Query struct {
Measurement string Measurement string
Policy string Policy string
...@@ -10,8 +8,7 @@ type Query struct { ...@@ -10,8 +8,7 @@ type Query struct {
GroupBy []*QueryPart GroupBy []*QueryPart
Selects []*Select Selects []*Select
Interval string Interval string
TimeRange tsdb.TimeRange
} }
type Tag struct { type Tag struct {
......
...@@ -33,18 +33,18 @@ func (*QueryBuilder) Build(query *Query, queryContext *tsdb.QueryContext) (strin ...@@ -33,18 +33,18 @@ func (*QueryBuilder) Build(query *Query, queryContext *tsdb.QueryContext) (strin
res := renderSelectors(query) res := renderSelectors(query)
res += renderMeasurement(query) res += renderMeasurement(query)
res += renderWhereClause(query) res += renderWhereClause(query)
res += renderTimeFilter(query) res += renderTimeFilter(query, queryContext)
res += renderGroupBy(query) res += renderGroupBy(query)
return res, nil return res, nil
} }
func renderTimeFilter(query *Query) string { func renderTimeFilter(query *Query, queryContext *tsdb.QueryContext) string {
from := "now() - " + query.TimeRange.From from := "now() - " + queryContext.TimeRange.From
to := "" to := ""
if query.TimeRange.To != "now" && query.TimeRange.To != "" { if queryContext.TimeRange.To != "now" && queryContext.TimeRange.To != "" {
to = " and time < now() - " + strings.Replace(query.TimeRange.To, "now-", "", 1) to = " and time < now() - " + strings.Replace(queryContext.TimeRange.To, "now-", "", 1)
} }
return fmt.Sprintf("time > %s%s", from, to) return fmt.Sprintf("time > %s%s", from, to)
......
...@@ -22,7 +22,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) { ...@@ -22,7 +22,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
tag2 := &Tag{Key: "hostname", Value: "server2", Operator: "=", Condition: "OR"} tag2 := &Tag{Key: "hostname", Value: "server2", Operator: "=", Condition: "OR"}
queryContext := &tsdb.QueryContext{ queryContext := &tsdb.QueryContext{
TimeRange: tsdb.NewTimeRange("now-5m", "now"), TimeRange: tsdb.NewTimeRange("5m", "now"),
} }
Convey("can build simple query", func() { Convey("can build simple query", func() {
...@@ -32,7 +32,6 @@ func TestInfluxdbQueryBuilder(t *testing.T) { ...@@ -32,7 +32,6 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
Policy: "policy", Policy: "policy",
GroupBy: []*QueryPart{groupBy1, groupBy2}, GroupBy: []*QueryPart{groupBy1, groupBy2},
Interval: "10s", Interval: "10s",
TimeRange: tsdb.TimeRange{From: "5m"},
} }
rawQuery, err := builder.Build(query, queryContext) rawQuery, err := builder.Build(query, queryContext)
...@@ -47,23 +46,24 @@ func TestInfluxdbQueryBuilder(t *testing.T) { ...@@ -47,23 +46,24 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
GroupBy: []*QueryPart{groupBy1}, GroupBy: []*QueryPart{groupBy1},
Tags: []*Tag{tag1, tag2}, Tags: []*Tag{tag1, tag2},
Interval: "5s", Interval: "5s",
TimeRange: tsdb.TimeRange{From: "1h", To: "now-1m"},
} }
rawQuery, err := builder.Build(query, queryContext) rawQuery, err := builder.Build(query, queryContext)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "cpu" WHERE "hostname" = 'server1' OR "hostname" = 'server2' AND time > now() - 1h and time < now() - 1m GROUP BY time(10s)`) So(rawQuery, ShouldEqual, `SELECT mean("value") FROM "cpu" WHERE "hostname" = 'server1' OR "hostname" = 'server2' AND time > now() - 5m GROUP BY time(10s)`)
}) })
Convey("can render time range", func() { Convey("can render time range", func() {
query := Query{}
Convey("render from: 2h to now-1h", func() { Convey("render from: 2h to now-1h", func() {
query := Query{TimeRange: tsdb.TimeRange{From: "2h", To: "now-1h"}} query := Query{}
So(renderTimeFilter(&query), ShouldEqual, "time > now() - 2h and time < now() - 1h") queryContext := &tsdb.QueryContext{TimeRange: tsdb.NewTimeRange("2h", "now-1h")}
So(renderTimeFilter(&query, queryContext), ShouldEqual, "time > now() - 2h and time < now() - 1h")
}) })
Convey("render from: 10m", func() { Convey("render from: 10m", func() {
query := Query{TimeRange: tsdb.TimeRange{From: "10m"}} queryContext := &tsdb.QueryContext{TimeRange: tsdb.NewTimeRange("10m", "now")}
So(renderTimeFilter(&query), ShouldEqual, "time > now() - 10m") So(renderTimeFilter(&query, queryContext), ShouldEqual, "time > now() - 10m")
}) })
}) })
}) })
......
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