Skip to content

Commit

Permalink
Merge pull request #206 from mortenlj/multi-variables
Browse files Browse the repository at this point in the history
Support multi flag on multiple variable types (YAML)
  • Loading branch information
K-Phoen authored Feb 19, 2023
2 parents f608f86 + 0756c91 commit 24710ed
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/builder-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func main() {
),
dashboard.VariableAsCustom(
"vX",
custom.Multi(),
custom.Multiple(),
custom.IncludeAll(),
custom.Values(map[string]string{
"v1": "v1",
Expand Down
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(),
variablesMultiple(),
textPanel(),
graphPanel(),
singleStatPanel(),
Expand Down Expand Up @@ -391,6 +392,42 @@ variables:
}
}

func variablesMultiple() 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)"
multiple: true
- custom:
name: vX
label: vX
default: v1
values_map:
v1: v1
multiple: true
- datasource:
name: datasource
label: datasource
type: prometheus
regex: /applications-(?!staging)/
include_all: true
multiple: true
`
return testCase{
name: "variables_multiple",
yaml: yaml,
expectedGrafanaJSON: "variables_multiple.json",
}
}

func textPanel() testCase {
yaml := `title: Awesome dashboard
Expand Down
107 changes: 107 additions & 0 deletions decoder/testdata/variables_multiple.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"`
Multiple 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.Multiple {
opts = append(opts, custom.Multiple())
}

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"`
Multiple 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.Multiple {
opts = append(opts, query.Multiple())
}

switch variable.Hide {
case "":
Expand All @@ -217,6 +225,7 @@ type VariableDatasource struct {
Regex string
IncludeAll bool `yaml:"include_all"`
Hide string `yaml:",omitempty"`
Multiple 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.Multiple {
opts = append(opts, datasource.Multiple())
}

switch variable.Hide {
case "":
Expand Down
4 changes: 2 additions & 2 deletions variable/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func Hide() Option {
}
}

// Multi allows several values to be selected.
func Multi() Option {
// Multiple allows several values to be selected.
func Multiple() Option {
return func(custom *Custom) {
custom.Builder.Multi = true
}
Expand Down
2 changes: 1 addition & 1 deletion variable/custom/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestVariableCanBeHidden(t *testing.T) {
func TestMultipleVariablesCanBeSelected(t *testing.T) {
req := require.New(t)

panel := New("", Multi())
panel := New("", Multiple())

req.True(panel.Builder.Multi)
}
Expand Down
4 changes: 2 additions & 2 deletions variable/datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func Hide() Option {
}
}

// Multi allows several values to be selected.
func Multi() Option {
// Multiple allows several values to be selected.
func Multiple() Option {
return func(query *Datasource) {
query.Builder.Multi = true
}
Expand Down
2 changes: 1 addition & 1 deletion variable/datasource/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestVariableCanBeHidden(t *testing.T) {
func TestMultipleVariablesCanBeSelected(t *testing.T) {
req := require.New(t)

panel := New("", Multi())
panel := New("", Multiple())

req.True(panel.Builder.Multi)
}
Expand Down
4 changes: 2 additions & 2 deletions variable/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func Hide() Option {
}
}

// Multi allows several values to be selected.
func Multi() Option {
// Multiple allows several values to be selected.
func Multiple() Option {
return func(query *Query) {
query.Builder.Multi = true
}
Expand Down
2 changes: 1 addition & 1 deletion variable/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestVariableCanBeHidden(t *testing.T) {
func TestMultipleVariablesCanBeSelected(t *testing.T) {
req := require.New(t)

panel := New("", Multi())
panel := New("", Multiple())

req.True(panel.Builder.Multi)
}
Expand Down

0 comments on commit 24710ed

Please sign in to comment.