Skip to content

Commit

Permalink
chore: switch away from deprecated sqlds types
Browse files Browse the repository at this point in the history
  • Loading branch information
njvrzm committed May 30, 2024
1 parent af4f39f commit 0345881
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions pkg/redshift/fake/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (s *RedshiftFakeDatasource) GetAsyncDB(ctx context.Context, config backend.
return nil, nil
}

func (s *RedshiftFakeDatasource) Macros() sqlds.Macros {
return sqlds.Macros{}
func (s *RedshiftFakeDatasource) Macros() sqlutil.Macros {
return sqlutil.Macros{}
}

func (s *RedshiftFakeDatasource) Regions(ctx context.Context) ([]string, error) {
Expand Down
25 changes: 13 additions & 12 deletions pkg/redshift/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
"time"

"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"github.com/grafana/sqlds/v3"
"github.com/pkg/errors"
)

func macroTimeEpoch(query *sqlds.Query, args []string) (string, error) {
func macroTimeEpoch(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 1 {
return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "expected 1 argument, received %d", len(args))
}

return fmt.Sprintf("extract(epoch from %s) as \"time\"", args[0]), nil
}

func macroTimeFilter(query *sqlds.Query, args []string) (string, error) {
func macroTimeFilter(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 1 {
return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "expected 1 argument, received %d", len(args))
}
Expand All @@ -32,16 +33,16 @@ func macroTimeFilter(query *sqlds.Query, args []string) (string, error) {
return fmt.Sprintf("%s BETWEEN '%s' AND '%s'", column, from, to), nil
}

func macroTimeFrom(query *sqlds.Query, args []string) (string, error) {
func macroTimeFrom(query *sqlutil.Query, args []string) (string, error) {
return fmt.Sprintf("'%s'", query.TimeRange.From.UTC().Format(time.RFC3339)), nil

}

func macroTimeTo(query *sqlds.Query, args []string) (string, error) {
func macroTimeTo(query *sqlutil.Query, args []string) (string, error) {
return fmt.Sprintf("'%s'", query.TimeRange.To.UTC().Format(time.RFC3339)), nil
}

func macroTimeGroup(query *sqlds.Query, args []string) (string, error) {
func macroTimeGroup(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 2 {
return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "macro $__timeGroup needs time column and interval")
}
Expand All @@ -54,19 +55,19 @@ func macroTimeGroup(query *sqlds.Query, args []string) (string, error) {
return fmt.Sprintf("floor(extract(epoch from %s)/%v)*%v AS \"time\"", args[0], interval.Seconds(), interval.Seconds()), nil
}

func macroSchema(query *sqlds.Query, args []string) (string, error) {
func macroSchema(query *sqlutil.Query, args []string) (string, error) {
return query.Schema, nil
}

func macroTable(query *sqlds.Query, args []string) (string, error) {
func macroTable(query *sqlutil.Query, args []string) (string, error) {
return query.Table, nil
}

func macroColumn(query *sqlds.Query, args []string) (string, error) {
func macroColumn(query *sqlutil.Query, args []string) (string, error) {
return query.Column, nil
}

func macroUnixEpochFilter(query *sqlds.Query, args []string) (string, error) {
func macroUnixEpochFilter(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 1 {
return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "expected 1 argument, received %d", len(args))
}
Expand All @@ -80,7 +81,7 @@ func macroUnixEpochFilter(query *sqlds.Query, args []string) (string, error) {
return fmt.Sprintf("%s >= %d AND %s <= %d", column, from, args[0], to), nil
}

func macroUnixEpochGroup(query *sqlds.Query, args []string) (string, error) {
func macroUnixEpochGroup(query *sqlutil.Query, args []string) (string, error) {
if len(args) != 2 {
return "", errors.WithMessagef(sqlds.ErrorBadArgumentCount, "macro $__unixEpochGroup needs time column and interval")
}
Expand All @@ -93,7 +94,7 @@ func macroUnixEpochGroup(query *sqlds.Query, args []string) (string, error) {
return fmt.Sprintf(`floor(%s/%v)*%v AS "time"`, args[0], interval.Seconds(), interval.Seconds()), nil
}

var macros = map[string]sqlds.MacroFunc{
var macros = map[string]sqlutil.MacroFunc{
"timeEpoch": macroTimeEpoch,
"timeFilter": macroTimeFilter,
"timeFrom": macroTimeFrom,
Expand All @@ -106,6 +107,6 @@ var macros = map[string]sqlds.MacroFunc{
"unixEpochGroup": macroUnixEpochGroup,
}

func (s *RedshiftDatasource) Macros() sqlds.Macros {
func (s *RedshiftDatasource) Macros() sqlutil.Macros {
return macros
}
27 changes: 14 additions & 13 deletions pkg/redshift/macros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"github.com/grafana/sqlds/v3"
"github.com/pkg/errors"
)
Expand All @@ -13,23 +14,23 @@ func Test_macros(t *testing.T) {
tests := []struct {
description string
macro string
query *sqlds.Query
query *sqlutil.Query
args []string
expected string
expectedErr error
}{
{
"adds time as Unix",
"timeEpoch",
&sqlds.Query{},
&sqlutil.Query{},
[]string{"starttime"},
`extract(epoch from starttime) as "time"`,
nil,
},
{
"creates time filter",
"timeFilter",
&sqlds.Query{
&sqlutil.Query{
TimeRange: backend.TimeRange{
From: time.Date(2021, 6, 23, 0, 0, 0, 0, &time.Location{}),
To: time.Date(2021, 6, 23, 1, 0, 0, 0, &time.Location{}),
Expand All @@ -42,15 +43,15 @@ func Test_macros(t *testing.T) {
{
"wrong args for time filter",
"timeFilter",
&sqlds.Query{},
&sqlutil.Query{},
[]string{},
"",
sqlds.ErrorBadArgumentCount,
},
{
"creates time from filter",
"timeFrom",
&sqlds.Query{
&sqlutil.Query{
TimeRange: backend.TimeRange{
From: time.Date(2021, 6, 23, 0, 0, 0, 0, &time.Location{}),
To: time.Date(2021, 6, 23, 1, 0, 0, 0, &time.Location{}),
Expand All @@ -63,7 +64,7 @@ func Test_macros(t *testing.T) {
{
"creates time to filter",
"timeTo",
&sqlds.Query{
&sqlutil.Query{
TimeRange: backend.TimeRange{
From: time.Date(2021, 6, 23, 0, 0, 0, 0, &time.Location{}),
To: time.Date(2021, 6, 23, 1, 0, 0, 0, &time.Location{}),
Expand All @@ -76,47 +77,47 @@ func Test_macros(t *testing.T) {
{
"creates time group",
"timeGroup",
&sqlds.Query{},
&sqlutil.Query{},
[]string{"starttime", "'1m'"},
`floor(extract(epoch from starttime)/60)*60 AS "time"`,
nil,
},
{
"wrong args for time group",
"timeGroup",
&sqlds.Query{},
&sqlutil.Query{},
[]string{},
"",
sqlds.ErrorBadArgumentCount,
},
{
"adds a schema",
"schema",
&sqlds.Query{Schema: "foo"},
&sqlutil.Query{Schema: "foo"},
[]string{},
`foo`,
nil,
},
{
"adds a table",
"table",
&sqlds.Query{Table: "foo"},
&sqlutil.Query{Table: "foo"},
[]string{},
`foo`,
nil,
},
{
"adds a column",
"column",
&sqlds.Query{Column: "foo"},
&sqlutil.Query{Column: "foo"},
[]string{},
`foo`,
nil,
},
{
"unix epoch filter",
"unixEpochFilter",
&sqlds.Query{
&sqlutil.Query{
TimeRange: backend.TimeRange{
From: time.Date(2021, 6, 23, 0, 0, 0, 0, &time.Location{}),
To: time.Date(2021, 6, 23, 1, 0, 0, 0, &time.Location{}),
Expand All @@ -129,7 +130,7 @@ func Test_macros(t *testing.T) {
{
"unix epoch time group",
"unixEpochGroup",
&sqlds.Query{},
&sqlutil.Query{},
[]string{"starttime", "1h"},
`floor(starttime/3600)*3600 AS "time"`,
nil,
Expand Down

0 comments on commit 0345881

Please sign in to comment.