Skip to content

Commit

Permalink
Merge pull request #209 from K-Phoen/more-timeseries-overrides
Browse files Browse the repository at this point in the history
More timeseries overrides
  • Loading branch information
K-Phoen committed Mar 20, 2023
2 parents abe791b + ffda668 commit 6a73314
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 24 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/K-Phoen/dark
go 1.19

require (
github.com/K-Phoen/grabana v0.21.14
github.com/K-Phoen/grabana v0.21.15
github.com/K-Phoen/sdk v0.12.0
github.com/go-logr/logr v1.2.3
github.com/onsi/ginkgo v1.16.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/K-Phoen/grabana v0.21.14 h1:sosOZQ5APT4s08F6amqLEbv7b+//7YFyzSdWazIQPsQ=
github.com/K-Phoen/grabana v0.21.14/go.mod h1:HDl99djdna5Auu7RcOgxE64HnvQ9UBIpqc+A2A5XnL8=
github.com/K-Phoen/grabana v0.21.15 h1:rUyJm9pV2XiPWQNVDjR+ubEBP+A8zv0hpRQmZjFcBnM=
github.com/K-Phoen/grabana v0.21.15/go.mod h1:HDl99djdna5Auu7RcOgxE64HnvQ9UBIpqc+A2A5XnL8=
github.com/K-Phoen/sdk v0.12.0 h1:+0QqHoDZbO6zetFMggM3zKF48GKRu744Ycc9w4oyY0E=
github.com/K-Phoen/sdk v0.12.0/go.mod h1:wp7qXARaIhCYktmoOjRZ+TDMlek5nbayC+waN7vigxI=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
Expand Down
8 changes: 0 additions & 8 deletions internal/pkg/converter/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,3 @@ func TestConvertLinks(t *testing.T) {
req.Equal(1, len(dashboard.DashboardLinks))
req.Equal(1, len(dashboard.ExternalLinks))
}

func strPtr(input string) *string {
return &input
}

func boolPtr(input bool) *bool {
return &input
}
104 changes: 104 additions & 0 deletions internal/pkg/converter/timeseries.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package converter

import (
"fmt"

grabana "github.com/K-Phoen/grabana/decoder"
"github.com/K-Phoen/sdk"
"go.uber.org/zap"
Expand All @@ -14,6 +16,7 @@ func (converter *JSON) convertTimeSeries(panel sdk.Panel) grabana.DashboardPanel
Legend: converter.convertTimeSeriesLegend(panel.TimeseriesPanel.Options.Legend),
Visualization: converter.convertTimeSeriesVisualization(panel),
Axis: converter.convertTimeSeriesAxis(panel),
Overrides: converter.convertTimeSeriesOverrides(panel),
}

if panel.Description != nil {
Expand Down Expand Up @@ -210,3 +213,104 @@ func (converter *JSON) convertTimeSeriesLegend(legend sdk.TimeseriesLegendOption

return options
}

func (converter *JSON) convertTimeSeriesOverrides(panel sdk.Panel) []grabana.TimeSeriesOverride {
overrides := make([]grabana.TimeSeriesOverride, 0, len(panel.TimeseriesPanel.FieldConfig.Overrides))

for _, sdkOverride := range panel.TimeseriesPanel.FieldConfig.Overrides {
override, err := converter.convertTimeSeriesOverride(sdkOverride)
if err != nil {
converter.logger.Warn("could not convert field override: skipping", zap.Error(err))
continue
}

overrides = append(overrides, override)
}

return overrides
}

func (converter *JSON) convertTimeSeriesOverride(sdkOverride sdk.FieldConfigOverride) (grabana.TimeSeriesOverride, error) {
override := grabana.TimeSeriesOverride{}

matcher, err := converter.convertTimeSeriesOverrideMatcher(sdkOverride.Matcher)
if err != nil {
return override, err
}
override.Matcher = matcher

properties, err := converter.convertTimeSeriesOverrideProperties(sdkOverride.Properties)
if err != nil {
return override, err
}
override.Properties = properties

return override, nil
}

func (converter *JSON) convertTimeSeriesOverrideMatcher(matcher struct {
ID string `json:"id"`
Options string `json:"options"`
}) (grabana.TimeSeriesOverrideMatcher, error) {
switch matcher.ID {
case "byName":
return grabana.TimeSeriesOverrideMatcher{FieldName: &matcher.Options}, nil
case "byFrameRefID":
return grabana.TimeSeriesOverrideMatcher{QueryRef: &matcher.Options}, nil
case "byRegexp":
return grabana.TimeSeriesOverrideMatcher{Regex: &matcher.Options}, nil
case "byType":
return grabana.TimeSeriesOverrideMatcher{Type: &matcher.Options}, nil
default:
return grabana.TimeSeriesOverrideMatcher{}, fmt.Errorf("unknown field override matcher '%s'", matcher.ID)
}
}

func (converter *JSON) convertTimeSeriesOverrideProperties(sdkProperties []sdk.FieldConfigOverrideProperty) (grabana.TimeSeriesOverrideProperties, error) {
properties := grabana.TimeSeriesOverrideProperties{}

for _, sdkProperty := range sdkProperties {
converter.convertTimeSeriesOverrideProperty(sdkProperty, &properties)
}

return properties, nil
}

func (converter *JSON) convertTimeSeriesOverrideProperty(sdkProperty sdk.FieldConfigOverrideProperty, properties *grabana.TimeSeriesOverrideProperties) {
switch sdkProperty.ID {
case "unit":
properties.Unit = strPtr(sdkProperty.Value.(string))
case "custom.axisPlacement":
properties.AxisDisplay = strPtr(sdkProperty.Value.(string))
case "custom.fillOpacity":
properties.FillOpacity = intPtr(int(sdkProperty.Value.(float64)))
case "custom.stacking":
options, ok := sdkProperty.Value.(map[string]interface{})
if !ok {
converter.logger.Warn("could not convert custom.stacking field override: invalid options")
break
}
properties.Stack = strPtr(options["mode"].(string))
case "custom.transform":
transformType := sdkProperty.Value.(string)
if transformType != "negative-Y" {
converter.logger.Warn("could not convert transform field override: invalid option")
break
}
properties.NegativeY = boolPtr(true)
case "color":
options, ok := sdkProperty.Value.(map[string]interface{})
if !ok {
converter.logger.Warn("could not convert color field override: invalid options")
break
}
if options["mode"] != "fixed" {
converter.logger.Warn("could not convert color field override: unsupported mode")
break
}

properties.Color = strPtr(options["fixedColor"].(string))
default:
converter.logger.Warn(fmt.Sprintf("unhandled override type '%s'", sdkProperty.ID))
}
}
8 changes: 8 additions & 0 deletions internal/pkg/converter/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ func stringInSlice(search string, haystack []string) bool {
return false
}

func boolPtr(input bool) *bool {
return &input
}

func intPtr(input int) *int {
return &input
}

func float64Ptr(input float64) *float64 {
return &input
}

func strPtr(input string) *string {
return &input
}
119 changes: 110 additions & 9 deletions vendor/github.com/K-Phoen/grabana/decoder/timeseries.go

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

1 change: 1 addition & 0 deletions vendor/github.com/K-Phoen/grabana/gauge/gauge.go

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

1 change: 1 addition & 0 deletions vendor/github.com/K-Phoen/grabana/heatmap/heatmap.go

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

1 change: 1 addition & 0 deletions vendor/github.com/K-Phoen/grabana/singlestat/singlestat.go

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

1 change: 1 addition & 0 deletions vendor/github.com/K-Phoen/grabana/stat/stat.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/github.com/K-Phoen/grabana/timeseries/axis/axis.go

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

Loading

0 comments on commit 6a73314

Please sign in to comment.