Skip to content

Commit

Permalink
Support multi flag on multiple variable types
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenlj authored and K-Phoen committed Feb 19, 2023
1 parent f608f86 commit 3f50faa
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
37 changes: 37 additions & 0 deletions decoder/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestUnmarshalYAML(t *testing.T) {
generalOptions(),
tagAnnotations(),
variables(),
variablesMulti(),
textPanel(),
graphPanel(),
singleStatPanel(),
Expand Down Expand Up @@ -391,6 +392,42 @@ variables:
}
}

func variablesMulti() testCase {
yaml := `title: Awesome dashboard
timezone: browser
variables:
- query:
name: status
label: HTTP status
datasource: prometheus-default
include_all: true
default_all: true
request: "label_values(prometheus_http_requests_total, code)"
multi: true
- custom:
name: vX
label: vX
default: v1
values_map:
v1: v1
multi: true
- datasource:
name: datasource
label: datasource
type: prometheus
regex: /applications-(?!staging)/
include_all: true
multi: true
`
return testCase{
name: "variables_multi",
yaml: yaml,
expectedGrafanaJSON: "variables_multi.json",
}
}

func textPanel() testCase {
yaml := `title: Awesome dashboard
Expand Down
107 changes: 107 additions & 0 deletions decoder/testdata/variables_multi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"slug": "",
"title": "Awesome dashboard",
"originalTitle": "",
"tags": null,
"style": "dark",
"timezone": "browser",
"editable": false,
"hideControls": false,
"sharedCrosshair": false,
"templating": {
"list": [
{
"name": "status",
"type": "query",
"datasource": "prometheus-default",
"refresh": 1,
"options": [
{
"text": "All",
"value": "$__all",
"selected": false
}
],
"includeAll": true,
"allFormat": "",
"allValue": "",
"multi": true,
"multiFormat": "",
"query": "label_values(prometheus_http_requests_total, code)",
"regex": "",
"current": {
"text": ["All"],
"value": "$__all"
},
"label": "HTTP status",
"hide": 0,
"sort": 0
},
{
"name": "vX",
"type": "custom",
"datasource": null,
"refresh": false,
"options": [
{
"text": "v1",
"value": "v1",
"selected": false
}
],
"includeAll": false,
"allFormat": "",
"allValue": "",
"multi": true,
"multiFormat": "",
"query": "v1",
"regex": "",
"current": {
"text": ["v1"],
"value": "v1"
},
"label": "vX",
"hide": 0,
"sort": 0
},
{
"name": "datasource",
"type": "datasource",
"datasource": null,
"refresh": 1,
"options": [
{
"text": "All",
"value": "$__all",
"selected": false
}
],
"includeAll": true,
"allFormat": "",
"allValue": "",
"multi": true,
"multiFormat": "",
"query": "prometheus",
"regex": "/applications-(?!staging)/",
"current": {
"text": null,
"value": null
},
"label": "datasource",
"hide": 0,
"sort": 0
}
]
},
"annotations": {"list": null},
"links": null,
"panels": null,
"rows": [],
"time": {"from": "now-3h", "to": "now"},
"timepicker": {
"refresh_intervals": ["5s","10s","30s","1m","5m","15m","30m","1h","2h","1d"],
"time_options": ["5m","15m","1h","6h","12h","24h","2d","7d","30d"]
},
"schemaVersion": 0,
"version": 0
}
12 changes: 12 additions & 0 deletions decoder/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type VariableCustom struct {
IncludeAll bool `yaml:"include_all"`
AllValue string `yaml:"all_value,omitempty"`
Hide string `yaml:",omitempty"`
Multi bool `yaml:",omitempty"`
}

func (variable *VariableCustom) toOption() (dashboard.Option, error) {
Expand All @@ -104,6 +105,9 @@ func (variable *VariableCustom) toOption() (dashboard.Option, error) {
if variable.IncludeAll {
opts = append(opts, custom.IncludeAll())
}
if variable.Multi {
opts = append(opts, custom.Multi())
}

switch variable.Hide {
case "":
Expand Down Expand Up @@ -167,6 +171,7 @@ type VariableQuery struct {
DefaultAll bool `yaml:"default_all"`
AllValue string `yaml:"all_value,omitempty"`
Hide string `yaml:",omitempty"`
Multi bool `yaml:",omitempty"`
}

func (variable *VariableQuery) toOption() (dashboard.Option, error) {
Expand All @@ -192,6 +197,9 @@ func (variable *VariableQuery) toOption() (dashboard.Option, error) {
if variable.DefaultAll {
opts = append(opts, query.DefaultAll())
}
if variable.Multi {
opts = append(opts, query.Multi())
}

switch variable.Hide {
case "":
Expand All @@ -217,6 +225,7 @@ type VariableDatasource struct {
Regex string
IncludeAll bool `yaml:"include_all"`
Hide string `yaml:",omitempty"`
Multi bool `yaml:",omitempty"`
}

func (variable *VariableDatasource) toOption() (dashboard.Option, error) {
Expand All @@ -233,6 +242,9 @@ func (variable *VariableDatasource) toOption() (dashboard.Option, error) {
if variable.IncludeAll {
opts = append(opts, datasource.IncludeAll())
}
if variable.Multi {
opts = append(opts, datasource.Multi())
}

switch variable.Hide {
case "":
Expand Down

0 comments on commit 3f50faa

Please sign in to comment.