-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
graph.go
425 lines (347 loc) · 9.82 KB
/
graph.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
package graph
import (
"fmt"
"github.com/K-Phoen/grabana/alert"
"github.com/K-Phoen/grabana/axis"
"github.com/K-Phoen/grabana/errors"
"github.com/K-Phoen/grabana/graph/series"
"github.com/K-Phoen/grabana/links"
"github.com/K-Phoen/grabana/target/graphite"
"github.com/K-Phoen/grabana/target/influxdb"
"github.com/K-Phoen/grabana/target/prometheus"
"github.com/K-Phoen/grabana/target/stackdriver"
"github.com/K-Phoen/sdk"
)
// Option represents an option that can be used to configure a graph panel.
type Option func(graph *Graph) error
// DrawMode represents a type of visualization that will be drawn in the graph
// (lines, bars, points)
type DrawMode uint8
const (
// Bars will display bars.
Bars DrawMode = iota
// Lines will display lines.
Lines
// Points will display points.
Points
)
// NullValue describes how null values are displayed.
type NullValue string
const (
// AsZero treats null values as zero values.
AsZero NullValue = "null as zero"
// AsNull treats null values as null.
AsNull NullValue = "null"
// Connected connects null values.
Connected NullValue = "connected"
)
// LegendOption allows to configure a legend.
type LegendOption uint16
const (
// Hide keeps the legend from being displayed.
Hide LegendOption = iota
// AsTable displays the legend as a table.
AsTable
// ToTheRight displays the legend on the right side of the graph.
ToTheRight
// Min displays the smallest value of the series.
Min
// Max displays the largest value of the series.
Max
// Avg displays the average of the series.
Avg
// Current displays the current value of the series.
Current
// Total displays the total value of the series.
Total
// NoNullSeries hides series with only null values from the legend.
NoNullSeries
// NoZeroSeries hides series with only 0 values from the legend.
NoZeroSeries
)
// Graph represents a graph panel.
type Graph struct {
Builder *sdk.Panel
Alert *alert.Alert
}
// New creates a new graph panel.
func New(title string, options ...Option) (*Graph, error) {
panel := &Graph{Builder: sdk.NewGraph(title)}
panel.Builder.AliasColors = make(map[string]interface{})
panel.Builder.IsNew = false
panel.Builder.GraphPanel.Tooltip.Sort = 2
panel.Builder.GraphPanel.Tooltip.Shared = true
for _, opt := range append(defaults(), options...) {
if err := opt(panel); err != nil {
return nil, err
}
}
return panel, nil
}
func defaults() []Option {
return []Option{
Draw(Lines),
Span(6),
Fill(1),
Null(AsZero),
LineWidth(1),
Legend(NoZeroSeries, NoNullSeries),
defaultAxes(),
}
}
func defaultAxes() Option {
return func(graph *Graph) error {
graph.Builder.GraphPanel.YAxis = true
graph.Builder.GraphPanel.XAxis = true
graph.Builder.GraphPanel.Yaxes = []sdk.Axis{
*axis.New().Builder,
*axis.New(axis.Hide()).Builder,
}
graph.Builder.GraphPanel.Xaxis = *axis.New(axis.Unit("time")).Builder
return nil
}
}
// Links adds links to be displayed on this panel.
func Links(panelLinks ...links.Link) Option {
return func(graph *Graph) error {
graph.Builder.Links = make([]sdk.Link, 0, len(panelLinks))
for _, link := range panelLinks {
graph.Builder.Links = append(graph.Builder.Links, link.Builder)
}
return nil
}
}
// WithPrometheusTarget adds a prometheus query to the graph.
func WithPrometheusTarget(query string, options ...prometheus.Option) Option {
target := prometheus.New(query, options...)
return func(graph *Graph) error {
graph.Builder.AddTarget(&sdk.Target{
RefID: target.Ref,
Hide: target.Hidden,
Expr: target.Expr,
IntervalFactor: target.IntervalFactor,
Interval: target.Interval,
Step: target.Step,
LegendFormat: target.LegendFormat,
Instant: target.Instant,
Format: target.Format,
})
return nil
}
}
// WithGraphiteTarget adds a Graphite target to the table.
func WithGraphiteTarget(query string, options ...graphite.Option) Option {
target := graphite.New(query, options...)
return func(graph *Graph) error {
graph.Builder.AddTarget(target.Builder)
return nil
}
}
// WithInfluxDBTarget adds an InfluxDB target to the graph.
func WithInfluxDBTarget(query string, options ...influxdb.Option) Option {
target := influxdb.New(query, options...)
return func(graph *Graph) error {
graph.Builder.AddTarget(target.Builder)
return nil
}
}
// WithStackdriverTarget adds a stackdriver query to the graph.
func WithStackdriverTarget(target *stackdriver.Stackdriver) Option {
return func(graph *Graph) error {
graph.Builder.AddTarget(target.Builder)
return nil
}
}
// DataSource sets the data source to be used by the graph.
func DataSource(source string) Option {
return func(graph *Graph) error {
graph.Builder.Datasource = &sdk.DatasourceRef{LegacyName: source}
return nil
}
}
// Span sets the width of the panel, in grid units. Should be a positive
// number between 1 and 12. Example: 6.
func Span(span float32) Option {
return func(graph *Graph) error {
if span < 1 || span > 12 {
return fmt.Errorf("span must be between 1 and 12: %w", errors.ErrInvalidArgument)
}
graph.Builder.Span = span
return nil
}
}
// Height sets the height of the panel, in pixels. Example: "400px".
func Height(height string) Option {
return func(graph *Graph) error {
graph.Builder.Height = &height
return nil
}
}
// Description annotates the current visualization with a human-readable description.
func Description(content string) Option {
return func(graph *Graph) error {
graph.Builder.Description = &content
return nil
}
}
// Transparent makes the background transparent.
func Transparent() Option {
return func(graph *Graph) error {
graph.Builder.Transparent = true
return nil
}
}
// LeftYAxis configures the left Y axis.
func LeftYAxis(opts ...axis.Option) Option {
return func(graph *Graph) error {
graph.Builder.GraphPanel.Yaxes[0] = *axis.New(opts...).Builder
return nil
}
}
// RightYAxis configures the right Y axis.
func RightYAxis(opts ...axis.Option) Option {
return func(graph *Graph) error {
graph.Builder.GraphPanel.Yaxes[1] = *axis.New(opts...).Builder
return nil
}
}
// XAxis configures the X axis.
func XAxis(opts ...axis.Option) Option {
return func(graph *Graph) error {
graph.Builder.GraphPanel.Xaxis = *axis.New(opts...).Builder
return nil
}
}
// Alert creates an alert for this graph.
func Alert(name string, opts ...alert.Option) Option {
return func(graph *Graph) error {
graph.Alert = alert.New(graph.Builder.Title, append(opts, alert.Summary(name))...)
graph.Alert.Builder.Name = graph.Builder.Title
return nil
}
}
// Draw specifies how the graph will be drawn.
func Draw(modes ...DrawMode) Option {
return func(graph *Graph) error {
graph.Builder.Bars = false
graph.Builder.Lines = false
graph.Builder.Points = false
for _, mode := range modes {
switch mode {
case Bars:
graph.Builder.Bars = true
case Lines:
graph.Builder.Lines = true
case Points:
graph.Builder.Points = true
default:
return errors.ErrInvalidArgument
}
}
return nil
}
}
// Fill defines the amount of color fill for a series (default 1, max 10, 0 is none).
func Fill(value int) Option {
return func(graph *Graph) error {
if value < 0 || value > 10 {
return fmt.Errorf("fill must be between 0 and 10: %w", errors.ErrInvalidArgument)
}
graph.Builder.Fill = value
return nil
}
}
// LineWidth defines the width of the line for a series (default 1, max 10, 0 is none).
func LineWidth(value uint) Option {
return func(graph *Graph) error {
if value > 10 {
return fmt.Errorf("line width must be between 0 and 10: %w", errors.ErrInvalidArgument)
}
graph.Builder.Linewidth = value
return nil
}
}
// Staircase draws adjacent points as staircase.
func Staircase() Option {
return func(graph *Graph) error {
graph.Builder.GraphPanel.SteppedLine = true
return nil
}
}
// PointRadius adjusts the size of points when Points are selected as Draw Mode.
func PointRadius(value float32) Option {
return func(graph *Graph) error {
if value < 0 || value > 10 {
return fmt.Errorf("point radius must be between 0 and 10: %w", errors.ErrInvalidArgument)
}
graph.Builder.Pointradius = value
return nil
}
}
// Null configures how null values are displayed.
func Null(mode NullValue) Option {
return func(graph *Graph) error {
graph.Builder.GraphPanel.NullPointMode = string(mode)
return nil
}
}
// Repeat configures repeating a panel for a variable
func Repeat(repeat string) Option {
return func(graph *Graph) error {
graph.Builder.Repeat = &repeat
return nil
}
}
// SeriesOverride configures how null values are displayed.
// See https://grafana.com/docs/grafana/latest/panels/field-options/
func SeriesOverride(opts ...series.OverrideOption) Option {
return func(graph *Graph) error {
override := sdk.SeriesOverride{}
for _, opt := range opts {
if err := opt(&override); err != nil {
return err
}
}
graph.Builder.GraphPanel.SeriesOverrides = append(graph.Builder.GraphPanel.SeriesOverrides, override)
return nil
}
}
// Legend defines what should be shown in the legend.
func Legend(opts ...LegendOption) Option {
return func(graph *Graph) error {
legend := sdk.Legend{Show: true}
for _, opt := range opts {
switch opt {
case Hide:
legend.Show = false
case AsTable:
legend.AlignAsTable = true
case ToTheRight:
legend.RightSide = true
case Min:
legend.Min = true
legend.Values = true
case Max:
legend.Max = true
legend.Values = true
case Avg:
legend.Avg = true
legend.Values = true
case Current:
legend.Current = true
legend.Values = true
case Total:
legend.Total = true
legend.Values = true
case NoNullSeries:
legend.HideEmpty = true
case NoZeroSeries:
legend.HideZero = true
default:
return errors.ErrInvalidArgument
}
}
graph.Builder.GraphPanel.Legend = legend
return nil
}
}