| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | package mssql | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-19 20:32:04 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-15 20:11:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/tsdb" | 
					
						
							|  |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestMacroEngine(t *testing.T) { | 
					
						
							|  |  |  | 	Convey("MacroEngine", t, func() { | 
					
						
							| 
									
										
										
										
											2018-07-27 00:11:10 +08:00
										 |  |  | 		engine := &msSqlMacroEngine{} | 
					
						
							| 
									
										
										
										
											2018-03-15 20:11:26 +08:00
										 |  |  | 		query := &tsdb.Query{ | 
					
						
							|  |  |  | 			Model: simplejson.New(), | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 		Convey("Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05", func() { | 
					
						
							|  |  |  | 			from := time.Date(2018, 4, 12, 18, 0, 0, 0, time.UTC) | 
					
						
							|  |  |  | 			to := from.Add(5 * time.Minute) | 
					
						
							|  |  |  | 			timeRange := tsdb.NewFakeTimeRange("5m", "now", to) | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __time function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, nil, "select $__time(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(sql, ShouldEqual, "select time_column AS time") | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-12-13 04:43:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeEpoch function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, nil, "select $__timeEpoch(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-12-13 04:43:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(sql, ShouldEqual, "select DATEDIFF(second, '1970-01-01', time_column) AS time") | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-12-13 04:43:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeEpoch function wrapped in aggregation", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, nil, "select min($__timeEpoch(time_column))") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(sql, ShouldEqual, "select min(DATEDIFF(second, '1970-01-01', time_column) AS time)") | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 11:48:14 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339))) | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-03-14 05:49:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 21:18:33 +08:00
										 |  |  | 			Convey("interpolate __timeFrom function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, "select '2018-04-12T18:00:00Z'") | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __timeTo function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__timeTo()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, "select '2018-04-12T18:05:00Z'") | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeGroup function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-08-02 02:58:51 +08:00
										 |  |  | 				sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column,'5m')") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-03-14 05:49:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-01 21:57:02 +08:00
										 |  |  | 				So(sql, ShouldEqual, "GROUP BY FLOOR(DATEDIFF(second, '1970-01-01', time_column)/300)*300") | 
					
						
							| 
									
										
										
										
											2018-08-02 02:58:51 +08:00
										 |  |  | 				So(sql2, ShouldEqual, sql+" AS [time]") | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-03-14 05:49:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeGroup function with spaces around arguments", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-08-02 02:58:51 +08:00
										 |  |  | 				sql2, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroupAlias(time_column , '5m')") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-03-14 05:49:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-01 21:57:02 +08:00
										 |  |  | 				So(sql, ShouldEqual, "GROUP BY FLOOR(DATEDIFF(second, '1970-01-01', time_column)/300)*300") | 
					
						
							| 
									
										
										
										
											2018-08-02 02:58:51 +08:00
										 |  |  | 				So(sql2, ShouldEqual, sql+" AS [time]") | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-03-19 20:32:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeGroup function with fill (value = NULL)", func() { | 
					
						
							|  |  |  | 				_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', NULL)") | 
					
						
							| 
									
										
										
										
											2018-03-19 20:32:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				fill := query.Model.Get("fill").MustBool() | 
					
						
							| 
									
										
										
										
											2018-07-30 17:04:04 +08:00
										 |  |  | 				fillMode := query.Model.Get("fillMode").MustString() | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				fillInterval := query.Model.Get("fillInterval").MustInt() | 
					
						
							| 
									
										
										
										
											2018-03-19 20:32:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(fill, ShouldBeTrue) | 
					
						
							| 
									
										
										
										
											2018-07-30 17:04:04 +08:00
										 |  |  | 				So(fillMode, ShouldEqual, "null") | 
					
						
							|  |  |  | 				So(fillInterval, ShouldEqual, 5*time.Minute.Seconds()) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-08 03:01:41 +08:00
										 |  |  | 			Convey("interpolate __timeGroup function with fill (value = previous)", func() { | 
					
						
							|  |  |  | 				_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', previous)") | 
					
						
							| 
									
										
										
										
											2018-07-30 17:04:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				fill := query.Model.Get("fill").MustBool() | 
					
						
							|  |  |  | 				fillMode := query.Model.Get("fillMode").MustString() | 
					
						
							|  |  |  | 				fillInterval := query.Model.Get("fillInterval").MustInt() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(fill, ShouldBeTrue) | 
					
						
							| 
									
										
										
										
											2018-08-08 03:01:41 +08:00
										 |  |  | 				So(fillMode, ShouldEqual, "previous") | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(fillInterval, ShouldEqual, 5*time.Minute.Seconds()) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-03-19 20:32:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeGroup function with fill (value = float)", func() { | 
					
						
							|  |  |  | 				_, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m', 1.5)") | 
					
						
							| 
									
										
										
										
											2018-03-19 20:32:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				fill := query.Model.Get("fill").MustBool() | 
					
						
							|  |  |  | 				fillValue := query.Model.Get("fillValue").MustFloat64() | 
					
						
							|  |  |  | 				fillInterval := query.Model.Get("fillInterval").MustInt() | 
					
						
							| 
									
										
										
										
											2018-03-19 20:32:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				So(fill, ShouldBeTrue) | 
					
						
							|  |  |  | 				So(fillValue, ShouldEqual, 1.5) | 
					
						
							|  |  |  | 				So(fillInterval, ShouldEqual, 5*time.Minute.Seconds()) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __unixEpochFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.Unix(), to.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-04 04:43:08 +08:00
										 |  |  | 			Convey("interpolate __unixEpochNanoFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFilter(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.UnixNano(), to.UnixNano())) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochNanoFrom function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFrom()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-04 05:25:10 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select %d", from.UnixNano())) | 
					
						
							| 
									
										
										
										
											2019-01-04 04:43:08 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochNanoTo function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoTo()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-04 05:25:10 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select %d", to.UnixNano())) | 
					
						
							| 
									
										
										
										
											2019-01-04 04:43:08 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-13 18:23:42 +08:00
										 |  |  | 			Convey("interpolate __unixEpochGroup function", func() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 				sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, "SELECT FLOOR(time_column/300)*300") | 
					
						
							|  |  |  | 				So(sql2, ShouldEqual, sql+" AS [time]") | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 		Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() { | 
					
						
							|  |  |  | 			from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC) | 
					
						
							|  |  |  | 			to := time.Date(1965, 2, 3, 8, 0, 0, 0, time.UTC) | 
					
						
							|  |  |  | 			timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __timeFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 11:48:14 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339))) | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.Unix(), to.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2019-01-04 04:43:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochNanoFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFilter(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.UnixNano(), to.UnixNano())) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 		Convey("Given a time range between 1960-02-01 07:00 and 1980-02-03 08:00", func() { | 
					
						
							|  |  |  | 			from := time.Date(1960, 2, 1, 7, 0, 0, 0, time.UTC) | 
					
						
							|  |  |  | 			to := time.Date(1980, 2, 3, 8, 0, 0, 0, time.UTC) | 
					
						
							|  |  |  | 			timeRange := tsdb.NewTimeRange(strconv.FormatInt(from.UnixNano()/int64(time.Millisecond), 10), strconv.FormatInt(to.UnixNano()/int64(time.Millisecond), 10)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __timeFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "WHERE $__timeFilter(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-29 11:48:14 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("WHERE time_column BETWEEN '%s' AND '%s'", from.Format(time.RFC3339), to.Format(time.RFC3339))) | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.Unix(), to.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2019-01-04 04:43:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochNanoFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFilter(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select time_column >= %d AND time_column <= %d", from.UnixNano(), to.UnixNano())) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-12-04 02:56:21 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |