From f3eb3fa7738fec2542ca72e4ec281bd791ea0394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sun, 9 Feb 2020 16:50:15 +0100 Subject: [PATCH] Fix variables definition --- cmd/example/main.go | 2 +- variable/constant/constant.go | 29 ++++++++++++- variable/constant/constant_test.go | 1 + variable/custom/custom.go | 65 +++++++++++++++++++++--------- variable/interval/interval.go | 3 +- 5 files changed, 78 insertions(+), 22 deletions(-) diff --git a/cmd/example/main.go b/cmd/example/main.go index e6da323a..3b443576 100644 --- a/cmd/example/main.go +++ b/cmd/example/main.go @@ -84,7 +84,7 @@ func main() { "95th": "95", "99th": "99", }), - constant.Default("80th"), + constant.Default("80"), ), grabana.VariableAsCustom( "vX", diff --git a/variable/constant/constant.go b/variable/constant/constant.go index dd4e3afb..34236d41 100644 --- a/variable/constant/constant.go +++ b/variable/constant/constant.go @@ -1,6 +1,8 @@ package constant import ( + "strings" + "github.com/grafana-tools/sdk" ) @@ -10,9 +12,30 @@ type Option func(constant *Constant) // ValuesMap represent a "label" to "value" map of options for a constant variable. type ValuesMap map[string]string +func (values ValuesMap) asQuery() string { + valuesList := make([]string, 0, len(values)) + + for _, value := range values { + valuesList = append(valuesList, value) + } + + return strings.Join(valuesList, ",") +} + +func (values ValuesMap) labelFor(value string) string { + for label, val := range values { + if val == value { + return label + } + } + + return value +} + // Constant represents a "constant" templated variable. type Constant struct { Builder sdk.TemplateVar + values ValuesMap } // New creates a new "constant" templated variable. @@ -39,6 +62,9 @@ func Values(values ValuesMap) Option { Value: value, }) } + + constant.values = values + constant.Builder.Query = values.asQuery() } } @@ -46,7 +72,8 @@ func Values(values ValuesMap) Option { func Default(value string) Option { return func(constant *Constant) { constant.Builder.Current = sdk.Current{ - Text: value, + Text: constant.values.labelFor(value), + Value: value, } } } diff --git a/variable/constant/constant_test.go b/variable/constant/constant_test.go index a7f2d539..80a6c30a 100644 --- a/variable/constant/constant_test.go +++ b/variable/constant/constant_test.go @@ -53,6 +53,7 @@ func TestDefaultValueCanBeSet(t *testing.T) { panel := New("const", Default("99th")) req.Equal("99th", panel.Builder.Current.Text) + req.Equal("99th", panel.Builder.Current.Value) } func TestLabelCanBeHidden(t *testing.T) { diff --git a/variable/custom/custom.go b/variable/custom/custom.go index 263ae116..37e6dfb9 100644 --- a/variable/custom/custom.go +++ b/variable/custom/custom.go @@ -1,6 +1,8 @@ package custom import ( + "strings" + "github.com/grafana-tools/sdk" ) @@ -10,80 +12,105 @@ type Option func(constant *Custom) // ValuesMap represent a "label" to "value" map of options for a custom variable. type ValuesMap map[string]string +func (values ValuesMap) asQuery() string { + valuesList := make([]string, 0, len(values)) + + for _, value := range values { + valuesList = append(valuesList, value) + } + + return strings.Join(valuesList, ",") +} + +func (values ValuesMap) labelFor(value string) string { + for label, val := range values { + if val == value { + return label + } + } + + return value +} + // Custom represents a "custom" templated variable. type Custom struct { Builder sdk.TemplateVar + values ValuesMap } // New creates a new "custom" templated variable. func New(name string, options ...Option) *Custom { - constant := &Custom{Builder: sdk.TemplateVar{ + custom := &Custom{Builder: sdk.TemplateVar{ Name: name, Label: name, Type: "custom", }} for _, opt := range options { - opt(constant) + opt(custom) } - return constant + return custom } // Values sets the possible values for the variable. func Values(values ValuesMap) Option { - return func(constant *Custom) { + return func(custom *Custom) { for label, value := range values { - constant.Builder.Options = append(constant.Builder.Options, sdk.Option{ + custom.Builder.Options = append(custom.Builder.Options, sdk.Option{ Text: label, Value: value, }) } + + custom.values = values + custom.Builder.Query = values.asQuery() } } // Default sets the default value of the variable. func Default(value string) Option { - return func(constant *Custom) { - constant.Builder.Current = sdk.Current{ - Text: value, + return func(custom *Custom) { + custom.Builder.Current = sdk.Current{ + Text: custom.values.labelFor(value), + Value: value, } } } // Label sets the label of the variable. func Label(label string) Option { - return func(constant *Custom) { - constant.Builder.Label = label + return func(custom *Custom) { + custom.Builder.Label = label } } // HideLabel ensures that this variable's label will not be displayed. func HideLabel() Option { - return func(constant *Custom) { - constant.Builder.Hide = 1 + return func(custom *Custom) { + custom.Builder.Hide = 1 } } // Hide ensures that the variable will not be displayed. func Hide() Option { - return func(constant *Custom) { - constant.Builder.Hide = 2 + return func(custom *Custom) { + custom.Builder.Hide = 2 } } // Multi allows several values to be selected. func Multi() Option { - return func(constant *Custom) { - constant.Builder.Multi = true + return func(custom *Custom) { + custom.Builder.Multi = true } } // IncludeAll adds an option to allow all values to be selected. func IncludeAll() Option { - return func(constant *Custom) { - constant.Builder.IncludeAll = true - constant.Builder.Options = append(constant.Builder.Options, sdk.Option{ + return func(custom *Custom) { + custom.Builder.IncludeAll = true + custom.Builder.Options = append(custom.Builder.Options, sdk.Option{ Text: "All", Value: "$__all", }) diff --git a/variable/interval/interval.go b/variable/interval/interval.go index a201adc5..09186796 100644 --- a/variable/interval/interval.go +++ b/variable/interval/interval.go @@ -43,7 +43,8 @@ func Values(values ValuesList) Option { func Default(value string) Option { return func(interval *Interval) { interval.Builder.Current = sdk.Current{ - Text: value, + Text: value, + Value: value, } } }