forked from grafana/grafonnet-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat_panel.libsonnet
222 lines (209 loc) · 7.79 KB
/
stat_panel.libsonnet
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
{
/**
* Creates a [stat panel](https://grafana.com/docs/grafana/latest/panels/visualizations/stat-panel/).
*
* @name statPanel.new
*
* @param title Panel title.
* @param description (optional) Panel description.
* @param transparent (default `false`) Whether to display the panel without a background.
* @param datasource (optional) Panel datasource.
* @param allValues (default `false`) Show all values instead of reducing to one.
* @param valueLimit (optional) Limit of values in all values mode.
* @param reducerFunction (default `'mean'`) Function to use to reduce values to when using single value.
* @param fields (default `''`) Fields that should be included in the panel.
* @param orientation (default `'auto'`) Stacking direction in case of multiple series or fields.
* @param colorMode (default `'value'`) 'value' or 'background'.
* @param graphMode (default `'area'`) 'none' or 'area' to enable sparkline mode.
* @param textMode (default `'auto'`) Control if name and value is displayed or just name.
* @param justifyMode (default `'auto'`) 'auto' or 'center'.
* @param unit (default `'none'`) Panel unit field option.
* @param min (optional) Leave empty to calculate based on all values.
* @param max (optional) Leave empty to calculate based on all values.
* @param decimals (optional) Number of decimal places to show.
* @param displayName (optional) Change the field or series name.
* @param noValue (optional) What to show when there is no value.
* @param thresholdsMode (default `'absolute'`) 'absolute' or 'percentage'.
* @param timeFrom (optional) Override the relative time range.
* @param repeat (optional) Name of variable that should be used to repeat this panel.
* @param repeatDirection (default `'h'`) 'h' for horizontal or 'v' for vertical.
* @param maxPerRow (optional) Maximum panels per row in repeat mode.
* @param pluginVersion (default `'7'`) Plugin version the panel should be modeled for. This has been tested with the default, '7', and '6.7'.
*
* @method addTarget(target) Adds a target object.
* @method addTargets(targets) Adds an array of targets.
* @method addLink(link) Adds a [panel link](https://grafana.com/docs/grafana/latest/linking/panel-links/). Argument format: `{ title: 'Link Title', url: 'https://...', targetBlank: true }`.
* @method addLinks(links) Adds an array of links.
* @method addThreshold(step) Adds a [threshold](https://grafana.com/docs/grafana/latest/panels/thresholds/) step. Argument format: `{ color: 'green', value: 0 }`.
* @method addThresholds(steps) Adds an array of threshold steps.
* @method addMapping(mapping) Adds a value mapping.
* @method addMappings(mappings) Adds an array of value mappings.
* @method addDataLink(link) Adds a data link.
* @method addDataLinks(links) Adds an array of data links.
*/
new(
title,
description=null,
transparent=false,
datasource=null,
allValues=false,
valueLimit=null,
reducerFunction='mean',
fields='',
orientation='auto',
colorMode='value',
graphMode='area',
textMode='auto',
justifyMode='auto',
unit='none',
min=null,
max=null,
decimals=null,
displayName=null,
noValue=null,
thresholdsMode='absolute',
timeFrom=null,
repeat=null,
repeatDirection='h',
maxPerRow=null,
pluginVersion='7',
):: {
type: 'stat',
title: title,
[if description != null then 'description']: description,
transparent: transparent,
datasource: datasource,
targets: [],
links: [],
[if repeat != null then 'repeat']: repeat,
[if repeat != null then 'repeatDirection']: repeatDirection,
[if timeFrom != null then 'timeFrom']: timeFrom,
[if repeat != null then 'maxPerRow']: maxPerRow,
// targets
_nextTarget:: 0,
addTarget(target):: self {
local nextTarget = super._nextTarget,
_nextTarget: nextTarget + 1,
targets+: [target { refId: std.char(std.codepoint('A') + nextTarget) }],
},
addTargets(targets):: std.foldl(function(p, t) p.addTarget(t), targets, self),
// links
addLink(link):: self {
links+: [link],
},
addLinks(links):: std.foldl(function(p, l) p.addLink(l), links, self),
pluginVersion: pluginVersion,
} + (
if pluginVersion >= '7' then {
options: {
reduceOptions: {
values: allValues,
[if allValues && valueLimit != null then 'limit']: valueLimit,
calcs: [
reducerFunction,
],
fields: fields,
},
orientation: orientation,
colorMode: colorMode,
graphMode: graphMode,
justifyMode: justifyMode,
textMode: textMode,
},
fieldConfig: {
defaults: {
unit: unit,
[if min != null then 'min']: min,
[if max != null then 'max']: max,
[if decimals != null then 'decimals']: decimals,
[if displayName != null then 'displayName']: displayName,
[if noValue != null then 'noValue']: noValue,
thresholds: {
mode: thresholdsMode,
steps: [],
},
mappings: [],
links: [],
},
},
// thresholds
addThreshold(step):: self {
fieldConfig+: { defaults+: { thresholds+: { steps+: [step] } } },
},
// mappings
_nextMapping:: 0,
addMapping(mapping):: self {
local nextMapping = super._nextMapping,
_nextMapping: nextMapping + 1,
fieldConfig+: { defaults+: { mappings+: [mapping { id: nextMapping }] } },
},
// data links
addDataLink(link):: self {
fieldConfig+: { defaults+: { links+: [link] } },
},
// Overrides
addOverride(
matcher=null,
properties=null,
):: self {
fieldConfig+: {
overrides+: [
{
[if matcher != null then 'matcher']: matcher,
[if properties != null then 'properties']: properties,
},
],
},
},
addOverrides(overrides):: std.foldl(function(p, o) p.addOverride(o.matcher, o.properties), overrides, self),
} else {
options: {
fieldOptions: {
values: allValues,
[if allValues && valueLimit != null then 'limit']: valueLimit,
calcs: [
reducerFunction,
],
fields: fields,
defaults: {
unit: unit,
[if min != null then 'min']: min,
[if max != null then 'max']: max,
[if decimals != null then 'decimals']: decimals,
[if displayName != null then 'displayName']: displayName,
[if noValue != null then 'noValue']: noValue,
thresholds: {
mode: thresholdsMode,
steps: [],
},
mappings: [],
links: [],
},
},
orientation: orientation,
colorMode: colorMode,
graphMode: graphMode,
justifyMode: justifyMode,
},
// thresholds
addThreshold(step):: self {
options+: { fieldOptions+: { defaults+: { thresholds+: { steps+: [step] } } } },
},
// mappings
_nextMapping:: 0,
addMapping(mapping):: self {
local nextMapping = super._nextMapping,
_nextMapping: nextMapping + 1,
options+: { fieldOptions+: { defaults+: { mappings+: [mapping { id: nextMapping }] } } },
},
// data links
addDataLink(link):: self {
options+: { fieldOptions+: { defaults+: { links+: [link] } } },
},
}
) + {
addThresholds(steps):: std.foldl(function(p, s) p.addThreshold(s), steps, self),
addMappings(mappings):: std.foldl(function(p, m) p.addMapping(m), mappings, self),
addDataLinks(links):: std.foldl(function(p, l) p.addDataLink(l), links, self),
},
}