mssql: adds fill to timeGroup macro.

This commit is contained in:
Leonard Gram 2018-03-15 13:11:26 +01:00
parent d2267643ed
commit 571556e1d8
2 changed files with 44 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
"strconv"
) )
//const rsString = `(?:"([^"]*)")`; //const rsString = `(?:"([^"]*)")`;
@ -99,7 +100,19 @@ func (m *MsSqlMacroEngine) evaluateMacro(name string, args []string) (string, er
if err != nil { if err != nil {
return "", fmt.Errorf("error parsing interval %v", args[1]) return "", fmt.Errorf("error parsing interval %v", args[1])
} }
if len(args) == 3 {
m.Query.Model.Set("fill", true)
m.Query.Model.Set("fillInterval", interval.Seconds())
if args[2] == "NULL" {
m.Query.Model.Set("fillNull", true)
} else {
floatVal, err := strconv.ParseFloat(args[2], 64)
if err != nil {
return "", fmt.Errorf("error parsing fill value %v", args[2])
}
m.Query.Model.Set("fillValue", floatVal)
}
}
return fmt.Sprintf("cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), %s))/%.0f as int)*%.0f as int)", args[0], interval.Seconds(), interval.Seconds()), nil return fmt.Sprintf("cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), %s))/%.0f as int)*%.0f as int)", args[0], interval.Seconds(), interval.Seconds()), nil
case "__unixEpochFilter": case "__unixEpochFilter":
if len(args) == 0 { if len(args) == 0 {

View File

@ -3,15 +3,19 @@ package mssql
import ( import (
"testing" "testing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
"time"
) )
func TestMacroEngine(t *testing.T) { func TestMacroEngine(t *testing.T) {
Convey("MacroEngine", t, func() { Convey("MacroEngine", t, func() {
engine := &MsSqlMacroEngine{} engine := &MsSqlMacroEngine{}
timeRange := &tsdb.TimeRange{From: "5m", To: "now"} timeRange := &tsdb.TimeRange{From: "5m", To: "now"}
query := &tsdb.Query{} query := &tsdb.Query{
Model: simplejson.New(),
}
Convey("interpolate __time function", func() { Convey("interpolate __time function", func() {
sql, err := engine.Interpolate(query, nil, "select $__time(time_column)") sql, err := engine.Interpolate(query, nil, "select $__time(time_column)")
@ -97,5 +101,30 @@ func TestMacroEngine(t *testing.T) {
So(sql, ShouldEqual, "select 18446744066914187038") So(sql, ShouldEqual, "select 18446744066914187038")
}) })
Convey("interpolate __timeGroup function with fill (value = NULL)", func() {
_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', NULL)")
fill := query.Model.Get("fill").MustBool()
fillNull := query.Model.Get("fillNull").MustBool()
fillInterval := query.Model.Get("fillInterval").MustInt()
So(err, ShouldBeNil)
So(fill, ShouldBeTrue)
So(fillNull, ShouldBeTrue)
So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
})
Convey("interpolate __timeGroup function with fill (value = float)", func() {
_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', 1.5)")
fill := query.Model.Get("fill").MustBool()
fillValue := query.Model.Get("fillValue").MustFloat64()
fillInterval := query.Model.Get("fillInterval").MustInt()
So(err, ShouldBeNil)
So(fill, ShouldBeTrue)
So(fillValue, ShouldEqual, 1.5)
So(fillInterval, ShouldEqual, 5*time.Minute.Seconds())
})
}) })
} }