-
Notifications
You must be signed in to change notification settings - Fork 33
/
dashboards.go
279 lines (258 loc) · 6.94 KB
/
dashboards.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
268
269
270
271
272
273
274
275
276
277
278
279
package mackerel
import (
"encoding/json"
"fmt"
)
/*
`/dashboards` Response
{
"dashboards": [
{
"id": "2c5bLca8e",
"title": "My Custom Dashboard(Current)",
"urlPath": "2u4PP3TJqbv",
"createdAt": 1552909732,
"updatedAt": 1552992837,
"memo": "A test Current Dashboard"
}
]
}
*/
/*
`/dashboards/${ID}` Response`
{
"id": "2c5bLca8e",
"createdAt": 1552909732,
"updatedAt": 1552992837,
"title": "My Custom Dashboard(Current),
"urlPath": "2u4PP3TJqbv",
"memo": "A test Current Dashboard",
"widgets": [
{
"type": "markdown",
"title": "markdown",
"markdown": "# body",
"layout": {
"x": 0,
"y": 0,
"width": 24,
"height": 3
}
},
{
"type": "graph",
"title": "graph",
"graph": {
"type": "host",
"hostId": "2u4PP3TJqbw",
"name": "loadavg.loadavg15"
},
"layout": {
"x": 0,
"y": 7,
"width": 8,
"height": 10
}
},
{
"type": "value",
"title": "value",
"fractionSize": 2,
"suffix": "total",
"metric": {
"type": "expression",
"expression": "alias(scale(\nsum(\n group(\n host(2u4PP3TJqbx,loadavg.*)\n )\n),\n1\n), 'test')"
},
"layout": {
"x": 0,
"y": 17,
"width": 8,
"height": 5
}
},
{
"type": "alertStatus",
"title": "alertStatus",
"roleFullname": "test:dashboard",
"layout": {
"x": 0,
"y": 17,
"width": 8,
"height": 5
}
}
]
}
*/
// Dashboard information
type Dashboard struct {
ID string `json:"id,omitempty"`
Title string `json:"title"`
URLPath string `json:"urlPath"`
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
Memo string `json:"memo"`
Widgets []Widget `json:"widgets"`
}
// Widget information
type Widget struct {
Type string `json:"type"`
Title string `json:"title"`
Layout Layout `json:"layout"`
Metric Metric `json:"metric,omitempty"`
Graph Graph `json:"graph,omitempty"`
Range Range `json:"range,omitempty"`
Markdown string `json:"markdown,omitempty"`
ReferenceLines []ReferenceLine `json:"referenceLines,omitempty"`
// If this field is nil, it will be treated as a two-digit display after the decimal point.
FractionSize *int64 `json:"fractionSize,omitempty"`
Suffix string `json:"suffix,omitempty"`
FormatRules []FormatRule `json:"formatRules,omitempty"`
RoleFullName string `json:"roleFullname,omitempty"`
}
// Metric information
type Metric struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
HostID string `json:"hostId,omitempty"`
ServiceName string `json:"serviceName,omitempty"`
Expression string `json:"expression,omitempty"`
Query string `json:"query,omitempty"`
Legend string `json:"legend,omitempty"`
}
type metricQuery struct {
Type string `json:"type"`
Name string `json:"-"`
HostID string `json:"-"`
ServiceName string `json:"-"`
Expression string `json:"-"`
Query string `json:"query"`
Legend string `json:"legend"`
}
// MarshalJSON marshals as JSON
func (m Metric) MarshalJSON() ([]byte, error) {
type Alias Metric
switch m.Type {
case "":
return []byte("null"), nil
case "query":
return json.Marshal(metricQuery(m))
default:
return json.Marshal(Alias(m))
}
}
// FormatRule information
type FormatRule struct {
Name string `json:"name"`
Threshold float64 `json:"threshold"`
Operator string `json:"operator"`
}
// Graph information
type Graph struct {
Type string `json:"type"`
Name string `json:"name,omitempty"`
HostID string `json:"hostId,omitempty"`
RoleFullName string `json:"roleFullname,omitempty"`
IsStacked bool `json:"isStacked,omitempty"`
ServiceName string `json:"serviceName,omitempty"`
Expression string `json:"expression,omitempty"`
Query string `json:"query,omitempty"`
Legend string `json:"legend,omitempty"`
}
type graphQuery struct {
Type string `json:"type"`
Name string `json:"-"`
HostID string `json:"-"`
RoleFullName string `json:"-"`
IsStacked bool `json:"-"`
ServiceName string `json:"-"`
Expression string `json:"-"`
Query string `json:"query"`
Legend string `json:"legend"`
}
// MarshalJSON marshals as JSON
func (g Graph) MarshalJSON() ([]byte, error) {
type Alias Graph
switch g.Type {
case "":
return []byte("null"), nil
case "query":
return json.Marshal(graphQuery(g))
default:
return json.Marshal(Alias(g))
}
}
// Range information
type Range struct {
Type string `json:"type"`
Period int64 `json:"period,omitempty"`
Offset int64 `json:"offset,omitempty"`
Start int64 `json:"start,omitempty"`
End int64 `json:"end,omitempty"`
}
type rangeAbsolute struct {
Type string `json:"type"`
Period int64 `json:"-"`
Offset int64 `json:"-"`
Start int64 `json:"start"`
End int64 `json:"end"`
}
type rangeRelative struct {
Type string `json:"type"`
Period int64 `json:"period"`
Offset int64 `json:"offset"`
Start int64 `json:"-"`
End int64 `json:"-"`
}
// MarshalJSON marshals as JSON
func (r Range) MarshalJSON() ([]byte, error) {
switch r.Type {
case "absolute":
return json.Marshal(rangeAbsolute(r))
case "relative":
return json.Marshal(rangeRelative(r))
default:
return []byte("null"), nil
}
}
// ReferenceLine information
type ReferenceLine struct {
Label string `json:"label"`
Value float64 `json:"value"`
}
// Layout information
type Layout struct {
X int64 `json:"x"`
Y int64 `json:"y"`
Width int64 `json:"width"`
Height int64 `json:"height"`
}
// FindDashboards finds dashboards.
func (c *Client) FindDashboards() ([]*Dashboard, error) {
data, err := requestGet[struct {
Dashboards []*Dashboard `json:"dashboards"`
}](c, "/api/v0/dashboards")
if err != nil {
return nil, err
}
return data.Dashboards, nil
}
// CreateDashboard creates a dashboard.
func (c *Client) CreateDashboard(param *Dashboard) (*Dashboard, error) {
return requestPost[Dashboard](c, "/api/v0/dashboards", param)
}
// FindDashboard finds a dashboard.
func (c *Client) FindDashboard(dashboardID string) (*Dashboard, error) {
path := fmt.Sprintf("/api/v0/dashboards/%s", dashboardID)
return requestGet[Dashboard](c, path)
}
// UpdateDashboard updates a dashboard.
func (c *Client) UpdateDashboard(dashboardID string, param *Dashboard) (*Dashboard, error) {
path := fmt.Sprintf("/api/v0/dashboards/%s", dashboardID)
return requestPut[Dashboard](c, path, param)
}
// DeleteDashboard deletes a dashboard.
func (c *Client) DeleteDashboard(dashboardID string) (*Dashboard, error) {
path := fmt.Sprintf("/api/v0/dashboards/%s", dashboardID)
return requestDelete[Dashboard](c, path)
}