| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | package postgres | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/tsdb" | 
					
						
							|  |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestMacroEngine(t *testing.T) { | 
					
						
							|  |  |  | 	Convey("MacroEngine", t, func() { | 
					
						
							| 
									
										
										
										
											2018-04-16 00:38:20 +08:00
										 |  |  | 		engine := NewPostgresMacroEngine() | 
					
						
							| 
									
										
										
										
											2018-04-16 04:14:13 +08:00
										 |  |  | 		query := &tsdb.Query{} | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +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-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __time function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__time(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(sql, ShouldEqual, "select time_column AS \"time\"") | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __time function wrapped in aggregation", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select min($__time(time_column))") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(sql, ShouldEqual, "select min(time_column AS \"time\")") | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +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-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-16 04:14:13 +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
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeFrom function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 05:02:24 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select '%s'", from.Format(time.RFC3339))) | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeGroup function", func() { | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-03-03 02:20:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(sql, ShouldEqual, "GROUP BY (extract(epoch from time_column)/300)::bigint*300 AS time") | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-03-03 02:20:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeGroup function with spaces between args", func() { | 
					
						
							| 
									
										
										
										
											2018-03-03 02:20:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(sql, ShouldEqual, "GROUP BY (extract(epoch from time_column)/300)::bigint*300 AS time") | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __timeTo function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 05:02:24 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select '%s'", to.Format(time.RFC3339))) | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __unixEpochFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __unixEpochFrom function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select %d", from.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochTo function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +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-04-16 04:14:13 +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 __timeFrom function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 05:02:24 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select '%s'", from.Format(time.RFC3339))) | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __timeTo function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 05:02:24 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select '%s'", to.Format(time.RFC3339))) | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			Convey("interpolate __unixEpochFilter function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochFrom function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select %d", from.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochTo function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +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-04-16 04:14:13 +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 __timeFrom function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__timeFrom(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 05:02:24 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select '%s'", from.Format(time.RFC3339))) | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __timeTo function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__timeTo(time_column)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 05:02:24 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select '%s'", 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)") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.Unix(), to.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochFrom function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFrom()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select %d", from.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("interpolate __unixEpochTo function", func() { | 
					
						
							|  |  |  | 				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochTo()") | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-04-09 19:28:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-13 00:53:12 +08:00
										 |  |  | 				So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix())) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-04-09 19:28:32 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2017-10-10 21:19:14 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } |