mirror of
https://github.com/grafana/grafana.git
synced 2025-09-23 18:13:47 +08:00
Add $__unixEpochGroup macro to postgres datasource
This commit is contained in:
@ -68,6 +68,8 @@ Macro example | Description
|
|||||||
*$__unixEpochFilter(dateColumn)* | Will be replaced by a time range filter using the specified column name with times represented as unix timestamp. For example, *dateColumn >= 1494410783 AND dateColumn <= 1494497183*
|
*$__unixEpochFilter(dateColumn)* | Will be replaced by a time range filter using the specified column name with times represented as unix timestamp. For example, *dateColumn >= 1494410783 AND dateColumn <= 1494497183*
|
||||||
*$__unixEpochFrom()* | Will be replaced by the start of the currently active time selection as unix timestamp. For example, *1494410783*
|
*$__unixEpochFrom()* | Will be replaced by the start of the currently active time selection as unix timestamp. For example, *1494410783*
|
||||||
*$__unixEpochTo()* | Will be replaced by the end of the currently active time selection as unix timestamp. For example, *1494497183*
|
*$__unixEpochTo()* | Will be replaced by the end of the currently active time selection as unix timestamp. For example, *1494497183*
|
||||||
|
*$__unixEpochGroup(dateColumn,'5m', [fillmode])* | Same as $__timeGroup but for times stored as unix timestamp (only available in Grafana 5.3+).
|
||||||
|
*$__unixEpochGroupAlias(dateColumn,'5m', [fillmode])* | Same as above but also adds a column alias (only available in Grafana 5.3+).
|
||||||
|
|
||||||
We plan to add many more macros. If you have suggestions for what macros you would like to see, please [open an issue](https://github.com/grafana/grafana) in our GitHub repo.
|
We plan to add many more macros. If you have suggestions for what macros you would like to see, please [open an issue](https://github.com/grafana/grafana) in our GitHub repo.
|
||||||
|
|
||||||
|
@ -134,6 +134,27 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
|
|||||||
return fmt.Sprintf("%d", m.timeRange.GetFromAsSecondsEpoch()), nil
|
return fmt.Sprintf("%d", m.timeRange.GetFromAsSecondsEpoch()), nil
|
||||||
case "__unixEpochTo":
|
case "__unixEpochTo":
|
||||||
return fmt.Sprintf("%d", m.timeRange.GetToAsSecondsEpoch()), nil
|
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:
|
default:
|
||||||
return "", fmt.Errorf("Unknown macro %v", name)
|
return "", fmt.Errorf("Unknown macro %v", name)
|
||||||
}
|
}
|
||||||
|
@ -110,6 +110,18 @@ func TestMacroEngine(t *testing.T) {
|
|||||||
|
|
||||||
So(sql, ShouldEqual, fmt.Sprintf("select %d", to.Unix()))
|
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() {
|
Convey("Given a time range between 1960-02-01 07:00 and 1965-02-03 08:00", func() {
|
||||||
|
@ -57,6 +57,8 @@ Macros:
|
|||||||
by setting fillvalue grafana will fill in missing values according to the interval
|
by setting fillvalue grafana will fill in missing values according to the interval
|
||||||
fillvalue can be either a literal value, NULL or previous; previous will fill in the previous seen value or NULL if none has been seen yet
|
fillvalue can be either a literal value, NULL or previous; previous will fill in the previous seen value or NULL if none has been seen yet
|
||||||
- $__timeGroupAlias(column,'5m') -> (extract(epoch from column)/300)::bigint*300 AS "time"
|
- $__timeGroupAlias(column,'5m') -> (extract(epoch from column)/300)::bigint*300 AS "time"
|
||||||
|
- $__unixEpochGroup(column,'5m') -> floor(column/300)*300
|
||||||
|
- $__unixEpochGroupAlias(column,'5m') -> floor(column/300)*300 AS "time"
|
||||||
|
|
||||||
Example of group by and order by with $__timeGroup:
|
Example of group by and order by with $__timeGroup:
|
||||||
SELECT
|
SELECT
|
||||||
|
Reference in New Issue
Block a user