Skip to content

Commit 17f5f2f

Browse files
authored
Refactor Redis commands (#210)
* Refactor Redis commands * Update dashboard for Grafana 8
1 parent 40667c1 commit 17f5f2f

27 files changed

+765
-373
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
[![Grafana 7](https://img.shields.io/badge/Grafana-7-orange)](https://www.grafana.com)
66
[![Redis Data Source](https://img.shields.io/badge/dynamic/json?color=blue&label=Redis%20Data%20Source&query=%24.version&url=https%3A%2F%2Fgrafana.com%2Fapi%2Fplugins%2Fredis-datasource)](https://grafana.com/grafana/plugins/redis-datasource)
7-
[![Redis Application plug-in](https://img.shields.io/badge/dynamic/json?color=blue&label=Redis%20Application%20plug-in&query=%24.version&url=https%3A%2F%2Fgrafana.com%2Fapi%2Fplugins%2Fredis-app)](https://grafana.com/grafana/plugins/redis-app)
8-
[![Redis Explorer plug-in](https://img.shields.io/badge/dynamic/json?color=blue&label=Redis%20Explorer%20plug-in&query=%24.version&url=https%3A%2F%2Fgrafana.com%2Fapi%2Fplugins%2Fredis-explorer-app)](https://grafana.com/grafana/plugins/redis-explorer-app)
7+
[![Redis Application plugin](https://img.shields.io/badge/dynamic/json?color=blue&label=Redis%20Application%20plugin&query=%24.version&url=https%3A%2F%2Fgrafana.com%2Fapi%2Fplugins%2Fredis-app)](https://grafana.com/grafana/plugins/redis-app)
8+
[![Redis Explorer plugin](https://img.shields.io/badge/dynamic/json?color=blue&label=Redis%20Explorer%20plugin&query=%24.version&url=https%3A%2F%2Fgrafana.com%2Fapi%2Fplugins%2Fredis-explorer-app)](https://grafana.com/grafana/plugins/redis-explorer-app)
99
[![Go Report Card](https://goreportcard.com/badge/github.com/RedisGrafana/grafana-redis-datasource)](https://goreportcard.com/report/github.com/RedisGrafana/grafana-redis-datasource)
1010
![CI](https://github.com/RedisGrafana/grafana-redis-datasource/workflows/CI/badge.svg)
1111
[![codecov](https://codecov.io/gh/RedisGrafana/grafana-redis-datasource/branch/master/graph/badge.svg?token=YX7995RPCF)](https://codecov.io/gh/RedisGrafana/grafana-redis-datasource)
@@ -23,19 +23,19 @@
2323

2424
## Introduction
2525

26-
The Redis Data Source for Grafana is a plug-in that allows users to connect to any Redis database On-Premises or in the Cloud. It provides out-of-the-box predefined dashboards and lets you build customized dashboards to monitor Redis and application data.
26+
The Redis Data Source for Grafana is a plugin that allows users to connect to any Redis database On-Premises or in the Cloud. It provides out-of-the-box predefined dashboards and lets you build customized dashboards to monitor Redis and application data.
2727

2828
### Requirements
2929

30-
Only **Grafana 7.1+** with a new Backend plug-in platform supports Redis plug-ins.
30+
Only **Grafana 7.1+** with a new Backend plugin platform supports Redis plugins.
3131

32-
### Redis Application plug-in
32+
### Redis Application plugin
3333

34-
You can add as many data sources as you want to support multiple Redis databases. [Redis Application plug-in](https://grafana.com/grafana/plugins/redis-app) helps manage various Redis Data Sources and provides Custom panels.
34+
You can add as many data sources as you want to support multiple Redis databases. [Redis Application plugin](https://grafana.com/grafana/plugins/redis-app) helps manage various Redis Data Sources and provides Custom panels.
3535

36-
### Redis Explorer plug-in
36+
### Redis Explorer plugin
3737

38-
[The Redis Explorer plug-in](https://grafana.com/grafana/plugins/redis-explorer-app) connects to Redis Enterprise software clusters using REST API. It provides application pages to add Redis Data Sources for managed databases and dashboards to see cluster configuration.
38+
[The Redis Explorer plugin](https://grafana.com/grafana/plugins/redis-explorer-app) connects to Redis Enterprise software clusters using REST API. It provides application pages to add Redis Data Sources for managed databases and dashboards to see cluster configuration.
3939

4040
## Getting Started
4141

@@ -55,7 +55,7 @@ Data Source allows to connect to Redis using TCP port, Unix socket, Cluster, Sen
5555

5656
## Documentation
5757

58-
Please take a look at the [Documentation](https://redisgrafana.github.io/redis-datasource/overview/) to learn more about plug-in and features.
58+
Please take a look at the [Documentation](https://redisgrafana.github.io/redis-datasource/overview/) to learn more about plugin and features.
5959

6060
### Supported commands
6161

@@ -67,7 +67,7 @@ List of all supported commands and how to use them with examples you can find in
6767

6868
[Developing Redis Data Source](https://redisgrafana.github.io/development/redis-datasource/) page provides instructions on building the data source.
6969

70-
Are you interested in the latest features and updates? Start nightly built [Docker image for Redis Application plug-in](https://redisgrafana.github.io/development/images/), including Redis Data Source.
70+
Are you interested in the latest features and updates? Start nightly built [Docker image for Redis Application plugin](https://redisgrafana.github.io/development/images/), including Redis Data Source.
7171

7272
## Feedback
7373

pkg/datasource_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/grafana/grafana-plugin-sdk-go/backend"
12+
"github.com/redisgrafana/grafana-redis-datasource/pkg/models"
1213
"github.com/stretchr/testify/mock"
1314
"github.com/stretchr/testify/require"
1415
)
@@ -211,7 +212,7 @@ func TestQueryData(t *testing.T) {
211212
im.On("Get", mock.Anything).Return(&is, nil)
212213

213214
// HGET
214-
dm := queryModel{Command: "hget", Key: "test1", Field: "key1"}
215+
dm := queryModel{Command: models.HGet, Key: "test1", Field: "key1"}
215216
marshaled, _ := json.Marshal(dm)
216217

217218
// Response
@@ -247,7 +248,7 @@ func TestQueryDataWithError(t *testing.T) {
247248
im.On("Get", mock.Anything).Return(&is, errors.New("some_err"))
248249

249250
// HGET
250-
dm := queryModel{Command: "hget", Key: "test1", Field: "key1"}
251+
dm := queryModel{Command: models.HGet, Key: "test1", Field: "key1"}
251252
marshaled, _ := json.Marshal(dm)
252253

253254
// Query
@@ -370,7 +371,7 @@ func TestStreamingTimeSeries(t *testing.T) {
370371
im.On("Get", mock.Anything).Return(&is, nil)
371372

372373
// INFO
373-
dm := queryModel{Command: "info", Section: "server", Streaming: true}
374+
dm := queryModel{Command: models.Info, Section: "server", Streaming: true}
374375
marshaled, _ := json.Marshal(dm)
375376

376377
// Response
@@ -415,7 +416,7 @@ func TestStreamingTimeSeriesWithField(t *testing.T) {
415416
im.On("Get", mock.Anything).Return(&is, nil)
416417

417418
// HGET
418-
dm := queryModel{Command: "hget", Key: "test1", Field: "key1", Streaming: true}
419+
dm := queryModel{Command: models.HGet, Key: "test1", Field: "key1", Streaming: true}
419420
marshaled, _ := json.Marshal(dm)
420421

421422
// Response
@@ -460,7 +461,7 @@ func TestStreamingTimeSeriesWithErrorField(t *testing.T) {
460461
im.On("Get", mock.Anything).Return(&is, nil)
461462

462463
// INFO
463-
dm := queryModel{Command: "info", Field: "\"", Streaming: true}
464+
dm := queryModel{Command: models.Info, Field: "\"", Streaming: true}
464465
marshaled, _ := json.Marshal(dm)
465466

466467
// Response
@@ -500,7 +501,7 @@ func TestStreamingTimeSeriesWithWrongField(t *testing.T) {
500501
im.On("Get", mock.Anything).Return(&is, nil)
501502

502503
// INFO
503-
dm := queryModel{Command: "info", Section: "server", Field: "key1", Streaming: true}
504+
dm := queryModel{Command: models.Info, Section: "server", Field: "key1", Streaming: true}
504505
marshaled, _ := json.Marshal(dm)
505506

506507
// Response

pkg/models/custom.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package models
2+
3+
/**
4+
* Custom Commands
5+
*/
6+
const (
7+
TMScan = "tmscan"
8+
)

pkg/models/redis-search.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package models
2+
3+
/**
4+
* RediSearch Commands
5+
*/
6+
const (
7+
SearchInfo = "ft.info"
8+
)
9+
10+
/**
11+
* FT.INFO field configuration
12+
*/
13+
var SearchInfoConfig = map[string]string{
14+
"inverted_sz_mb": "decmbytes",
15+
"offset_vectors_sz_mb": "decmbytes",
16+
"doc_table_size_mb": "decmbytes",
17+
"sortable_values_size_mb": "decmbytes",
18+
"key_table_size_mb": "decmbytes",
19+
"percent_indexed": "percentunit",
20+
}

pkg/models/redis.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package models
2+
3+
/**
4+
* Redis Commands
5+
*/
6+
const (
7+
ClientList = "clientList"
8+
ClusterInfo = "clusterInfo"
9+
ClusterNodes = "clusterNodes"
10+
Get = "get"
11+
HGet = "hget"
12+
HGetAll = "hgetall"
13+
HKeys = "hkeys"
14+
HLen = "hlen"
15+
HMGet = "hmget"
16+
Info = "info"
17+
LLen = "llen"
18+
SCard = "scard"
19+
SlowlogGet = "slowlogGet"
20+
SMembers = "smembers"
21+
TTL = "ttl"
22+
Type = "type"
23+
XInfoStream = "xinfoStream"
24+
XLen = "xlen"
25+
XRange = "xrange"
26+
XRevRange = "xrevrange"
27+
)

pkg/query.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,55 +55,55 @@ func query(ctx context.Context, query backend.DataQuery, client redisClient, qm
5555
/**
5656
* Hash, Set, etc.
5757
*/
58-
case "hgetall":
58+
case models.HGetAll:
5959
return queryHGetAll(qm, client)
60-
case "hget":
60+
case models.HGet:
6161
return queryHGet(qm, client)
62-
case "hmget":
62+
case models.HMGet:
6363
return queryHMGet(qm, client)
64-
case "smembers", "hkeys":
64+
case models.SMembers, models.HKeys:
6565
return querySMembers(qm, client)
66-
case "type", "get", "ttl", "hlen", "xlen", "llen", "scard":
66+
case models.Type, models.Get, models.TTL, models.HLen, models.XLen, models.LLen, models.SCard:
6767
return queryKeyCommand(qm, client)
6868

6969
/**
7070
* Info
7171
*/
72-
case "info":
72+
case models.Info:
7373
return queryInfo(qm, client)
74-
case "clientList":
74+
case models.ClientList:
7575
return queryClientList(qm, client)
76-
case "slowlogGet":
76+
case models.SlowlogGet:
7777
return querySlowlogGet(qm, client)
7878

7979
/**
8080
* Streams
8181
*/
82-
case "xinfoStream":
82+
case models.XInfoStream:
8383
return queryXInfoStream(qm, client)
84-
case "xrange":
84+
case models.XRange:
8585
return queryXRange(qm, client)
86-
case "xrevrange":
86+
case models.XRevRange:
8787
return queryXRevRange(qm, client)
8888

8989
/**
9090
* Cluster
9191
*/
92-
case "clusterInfo":
92+
case models.ClusterInfo:
9393
return queryClusterInfo(qm, client)
94-
case "clusterNodes":
94+
case models.ClusterNodes:
9595
return queryClusterNodes(qm, client)
9696

9797
/**
9898
* RediSearch
9999
*/
100-
case "ft.info":
100+
case models.SearchInfo:
101101
return queryFtInfo(qm, client)
102102

103103
/**
104104
* Custom commands
105105
*/
106-
case "tmscan":
106+
case models.TMScan:
107107
return queryTMScan(qm, client)
108108

109109
/**

pkg/query_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@ func TestQuery(t *testing.T) {
2727
{queryModel{Command: models.TimeSeriesQueryIndex}},
2828
{queryModel{Command: models.TimeSeriesRange}},
2929
{queryModel{Command: models.TimeSeriesMRange}},
30-
{queryModel{Command: "hgetall"}},
31-
{queryModel{Command: "smembers"}},
32-
{queryModel{Command: "hkeys"}},
33-
{queryModel{Command: "hget"}},
34-
{queryModel{Command: "hmget"}},
35-
{queryModel{Command: "info"}},
36-
{queryModel{Command: "clientList"}},
37-
{queryModel{Command: "slowlogGet"}},
38-
{queryModel{Command: "type"}},
39-
{queryModel{Command: "xinfoStream"}},
40-
{queryModel{Command: "clusterInfo"}},
41-
{queryModel{Command: "clusterNodes"}},
42-
{queryModel{Command: "ft.info"}},
43-
{queryModel{Command: "xinfoStream"}},
44-
{queryModel{Command: "tmscan"}},
30+
{queryModel{Command: models.HGetAll}},
31+
{queryModel{Command: models.SMembers}},
32+
{queryModel{Command: models.HKeys}},
33+
{queryModel{Command: models.HGet}},
34+
{queryModel{Command: models.HMGet}},
35+
{queryModel{Command: models.Info}},
36+
{queryModel{Command: models.ClientList}},
37+
{queryModel{Command: models.SlowlogGet}},
38+
{queryModel{Command: models.Type}},
39+
{queryModel{Command: models.XInfoStream}},
40+
{queryModel{Command: models.ClusterInfo}},
41+
{queryModel{Command: models.ClusterNodes}},
42+
{queryModel{Command: models.SearchInfo}},
43+
{queryModel{Command: models.XInfoStream}},
44+
{queryModel{Command: models.TMScan}},
4545
{queryModel{Command: models.GearsPyStats}},
4646
{queryModel{Command: models.GearsDumpRegistrations}},
4747
{queryModel{Command: models.GearsPyExecute}},
4848
{queryModel{Command: models.GearsPyDumpReqs}},
49-
{queryModel{Command: "xrange"}},
50-
{queryModel{Command: "xrevrange"}},
49+
{queryModel{Command: models.XRange}},
50+
{queryModel{Command: models.XRevRange}},
5151
{queryModel{Command: models.GraphConfig}},
5252
{queryModel{Command: models.GraphExplain}},
5353
{queryModel{Command: models.GraphProfile}},
@@ -161,7 +161,7 @@ func TestQueryKeyCommand(t *testing.T) {
161161
}{
162162
{
163163
"should handle string value",
164-
queryModel{Command: "get", Key: "test1"},
164+
queryModel{Command: models.Get, Key: "test1"},
165165
"someStr",
166166
1,
167167
1,
@@ -172,7 +172,7 @@ func TestQueryKeyCommand(t *testing.T) {
172172
},
173173
{
174174
"should handle float64 value",
175-
queryModel{Command: "get", Key: "test1"},
175+
queryModel{Command: models.Get, Key: "test1"},
176176
"3.14",
177177
1,
178178
1,
@@ -183,7 +183,7 @@ func TestQueryKeyCommand(t *testing.T) {
183183
},
184184
{
185185
"should handle error",
186-
queryModel{Command: "get"},
186+
queryModel{Command: models.Get},
187187
nil,
188188
0,
189189
0,

pkg/redis-cluster_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"testing"
66

7+
"github.com/redisgrafana/grafana-redis-datasource/pkg/models"
78
"github.com/stretchr/testify/require"
89
)
910

@@ -24,7 +25,7 @@ func TestQueryClusterInfo(t *testing.T) {
2425
}{
2526
{
2627
"should parse clusterInfo bulk string",
27-
queryModel{Command: "clusterInfo"},
28+
queryModel{Command: models.ClusterInfo},
2829
"cluster_state:ok\r\ncluster_slots_assigned:16384\r\ncluster_slots_ok:16384\r\ncluster_slots_pfail:0\r\ncluster_slots_fail:0\r\ncluster_known_nodes:6\r\ncluster_size:3\r\ncluster_current_epoch:6\r\ncluster_my_epoch:2\r\ncluster_stats_messages_sent:1483972\r\ncluster_stats_messages_received:1483968",
2930
11,
3031
1,
@@ -37,7 +38,7 @@ func TestQueryClusterInfo(t *testing.T) {
3738
},
3839
{
3940
"should parse string and ignore non-pairing param",
40-
queryModel{Command: "clusterInfo"},
41+
queryModel{Command: models.ClusterInfo},
4142
"cluster_state:ok\r\ncluster_slots_assigned\r\ncluster_slots_ok:16384\r\ncluster_slots_pfail:0\r\ncluster_slots_fail:0\r\ncluster_known_nodes:6\r\ncluster_size:3\r\ncluster_current_epoch:6\r\ncluster_my_epoch:2\r\ncluster_stats_messages_sent:1483972\r\ncluster_stats_messages_received:1483968",
4243
10,
4344
1,
@@ -46,7 +47,7 @@ func TestQueryClusterInfo(t *testing.T) {
4647
},
4748
{
4849
"should handle error",
49-
queryModel{Command: "info"},
50+
queryModel{Command: models.Info},
5051
nil,
5152
0,
5253
0,
@@ -102,7 +103,7 @@ func TestQueryClusterNodes(t *testing.T) {
102103
}{
103104
{
104105
"should parse clusterNodes bulk string",
105-
queryModel{Command: "clusterNodes"},
106+
queryModel{Command: models.ClusterNodes},
106107
"07c37dfeb235213a872192d90877d0cd55635b91 127.0.0.1:30004@31004 slave e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca 1609783649927 1426238317239 4 connected\r\n67ed2db8d677e59ec4a4cefb06858cf2a1a89fa1 127.0.0.1:30002@31002 master - 0 1426238316232 2 connected 5461-10922\r\n292f8b365bb7edb5e285caf0b7e6ddc7265d2f4f 127.0.0.1:30003@31003 master - 0 1426238318243 3 connected 10923-16383\r\n6ec23923021cf3ffec47632106199cb7f496ce01 127.0.0.1:30005@31005 slave 67ed2db8d677e59ec4a4cefb06858cf2a1a89fa1 0 1426238316232 5 connected\r\n824fe116063bc5fcf9f4ffd895bc17aee7731ac3 127.0.0.1:30006@31006 slave 292f8b365bb7edb5e285caf0b7e6ddc7265d2f4f 0 1426238317741 6 connected\r\ne7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca 127.0.0.1:30001@31001 myself,master - 0 0 1 connected 0-5460",
107108
9,
108109
6,
@@ -118,7 +119,7 @@ func TestQueryClusterNodes(t *testing.T) {
118119
},
119120
{
120121
"should handle string with invalid number of values",
121-
queryModel{Command: "clusterNodes"},
122+
queryModel{Command: models.ClusterNodes},
122123
"07c37dfeb235213a872192d90877d0cd55635b91 127.0.0.1:30004@31004 e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca 1609783649927 1426238317239 4 connected",
123124
9,
124125
0,
@@ -127,7 +128,7 @@ func TestQueryClusterNodes(t *testing.T) {
127128
},
128129
{
129130
"should handle error",
130-
queryModel{Command: "clusterNodes"},
131+
queryModel{Command: models.ClusterNodes},
131132
nil,
132133
0,
133134
0,

0 commit comments

Comments
 (0)