-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
heatmap.go
277 lines (239 loc) · 7.13 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
package heatmap
import (
"github.com/K-Phoen/grabana/heatmap/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/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)
// Heatmap represents a heatmap panel.
type Heatmap struct {
Builder *sdk.Panel
}
// New creates a new heatmap panel.
func New(title string, options ...Option) *Heatmap {
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...) {
opt(panel)
}
return panel
}
func defaults() []Option {
return []Option{
Span(6),
DataFormat(TimeSeriesBuckets),
HideZeroBuckets(),
HighlightCards(),
defaultYAxis(),
}
}
func defaultYAxis() Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.YAxis = *axis.New().Builder
}
}
// DataSource sets the data source to be used by the panel.
func DataSource(source string) Option {
return func(heatmap *Heatmap) {
heatmap.Builder.Datasource = &source
}
}
// DataFormat sets how the data should be interpreted.
func DataFormat(format DataFormatMode) Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.DataFormat = string(format)
}
}
// 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) {
heatmap.Builder.AddTarget(&sdk.Target{
RefID: target.Ref,
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(heatmap *Heatmap) {
heatmap.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(heatmap *Heatmap) {
heatmap.Builder.AddTarget(target.Builder)
}
}
// WithStackdriverTarget adds a stackdriver query to the graph.
func WithStackdriverTarget(target *stackdriver.Stackdriver) Option {
return func(heatmap *Heatmap) {
heatmap.Builder.AddTarget(target.Builder)
}
}
// 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) {
heatmap.Builder.Span = span
}
}
// Height sets the height of the panel, in pixels. Example: "400px".
func Height(height string) Option {
return func(heatmap *Heatmap) {
heatmap.Builder.Height = &height
}
}
// Description annotates the current visualization with a human-readable description.
func Description(content string) Option {
return func(heatmap *Heatmap) {
heatmap.Builder.Description = &content
}
}
// Transparent makes the background transparent.
func Transparent() Option {
return func(heatmap *Heatmap) {
heatmap.Builder.Transparent = true
}
}
// Legend defines what should be shown in the legend.
func Legend(opts ...LegendOption) Option {
return func(heatmap *Heatmap) {
for _, opt := range opts {
if opt == Hide {
heatmap.Builder.HeatmapPanel.Legend.Show = false
}
}
}
}
// ShowZeroBuckets forces the display of "zero" buckets.
func ShowZeroBuckets() Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.HideZeroBuckets = false
}
}
// HideZeroBuckets hides "zero" buckets.
func HideZeroBuckets() Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.HideZeroBuckets = true
}
}
// HighlightCards highlights bucket cards.
func HighlightCards() Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.HighlightCards = true
}
}
// NoHighlightCards disables the highlighting of bucket cards.
func NoHighlightCards() Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.HighlightCards = false
}
}
// ReverseYBuckets reverses the order of bucket on the Y-axis.
func ReverseYBuckets() Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.ReverseYBuckets = true
}
}
// HideTooltip prevents the tooltip from being displayed.
func HideTooltip() Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.Tooltip.Show = false
}
}
// 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) {
heatmap.Builder.HeatmapPanel.Tooltip.ShowHistogram = false
}
}
// TooltipDecimals sets the number of decimals to be displayed in tooltips.
func TooltipDecimals(decimals int) Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.TooltipDecimals = decimals
}
}
// HideXAxis prevents the X-axis from being displayed.
func HideXAxis() Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.XAxis.Show = false
}
}
// YAxis configures the Y axis.
func YAxis(opts ...axis.Option) Option {
return func(heatmap *Heatmap) {
heatmap.Builder.HeatmapPanel.YAxis = *axis.New(opts...).Builder
}
}
// Repeat configures repeating a panel for a variable
func Repeat(repeat string) Option {
return func(heatmap *Heatmap) {
heatmap.Builder.Repeat = &repeat
}
}