Skip to content

Commit

Permalink
doc: improve documentation and fix sonar issue
Browse files Browse the repository at this point in the history
  • Loading branch information
devnied committed Nov 22, 2024
1 parent 7f933cf commit e5b0ade
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ With the Snowflake plugin, you can visualize your Snowflake data in Grafana and
#### Install the Data Source

1. Install the plugin into the grafana plugin folder:

**With grafana-cli**
```shell
grafana-cli --pluginUrl https://github.com/michelin/snowflake-grafana-datasource/releases/latest/download/snowflake-grafana-datasource.zip plugins install michelin-snowflake-datasource
grafana cli --pluginUrl https://github.com/michelin/snowflake-grafana-datasource/releases/latest/download/snowflake-grafana-datasource.zip plugins install michelin-snowflake-datasource
```
or
`--pluginsDir` option can be used to specify a custom plugin directory

**Manually**
```shell
cd /var/lib/grafana/plugins/
wget https://github.com/michelin/snowflake-grafana-datasource/releases/latest/download/snowflake-grafana-datasource.zip
unzip snowflake-grafana-datasource.zip
```

2. Edit the grafana configuration file to allow unsigned plugins:
2. Edit the grafana configuration file `grafana.ini` to allow unsigned plugins:
* Linux:/etc/grafana/grafana.ini
* macOS:/usr/local/etc/grafana/grafana.ini
```shell
Expand Down Expand Up @@ -187,6 +191,12 @@ More info about snowflake-side caching: https://docs.snowflake.com/en/user-guide

The snowflake datasource is a data source backend plugin composed of both frontend and backend components.

To build the project you must have the following tools installed:
* Go
* Node
* yarn


### Frontend

1. Install dependencies
Expand Down
14 changes: 8 additions & 6 deletions pkg/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

const rsIdentifier = `([_a-zA-Z0-9]+)`
const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)`
const missingColumnMessage = "missing time column argument for macro %v"

func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string {
result := ""
Expand Down Expand Up @@ -78,20 +79,21 @@ func SetupFillmode(configStruct *queryConfigStruct, fillmode string) error {
// evaluateMacro convert macro expression to sql expression
func evaluateMacro(name string, args []string, configStruct *queryConfigStruct) (string, error) {
timeRange := configStruct.TimeRange

switch name {
case "__time":
if len(args) == 0 || args[0] == "" {
return "", fmt.Errorf("missing time column argument for macro %v", name)
return "", fmt.Errorf(missingColumnMessage, name)
}
return fmt.Sprintf("TRY_TO_TIMESTAMP_NTZ(%s) AS time", args[0]), nil
case "__timeEpoch":
if len(args) == 0 || args[0] == "" {
return "", fmt.Errorf("missing time column argument for macro %v", name)
return "", fmt.Errorf(missingColumnMessage, name)
}
return fmt.Sprintf("extract(epoch from %s) as time", args[0]), nil
case "__timeFilter":
if len(args) == 0 || args[0] == "" {
return "", fmt.Errorf("missing time column argument for macro %v", name)
return "", fmt.Errorf(missingColumnMessage, name)
}
column := args[0]
timezone := "'UTC'"
Expand All @@ -101,7 +103,7 @@ func evaluateMacro(name string, args []string, configStruct *queryConfigStruct)
return fmt.Sprintf("%s > CONVERT_TIMEZONE('UTC', %s, '%s'::timestamp_ntz) AND %s < CONVERT_TIMEZONE('UTC', %s, '%s'::timestamp_ntz)", column, timezone, timeRange.From.UTC().Format(time.RFC3339Nano), column, timezone, timeRange.To.UTC().Format(time.RFC3339Nano)), nil
case "__timeTzFilter":
if len(args) == 0 || args[0] == "" {
return "", fmt.Errorf("missing time column argument for macro %v", name)
return "", fmt.Errorf(missingColumnMessage, name)
}
column := args[0]
return fmt.Sprintf("%s > '%s'::timestamp_tz AND %s < '%s'::timestamp_tz", column, timeRange.From.UTC().Format(time.RFC3339Nano), column, timeRange.To.UTC().Format(time.RFC3339Nano)), nil
Expand Down Expand Up @@ -164,12 +166,12 @@ func evaluateMacro(name string, args []string, configStruct *queryConfigStruct)
return "", err
case "__unixEpochFilter":
if len(args) == 0 || args[0] == "" {
return "", fmt.Errorf("missing time column argument for macro %v", name)
return "", fmt.Errorf(missingColumnMessage, name)
}
return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], timeRange.From.UTC().Unix(), args[0], timeRange.To.UTC().Unix()), nil
case "__unixEpochNanoFilter":
if len(args) == 0 || args[0] == "" {
return "", fmt.Errorf("missing time column argument for macro %v", name)
return "", fmt.Errorf(missingColumnMessage, name)
}
return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], timeRange.From.UTC().UnixNano(), args[0], timeRange.To.UTC().UnixNano()), nil
case "__unixEpochNanoFrom":
Expand Down

0 comments on commit e5b0ade

Please sign in to comment.