From 996597ad057ab693ff4106c12fd1b4f7dff8f16e Mon Sep 17 00:00:00 2001 From: Alvaro Aguilar-Tablada Espinosa Date: Tue, 28 May 2024 18:36:42 +0200 Subject: [PATCH 1/2] Allow configuring Stat reduceOptions --- stat/stat.go | 18 ++++++++++++++++++ stat/stat_test.go | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/stat/stat.go b/stat/stat.go index 6c35b18e..8e118671 100644 --- a/stat/stat.go +++ b/stat/stat.go @@ -318,6 +318,24 @@ func ValueType(valueType ReductionType) Option { } } +// ValueField selects the fields that should be includedin the panel +func ValueField(field string) Option { + return func(stat *Stat) error { + stat.Builder.StatPanel.Options.ReduceOptions.Fields = field + + return nil + } +} + +// ShowValues allows to enable or disable showing values +func ShowValues(show bool) Option { + return func(stat *Stat) error { + stat.Builder.StatPanel.Options.ReduceOptions.Values = show + + return nil + } +} + // ValueFontSize sets the font size used to display the value. func ValueFontSize(size int) Option { return func(stat *Stat) error { diff --git a/stat/stat_test.go b/stat/stat_test.go index 03de680c..109b8ced 100644 --- a/stat/stat_test.go +++ b/stat/stat_test.go @@ -342,6 +342,24 @@ func TestInvalidValueTypeIsRejected(t *testing.T) { req.ErrorIs(err, errors.ErrInvalidArgument) } +func TestValueFieldCanBeSet(t *testing.T) { + req := require.New(t) + + panel, err := New("", ValueField("somefield")) + + req.NoError(err) + req.Equal("somefield", panel.Builder.StatPanel.Options.ReduceOptions.Fields) +} + +func TestShowValuesCanBeSet(t *testing.T) { + req := require.New(t) + + panel, err := New("", ShowValues(true)) + + req.NoError(err) + req.Equal(true, panel.Builder.StatPanel.Options.ReduceOptions.Values) +} + func TestValueFontSizeCanBeSet(t *testing.T) { req := require.New(t) From 8f374afa521fbc0fc219a784e086d13714e405a8 Mon Sep 17 00:00:00 2001 From: Alvaro Aguilar-Tablada Espinosa Date: Wed, 29 May 2024 11:05:17 +0200 Subject: [PATCH 2/2] include new options in stat decoder --- decoder/stat.go | 8 ++++++++ stat/stat.go | 4 ++-- stat/stat_test.go | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/decoder/stat.go b/decoder/stat.go index 799029b7..5f78db23 100644 --- a/decoder/stat.go +++ b/decoder/stat.go @@ -37,6 +37,8 @@ type DashboardStat struct { Orientation string `yaml:",omitempty"` Text string `yaml:",omitempty"` ValueType string `yaml:"value_type,omitempty"` + ValueField string `yaml:"value_field,omitempty"` + Values bool `yaml:"values,omitempty"` ColorMode string `yaml:"color_mode,omitempty"` TitleFontSize int `yaml:"title_font_size,omitempty"` ValueFontSize int `yaml:"value_font_size,omitempty"` @@ -114,6 +116,12 @@ func (statPanel DashboardStat) toOption() (row.Option, error) { opts = append(opts, opt) } + if statPanel.ValueField != "" { + opts = append(opts, stat.ValueField(statPanel.ValueField)) + } + if statPanel.Values { + opts = append(opts, stat.Values(statPanel.Values)) + } if statPanel.ColorMode != "" { opt, err := statPanel.colorMode() if err != nil { diff --git a/stat/stat.go b/stat/stat.go index 8e118671..a812204b 100644 --- a/stat/stat.go +++ b/stat/stat.go @@ -327,8 +327,8 @@ func ValueField(field string) Option { } } -// ShowValues allows to enable or disable showing values -func ShowValues(show bool) Option { +// Values allows to enable or disable showing values +func Values(show bool) Option { return func(stat *Stat) error { stat.Builder.StatPanel.Options.ReduceOptions.Values = show diff --git a/stat/stat_test.go b/stat/stat_test.go index 109b8ced..226334ed 100644 --- a/stat/stat_test.go +++ b/stat/stat_test.go @@ -354,7 +354,7 @@ func TestValueFieldCanBeSet(t *testing.T) { func TestShowValuesCanBeSet(t *testing.T) { req := require.New(t) - panel, err := New("", ShowValues(true)) + panel, err := New("", Values(true)) req.NoError(err) req.Equal(true, panel.Builder.StatPanel.Options.ReduceOptions.Values)