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

Refactor RedisTimeSeries and RedisGears commands #202

Merged
merged 2 commits into from
May 18, 2021
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
57 changes: 57 additions & 0 deletions pkg/models/redis-gears.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package models

/**
* RedisGears Commands
*/
const (
GearsPyStats = "rg.pystats"
GearsDumpRegistrations = "rg.dumpregistrations"
GearsPyExecute = "rg.pyexecute"
GearsPyDumpReqs = "rg.pydumpreqs"
)

/**
* RG.PYSTATS Radix marshaling
*/
type PyStats struct {
TotalAllocated int64 `redis:"TotalAllocated"`
PeakAllocated int64 `redis:"PeakAllocated"`
CurrAllocated int64 `redis:"CurrAllocated"`
}

/**
* RG.DUMPREGISTRATIONS Radix marshaling
*/
type DumpRegistrations struct {
ID string `redis:"id"`
Reader string `redis:"reader"`
Desc string `redis:"desc"`
RegistrationData RegistrationData `redis:"RegistrationData"`
PD string `redis:"PD"`
}

/**
* Registration data for RG.DUMPREGISTRATIONS Radix marshaling
*/
type RegistrationData struct {
Mode string `redis:"mode"`
NumTriggered int64 `redis:"numTriggered"`
NumSuccess int64 `redis:"numSuccess"`
NumFailures int64 `redis:"numFailures"`
NumAborted int64 `redis:"numAborted"`
LastError string `redis:"lastError"`
Args map[string]interface{} `redis:"args"`
Status string `redis:"status"`
}

/**
* RG.PYDUMPREQS Radix marshaling
*/
type PyDumpReq struct {
GearReqVersion int64 `redis:"GearReqVersion"`
Name string `redis:"Name"`
IsDownloaded string `redis:"IsDownloaded"`
IsInstalled string `redis:"IsInstalled"`
CompiledOs string `redis:"CompiledOs"`
Wheels []string `redis:"Wheels"`
}
14 changes: 8 additions & 6 deletions pkg/models/redis-graph.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package models

/**
* Commands
* RedisGraph Commands
*/
const GraphConfig = "graph.config"
const GraphExplain = "graph.explain"
const GraphProfile = "graph.profile"
const GraphQuery = "graph.query"
const GraphSlowlog = "graph.slowlog"
const (
GraphConfig = "graph.config"
GraphExplain = "graph.explain"
GraphProfile = "graph.profile"
GraphQuery = "graph.query"
GraphSlowlog = "graph.slowlog"
)

