-
Notifications
You must be signed in to change notification settings - Fork 32
/
dashboards.go
533 lines (471 loc) · 12.8 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
package main
import (
"fmt"
"io/ioutil"
"net/url"
"strconv"
"strings"
"github.com/mackerelio/mackerel-client-go"
"github.com/mackerelio/mkr/logger"
"github.com/mackerelio/mkr/mackerelclient"
"github.com/urfave/cli"
yaml "gopkg.in/yaml.v2"
)
var commandDashboards = cli.Command{
Name: "dashboards",
Usage: "Generating custom dashboards",
Description: `
Generating dashboards. See https://mackerel.io/docs/entry/advanced/cli
`,
Subcommands: []cli.Command{
{
Name: "generate",
Usage: "Generate custom dashboard",
ArgsUsage: "[--print | -p] <file>",
Description: `
A custom dashboard is registered from a yaml file.
Requests "POST /api/v0/dashboards". See https://mackerel.io/api-docs/entry/dashboards#create.
`,
Action: doGenerateDashboards,
Flags: []cli.Flag{
cli.BoolFlag{Name: "print, p", Usage: "markdown is output in standard output."},
},
},
},
}
type graphsConfig struct {
ConfigVersion string `yaml:"config_version"`
Title string `yaml:"title"`
URLPath string `yaml:"url_path"`
Format string `yaml:"format"`
Height int `yaml:"height"`
Width int `yaml:"width"`
HostGraphFormat []*hostGraphFormat `yaml:"host_graphs"`
GraphFormat []*graphFormat `yaml:"graphs"`
}
type hostGraphFormat struct {
Headline string `yaml:"headline"`
HostIDs []string `yaml:"host_ids"`
GraphNames []string `yaml:"graph_names"`
Period string `yaml:"period"`
}
type graphFormat struct {
Headline string `yaml:"headline"`
ColumnCount int `yaml:"column_count"`
GraphDefs []*graphDef `yaml:"graph_def"`
}
type graphDef struct {
HostID string `yaml:"host_id"`
ServiceName string `yaml:"service_name"`
RoleName string `yaml:"role_name"`
Query string `yaml:"query"`
GraphName string `yaml:"graph_name"`
GraphTitle string `yaml:"title"`
Unit string `yaml:"unit"`
Period string `yaml:"period"`
Stacked bool `yaml:"stacked"`
Simplified bool `yaml:"simplified"`
}
func (g graphDef) isHostGraph() bool {
return g.HostID != ""
}
func (g graphDef) isServiceGraph() bool {
return g.ServiceName != "" && g.RoleName == ""
}
func (g graphDef) isRoleGraph() bool {
return g.ServiceName != "" && g.RoleName != ""
}
func (g graphDef) isExpressionGraph() bool {
return g.Query != ""
}
func (g graphDef) getBaseGraph(graphType string, height int, width int) (baseGraph baseGraph, err error) {
if g.isHostGraph() {
if g.GraphName == "" {
return nil, cli.NewExitError("graph_name is required for host graph.", 1)
}
return hostGraph{
g.HostID,
graphType,
g.GraphName,
g.Period,
height,
width,
}, nil
}
if g.isServiceGraph() {
if g.GraphName == "" {
return nil, cli.NewExitError("graph_name is required for service graph.", 1)
}
return serviceGraph{
g.ServiceName,
graphType,
g.GraphName,
g.Period,
height,
width,
}, nil
}
if g.isRoleGraph() {
if g.GraphName == "" {
return nil, cli.NewExitError("graph_name is required for role graph.", 1)
}
return roleGraph{
g.ServiceName,
g.RoleName,
graphType,
g.GraphName,
g.Period,
g.Stacked,
g.Simplified,
height,
width,
}, nil
}
if g.isExpressionGraph() {
return expressionGraph{
g.Query,
graphType,
g.GraphTitle,
g.Unit,
g.Period,
height,
width,
}, nil
}
return nil, cli.NewExitError("either host_id, service_name or query should be specified.", 1)
}
type baseGraph interface {
getURL(string, bool) string
getPermalink(string) string
getHeight() int
getWidth() int
generateGraphString(orgName string) string
}
type hostGraph struct {
HostID string
GraphType string
Graph string
Period string
height int
width int
}
func (h hostGraph) getURL(orgName string, isImage bool) string {
extension := ""
if isImage {
extension = ".png"
}
u, _ := url.Parse(fmt.Sprintf("https://mackerel.io/embed/orgs/%s/hosts/%s"+extension, orgName, h.HostID))
param := url.Values{}
param.Add("graph", h.Graph)
param.Add("period", h.Period)
u.RawQuery = param.Encode()
return u.String()
}
func (h hostGraph) getPermalink(orgName string) string {
u, _ := url.Parse(fmt.Sprintf("https://mackerel.io/orgs/%s/hosts/%s/-/graphs/%s", orgName, h.HostID, url.QueryEscape(h.Graph)))
return u.String()
}
func (h hostGraph) generateGraphString(orgName string) string {
if h.GraphType == "iframe" {
return makeIframeTag(orgName, h)
}
return makeImageMarkdown(orgName, h)
}
func (h hostGraph) getHeight() int {
return h.height
}
func (h hostGraph) getWidth() int {
return h.width
}
type serviceGraph struct {
ServiceName string
GraphType string
Graph string
Period string
height int
width int
}
func (s serviceGraph) getURL(orgName string, isImage bool) string {
extension := ""
if isImage {
extension = ".png"
}
u, _ := url.Parse(fmt.Sprintf("https://mackerel.io/embed/orgs/%s/services/%s"+extension, orgName, s.ServiceName))
param := url.Values{}
param.Add("graph", s.Graph)
param.Add("period", s.Period)
u.RawQuery = param.Encode()
return u.String()
}
func (s serviceGraph) getPermalink(orgName string) string {
u, _ := url.Parse(fmt.Sprintf("https://mackerel.io/orgs/%s/services/%s/-/graphs", orgName, s.ServiceName))
param := url.Values{}
param.Add("name", s.Graph)
u.RawQuery = param.Encode()
return u.String()
}
func (s serviceGraph) generateGraphString(orgName string) string {
if s.GraphType == "iframe" {
return makeIframeTag(orgName, s)
}
return makeImageMarkdown(orgName, s)
}
func (s serviceGraph) getHeight() int {
return s.height
}
func (s serviceGraph) getWidth() int {
return s.width
}
type roleGraph struct {
ServiceName string
RoleName string
GraphType string
Graph string
Period string
Stacked bool
Simplified bool
height int
width int
}
func (r roleGraph) getURL(orgName string, isImage bool) string {
extension := ""
if isImage {
extension = ".png"
}
u, _ := url.Parse(fmt.Sprintf("https://mackerel.io/embed/orgs/%s/services/%s/%s"+extension, orgName, r.ServiceName, r.RoleName))
param := url.Values{}
param.Add("graph", r.Graph)
param.Add("stacked", strconv.FormatBool(r.Stacked))
param.Add("simplified", strconv.FormatBool(r.Simplified))
param.Add("period", r.Period)
u.RawQuery = param.Encode()
return u.String()
}
func (r roleGraph) getPermalink(orgName string) string {
u, _ := url.Parse(fmt.Sprintf("https://mackerel.io/orgs/%s/services/%s/%s/-/graph", orgName, r.ServiceName, r.RoleName))
param := url.Values{}
param.Add("name", r.Graph)
u.RawQuery = param.Encode()
return u.String()
}
func (r roleGraph) generateGraphString(orgName string) string {
if r.GraphType == "iframe" {
return makeIframeTag(orgName, r)
}
return makeImageMarkdown(orgName, r)
}
func (r roleGraph) getHeight() int {
return r.height
}
func (r roleGraph) getWidth() int {
return r.width
}
type expressionGraph struct {
Query string
GraphType string
Title string
Unit string
Period string
height int
width int
}
func (e expressionGraph) getURL(orgName string, isImage bool) string {
extension := ""
if isImage {
extension = ".png"
}
u, _ := url.Parse(fmt.Sprintf("https://mackerel.io/embed/orgs/%s/advanced-graph"+extension, orgName))
param := url.Values{}
param.Add("query", e.Query)
param.Add("period", e.Period)
param.Add("title", e.Title)
param.Add("unit", e.Unit)
u.RawQuery = param.Encode()
return u.String()
}
func (e expressionGraph) getPermalink(orgName string) string {
u, _ := url.Parse(fmt.Sprintf("https://mackerel.io/orgs/%s/advanced-graph", orgName))
param := url.Values{}
param.Add("query", e.Query)
param.Add("title", e.Title)
param.Add("unit", e.Unit)
u.RawQuery = param.Encode()
return u.String()
}
func (e expressionGraph) generateGraphString(orgName string) string {
if e.GraphType == "iframe" {
return makeIframeTag(orgName, e)
}
return makeImageMarkdown(orgName, e)
}
func (e expressionGraph) getHeight() int {
return e.height
}
func (e expressionGraph) getWidth() int {
return e.width
}
type markdownFactory struct {
Headline string
TableHeader string
BaseGraphs []baseGraph
ColumnCount int
}
func (mdf markdownFactory) generate(orgName string) string {
markdown := ""
if mdf.Headline != "" {
markdown += fmt.Sprintf("## %s\n", mdf.Headline)
}
markdown += mdf.TableHeader
for i, g := range mdf.BaseGraphs {
markdown += "|" + g.generateGraphString(orgName)
if i%mdf.ColumnCount >= mdf.ColumnCount-1 || i >= len(mdf.BaseGraphs)-1 {
markdown += "|\n"
}
}
return markdown
}
func makeIframeTag(orgName string, g baseGraph) string {
return fmt.Sprintf(`<iframe src="%s" height="%d" width="%d" frameborder="0"></iframe>`, g.getURL(orgName, false), g.getHeight(), g.getWidth())
}
func makeImageMarkdown(orgName string, g baseGraph) string {
return fmt.Sprintf("[![graph](%s)](%s)", g.getURL(orgName, true), g.getPermalink(orgName))
}
func doGenerateDashboards(c *cli.Context) error {
isStdout := c.Bool("print")
argFilePath := c.Args()
if len(argFilePath) < 1 {
cli.ShowCommandHelp(c, "generate")
return cli.NewExitError("specify a yaml file.", 1)
}
buf, err := ioutil.ReadFile(argFilePath[0])
logger.DieIf(err)
yml := graphsConfig{}
err = yaml.Unmarshal(buf, &yml)
logger.DieIf(err)
client := mackerelclient.NewFromContext(c)
org, err := client.GetOrg()
logger.DieIf(err)
if yml.ConfigVersion == "" {
return cli.NewExitError("config_version is required in yaml.", 1)
}
if yml.ConfigVersion != "0.9" {
return cli.NewExitError(fmt.Sprintf("config_version %s is not suport.", yml.ConfigVersion), 1)
}
if yml.Title == "" {
return cli.NewExitError("title is required in yaml.", 1)
}
if yml.URLPath == "" {
return cli.NewExitError("url_path is required in yaml.", 1)
}
if yml.Format == "" {
yml.Format = "iframe"
}
if yml.Format != "iframe" && yml.Format != "image" {
return cli.NewExitError("graph_type should be 'iframe' or 'image'.", 1)
}
if yml.Height == 0 {
yml.Height = 200
}
if yml.Width == 0 {
yml.Width = 400
}
if yml.HostGraphFormat != nil && yml.GraphFormat != nil {
return cli.NewExitError("you cannot specify both 'graphs' and host_graphs'.", 1)
}
var markdown string
for _, h := range yml.HostGraphFormat {
mdf := generateHostGraphsMarkdownFactory(h, yml.Format, yml.Height, yml.Width)
markdown += mdf.generate(org.Name)
}
for _, g := range yml.GraphFormat {
mdf, err := generateGraphsMarkdownFactory(g, yml.Format, yml.Height, yml.Width)
if err != nil {
return err
}
markdown += mdf.generate(org.Name)
}
if isStdout {
fmt.Println(markdown)
} else {
updateDashboard := &mackerel.Dashboard{
Title: yml.Title,
BodyMarkDown: markdown,
URLPath: yml.URLPath,
}
dashboards, fetchError := client.FindDashboards()
logger.DieIf(fetchError)
dashboardID := ""
for _, ds := range dashboards {
if ds.URLPath == yml.URLPath {
dashboardID = ds.ID
}
}
if dashboardID == "" {
_, createError := client.CreateDashboard(updateDashboard)
logger.DieIf(createError)
} else {
_, updateError := client.UpdateDashboard(dashboardID, updateDashboard)
logger.DieIf(updateError)
}
}
return nil
}
func generateHostGraphsMarkdownFactory(hostGraphs *hostGraphFormat, graphType string, height int, width int) *markdownFactory {
if hostGraphs.Period == "" {
hostGraphs.Period = "1h"
}
var baseGraphs []baseGraph
tableHeader := generateHostGraphsTableHeader(hostGraphs.GraphNames)
for _, hostID := range hostGraphs.HostIDs {
for _, graphName := range hostGraphs.GraphNames {
baseGraphs = append(baseGraphs, hostGraph{
hostID,
graphType,
graphName,
hostGraphs.Period,
height,
width,
})
}
}
return &markdownFactory{
Headline: hostGraphs.Headline,
TableHeader: tableHeader,
BaseGraphs: baseGraphs,
ColumnCount: len(hostGraphs.GraphNames),
}
}
func generateHostGraphsTableHeader(graphNames []string) string {
var header string
for _, graphName := range graphNames {
header += "|" + graphName
}
header += "|\n" + generateAlignmentLine(len(graphNames))
return header
}
func generateGraphsMarkdownFactory(graphs *graphFormat, graphType string, height int, width int) (mdf *markdownFactory, err error) {
if graphs.ColumnCount == 0 {
graphs.ColumnCount = 1
}
tableHeader := generateAlignmentLine(graphs.ColumnCount)
var baseGraphs []baseGraph
for _, gd := range graphs.GraphDefs {
if gd.Period == "" {
gd.Period = "1h"
}
bg, err := gd.getBaseGraph(graphType, height, width)
if err != nil {
return nil, err
}
baseGraphs = append(baseGraphs, bg)
}
return &markdownFactory{
Headline: graphs.Headline,
TableHeader: tableHeader,
BaseGraphs: baseGraphs,
ColumnCount: graphs.ColumnCount,
}, nil
}
func generateAlignmentLine(count int) string {
return strings.Repeat("|:-:", count) + "|\n"
}