Skip to content

Commit f5f3b82

Browse files
feature: Adds op querying to backend
1 parent c7383b9 commit f5f3b82

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.19
55
require github.com/grafana/grafana-plugin-sdk-go v0.149.1
66

77
require (
8-
github.com/NeedleInAJayStack/haystack v0.1.6
8+
github.com/NeedleInAJayStack/haystack v0.1.7
99
github.com/google/go-cmp v0.5.9
1010
)
1111

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zum
3535
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
3636
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3737
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
38-
github.com/NeedleInAJayStack/haystack v0.1.6 h1:LhV4SO2Jgjq8YQPd/JcJdb/a3cq9+Lu548vTDj0NakU=
39-
github.com/NeedleInAJayStack/haystack v0.1.6/go.mod h1:Oho5sG64nQS27mApp6gWOUi7m4OgzpWllZrOtZn8qss=
38+
github.com/NeedleInAJayStack/haystack v0.1.7 h1:G0w/DdH6OiPv0pALTvaMYvBIorLaz3yhMupHTqn17Zc=
39+
github.com/NeedleInAJayStack/haystack v0.1.7/go.mod h1:Oho5sG64nQS27mApp6gWOUi7m4OgzpWllZrOtZn8qss=
4040
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
4141
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
4242
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=

pkg/plugin/datasource.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ func (datasource *Datasource) query(ctx context.Context, pCtx backend.PluginCont
123123

124124
var grid haystack.Grid
125125
switch model.Type {
126+
case "Ops":
127+
ops, err := datasource.ops()
128+
if err != nil {
129+
log.DefaultLogger.Error(err.Error())
130+
return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("Ops eval failure: %v", err.Error()))
131+
}
132+
grid = ops
126133
case "Eval":
127134
eval, err := datasource.eval(model.Eval, variables)
128135
if err != nil {
@@ -185,6 +192,22 @@ func (datasource *Datasource) CheckHealth(_ context.Context, req *backend.CheckH
185192
}, nil
186193
}
187194

195+
func (datasource *Datasource) ops() (haystack.Grid, error) {
196+
result, err := datasource.client.Ops()
197+
// If the error is a 404, try to reconnect and try again
198+
switch error := err.(type) {
199+
case client.HTTPError:
200+
if error.Code == 404 {
201+
datasource.client.Open()
202+
return datasource.client.Ops()
203+
} else {
204+
return result, err
205+
}
206+
default:
207+
return result, err
208+
}
209+
}
210+
188211
func (datasource *Datasource) eval(expr string, variables map[string]string) (haystack.Grid, error) {
189212
for name, val := range variables {
190213
expr = strings.ReplaceAll(expr, name, val)

pkg/plugin/datasource_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ func (c *testHaystackClient) About() (haystack.Dict, error) {
185185
return haystack.Dict{}, nil
186186
}
187187

188+
// Ops returns an empty grid
189+
func (c *testHaystackClient) Ops() (haystack.Grid, error) {
190+
return haystack.EmptyGrid(), nil
191+
}
192+
188193
// Eval returns the EvalResponse
189194
func (c *testHaystackClient) Eval(query string) (haystack.Grid, error) {
190195
return c.evalResponse, nil

pkg/plugin/haystackClient.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type HaystackClient interface {
99
Open() error
1010
Close() error
1111
About() (haystack.Dict, error)
12+
Ops() (haystack.Grid, error)
1213
Eval(string) (haystack.Grid, error)
1314
HisReadAbsDateTime(haystack.Ref, haystack.DateTime, haystack.DateTime) (haystack.Grid, error)
1415
Read(string) (haystack.Grid, error)

0 commit comments

Comments
 (0)