Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add QueryTag and query comments information #102

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 2.0.0 (not released yet)

### ⭐ Added
- Add a query tag that includes relevant Grafana context information.


## 1.9.1

### 🔨 Changed
Expand Down
4 changes: 3 additions & 1 deletion pkg/check_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"database/sql"
"fmt"
"github.com/michelin/snowflake-grafana-datasource/pkg/data"
"github.com/michelin/snowflake-grafana-datasource/pkg/utils"

"github.com/grafana/grafana-plugin-sdk-go/backend"
_ "github.com/snowflakedb/gosnowflake"
Expand Down Expand Up @@ -32,7 +34,7 @@ func (td *SnowflakeDatasource) CheckHealth(ctx context.Context, req *backend.Che
}
defer td.db.Close()

row, err := td.db.QueryContext(ctx, "SELECT 1")
row, err := td.db.QueryContext(utils.AddQueryTagInfos(ctx, &data.QueryConfigStruct{}), "SELECT 1")
if err != nil {
return &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Expand Down
51 changes: 51 additions & 0 deletions pkg/data/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package data

import (
"database/sql"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"time"
)

type QueryResult struct {
Tables []Table
}

// DataTable structure containing columns and rows
type Table struct {
Columns []*sql.ColumnType
Rows [][]interface{}
}

const TimeSeriesType = "time series"

func (qc *QueryConfigStruct) IsTimeSeriesType() bool {
return qc.QueryType == TimeSeriesType
}

type QueryConfigStruct struct {
FinalQuery string
QueryType string
RawQuery string
TimeColumns []string
TimeRange backend.TimeRange
Interval time.Duration
MaxDataPoints int64
FillMode string
FillValue float64
}

type QueryTagStruct struct {
PluginVersion string `json:"pluginVersion,omitempty"`
QueryType string `json:"queryType,omitempty"`
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
Grafana QueryTagGrafanaStruct `json:"grafana,omitempty"`
}

type QueryTagGrafanaStruct struct {
Version string `json:"version,omitempty"`
Host string `json:"host,omitempty"`
OrgId int64 `json:"orgId,omitempty"`
User string `json:"user,omitempty"`
DatasourceId string `json:"datasourceId,omitempty"`
}
12 changes: 7 additions & 5 deletions pkg/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"github.com/michelin/snowflake-grafana-datasource/pkg/data"
"github.com/michelin/snowflake-grafana-datasource/pkg/utils"
"math"
"regexp"
"strconv"
Expand Down Expand Up @@ -30,7 +32,7 @@ func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]str
return result + str[lastIndex:]
}

