@@ -4,9 +4,12 @@ import (
44 "errors"
55 "fmt"
66 "regexp"
7+ "strconv"
78 "strings"
89 "time"
910
11+ "github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
12+
1013 "golang.org/x/exp/maps"
1114)
1215
@@ -22,6 +25,22 @@ type MacroFunc func(*Query, []string) (string, error)
2225// Macros is a map of macro name to MacroFunc. The name must be regex friendly.
2326type Macros map [string ]MacroFunc
2427
28+ // Default macro to return interval
29+ // Example:
30+ //
31+ // $__interval => "10m"
32+ func macroInterval (query * Query , _ []string ) (string , error ) {
33+ return gtime .FormatInterval (query .Interval ), nil
34+ }
35+
36+ // Default macro to return interval in ms
37+ // Example if $__interval is "10m":
38+ //
39+ // $__interval_ms => "600000"
40+ func macroIntervalMS (query * Query , _ []string ) (string , error ) {
41+ return strconv .FormatInt (query .Interval .Milliseconds (), 10 ), nil
42+ }
43+
2544// Default time filter for SQL based on the query time range.
2645// It requires one argument, the time column to filter.
2746// Example:
@@ -116,12 +135,14 @@ func macroColumn(query *Query, _ []string) (string, error) {
116135}
117136
118137var DefaultMacros = Macros {
119- "timeFilter" : macroTimeFilter ,
120- "timeFrom" : macroTimeFrom ,
121- "timeGroup" : macroTimeGroup ,
122- "timeTo" : macroTimeTo ,
123- "table" : macroTable ,
124- "column" : macroColumn ,
138+ "interval" : macroInterval ,
139+ "interval_ms" : macroIntervalMS ,
140+ "timeFilter" : macroTimeFilter ,
141+ "timeFrom" : macroTimeFrom ,
142+ "timeGroup" : macroTimeGroup ,
143+ "timeTo" : macroTimeTo ,
144+ "table" : macroTable ,
145+ "column" : macroColumn ,
125146}
126147
127148type macroMatch struct {
0 commit comments