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

Use bitbucket.org/creachadair/shell to parse Time-Series filter and custom query #46

Merged
merged 1 commit into from
Aug 20, 2020
Merged
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/redistimeseries/grafana-redis-datasource
go 1.14

require (
bitbucket.org/creachadair/shell v0.0.6
github.com/grafana/grafana-plugin-sdk-go v0.75.0
github.com/magefile/mage v1.10.0 // indirect
github.com/mediocregopher/radix/v3 v3.5.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bitbucket.org/creachadair/shell v0.0.6 h1:reJflDbKqnlnqb4Oo2pQ1/BqmY/eCWcNGHrIUO8qIzc=
bitbucket.org/creachadair/shell v0.0.6/go.mod h1:8Qqi/cYk7vPnsOePHroKXDJYmb5x7ENhtiFtfZq8K+M=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
19 changes: 14 additions & 5 deletions pkg/redis-custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"strings"

"bitbucket.org/creachadair/shell"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/data"
Expand All @@ -16,13 +17,21 @@ import (
* Can PANIC if command is wrong
*/
func (ds *redisDatasource) executeCustomQuery(qm queryModel, client *radix.Pool) (interface{}, error) {
// Split query and parse command
query := strings.Fields(qm.Query)
command, params := query[0], query[1:]

var result interface{}
var err error

// Split query
query, ok := shell.Split(qm.Query)

// Check if query is valid
if !ok {
err = fmt.Errorf("Query is not valid")
return result, err
}

// Separate command from params
command, params := query[0], query[1:]

// Handle Panic from custom command to catch "should never get here"
defer func() {
if err := recover(); err != nil {
Expand All @@ -36,7 +45,7 @@ func (ds *redisDatasource) executeCustomQuery(qm queryModel, client *radix.Pool)
return result, err
}

// Extract key or 1st paremeter as required for FlatCmd
// Extract key or 1st parameter as required for FlatCmd
key, params := params[0], params[1:]
err = client.Do(radix.FlatCmd(&result, command, key, params))

Expand Down
18 changes: 15 additions & 3 deletions pkg/redis-time-series.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"fmt"
"strconv"
"strings"
"time"

"bitbucket.org/creachadair/shell"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana-plugin-sdk-go/data"
Expand Down Expand Up @@ -73,7 +73,13 @@ func (ds *redisDatasource) queryTsMRange(from int64, to int64, qm queryModel, cl
var err error

// Split Filter to array
filter := strings.Fields(qm.Filter)
filter, ok := shell.Split(qm.Filter)

// Check if filter is valid
if !ok {
response.Error = fmt.Errorf("Filter is not valid")
return response
}

// Execute command
if qm.Aggregation != "" {
Expand Down Expand Up @@ -283,7 +289,13 @@ func (ds *redisDatasource) queryTsQueryIndex(qm queryModel, client *radix.Pool)
response := backend.DataResponse{}

// Split Filter to array
filter := strings.Fields(qm.Filter)
filter, ok := shell.Split(qm.Filter)

// Check if filter is valid
if !ok {
response.Error = fmt.Errorf("Filter is not valid")
return response
}

// Execute command
var values []string
Expand Down