From 571556e1d89c3ce1cfbb1fc3fe8a4fb48d5a6105 Mon Sep 17 00:00:00 2001 From: Leonard Gram Date: Thu, 15 Mar 2018 13:11:26 +0100 Subject: [PATCH] mssql: adds fill to timeGroup macro. --- pkg/tsdb/mssql/macros.go | 15 ++++++++++++++- pkg/tsdb/mssql/macros_test.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pkg/tsdb/mssql/macros.go b/pkg/tsdb/mssql/macros.go index 02095fd75c1..5b1a115b700 100644 --- a/pkg/tsdb/mssql/macros.go +++ b/pkg/tsdb/mssql/macros.go @@ -7,6 +7,7 @@ import ( "time" "github.com/grafana/grafana/pkg/tsdb" + "strconv" ) //const rsString = `(?:"([^"]*)")`; @@ -99,7 +100,19 @@ func (m *MsSqlMacroEngine) evaluateMacro(name string, args []string) (string, er if err != nil { 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 case "__unixEpochFilter": if len(args) == 0 { diff --git a/pkg/tsdb/mssql/macros_test.go b/pkg/tsdb/mssql/macros_test.go index fdd2c39b7ce..a1984634110 100644 --- a/pkg/tsdb/mssql/macros_test.go +++ b/pkg/tsdb/mssql/macros_test.go @@ -3,15 +3,19 @@ package mssql import ( "testing" + "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/tsdb" . "github.com/smartystreets/goconvey/convey" + "time" ) func TestMacroEngine(t *testing.T) { Convey("MacroEngine", t, func() { engine := &MsSqlMacroEngine{} timeRange := &tsdb.TimeRange{From: "5m", To: "now"} - query := &tsdb.Query{} + query := &tsdb.Query{ + Model: simplejson.New(), + } Convey("interpolate __time function", func() { sql, err := engine.Interpolate(query, nil, "select $__time(time_column)") @@ -97,5 +101,30 @@ func TestMacroEngine(t *testing.T) { 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()) + }) }) }