Browse Source

mssql: adds fill to timeGroup macro.

Leonard Gram 7 years ago
parent
commit
571556e1d8
2 changed files with 44 additions and 2 deletions
  1. 14 1
      pkg/tsdb/mssql/macros.go
  2. 30 1
      pkg/tsdb/mssql/macros_test.go

+ 14 - 1
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 {

+ 30 - 1
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())
+		})
 	})
 }