Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 0d25d6e

Browse files
Add constant template variable
1 parent ba94551 commit 0d25d6e

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

grafonnet/template.libsonnet

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,41 @@
287287
name: name,
288288
type: 'adhoc',
289289
},
290+
/**
291+
* [Constant variables](https://grafana.com/docs/grafana/latest/variables/variable-types/add-constant-variable/)
292+
* allow you to define a hidden constant.
293+
* This is useful for metric path prefixes for dashboards you want to share.
294+
* When you export a dashboard, constant variables are converted to import options.
295+
*
296+
* @name template.constant
297+
*
298+
* @param name Variable name.
299+
* @param value Variable value. You can enter letters, numbers, and symbols. You can even use wildcards if you use [raw format](https://grafana.com/docs/grafana/latest/variables/advanced-variable-format-options/#raw).
300+
* @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.
301+
* @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.
302+
*
303+
* @return A constant variable.
304+
*/
305+
constant(
306+
name,
307+
value='',
308+
label='',
309+
hide='',
310+
)::
311+
{
312+
current: [{
313+
selected: true,
314+
value: value,
315+
text: value,
316+
}],
317+
options: [{
318+
selected: true,
319+
value: value,
320+
text: value,
321+
}],
322+
hide: $.hide(hide),
323+
label: label,
324+
name: name,
325+
type: 'constant',
326+
},
290327
}

tests/template/constant.jsonnet

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local grafana = import 'grafonnet/grafana.libsonnet';
2+
local template = grafana.template;
3+
4+
{
5+
basic: template.constant(
6+
'project_name',
7+
value='myProject',
8+
label='Project',
9+
hide='variable'
10+
),
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"basic": {
3+
"current": [
4+
{
5+
"selected": true,
6+
"text": "myProject",
7+
"value": "myProject"
8+
}
9+
],
10+
"hide": 2,
11+
"label": "Project",
12+
"name": "project_name",
13+
"options": [
14+
{
15+
"selected": true,
16+
"text": "myProject",
17+
"value": "myProject"
18+
}
19+
],
20+
"type": "constant"
21+
}
22+
}

0 commit comments

Comments
 (0)