Current implementation of custom template variable in grafana only expects cofiguration as a comma separated list of values, which is used later in dropdown widget ( component).
This works nicely in simple cases, but not if one wants to display user-friendly text in dropdown and use internal var value in queries and human-friendly - in label or title.
Proposal: Instead of this sample definition of template variable $timeshift:
{
"hide": 0,
"includeAll": false,
"label": "Days ago",
"multi": false,
"name": "timeshift",
"options": [
{
"selected": false,
"text": "7",
"value": "7"
},
{
"selected": true,
"text": "14",
"value": "14"
}
],
"query": "7, 14",
"type": "custom"
}
Iâd like to have something like this:
{
"hide": 0,
"includeAll": false,
"label": "Days ago",
"multi": false,
"name": "timeshift",
"options": [
{
"selected": false,
"text": "1 week",
"value": "7"
},
{
"selected": true,
"text": "two weeks",
"value": "14"
}
],
"query": "7: 1 week, 14: two weeks", //possible format for key-value list string
"type": "custom"
}
And then use $timeshift.value in query like:
SELECT something AS metric_[[timeshift.text]]_ago FROM table WHERE d>now()-[[timeshift.value]]
"query": "7, 1 week; 14, two weeks", //alternative format for key-value list string definition in form
Ultimately, this might be extended to have required standard (value, text) attributes plus optional arbitrary attributes in âoptionsâ entry:
"options": [
{
"selected": false,
"text": "1 week",
"value": "7",
"some_custom_attribute": "This is a custom attribute",
...
},
.....
and âqueryâ = some stringified version of âoptionsâ if needed.
Existing widget allows to enter values not listed in options and use text=value. With a proposed extended format missing attribute can be set to an empty string or entered value string in such case.