Skip to content

Commit

Permalink
Variables support (#169)
Browse files Browse the repository at this point in the history
* Fixes minimal supported grafana version
* Upgrade makefile build deps
* Fixes #100
* Moved makefile tests to docker
* Fixed tests
* Build fixed
  • Loading branch information
HadesArchitect authored Oct 5, 2023
1 parent 90c58e6 commit 72a7088
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 40 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
- name: Checkout the repository
uses: actions/checkout@master
- name: yarn install
run: docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:16-alpine yarn install
run: docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:20-alpine yarn install
- name: yarn build
run: docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:16-alpine yarn build
run: docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:20-alpine yarn build
- name: go mod vendor
run: docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend golang:1-alpine go mod vendor
run: docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend golang:1.21.1-alpine go mod vendor
- name: go build linux
run: docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend golang:1-alpine go build -buildvcs=false -o ../dist/cassandra-plugin_linux_amd64 .
run: docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend golang:1.21.1-alpine go build -buildvcs=false -o ../dist/cassandra-plugin_linux_amd64 .
- name: backend tests
run: docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend golang:1-alpine go test -buildvcs=false -v -vet=off ./...
run: docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend golang:1.21.1-alpine go test ./...
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ stop: ## Stops dev environment
docker-compose stop

fe-deps: ## Install frontend dependencies
docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:16-alpine yarn install
docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:20-alpine yarn install

fe-build: ## Build frontend
docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:16-alpine yarn build
docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:20-alpine yarn build

fe-watch: ## Watch frontend
docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:16-alpine yarn watch
docker run --rm -v ${PWD}:/opt/gcds -w /opt/gcds node:20-alpine yarn watch

be-deps: ## Install backend dependencies
docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend golang:1-alpine go mod vendor
docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend golang:1.21.1-alpine go mod vendor

be-build: ## Build backend (Builds linux-amd64 version by deafult. Run with args to adjust target (make be-build OS=windows ARCH=arm64))
docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend -e CGO_ENABLED=0 -e GOOS=$(OS) -e GOARCH=$(ARCH) golang:1-alpine go build -buildvcs=false -o ../dist/cassandra-plugin_$(OS)_$(ARCH) .
docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend -e CGO_ENABLED=0 -e GOOS=$(OS) -e GOARCH=$(ARCH) golang:1.21.1-alpine go build -buildvcs=false -o ../dist/cassandra-plugin_$(OS)_$(ARCH) .

be-test: ## Run backend unit tests
cd backend && go test ./...
docker run --rm -v ${PWD}:/go/src/github.com/ha/gcp -w /go/src/github.com/ha/gcp/backend golang:1.21.1-alpine go test ./...
26 changes: 18 additions & 8 deletions backend/cassandra/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"fmt"
"time"
"strings"

"github.com/gocql/gocql"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)

