Merge pull request #12891 from svenklemm/unixepochgroup
add $__unixEpochGroup and $__unixEpochGroupAlias to sql datasources
This commit is contained in:
@@ -140,6 +140,27 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
|
||||
return fmt.Sprintf("%d", m.timeRange.GetFromAsSecondsEpoch()), nil
|
||||
case "__unixEpochTo":
|
||||
return fmt.Sprintf("%d", m.timeRange.GetToAsSecondsEpoch()), nil
|
||||
case "__unixEpochGroup":
|
||||
if len(args) < 2 {
|
||||
return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
|
||||
}
|
||||
interval, err := time.ParseDuration(strings.Trim(args[1], `'`))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error parsing interval %v", args[1])
|
||||
}
|
||||
if len(args) == 3 {
|
||||
err := tsdb.SetupFillmode(m.query, interval, args[2])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("floor(%s/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil
|
||||
case "__unixEpochGroupAlias":
|
||||
tg, err := m.evaluateMacro("__unixEpochGroup", args)
|
||||
if err == nil {
|
||||
return tg + " AS \"time\"", err
|
||||
}
|
||||
return "", err
|
||||
default:
|
||||
return "", fmt.Errorf("Unknown macro %v", name)
|
||||
}
|
||||
|
||||
@@ -129,6 +129,18 @@ func TestMacroEngine(t *testing.T) {
|
||||
|
||||
So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix()))
|
||||
})
|
||||
|
||||
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\"")
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
|
||||
|
||||
Reference in New Issue
Block a user