Skip to content

Commit

Permalink
SQL: Add default interval and interval_ms macros to sqlutil (#982)
Browse files Browse the repository at this point in the history
* Add default interval and interval_ms macros to sqlutil

* fix lint
  • Loading branch information
kevinwcyu authored May 15, 2024
1 parent cecfe39 commit 36b8fe3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
33 changes: 27 additions & 6 deletions data/sqlutil/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"

"golang.org/x/exp/maps"
)

Expand All @@ -22,6 +25,22 @@ type MacroFunc func(*Query, []string) (string, error)
// Macros is a map of macro name to MacroFunc. The name must be regex friendly.
type Macros map[string]MacroFunc

// Default macro to return interval
// Example:
//
// $__interval => "10m"
func macroInterval(query *Query, _ []string) (string, error) {
return gtime.FormatInterval(query.Interval), nil
}

// Default macro to return interval in ms
// Example if $__interval is "10m":
//
// $__interval_ms => "600000"
func macroIntervalMS(query *Query, _ []string) (string, error) {
return strconv.FormatInt(query.Interval.Milliseconds(), 10), nil
}

// Default time filter for SQL based on the query time range.
// It requires one argument, the time column to filter.
// Example:
Expand Down Expand Up @@ -116,12 +135,14 @@ func macroColumn(query *Query, _ []string) (string, error) {
}

var DefaultMacros = Macros{
"timeFilter": macroTimeFilter,
"timeFrom": macroTimeFrom,
"timeGroup": macroTimeGroup,
"timeTo": macroTimeTo,
"table": macroTable,
"column": macroColumn,
"interval": macroInterval,
"interval_ms": macroIntervalMS,
"timeFilter": macroTimeFilter,
"timeFrom": macroTimeFrom,
"timeGroup": macroTimeGroup,
"timeTo": macroTimeTo,
"table": macroTable,
"column": macroColumn,
}

type macroMatch struct {
Expand Down
18 changes: 15 additions & 3 deletions data/sqlutil/macros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -110,6 +111,16 @@ func TestInterpolate(t *testing.T) {
input: "select * from $__params(world) AND $__foo() AND $__params(hello)",
output: "select * from bar_world AND baz AND bar_hello",
},
{
name: "default interval",
input: "select * from foo group by bin(time, $__interval)",
output: "select * from foo group by bin(time, 10m)",
},
{
name: "default interval_ms",
input: "select count(*) / $__interval_ms * 1000 as qps from foo",
output: "select count(*) / 600000 * 1000 as qps from foo",
},
{
name: "default timeFilter",
input: "select * from foo where $__timeFilter(time)",
Expand Down Expand Up @@ -199,9 +210,10 @@ func TestInterpolate(t *testing.T) {
for i, tc := range tests {
t.Run(fmt.Sprintf("[%d/%d] %s", i+1, len(tests), tc.name), func(t *testing.T) {
query := &Query{
RawSQL: tc.input,
Table: tableName,
Column: tableColumn,
RawSQL: tc.input,
Table: tableName,
Column: tableColumn,
Interval: time.Duration(10) * time.Minute,
}
interpolatedQuery, err := Interpolate(query, macros)
assert.Equal(t, err != nil, tc.wantErr, "wantErr != gotErr")
Expand Down

0 comments on commit 36b8fe3

Please sign in to comment.