forked from nytm/go-grafana-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdashboard.go
267 lines (219 loc) · 5.62 KB
/
dashboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package gapi
import (
"fmt"
"net/url"
"strings"
)
// Tags represents a set of tags
type Tags map[string]bool
// NewTags creates a new Tags object from the given string slice
func NewTags(tagslice []string) Tags {
tags := Tags{}
tags.Set(tagslice...)
return tags
}
// Add will add the given tags to the Tag list
func (t Tags) Add(tags ...string) {
for _, tag := range tags {
t[tag] = true
}
}
// Remove will remote the given tags to the Tag list
func (t Tags) Remove(tags ...string) {
for _, tag := range tags {
delete(t, tag)
}
}
// Set will delete all existing tags, and add the given tags
func (t Tags) Set(tags ...string) {
for tag := range t {
delete(t, tag)
}
t.Add(tags...)
}
// Strings will give back the tags as a string slice
func (t Tags) Strings() []string {
tags := []string{}
for tag := range t {
tags = append(tags, tag)
}
return tags
}
// DashboardMeta holds dashboard metadata
type DashboardMeta struct {
IsStarred bool `json:"isStarred"`
ID int64 `json:"id"`
Slug string `json:"slug"`
Title string `json:"title"`
URI string `json:"uri"`
Type string `json:"type"`
Tags []string `json:"tags"`
}
func (meta *DashboardMeta) SetSlug() {
meta.Slug = strings.Split(meta.URI, "/")[1]
}
// DashboardSaveResponse represents the response from the API when
// a dashboard is saved
type DashboardSaveResponse struct {
ID int64 `json:"id"`
UID string `json:"uid"`
URL string `json:"url"`
Status string `json:"status"`
Version int64 `json:"version"`
Slug string `json:"slug"`
}
// Dashboard represents a Grafana dashboard
type Dashboard struct {
Meta DashboardMeta `json:"meta"`
Model map[string]interface{} `json:"dashboard"`
}
// NewDashboard creates a new blank dashboard
func NewDashboard() *Dashboard {
return &Dashboard{Meta: DashboardMeta{}, Model: map[string]interface{}{}}
}
// Title returns the title of the dashboard
func (d Dashboard) Title() (string, bool) {
ititle, found := d.Model["title"]
if !found {
return "", false
}
title, ok := ititle.(string)
return title, ok
}
// Tags returns the tags for the dashboard
func (d *Dashboard) Tags() []string {
itagslice, found := d.Model["tags"]
if !found {
return []string{}
}
tagslice := []string{}
switch slice := itagslice.(type) {
case []interface{}:
for _, s := range slice {
switch ss := s.(type) {
case string:
tagslice = append(tagslice, ss)
case []string:
tagslice = append(tagslice, ss...)
}
}
case []string:
tagslice = slice
}
return tagslice
}
// AddTags will add the given tags to the dashboard
func (d *Dashboard) AddTags(newtags ...string) {
tags := NewTags(d.Tags())
for _, tag := range newtags {
tags[tag] = true
}
d.Model["tags"] = tags.Strings()
}
// RemoveTags will remove the given tags to the dashboard
func (d *Dashboard) RemoveTags(deltags ...string) {
tags := NewTags(d.Tags())
for _, tag := range deltags {
delete(tags, tag)
}
d.Model["tags"] = tags.Strings()
}
// SetTags will set the given tags on the dashboard (deleting all others)
func (d *Dashboard) SetTags(newtags ...string) {
tags := NewTags(newtags)
d.Model["tags"] = tags.Strings()
}
// SaveDashboard saves the given dashboard model to the API
func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*DashboardSaveResponse, error) {
wrapper := map[string]interface{}{
"dashboard": model,
"overwrite": overwrite,
}
res, err := c.doJSONRequest("POST", "/api/dashboards/db", wrapper)
if err != nil {
return nil, err
}
if !res.OK() {
return nil, res.Error()
}
result := &DashboardSaveResponse{}
err = res.BindJSON(&result)
return result, err
}
// Dashboard gets the dashboard with the given URI from Grafana
func (c *Client) Dashboard(slug string) (*Dashboard, error) {
path := fmt.Sprintf("/api/dashboards/%s", slug)
res, err := c.doRequest("GET", path, nil)
if err != nil {
return nil, err
}
if !res.OK() {
return nil, res.Error()
}
result := &Dashboard{}
err = res.BindJSON(&result)
return result, err
}
// DashboardMetas returns the dashboard metadata for the current
// organisation context. These can then be used to get specific
// dashboards
func (c *Client) DashboardMetas() ([]*DashboardMeta, error) {
return c.SearchDashboards("", "", false)
}
// SearchDashboards allows to search via query, tag or starred dashboards
func (c *Client) SearchDashboards(query string, tag string, starred bool) ([]*DashboardMeta, error) {
vals := make(url.Values)
if tag != "" {
vals.Set("tag", tag)
}
if query != "" {
vals.Set("query", query)
}
if starred {
vals.Set("starred", "true")
}
q := vals.Encode()
q = strings.Replace(q, "+", "%20", -1)
uri := "/api/search"
if q != "" {
uri += "?" + q
}
res, err := c.doRequest("GET", uri, nil)
if err != nil {
return nil, err
}
if !res.OK() {
return nil, res.Error()
}
result := []*DashboardMeta{}
err = res.BindJSON(&result)
for _, meta := range result {
meta.SetSlug()
}
return result, err
}
// Dashboards returns the dashboards for the current org
func (c *Client) Dashboards() ([]*Dashboard, error) {
dashes := []*Dashboard{}
metas, err := c.DashboardMetas()
if err != nil {
return dashes, err
}
for _, meta := range metas {
dash, err := c.Dashboard(meta.URI)
if err != nil {
return dashes, err
}
dashes = append(dashes, dash)
}
return dashes, nil
}
// DeleteDashboard will delete the dashboard with the given slug from Grafana
func (c *Client) DeleteDashboard(uri string) error {
path := fmt.Sprintf("/api/dashboards/db/%s", uri)
res, err := c.doRequest("DELETE", path, nil)
if err != nil {
return err
}
return res.Error()
}