Commit 0633e2c0 by bergquist

feat(influxdb): add real support for time ranges

parent e36f9fa7
package influxdb
import "github.com/grafana/grafana/pkg/tsdb"
type Query struct {
Measurement string
Policy string
......@@ -8,7 +10,8 @@ type Query struct {
GroupBy []*QueryPart
Selects []*Select
Interval string
Interval string
TimeRange tsdb.TimeRange
}
type Tag struct {
......
......@@ -40,9 +40,14 @@ func (*QueryBuilder) Build(query *Query, queryContext *tsdb.QueryContext) (strin
}
func renderTimeFilter(query *Query) string {
//res += "$timeFilter"
//res += "time > now() -" + strings.Replace(queryContext.TimeRange.From, "now", "", 1)
return "time > now() - 5m"
from := "now() - " + query.TimeRange.From
to := ""
if query.TimeRange.To != "now" && query.TimeRange.To != "" {
to = " and time < now() - " + strings.Replace(query.TimeRange.To, "now-", "", 1)
}
return fmt.Sprintf("time > %s%s", from, to)
}
func renderSelectors(query *Query) string {
......
......@@ -8,6 +8,7 @@ import (
)
func TestInfluxdbQueryBuilder(t *testing.T) {
Convey("Influxdb query builder", t, func() {
builder := QueryBuilder{}
......@@ -31,6 +32,7 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
Policy: "policy",
GroupBy: []*QueryPart{groupBy1, groupBy2},
Interval: "10s",
TimeRange: tsdb.TimeRange{From: "5m"},
}
rawQuery, err := builder.Build(query, queryContext)
......@@ -45,11 +47,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() - 5m GROUP BY time(10s)`)
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)`)
})
Convey("can render time range", func() {
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")
})
Convey("render from: 10m", func() {
query := Query{TimeRange: tsdb.TimeRange{From: "10m"}}
So(renderTimeFilter(&query), 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