-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
heatmap.go
347 lines (281 loc) · 8.23 KB
/
heatmap.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
package heatmap
import (
"fmt"
"github.com/K-Phoen/grabana/errors"
"github.com/K-Phoen/grabana/heatmap/axis"
"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"
)
// DataFormatMode represents the data format modes.
type DataFormatMode string
const (
// Grafana does the bucketing by going through all time series values
TimeSeriesBuckets DataFormatMode = "tsbuckets"
// Each time series already represents a Y-Axis bucket.
TimeSeries DataFormatMode = "timeseries"
)
// LegendOption allows to configure a legend.
type LegendOption uint16
const (
// Hide keeps the legend from being displayed.
Hide LegendOption = iota
)
// Option represents an option that can be used to configure a heatmap panel.
type Option func(stat *Heatmap) error
// Heatmap represents a heatmap panel.
type Heatmap struct {
Builder *sdk.Panel
}
// New creates a new heatmap panel.
func New(title string, options ...Option) (*Heatmap, error) {
panel := &Heatmap{Builder: sdk.NewHeatmap(title)}
panel.Builder.IsNew = false
panel.Builder.HeatmapPanel.Cards = struct {
CardPadding *float64 `json:"cardPadding"`
CardRound *float64 `json:"cardRound"`
}{}
panel.Builder.HeatmapPanel.Color = struct {
CardColor string `json:"cardColor"`
ColorScale string `json:"colorScale"`
ColorScheme string `json:"colorScheme"`
Exponent float64 `json:"exponent"`
Min *float64 `json:"min,omitempty"`
Max *float64 `json:"max,omitempty"`
Mode string `json:"mode"`
}{
CardColor: "#b4ff00",
ColorScale: "sqrt",
ColorScheme: "interpolateSpectral",
Exponent: 0.5,
Mode: "spectrum",
}
panel.Builder.HeatmapPanel.Legend = struct {
Show bool `json:"show"`
}{
Show: true,
}
panel.Builder.HeatmapPanel.Tooltip = struct {
Show bool `json:"show"`
ShowHistogram bool `json:"showHistogram"`
}{
Show: true,
ShowHistogram: true,
}
panel.Builder.HeatmapPanel.XAxis = struct {
Show bool `json:"show"`
}{
Show: true,
}
panel.Builder.HeatmapPanel.YBucketBound = "auto"
for _, opt := range append(defaults(), options...) {
if err := opt(panel); err != nil {
return nil, err
}
}
return panel, nil
}
func defaults() []Option {
return []Option{
Span(6),
DataFormat(TimeSeriesBuckets),
HideZeroBuckets(),
HighlightCards(),
defaultYAxis(),
}
}
func defaultYAxis() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.YAxis = *axis.New().Builder
return nil
}
}
// Links adds links to be displayed on this panel.
func Links(panelLinks ...links.Link) Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.Links = make([]sdk.Link, 0, len(panelLinks))
for _, link := range panelLinks {
heatmap.Builder.Links = append(heatmap.Builder.Links, link.Builder)
}
return nil
}
}
// DataSource sets the data source to be used by the panel.
func DataSource(source string) Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.Datasource = &sdk.DatasourceRef{LegacyName: source}
return nil
}
}
// DataFormat sets how the data should be interpreted.
func DataFormat(format DataFormatMode) Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.DataFormat = string(format)
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(heatmap *Heatmap) error {
heatmap.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(heatmap *Heatmap) error {
heatmap.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(heatmap *Heatmap) error {
heatmap.Builder.AddTarget(target.Builder)
return nil
}
}
// WithStackdriverTarget adds a stackdriver query to the graph.
func WithStackdriverTarget(target *stackdriver.Stackdriver) Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.AddTarget(target.Builder)
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(heatmap *Heatmap) error {
if span < 1 || span > 12 {
return fmt.Errorf("span must be between 1 and 12: %w", errors.ErrInvalidArgument)
}
heatmap.Builder.Span = span
return nil
}
}
// Height sets the height of the panel, in pixels. Example: "400px".
func Height(height string) Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.Height = &height
return nil
}
}
// Description annotates the current visualization with a human-readable description.
func Description(content string) Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.Description = &content
return nil
}
}
// Transparent makes the background transparent.
func Transparent() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.Transparent = true
return nil
}
}
// Legend defines what should be shown in the legend.
func Legend(opts ...LegendOption) Option {
return func(heatmap *Heatmap) error {
for _, opt := range opts {
if opt == Hide {
heatmap.Builder.HeatmapPanel.Legend.Show = false
}
}
return nil
}
}
// ShowZeroBuckets forces the display of "zero" buckets.
func ShowZeroBuckets() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.HideZeroBuckets = false
return nil
}
}
// HideZeroBuckets hides "zero" buckets.
func HideZeroBuckets() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.HideZeroBuckets = true
return nil
}
}
// HighlightCards highlights bucket cards.
func HighlightCards() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.HighlightCards = true
return nil
}
}
// NoHighlightCards disables the highlighting of bucket cards.
func NoHighlightCards() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.HighlightCards = false
return nil
}
}
// ReverseYBuckets reverses the order of bucket on the Y-axis.
func ReverseYBuckets() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.ReverseYBuckets = true
return nil
}
}
// HideTooltip prevents the tooltip from being displayed.
func HideTooltip() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.Tooltip.Show = false
return nil
}
}
// HideTooltipHistogram prevents the histograms from being displayed in tooltips.
// Histogram represents the distribution of the bucket values for the specific timestamp.
func HideTooltipHistogram() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.Tooltip.ShowHistogram = false
return nil
}
}
// TooltipDecimals sets the number of decimals to be displayed in tooltips.
func TooltipDecimals(decimals int) Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.TooltipDecimals = decimals
return nil
}
}
// HideXAxis prevents the X-axis from being displayed.
func HideXAxis() Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.XAxis.Show = false
return nil
}
}
// YAxis configures the Y axis.
func YAxis(opts ...axis.Option) Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.HeatmapPanel.YAxis = *axis.New(opts...).Builder
return nil
}
}
// Repeat configures repeating a panel for a variable
func Repeat(repeat string) Option {
return func(heatmap *Heatmap) error {
heatmap.Builder.Repeat = &repeat
return nil
}
}