Commit f4e0a924 by bergquist

feat(influxdb): choose highest interval value

parent 4144eacc
...@@ -3,6 +3,7 @@ package influxdb ...@@ -3,6 +3,7 @@ package influxdb
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
) )
...@@ -92,12 +93,27 @@ func fieldRenderer(query *Query, queryContext *tsdb.QueryContext, part *QueryPar ...@@ -92,12 +93,27 @@ func fieldRenderer(query *Query, queryContext *tsdb.QueryContext, part *QueryPar
return fmt.Sprintf(`"%s"`, part.Params[0]) return fmt.Sprintf(`"%s"`, part.Params[0])
} }
func getDefinedInterval(query *Query, queryContext *tsdb.QueryContext) string {
setInterval := strings.Replace(strings.Replace(query.Interval, "<", "", 1), ">", "", 1)
defaultInterval := tsdb.CalculateInterval(queryContext.TimeRange)
if strings.Contains(query.Interval, ">") {
parsedDefaultInterval, err := time.ParseDuration(defaultInterval)
parsedSetInterval, err2 := time.ParseDuration(setInterval)
if err == nil && err2 == nil && parsedDefaultInterval > parsedSetInterval {
return defaultInterval
}
}
return setInterval
}
func functionRenderer(query *Query, queryContext *tsdb.QueryContext, part *QueryPart, innerExpr string) string { func functionRenderer(query *Query, queryContext *tsdb.QueryContext, part *QueryPart, innerExpr string) string {
for i, v := range part.Params { for i, param := range part.Params {
if v == "$interval" { if param == "$interval" {
if query.Interval != "" { if query.Interval != "" {
interval := strings.Replace(strings.Replace(query.Interval, "<", "", 1), ">", "", 1) part.Params[i] = getDefinedInterval(query, queryContext)
part.Params[i] = interval
} else { } else {
part.Params[i] = tsdb.CalculateInterval(queryContext.TimeRange) part.Params[i] = tsdb.CalculateInterval(queryContext.TimeRange)
} }
......
...@@ -45,6 +45,27 @@ func TestInfluxdbQueryPart(t *testing.T) { ...@@ -45,6 +45,27 @@ func TestInfluxdbQueryPart(t *testing.T) {
So(res, ShouldEqual, "time(200ms)") So(res, ShouldEqual, "time(200ms)")
}) })
Convey("render time interval >10s", func() {
part, err := NewQueryPart("time", []string{"$interval"})
So(err, ShouldBeNil)
query.Interval = ">10s"
res := part.Render(query, queryContext, "")
So(res, ShouldEqual, "time(10s)")
})
Convey("render time interval >1s and higher interval calculation", func() {
part, err := NewQueryPart("time", []string{"$interval"})
queryContext := &tsdb.QueryContext{TimeRange: tsdb.NewTimeRange("1y", "now")}
So(err, ShouldBeNil)
query.Interval = ">1s"
res := part.Render(query, queryContext, "")
So(res, ShouldEqual, "time(168h)")
})
Convey("render spread", func() { Convey("render spread", func() {
part, err := NewQueryPart("spread", []string{}) part, err := NewQueryPart("spread", []string{})
So(err, ShouldBeNil) So(err, ShouldBeNil)
......
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