// Session is a convenience wrapper for the gocql.Session.
Expand Down Expand Up @@ -73,22 +75,27 @@ func (s *Session) ExecRawQuery(ctx context.Context, q *Query) (map[string][]*Tim

// ExecStrictQuery queries cassandra with passed Query parameters
// and returns a slice of time series points.
func (s *Session) ExecStrictQuery(ctx context.Context, q *Query) ([]*TimeSeriesPoint, error) {
statement := buildStatement(q)
iter := s.session.Query(statement, q.ValueID, q.TimeFrom, q.TimeTo).WithContext(ctx).Iter()
func (s *Session) ExecStrictQuery(ctx context.Context, q *Query) (map[string][]*TimeSeriesPoint, error) {
iter := s.session.Query(
buildStatement(q),
strings.Split(q.ValueID, ","),
q.TimeFrom,
q.TimeTo).WithContext(ctx).Iter()

ts := make(map[string][]*TimeSeriesPoint)
var (
ts []*TimeSeriesPoint
id string
value float64
timestamp time.Time
)

for iter.Scan(&timestamp, &value) {
ts = append(ts, &TimeSeriesPoint{
for iter.Scan(&id, &value, &timestamp) {
ts[id] = append(ts[id], &TimeSeriesPoint{
Timestamp: timestamp,
Value: value,
})
}

if err := iter.Close(); err != nil {
return nil, fmt.Errorf("strict query processing: %w", err)
}
Expand Down Expand Up @@ -177,9 +184,10 @@ func buildStatement(q *Query) string {
}

statement := fmt.Sprintf(
"SELECT %s, CAST(%s as double) FROM %s.%s WHERE %s = ? AND %s >= ? AND %s <= ?%s",
q.ColumnTime,
"SELECT %s, CAST(%s as double), %s FROM %s.%s WHERE %s IN ? AND %s >= ? AND %s <= ?%s",
q.ColumnID,
q.ColumnValue,
q.ColumnTime,
q.Keyspace,
q.Table,
q.ColumnID,
Expand All @@ -188,5 +196,7 @@ func buildStatement(q *Query) string {
allowFiltering,
)

backend.Logger.Debug("Built strict statement", "statement", statement)

return statement
}
4 changes: 2 additions & 2 deletions backend/cassandra/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Test_buildStatement(t *testing.T) {
ColumnTime: "Time",
AllowFiltering: false,
},
want: "SELECT Time, CAST(Value as double) FROM Keyspace.Table WHERE ID = ? AND Time >= ? AND Time <= ?",
want: "SELECT ID, CAST(Value as double), Time FROM Keyspace.Table WHERE ID IN ? AND Time >= ? AND Time <= ?",
},
{
name: "with AllowFiltering",
Expand All @@ -34,7 +34,7 @@ func Test_buildStatement(t *testing.T) {
ColumnTime: "Time",
AllowFiltering: true,
},
want: "SELECT Time, CAST(Value as double) FROM Keyspace.Table WHERE ID = ? AND Time >= ? AND Time <= ? ALLOW FILTERING",
want: "SELECT ID, CAST(Value as double), Time FROM Keyspace.Table WHERE ID IN ? AND Time >= ? AND Time <= ? ALLOW FILTERING",
},
}

Expand Down
20 changes: 12 additions & 8 deletions backend/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (

"github.com/HadesArchitect/GrafanaCassandraDatasource/backend/cassandra"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)

type repository interface {
ExecRawQuery(ctx context.Context, q *cassandra.Query) (map[string][]*cassandra.TimeSeriesPoint, error)
ExecStrictQuery(ctx context.Context, q *cassandra.Query) ([]*cassandra.TimeSeriesPoint, error)
ExecStrictQuery(ctx context.Context, q *cassandra.Query) (map[string][]*cassandra.TimeSeriesPoint, error)
GetKeyspaces(ctx context.Context) ([]string, error)
GetTables(keyspace string) ([]string, error)
GetColumns(keyspace, table, needType string) ([]string, error)
Expand All @@ -38,6 +39,7 @@ func (p *Plugin) ExecQuery(ctx context.Context, q *cassandra.Query) (data.Frames
err error
)

backend.Logger.Debug("ExecQuery", "query", q)
switch q.RawQuery {
case true:
dataFrames, err = p.execRawMetricQuery(ctx, q)
Expand Down Expand Up @@ -70,19 +72,21 @@ func (p *Plugin) execRawMetricQuery(ctx context.Context, q *cassandra.Query) (da

// execStrictMetricQuery executes repository ExecStrictQuery method and transforms reposonse to data.Frames.
func (p *Plugin) execStrictMetricQuery(ctx context.Context, q *cassandra.Query) (data.Frames, error) {
tsPoints, err := p.repo.ExecStrictQuery(ctx, q)
tsPointsMap, err := p.repo.ExecStrictQuery(ctx, q)
if err != nil {
return nil, fmt.Errorf("repo.ExecStrictQuery: %w", err)
}

id := q.AliasID
if id == "" {
id = q.ValueID
var frames data.Frames
for id, points := range tsPointsMap {
if q.AliasID != "" && len(tsPointsMap) == 1 {
id = q.AliasID
}
frame := makeDataFrameFromPoints(id, points)
frames = append(frames, frame)
}

frame := makeDataFrameFromPoints(id, tsPoints)

return data.Frames{frame}, nil
return frames, nil
}

// GetKeyspaces fetches and returns Cassandra's list of keyspaces.
Expand Down
18 changes: 10 additions & 8 deletions backend/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type repositoryMock struct {
onExecRawQuery func(ctx context.Context, q *cassandra.Query) (map[string][]*cassandra.TimeSeriesPoint, error)
onExecStrictQuery func(ctx context.Context, q *cassandra.Query) ([]*cassandra.TimeSeriesPoint, error)
onExecStrictQuery func(ctx context.Context, q *cassandra.Query) (map[string][]*cassandra.TimeSeriesPoint, error)
onGetKeyspaces func(ctx context.Context) ([]string, error)
onGetTables func(keyspace string) ([]string, error)
onGetColumns func(keyspace, table, needType string) ([]string, error)
Expand All @@ -22,7 +22,7 @@ func (m *repositoryMock) ExecRawQuery(ctx context.Context, q *cassandra.Query) (
return m.onExecRawQuery(ctx, q)
}

func (m *repositoryMock) ExecStrictQuery(ctx context.Context, q *cassandra.Query) ([]*cassandra.TimeSeriesPoint, error) {
func (m *repositoryMock) ExecStrictQuery(ctx context.Context, q *cassandra.Query) (map[string][]*cassandra.TimeSeriesPoint, error) {
return m.onExecStrictQuery(ctx, q)
}

Expand Down Expand Up @@ -113,12 +113,14 @@ func TestPlugin_ExecQuery(t *testing.T) {
{
name: "Strict Query",
repo: &repositoryMock{
onExecStrictQuery: func(ctx context.Context, q *cassandra.Query) ([]*cassandra.TimeSeriesPoint, error) {
return []*cassandra.TimeSeriesPoint{
{Timestamp: time.Unix(1257894000, 0), Value: 3.141},
{Timestamp: time.Unix(1257894001, 0), Value: 6.283},
{Timestamp: time.Unix(1257894002, 0), Value: 2.718},
{Timestamp: time.Unix(1257894003, 0), Value: 1.618},
onExecStrictQuery: func(ctx context.Context, q *cassandra.Query) (map[string][]*cassandra.TimeSeriesPoint, error) {
return map[string][]*cassandra.TimeSeriesPoint{
"1": {
{Timestamp: time.Unix(1257894000, 0), Value: 3.141},
{Timestamp: time.Unix(1257894001, 0), Value: 6.283},
{Timestamp: time.Unix(1257894002, 0), Value: 2.718},
{Timestamp: time.Unix(1257894003, 0), Value: 1.618},
},
}, nil
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class CassandraDatasource extends DataSourceWithBackend<CassandraQuery, C
datasourceId: target.datasourceId,
queryType: 'query',

target: getTemplateSrv().replace(target.target, options.scopedVars),
target: getTemplateSrv().replace(target.target, options.scopedVars, 'csv'),
refId: target.refId,
hide: target.hide,
rawQuery: target.rawQuery,
Expand All @@ -107,7 +107,7 @@ export class CassandraDatasource extends DataSourceWithBackend<CassandraQuery, C
columnTime: target.columnTime,
columnValue: target.columnValue,
columnId: target.columnId,
valueId: target.valueId,
valueId: getTemplateSrv().replace(target.valueId, options.scopedVars, 'csv'),
alias: target.alias,
};
});
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
]
},
"dependencies": {
"grafanaDependency": ">=7.0.0",
"grafanaDependency": ">=7.4.0",
"plugins": []
}
}

0 comments on commit 72a7088

Please sign in to comment.