func Interpolate(configStruct *queryConfigStruct) (string, error) {
func Interpolate(configStruct *data.QueryConfigStruct) (string, error) {
rExp, _ := regexp.Compile(sExpr)
var macroError error

Expand Down Expand Up @@ -58,7 +60,7 @@ func Interpolate(configStruct *queryConfigStruct) (string, error) {
return sql, nil
}

func SetupFillmode(configStruct *queryConfigStruct, fillmode string) error {
func SetupFillmode(configStruct *data.QueryConfigStruct, fillmode string) error {
switch fillmode {
case "NULL":
configStruct.FillMode = NullFill
Expand All @@ -77,7 +79,7 @@ func SetupFillmode(configStruct *queryConfigStruct, fillmode string) error {
}

// evaluateMacro convert macro expression to sql expression
func evaluateMacro(name string, args []string, configStruct *queryConfigStruct) (string, error) {
func evaluateMacro(name string, args []string, configStruct *data.QueryConfigStruct) (string, error) {
timeRange := configStruct.TimeRange

switch name {
Expand Down Expand Up @@ -146,7 +148,7 @@ func evaluateMacro(name string, args []string, configStruct *queryConfigStruct)
if len(args) < 2 {
return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
}
interval, err := ParseInterval(strings.Trim(args[1], `'`))
interval, err := utils.ParseInterval(strings.Trim(args[1], `'`))
if err != nil {
return "", fmt.Errorf("error parsing interval %v", args[1])
}
Expand Down Expand Up @@ -182,7 +184,7 @@ func evaluateMacro(name string, args []string, configStruct *queryConfigStruct)
if len(args) < 2 {
return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
}
interval, err := ParseInterval(strings.Trim(args[1], `'`))
interval, err := utils.ParseInterval(strings.Trim(args[1], `'`))
if err != nil {
return "", fmt.Errorf("error parsing interval %v", args[1])
}
Expand Down
35 changes: 18 additions & 17 deletions pkg/macros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/michelin/snowflake-grafana-datasource/pkg/data"
"testing"
"time"

Expand All @@ -16,14 +17,14 @@ func TestEvaluateMacro(t *testing.T) {
To: time.Now().Add(time.Minute),
}

configStruct := queryConfigStruct{
configStruct := data.QueryConfigStruct{
TimeRange: timeRange,
}

tcs := []struct {
args []string
name string
config queryConfigStruct
config data.QueryConfigStruct
response string
err string
fillMode string
Expand Down Expand Up @@ -128,13 +129,13 @@ func TestEvaluateMacro(t *testing.T) {
func TestInterpolate(t *testing.T) {
tests := []struct {
name string
configStruct *queryConfigStruct
configStruct *data.QueryConfigStruct
expectedSQL string
expectedError string
}{
{
name: "valid macro",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeFilter(col)",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
Expand All @@ -146,31 +147,31 @@ func TestInterpolate(t *testing.T) {
},
{
name: "missing time column argument",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeFilter()",
},
expectedSQL: "",
expectedError: "missing time column argument for macro __timeFilter",
},
{
name: "valid snowflake system macro",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT SYSTEM$TYPEOF('a')",
},
expectedSQL: "SELECT SYSTEM$TYPEOF('a')",
expectedError: "",
},
{
name: "unknown macro",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__unknownMacro(col)",
},
expectedSQL: "",
expectedError: "unknown macro \"__unknownMacro\"",
},
{
name: "check __timeRoundTo with default 15min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundTo()",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 7, 0, 0, time.UTC),
Expand All @@ -182,7 +183,7 @@ func TestInterpolate(t *testing.T) {
},
{
name: "check __timeRoundFrom with default 15min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundFrom()",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 7, 0, 0, time.UTC),
Expand All @@ -194,7 +195,7 @@ func TestInterpolate(t *testing.T) {
},
{
name: "check __timeRoundTo with 5min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundTo(5)",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 7, 0, 0, time.UTC),
Expand All @@ -206,7 +207,7 @@ func TestInterpolate(t *testing.T) {
},
{
name: "check __timeRoundFrom with 5min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundFrom(5)",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 7, 0, 0, time.UTC),
Expand All @@ -218,7 +219,7 @@ func TestInterpolate(t *testing.T) {
},
{
name: "check __timeRoundTo with 10min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundTo(10)",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 7, 0, 0, time.UTC),
Expand All @@ -230,7 +231,7 @@ func TestInterpolate(t *testing.T) {
},
{
name: "check __timeRoundFrom with 10min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundFrom(10)",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 7, 0, 0, time.UTC),
Expand All @@ -242,7 +243,7 @@ func TestInterpolate(t *testing.T) {
},
{
name: "check __timeRoundTo with 30min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundTo(30)",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 7, 0, 0, time.UTC),
Expand All @@ -254,7 +255,7 @@ func TestInterpolate(t *testing.T) {
},
{
name: "check __timeRoundFrom with 30min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundFrom(30)",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 59, 0, 0, time.UTC),
Expand All @@ -266,7 +267,7 @@ func TestInterpolate(t *testing.T) {
},
{
name: "check __timeRoundTo with 1440min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundTo(1440)",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 7, 0, 0, time.UTC),
Expand All @@ -278,7 +279,7 @@ func TestInterpolate(t *testing.T) {
},
{
name: "check __timeRoundFrom with 1440min",
configStruct: &queryConfigStruct{
configStruct: &data.QueryConfigStruct{
RawQuery: "SELECT * FROM table WHERE $__timeRoundFrom(1440)",
TimeRange: backend.TimeRange{
From: time.Date(2020, 1, 1, 0, 59, 0, 0, time.UTC),
Expand Down
Loading
Loading