-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
graph.go
314 lines (271 loc) · 7.58 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
package graph
import (
"github.com/K-Phoen/grabana/alert"
"github.com/K-Phoen/grabana/axis"
"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/grafana-tools/sdk"
)
// Option represents an option that can be used to configure a graph panel.
type Option func(graph *Graph)
// 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
}
// New creates a new graph panel.
func New(title string, options ...Option) *Graph {
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...) {
opt(panel)
}
return panel
}
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) {
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
}
}
// 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) {
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,
})
}
}
// 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) {
graph.Builder.AddTarget(target.Builder)
}
}
// 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) {
graph.Builder.AddTarget(target.Builder)
}
}
// WithStackdriverTarget adds a stackdriver query to the graph.
func WithStackdriverTarget(target *stackdriver.Stackdriver) Option {
return func(graph *Graph) {
graph.Builder.AddTarget(target.Builder)
}
}
// DataSource sets the data source to be used by the graph.
func DataSource(source string) Option {
return func(graph *Graph) {
graph.Builder.Datasource = &source
}
}
// 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) {
graph.Builder.Span = span
}
}
// Height sets the height of the panel, in pixels. Example: "400px".
func Height(height string) Option {
return func(graph *Graph) {
graph.Builder.Height = &height
}
}
// Description annotates the current visualization with a human-readable description.
func Description(content string) Option {
return func(graph *Graph) {
graph.Builder.Description = &content
}
}
// Transparent makes the background transparent.
func Transparent() Option {
return func(graph *Graph) {
graph.Builder.Transparent = true
}
}
// LeftYAxis configures the left Y axis.
func LeftYAxis(opts ...axis.Option) Option {
return func(graph *Graph) {
graph.Builder.GraphPanel.Yaxes[0] = *axis.New(opts...).Builder
}
}
// RightYAxis configures the right Y axis.
func RightYAxis(opts ...axis.Option) Option {
return func(graph *Graph) {
graph.Builder.GraphPanel.Yaxes[1] = *axis.New(opts...).Builder
}
}
// XAxis configures the X axis.
func XAxis(opts ...axis.Option) Option {
return func(graph *Graph) {
graph.Builder.GraphPanel.Xaxis = *axis.New(opts...).Builder
}
}
// Alert creates an alert for this graph.
func Alert(name string, opts ...alert.Option) Option {
return func(graph *Graph) {
graph.Builder.Alert = alert.New(name, opts...).Builder
}
}
// Draw specifies how the graph will be drawn.
func Draw(modes ...DrawMode) Option {
return func(graph *Graph) {
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
}
}
}
}
// 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) {
graph.Builder.Fill = value
}
}
// 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) {
graph.Builder.Linewidth = value
}
}
// Staircase draws adjacent points as staircase.
func Staircase() Option {
return func(graph *Graph) {
graph.Builder.GraphPanel.SteppedLine = true
}
}
// PointRadius adjusts the size of points when Points are selected as Draw Mode.
func PointRadius(value float32) Option {
return func(graph *Graph) {
graph.Builder.Pointradius = value
}
}
// Null configures how null values are displayed.
func Null(mode NullValue) Option {
return func(graph *Graph) {
graph.Builder.GraphPanel.NullPointMode = string(mode)
}
}
// Legend defines what should be shown in the legend.
func Legend(opts ...LegendOption) Option {
return func(graph *Graph) {
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
}
}
graph.Builder.GraphPanel.Legend = legend
}
}