Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

replace github.com/K-Phoen/sdk v0.12.2 => github.com/AndersSoee/grabana-sdk v0.0.0-20230502052338-319fd6875a4a
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/K-Phoen/sdk v0.12.2 h1:0QofDlKE+lloyBOzhjEEMW21061zts/WIpfpQ5NLLAs=
github.com/K-Phoen/sdk v0.12.2/go.mod h1:qmM0wO23CtoDux528MXPpYvS4XkRWkWX6rvX9Za8EVU=
github.com/AndersSoee/grabana-sdk v0.0.0-20230502052338-319fd6875a4a h1:1WbX3BNVuvi9b/XjA+k3WaiOFh5PVtFUneZpx+4mQSs=
github.com/AndersSoee/grabana-sdk v0.0.0-20230502052338-319fd6875a4a/go.mod h1:qmM0wO23CtoDux528MXPpYvS4XkRWkWX6rvX9Za8EVU=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down
147 changes: 134 additions & 13 deletions stat/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,6 @@ const (
Range
)

// ValueMap allows to map a value into explicit text.
type ValueMap struct {
Value string
Text string
}

// RangeMap allows to map a range of values into explicit text.
type RangeMap struct {
From string
To string
Text string
}

// Stat represents a stat panel.
type Stat struct {
Builder *sdk.Panel
Expand Down Expand Up @@ -407,6 +394,136 @@ func RelativeThresholds(steps []ThresholdStep) Option {
}
}

type StatValueMapping struct {
sdk.ValueMapping
}

type ValueMappingBuilder struct {
options func(*StatValueMapping)
resultOptions []ValueMappingResultOption
resultKey string
result struct {
text *string
color *string
}
}

func (vmb *ValueMappingBuilder) build() *StatValueMapping {
vm := &StatValueMapping{}
vmb.options(vm)
vm.Options[vmb.resultKey] = &sdk.ValueMappingResult{}
for _, resultOption := range vmb.resultOptions {
resultOption(vm)
}
return vm
}

type ValueMappingResultOption func(*StatValueMapping)

func (vmb *ValueMappingBuilder) Text(text string) *ValueMappingBuilder {
resultFunc := func(mapping *StatValueMapping) {
result, ok := mapping.Options[vmb.resultKey].(*sdk.ValueMappingResult)
if ok {
result.Text = &text
}
}
vmb.resultOptions = append(vmb.resultOptions, resultFunc)
return vmb
}

func (vmb *ValueMappingBuilder) Color(color string) *ValueMappingBuilder {
resultFunc := func(mapping *StatValueMapping) {
result, ok := mapping.Options[vmb.resultKey].(*sdk.ValueMappingResult)
if ok {
result.Color = &color
}
}
vmb.resultOptions = append(vmb.resultOptions, resultFunc)
return vmb
}

func ValueMappings(options ...*ValueMappingBuilder) Option {
return func(stat *Stat) error {
mappings := make([]sdk.ValueMapping, 0, len(options))

for idx, option := range options {
mapping := option.build()
result, ok := mapping.Options[option.resultKey].(*sdk.ValueMappingResult)
if ok {
result.Index = &idx
}
mappings = append(mappings, mapping.ValueMapping)
}
stat.Builder.StatPanel.FieldConfig.Defaults.ValueMappings = mappings
return nil
}
}

// RangeMapping allows to translate a range of values from the summary stat into explicit
// text or color.
func RangeMapping(from float64, to float64) *ValueMappingBuilder {
builder := &ValueMappingBuilder{}
builder.resultKey = "result"
builder.options = func(vm *StatValueMapping) {
vm.MappingType = string(sdk.MappingTypeRange)
vm.Options = map[string]interface{}{
sdk.MappingOptionFrom: from,
sdk.MappingOptionTo: to,
}
}
return builder
}

// RegexMapping allows to translate the value of the summary stat matching a regexp into explicit
// text or color.
func RegexMapping(pattern string) *ValueMappingBuilder {
builder := &ValueMappingBuilder{}
builder.resultKey = "result"
builder.options = func(vm *StatValueMapping) {
vm.MappingType = string(sdk.MappingTypeRegex)
vm.Options = map[string]interface{}{
sdk.MappingOptionPattern: pattern,
}
}
return builder
}

// ValueMapping allows to translate the value of the summary stat into explicit
// text or color, using a list of mapping functions.
func ValueMapping(match string) *ValueMappingBuilder {
builder := &ValueMappingBuilder{}
builder.resultKey = match
builder.options = func(vm *StatValueMapping) {
vm.MappingType = string(sdk.MappingTypeValue)
vm.Options = map[string]interface{}{}
}
return builder
}

type SpecialValue string

const SpecialNull = SpecialValue("null")
const SpecialNullOrNaN = SpecialValue("null+nan")
const SpecialNaN = SpecialValue("nan")
const SpecialTrue = SpecialValue("true")
const SpecialFalse = SpecialValue("false")
const SpecialEmpty = SpecialValue("empty")

// SpecialMapping allows to translate special values of the summary stat into explicit
// text or color.
func SpecialMapping(value SpecialValue) *ValueMappingBuilder {
builder := &ValueMappingBuilder{}
builder.resultKey = "result"
valueStr := string(value)
builder.options = func(vm *StatValueMapping) {
vm.MappingType = string(sdk.MappingTypeSpecial)
vm.Options = map[string]interface{}{
sdk.MappingOptionMatch: valueStr,
}
}
return builder
}

// Repeat configures repeating a panel for a variable
func Repeat(repeat string) Option {
return func(stat *Stat) error {
Expand Down Expand Up @@ -451,3 +568,7 @@ func NoValue(text string) Option {
return nil
}
}

func float64Ptr(input float64) *float64 {
return &input
}
32 changes: 30 additions & 2 deletions stat/stat_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stat

import (
"github.com/K-Phoen/sdk"
"testing"

"github.com/K-Phoen/grabana/errors"
Expand Down Expand Up @@ -426,6 +427,33 @@ func TestRelativeThresholdsCanBeConfigured(t *testing.T) {
req.Len(thresholds.Steps, 3)
}

func float64Ptr(input float64) *float64 {
return &input
func TestValueMappingsCanBeConfigured(t *testing.T) {
req := require.New(t)

panel, err := New("",
ValueMappings(
RangeMapping(0.0, 0.9).Text("DOWN"),
RangeMapping(1, 1.0).Text("UP"),
ValueMapping("UP").Color("red"),
SpecialMapping(SpecialNaN).Color("yellow"),
RegexMapping(".*").Color("blue"),
),
)

req.NoError(err)

mappings := panel.Builder.StatPanel.FieldConfig.Defaults.ValueMappings

req.Len(mappings, 5)

req.Equal("range", mappings[0].MappingType)
req.Equal(0.9, mappings[0].Options["to"])
result0, ok := mappings[0].Options["result"].(*sdk.ValueMappingResult)
req.True(ok)
req.Equal("DOWN", *result0.Text)

req.Equal("value", mappings[2].MappingType)
result2, ok := mappings[2].Options["UP"].(*sdk.ValueMappingResult)
req.True(ok)
req.Equal("red", *result2.Color)
}
50 changes: 41 additions & 9 deletions vendor/github.com/K-Phoen/sdk/panel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# github.com/K-Phoen/sdk v0.12.2
# github.com/K-Phoen/sdk v0.12.2 => github.com/AndersSoee/grabana-sdk v0.0.0-20230502052338-319fd6875a4a
## explicit; go 1.19
github.com/K-Phoen/sdk
# github.com/blang/semver v3.5.1+incompatible
Expand Down