This repository has been archived by the owner on Apr 21, 2021. It is now read-only.
forked from grafana/grafonnet-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.libsonnet
289 lines (286 loc) · 14.1 KB
/
template.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
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
{
/**
* Creates a [template](https://grafana.com/docs/grafana/latest/variables/templates-and-variables/#templates) that can be added to a dashboard.
*
* @name template.new
*
* @param name Name of variable.
* @param datasource Template [datasource](https://grafana.com/docs/grafana/latest/variables/variable-types/add-data-source-variable/)
* @param query [Query expression](https://grafana.com/docs/grafana/latest/variables/variable-types/add-query-variable/) for the datasource.
* @param label (optional) Display name of the variable dropdown. If null, then the dropdown label will be the variable name.
* @param allValues (optional) Formatting for [multi-value variables](https://grafana.com/docs/grafana/latest/variables/formatting-multi-value-variables/#formatting-multi-value-variables)
* @param tagValuesQuery (default `''`) Group values into [selectable tags](https://grafana.com/docs/grafana/latest/variables/variable-value-tags/)
* @param current (default `null`) Can be `null`, `'all'` for all, or any other custom text value.
* @param hide (default `''`) `''`: the variable dropdown displays the variable Name or Label value. `'label'`: the variable dropdown only displays the selected variable value and a down arrow. Any other value: no variable dropdown is displayed on the dashboard.
* @param regex (default `''`) Regex expression to filter or capture specific parts of the names returned by your data source query. To see examples, refer to [Filter variables with regex](https://grafana.com/docs/grafana/latest/variables/filter-variables-with-regex/).
* @param refresh (default `'never'`) `'never'`: variables queries are cached and values are not updated. This is fine if the values never change, but problematic if they are dynamic and change a lot. `'load'`: Queries the data source every time the dashboard loads. This slows down dashboard loading, because the variable query needs to be completed before dashboard can be initialized. `'time'`: Queries the data source when the dashboard time range changes. Only use this option if your variable options query contains a time range filter or is dependent on the dashboard time range.
* @param includeAll (default `false`) Whether all value option is available or not.
* @param multi (default `false`) Whether multiple values can be selected or not from variable value list.
* @param sort (default `0`) `0`: Without Sort, `1`: Alphabetical (asc), `2`: Alphabetical (desc), `3`: Numerical (asc), `4`: Numerical (desc).
*
* @return A [template](https://grafana.com/docs/grafana/latest/variables/templates-and-variables/#templates)
*/
new(
name,
datasource,
query,
label=null,
allValues=null,
tagValuesQuery='',
current=null,
hide='',
regex='',
refresh='never',
includeAll=false,
multi=false,
sort=0,
)::
{
allValue: allValues,
current: $.current(current),
datasource: datasource,
includeAll: includeAll,
hide: $.hide(hide),
label: label,
multi: multi,
name: name,
options: [],
query: query,
refresh: $.refresh(refresh),
regex: regex,
sort: sort,
tagValuesQuery: tagValuesQuery,
tags: [],
tagsQuery: '',
type: 'query',
useTags: false,
},
/**
* Use an [interval variable](https://grafana.com/docs/grafana/latest/variables/variable-types/add-interval-variable/) to represent time spans such as '1m', '1h', '1d'. You can think of them as a dashboard-wide "group by time" command. Interval variables change how the data is grouped in the visualization. You can also use the Auto Option to return a set number of data points per time span.
* You can use an interval variable as a parameter to group by time (for InfluxDB), date histogram interval (for Elasticsearch), or as a summarize function parameter (for Graphite).
*
* @name template.interval
*
* @param name Variable name
* @param query Comma separated values without spacing of intervals available for selection. Add `'auto'` in the query to turn on the Auto Option. Ex: `'auto,5m,10m,20m'`.
* @param current Currently selected interval. Must be one of the values in the query. `'auto'` is allowed if defined in the query.
* @param hide (default `''`) `''`: the variable dropdown displays the variable Name or Label value. `'label'`: the variable dropdown only displays the selected variable value and a down arrow. Any other value: no variable dropdown is displayed on the dashboard.
* @param label (optional) Display name of the variable dropdown. If null, then the dropdown label will be the variable name.
* @param auto_count (default `300`) Valid only if `'auto'` is defined in query. Number of times the current time range will be divided to calculate the value, similar to the Max data points query option. For example, if the current visible time range is 30 minutes, then the auto interval groups the data into 30 one-minute increments. The default value is 30 steps.
* @param auto_min (default `'10s'`) Valid only if `'auto'` is defined in query. The minimum threshold below which the step count intervals will not divide the time. To continue the 30 minute example, if the minimum interval is set to `'2m'`, then Grafana would group the data into 15 two-minute increments.
*
* @return A new interval variable for templating.
*/
interval(
name,
query,
current,
hide='',
label=null,
auto_count=300,
auto_min='10s',
)::
{
current: $.current(current),
hide: $.hide(hide),
label: label,
name: name,
query: std.join(',', std.filter($.filterAuto, std.split(query, ','))),
refresh: 2,
type: 'interval',
auto: std.count(std.split(query, ','), 'auto') > 0,
auto_count: auto_count,
auto_min: auto_min,
},
hide(hide)::
if hide == '' then 0 else if hide == 'label' then 1 else 2,
current(current):: {
[if current != null then 'text']: current,
[if current != null then 'value']: if current == 'auto' then
'$__auto_interval'
else if current == 'all' then
'$__all'
else
current,
},
/**
* Data [source variables](https://grafana.com/docs/grafana/latest/variables/variable-types/add-data-source-variable/)
* allow you to quickly change the data source for an entire dashboard.
* They are useful if you have multiple instances of a data source, perhaps in different environments.
*
* @name template.datasource
*
* @param name Data source variable name. Ex: `'PROMETHEUS_DS'`.
* @param query Type of data source. Ex: `'prometheus'`.
* @param current Ex: `'Prometheus'`.
* @param hide (default `''`) `''`: the variable dropdown displays the variable Name or Label value. `'label'`: the variable dropdown only displays the selected variable value and a down arrow. Any other value: no variable dropdown is displayed on the dashboard.
* @param label (optional) Display name of the variable dropdown. If null, then the dropdown label will be the variable name.
* @param regex (default `''`) Regex filter for which data source instances to choose from in the variable value drop-down list. Leave this field empty to display all instances.
* @param refresh (default `'load'`) `'never'`: Variables queries are cached and values are not updated. This is fine if the values never change, but problematic if they are dynamic and change a lot. `'load'`: Queries the data source every time the dashboard loads. This slows down dashboard loading, because the variable query needs to be completed before dashboard can be initialized. `'time'`: Queries the data source when the dashboard time range changes. Only use this option if your variable options query contains a time range filter or is dependent on the dashboard time range.
*
* @return A [data source variable](https://grafana.com/docs/grafana/latest/variables/variable-types/add-data-source-variable/).
*/
datasource(
name,
query,
current,
hide='',
label=null,
regex='',
refresh='load',
):: {
current: $.current(current),
hide: $.hide(hide),
label: label,
name: name,
options: [],
query: query,
refresh: $.refresh(refresh),
regex: regex,
type: 'datasource',
},
refresh(refresh):: if refresh == 'never'
then
0
else if refresh == 'load'
then
1
else if refresh == 'time'
then
2
else
refresh,
filterAuto(str):: str != 'auto',
/**
* Use a [custom variable](https://grafana.com/docs/grafana/latest/variables/variable-types/add-custom-variable/)
* for values that do not change.
*
* @name template.custom
* This might be numbers, strings, or even other variables.
* @param name Variable name
* @param query Comma separated without spacing list of selectable values.
* @param current Selected value
* @param refresh (default `'never'`) `'never'`: Variables queries are cached and values are not updated. This is fine if the values never change, but problematic if they are dynamic and change a lot. `'load'`: Queries the data source every time the dashboard loads. This slows down dashboard loading, because the variable query needs to be completed before dashboard can be initialized. `'time'`: Queries the data source when the dashboard time range changes. Only use this option if your variable options query contains a time range filter or is dependent on the dashboard time range.
* @param label (default `''`) Display name of the variable dropdown. If you don’t enter a display name, then the dropdown label will be the variable name.
* @param valuelabels (default `{}`) Display names for values defined in query. For example, if `query='new,old'`, then you may display them as follows `valuelabels={new: 'nouveau', old: 'ancien'}`.
* @param multi (default `false`) Whether multiple values can be selected or not from variable value list.
* @param allValues (optional) Formatting for [multi-value variables](https://grafana.com/docs/grafana/latest/variables/formatting-multi-value-variables/#formatting-multi-value-variables)
* @param includeAll (default `false`) Whether all value option is available or not.
* @param hide (default `''`) `''`: the variable dropdown displays the variable Name or Label value. `'label'`: the variable dropdown only displays the selected variable value and a down arrow. Any other value: no variable dropdown is displayed on the dashboard.
*
* @return A custom variable.
*/
custom(
name,
query,
current,
refresh='never',
label='',
valuelabels={},
multi=false,
allValues=null,
includeAll=false,
hide='',
)::
{
// self has dynamic scope, so self may not be myself below.
// '$' can't be used neither as this object is not top-level object.
local custom = self,
allValue: allValues,
current: {
// Both 'all' and 'All' are accepted for consistency.
value: if includeAll && (current == 'All' || current == 'all') then
if multi then ['$__all'] else '$__all'
else
current,
text: if std.isArray(current) then
std.join(' + ', std.map(custom.valuelabel, current))
else
custom.valuelabel(current),
[if multi then 'selected']: true,
},
options: std.map(self.option, self.query_array(query)),
hide: $.hide(hide),
includeAll: includeAll,
label: label,
refresh: $.refresh(refresh),
multi: multi,
name: name,
query: query,
type: 'custom',
valuelabel(value):: if value in valuelabels then
valuelabels[value]
else value,
option(option):: {
text: custom.valuelabel(option),
value: if includeAll && option == 'All' then '$__all' else option,
[if multi then 'selected']: if multi && std.isArray(current) then
std.member(current, option)
else if multi then
current == option
else
null,
},
query_array(query):: std.split(
if includeAll then 'All,' + query else query, ','
),
},
/**
* [Text box variables](https://grafana.com/docs/grafana/latest/variables/variable-types/add-text-box-variable/)
* display a free text input field with an optional default value.
* This is the most flexible variable, because you can enter any value.
* Use this type of variable if you have metrics with high cardinality or if you want to
* update multiple panels in a dashboard at the same time.
*
* @name template.text
*
* @param name Variable name.
* @param label (default `''`) Display name of the variable dropdown. If you don’t enter a display name, then the dropdown label will be the variable name.
*
* @return A text box variable.
*/
text(
name,
label=''
)::
{
current: {
selected: false,
text: '',
value: '',
},
name: name,
label: label,
query: '',
type: 'textbox',
},
/**
* [Ad hoc filters](https://grafana.com/docs/grafana/latest/variables/variable-types/add-ad-hoc-filters/)
* allow you to add key/value filters that are automatically added to all metric queries
* that use the specified data source. Unlike other variables, you do not use ad hoc filters in queries.
* Instead, you use ad hoc filters to write filters for existing queries.
* Note: Ad hoc filter variables only work with InfluxDB, Prometheus, and Elasticsearch data sources.
*
* @name template.adhoc
*
* @param name Variable name.
* @param datasource Target data source
* @param label (optional) Display name of the variable dropdown. If you don’t enter a display name, then the dropdown label will be the variable name.
* @param hide (default `''`) `''`: the variable dropdown displays the variable Name or Label value. `'label'`: the variable dropdown only displays the selected variable value and a down arrow. Any other value: no variable dropdown is displayed on the dashboard.
*
* @return An ad hoc filter
*/
adhoc(
name,
datasource,
label=null,
hide='',
)::
{
datasource: datasource,
hide: $.hide(hide),
label: label,
name: name,
type: 'adhoc',
},
}