Skip to content

Commit 36b8fe3

Browse files
authored
SQL: Add default interval and interval_ms macros to sqlutil (#982)
* Add default interval and interval_ms macros to sqlutil * fix lint
1 parent cecfe39 commit 36b8fe3

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

data/sqlutil/macros.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
2326
type 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

118137
var 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

127148
type macroMatch struct {

data/sqlutil/macros_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66
"testing"
7+
"time"
78

89
"github.com/stretchr/testify/assert"
910
)
@@ -110,6 +111,16 @@ func TestInterpolate(t *testing.T) {
110111
input: "select * from $__params(world) AND $__foo() AND $__params(hello)",
111112
output: "select * from bar_world AND baz AND bar_hello",
112113
},
114+
{
115+
name: "default interval",
116+
input: "select * from foo group by bin(time, $__interval)",
117+
output: "select * from foo group by bin(time, 10m)",
118+
},
119+
{
120+
name: "default interval_ms",
121+
input: "select count(*) / $__interval_ms * 1000 as qps from foo",
122+
output: "select count(*) / 600000 * 1000 as qps from foo",
123+
},
113124
{
114125
name: "default timeFilter",
115126
input: "select * from foo where $__timeFilter(time)",
@@ -199,9 +210,10 @@ func TestInterpolate(t *testing.T) {
199210
for i, tc := range tests {
200211
t.Run(fmt.Sprintf("[%d/%d] %s", i+1, len(tests), tc.name), func(t *testing.T) {
201212
query := &Query{
202-
RawSQL: tc.input,
203-
Table: tableName,
204-
Column: tableColumn,
213+
RawSQL: tc.input,
214+
Table: tableName,
215+
Column: tableColumn,
216+
Interval: time.Duration(10) * time.Minute,
205217
}
206218
interpolatedQuery, err := Interpolate(query, macros)
207219
assert.Equal(t, err != nil, tc.wantErr, "wantErr != gotErr")

0 commit comments

Comments
 (0)