/**
* Represents node
Expand Down
12 changes: 12 additions & 0 deletions pkg/models/redis-time-series.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

/**
* RedisTimeSeries Commands
*/
const (
TimeSeriesGet = "ts.get"
TimeSeriesInfo = "ts.info"
TimeSeriesQueryIndex = "ts.queryindex"
TimeSeriesRange = "ts.range"
TimeSeriesMRange = "ts.mrange"
)
18 changes: 9 additions & 9 deletions pkg/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func query(ctx context.Context, query backend.DataQuery, client redisClient, qm
/**
* Redis Timeseries
*/
case "ts.get":
case models.TimeSeriesGet:
return queryTsGet(qm, client)
case "ts.info":
case models.TimeSeriesInfo:
return queryTsInfo(qm, client)
case "ts.queryindex":
case models.TimeSeriesQueryIndex:
return queryTsQueryIndex(qm, client)
case "ts.range":
case models.TimeSeriesRange:
return queryTsRange(from, to, qm, client)
case "ts.mrange":
case models.TimeSeriesMRange:
return queryTsMRange(from, to, qm, client)

/**
Expand Down Expand Up @@ -107,13 +107,13 @@ func query(ctx context.Context, query backend.DataQuery, client redisClient, qm
/**
* Redis Gears
*/
case "rg.pystats":
case models.GearsPyStats:
return queryRgPystats(qm, client)
case "rg.dumpregistrations":
case models.GearsDumpRegistrations:
return queryRgDumpregistrations(qm, client)
case "rg.pyexecute":
case models.GearsPyExecute:
return queryRgPyexecute(qm, client)
case "rg.pydumpreqs":
case models.GearsPyDumpReqs:
return queryRgPydumpReqs(qm, client)

/**
Expand Down
18 changes: 9 additions & 9 deletions pkg/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func TestQuery(t *testing.T) {
tests := []struct {
qm queryModel
}{
{queryModel{Command: "ts.get"}},
{queryModel{Command: "ts.info"}},
{queryModel{Command: "ts.queryindex"}},
{queryModel{Command: "ts.range"}},
{queryModel{Command: "ts.mrange"}},
{queryModel{Command: models.TimeSeriesGet}},
{queryModel{Command: models.TimeSeriesInfo}},
{queryModel{Command: models.TimeSeriesQueryIndex}},
{queryModel{Command: models.TimeSeriesRange}},
{queryModel{Command: models.TimeSeriesMRange}},
{queryModel{Command: "hgetall"}},
{queryModel{Command: "smembers"}},
{queryModel{Command: "hkeys"}},
Expand All @@ -41,10 +41,10 @@ func TestQuery(t *testing.T) {
{queryModel{Command: "ft.info"}},
{queryModel{Command: "xinfoStream"}},
{queryModel{Command: "tmscan"}},
{queryModel{Command: "rg.pystats"}},
{queryModel{Command: "rg.dumpregistrations"}},
{queryModel{Command: "rg.pyexecute"}},
{queryModel{Command: "rg.pydumpreqs"}},
{queryModel{Command: models.GearsPyStats}},
{queryModel{Command: models.GearsDumpRegistrations}},
{queryModel{Command: models.GearsPyExecute}},
{queryModel{Command: models.GearsPyDumpReqs}},
{queryModel{Command: "xrange"}},
{queryModel{Command: "xrevrange"}},
{queryModel{Command: models.GraphConfig}},
Expand Down
71 changes: 13 additions & 58 deletions pkg/redis-gears.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,9 @@ import (
"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"
"github.com/redisgrafana/grafana-redis-datasource/pkg/models"
)

/**
* RG.PYSTATS Radix marshaling
*/
type pystats struct {
TotalAllocated int64 `redis:"TotalAllocated"`
PeakAllocated int64 `redis:"PeakAllocated"`
CurrAllocated int64 `redis:"CurrAllocated"`
}

/**
* RG.DUMPREGISTRATIONS Radix marshaling
*/
type dumpregistrations struct {
ID string `redis:"id"`
Reader string `redis:"reader"`
Desc string `redis:"desc"`
RegistrationData registrationData `redis:"RegistrationData"`
PD string `redis:"PD"`
}

/**
* Registration data for RG.DUMPREGISTRATIONS Radix marshaling
*/
type registrationData struct {
Mode string `redis:"mode"`
NumTriggered int64 `redis:"numTriggered"`
NumSuccess int64 `redis:"numSuccess"`
NumFailures int64 `redis:"numFailures"`
NumAborted int64 `redis:"numAborted"`
LastError string `redis:"lastError"`
Args map[string]interface{} `redis:"args"`
Status string `redis:"status"`
}

/**
* RG.PYDUMPREQS Radix marshaling
*/
type pydumpreq struct {
GearReqVersion int64 `redis:"GearReqVersion"`
Name string `redis:"Name"`
IsDownloaded string `redis:"IsDownloaded"`
IsInstalled string `redis:"IsInstalled"`
CompiledOs string `redis:"CompiledOs"`
Wheels []string `redis:"Wheels"`
}

/**
* RG.PYSTATS
*
Expand All @@ -67,10 +22,10 @@ func queryRgPystats(qm queryModel, client redisClient) backend.DataResponse {
response := backend.DataResponse{}

// Using radix marshaling of key-value arrays to structs
var stats pystats
var stats models.PyStats

// Run command
err := client.RunCmd(&stats, "RG.PYSTATS")
err := client.RunCmd(&stats, models.GearsPyStats)

// Check error
if err != nil {
Expand Down Expand Up @@ -99,10 +54,10 @@ func queryRgDumpregistrations(qm queryModel, client redisClient) backend.DataRes
response := backend.DataResponse{}

// Using radix marshaling of key-value arrays to structs
var models []dumpregistrations
var registrations []models.DumpRegistrations

// Run command
err := client.RunCmd(&models, "RG.DUMPREGISTRATIONS")
err := client.RunCmd(&registrations, models.GearsDumpRegistrations)

// Check error
if err != nil {
Expand All @@ -129,16 +84,16 @@ func queryRgDumpregistrations(qm queryModel, client redisClient) backend.DataRes
frame.Fields = append(frame.Fields, data.NewField("status", nil, []string{}))

// Registrations
for _, model := range models {
for _, registration := range registrations {
// Merging args to string like "key"="value"\n
args := new(bytes.Buffer)
for key, value := range model.RegistrationData.Args {
for key, value := range registration.RegistrationData.Args {
fmt.Fprintf(args, "\"%s\"=\"%s\"\n", key, value)
}

frame.AppendRow(model.ID, model.Reader, model.Desc, model.PD, model.RegistrationData.Mode,
model.RegistrationData.NumTriggered, model.RegistrationData.NumSuccess, model.RegistrationData.NumFailures,
model.RegistrationData.NumAborted, model.RegistrationData.LastError, args.String(), model.RegistrationData.Status)
frame.AppendRow(registration.ID, registration.Reader, registration.Desc, registration.PD, registration.RegistrationData.Mode,
registration.RegistrationData.NumTriggered, registration.RegistrationData.NumSuccess, registration.RegistrationData.NumFailures,
registration.RegistrationData.NumAborted, registration.RegistrationData.LastError, args.String(), registration.RegistrationData.Status)
}

return response
Expand Down Expand Up @@ -166,7 +121,7 @@ func queryRgPyexecute(qm queryModel, client redisClient) backend.DataResponse {
}

// Run command
err := client.RunFlatCmd(&result, "RG.PYEXECUTE", qm.Key, args...)
err := client.RunFlatCmd(&result, models.GearsPyExecute, qm.Key, args...)

// Check error
if err != nil {
Expand Down Expand Up @@ -227,10 +182,10 @@ func queryRgPydumpReqs(qm queryModel, client redisClient) backend.DataResponse {
response := backend.DataResponse{}

// Using radix marshaling of key-value arrays to structs
var reqs []pydumpreq
var reqs []models.PyDumpReq

// Run command
err := client.RunCmd(&reqs, "RG.PYDUMPREQS")
err := client.RunCmd(&reqs, models.GearsPyDumpReqs)

// Check error
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions pkg/redis-gears_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/mediocregopher/radix/v3"
"github.com/redisgrafana/grafana-redis-datasource/pkg/models"
"github.com/stretchr/testify/require"
)

Expand All @@ -20,7 +21,7 @@ func TestRgPystatsIntegration(t *testing.T) {
client := radixV3Impl{radixClient: radixClient}

// Response
resp := queryRgPystats(queryModel{Command: "rg.pystats"}, &client)
resp := queryRgPystats(queryModel{Command: models.GearsPyStats}, &client)
require.Len(t, resp.Frames, 1)
require.Len(t, resp.Frames[0].Fields, 3)
require.IsType(t, int64(0), resp.Frames[0].Fields[0].At(0))
Expand All @@ -40,7 +41,7 @@ func TestRgDumpregistrationsIntegration(t *testing.T) {
client := radixV3Impl{radixClient: radixClient}

// Response
resp := queryRgDumpregistrations(queryModel{Command: "rg.dumpregistrations"}, &client)
resp := queryRgDumpregistrations(queryModel{Command: models.GearsDumpRegistrations}, &client)
require.Len(t, resp.Frames[0].Fields, 12)
require.Equal(t, "id", resp.Frames[0].Fields[0].Name)
require.Equal(t, "reader", resp.Frames[0].Fields[1].Name)
Expand Down Expand Up @@ -74,7 +75,7 @@ func TestRgPyexecuteIntegration(t *testing.T) {

// Results
t.Run("Test command with full response", func(t *testing.T) {
resp := queryRgPyexecute(queryModel{Command: "rg.pyexecute", Key: "GB().run()"}, &client)
resp := queryRgPyexecute(queryModel{Command: models.GearsPyExecute, Key: "GB().run()"}, &client)
require.Len(t, resp.Frames, 2)
require.Len(t, resp.Frames[0].Fields, 1)
require.Equal(t, "results", resp.Frames[0].Name)
Expand All @@ -89,7 +90,7 @@ func TestRgPyexecuteIntegration(t *testing.T) {

// UNBLOCKING and REQUIREMENTS
t.Run("Test command with UNBLOCKING and REQUIREMENTS", func(t *testing.T) {
resp := queryRgPyexecute(queryModel{Command: "rg.pyexecute", Key: "GearsBuilder(reader=\"KeysReader\").run()", Unblocking: true, Requirements: "numpy"}, &client)
resp := queryRgPyexecute(queryModel{Command: models.GearsPyExecute, Key: "GearsBuilder(reader=\"KeysReader\").run()", Unblocking: true, Requirements: "numpy"}, &client)
require.Len(t, resp.Frames, 1)
require.Len(t, resp.Frames[0].Fields, 1)
require.Equal(t, "operationId", resp.Frames[0].Name)
Expand All @@ -100,7 +101,7 @@ func TestRgPyexecuteIntegration(t *testing.T) {

// OK
t.Run("Test command with full OK string", func(t *testing.T) {
resp := queryRgPyexecute(queryModel{Command: "rg.pyexecute", Key: "GB('CommandReader')"}, &client)
resp := queryRgPyexecute(queryModel{Command: models.GearsPyExecute, Key: "GB('CommandReader')"}, &client)
require.Len(t, resp.Frames, 2)
require.Len(t, resp.Frames[0].Fields, 1)
require.Equal(t, "results", resp.Frames[0].Name)
Expand All @@ -115,7 +116,7 @@ func TestRgPyexecuteIntegration(t *testing.T) {

// Error
t.Run("Test command with error", func(t *testing.T) {
resp := queryRgPyexecute(queryModel{Command: "rg.pyexecute", Key: "some key"}, &client)
resp := queryRgPyexecute(queryModel{Command: models.GearsPyExecute, Key: "some key"}, &client)
require.Len(t, resp.Frames, 0)
require.Error(t, resp.Error)
})
Expand All @@ -130,7 +131,7 @@ func TestRgDumpReqsIntegration(t *testing.T) {
client := radixV3Impl{radixClient: radixClient}

// Response
resp := queryRgPydumpReqs(queryModel{Command: "rg.pydumpreqs"}, &client)
resp := queryRgPydumpReqs(queryModel{Command: models.GearsPyDumpReqs}, &client)

require.Len(t, resp.Frames[0].Fields, 6)
require.Equal(t, "GearReqVersion", resp.Frames[0].Fields[0].Name)
Expand Down
Loading