Переглянути джерело

Added unixEpochNanoTo and unixEpochNanoFrom macros to postgresql

Ander Arguiñano 7 роки тому
батько
коміт
9105625f24
2 змінених файлів з 17 додано та 0 видалено
  1. 4 0
      pkg/tsdb/postgres/macros.go
  2. 13 0
      pkg/tsdb/postgres/macros_test.go

+ 4 - 0
pkg/tsdb/postgres/macros.go

@@ -127,6 +127,10 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
 			return "", fmt.Errorf("missing time column argument for macro %v", name)
 			return "", fmt.Errorf("missing time column argument for macro %v", name)
 		}
 		}
 		return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], m.timeRange.GetFromAsTimeUTC().UnixNano(), args[0], m.timeRange.GetToAsTimeUTC().UnixNano()), nil
 		return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], m.timeRange.GetFromAsTimeUTC().UnixNano(), args[0], m.timeRange.GetToAsTimeUTC().UnixNano()), nil
+	case "__unixEpochNanoFrom":
+		return fmt.Sprintf("'%d'", m.timeRange.GetFromAsTimeUTC().UnixNano()), nil
+	case "__unixEpochNanoTo":
+		return fmt.Sprintf("'%d'", m.timeRange.GetToAsTimeUTC().UnixNano()), nil
 	case "__unixEpochGroup":
 	case "__unixEpochGroup":
 		if len(args) < 2 {
 		if len(args) < 2 {
 			return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
 			return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)

+ 13 - 0
pkg/tsdb/postgres/macros_test.go

@@ -121,6 +121,19 @@ func TestMacroEngine(t *testing.T) {
 
 
 				So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()))
 				So(sql, ShouldEqual, fmt.Sprintf("select time >= %d AND time <= %d", from.UnixNano(), to.UnixNano()))
 			})
 			})
+			Convey("interpolate __unixEpochNanoFrom function", func() {
+				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoFrom()")
+				So(err, ShouldBeNil)
+
+				So(sql, ShouldEqual, fmt.Sprintf("select '%d'", from.UnixNano()))
+			})
+
+			Convey("interpolate __unixEpochNanoTo function", func() {
+				sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochNanoTo()")
+				So(err, ShouldBeNil)
+
+				So(sql, ShouldEqual, fmt.Sprintf("select '%d'", to.UnixNano()))
+			})
 
 
 			Convey("interpolate __unixEpochGroup function", func() {
 			Convey("interpolate __unixEpochGroup function", func() {