Add $__timeGroupAlias to postgres macros

This commit is contained in:
Sven Klemm
2018-08-01 08:48:22 +02:00
parent bd77541e09
commit 42f1892826
3 changed files with 25 additions and 12 deletions

View File

@ -110,6 +110,12 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
}
}
return fmt.Sprintf("floor(extract(epoch from %s)/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil
case "__timeGroupAlias":
tg, err := m.evaluateMacro("__timeGroup", args)
if err == nil {
return tg + " AS \"time\"", err
}
return "", err
case "__unixEpochFilter":
if len(args) == 0 {
return "", fmt.Errorf("missing time column argument for macro %v", name)

View File

@ -50,18 +50,24 @@ func TestMacroEngine(t *testing.T) {
Convey("interpolate __timeGroup function", func() {
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
sql, err := engine.Interpolate(query, timeRange, "$__timeGroup(time_column,'5m')")
So(err, ShouldBeNil)
sql2, err := engine.Interpolate(query, timeRange, "$__timeGroupAlias(time_column,'5m')")
So(err, ShouldBeNil)
So(sql, ShouldEqual, "GROUP BY floor(extract(epoch from time_column)/300)*300")
So(sql, ShouldEqual, "floor(extract(epoch from time_column)/300)*300")
So(sql2, ShouldEqual, sql+" AS \"time\"")
})
Convey("interpolate __timeGroup function with spaces between args", func() {
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
sql, err := engine.Interpolate(query, timeRange, "$__timeGroup(time_column , '5m')")
So(err, ShouldBeNil)
sql2, err := engine.Interpolate(query, timeRange, "$__timeGroupAlias(time_column , '5m')")
So(err, ShouldBeNil)
So(sql, ShouldEqual, "GROUP BY floor(extract(epoch from time_column)/300)*300")
So(sql, ShouldEqual, "floor(extract(epoch from time_column)/300)*300")
So(sql2, ShouldEqual, sql+" AS \"time\"")
})
Convey("interpolate __timeTo function", func() {