Commit e499e885 by bergquist

fix(influxdb): fix for timerange

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