Skip to content

Commit

Permalink
Support graph override series
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Mar 22, 2021
1 parent fe555f9 commit 815e64e
Show file tree
Hide file tree
Showing 21 changed files with 241 additions and 26 deletions.
8 changes: 4 additions & 4 deletions decoder/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ variables:
"query": "10m,12h,1h,1m,30m,30s,5m,6h",
"regex": "",
"current": {
"text": "30s",
"text": ["30s"],
"value": "30s"
},
"label": "Interval",
Expand All @@ -492,7 +492,7 @@ variables:
"query": "label_values(prometheus_http_requests_total, code)",
"regex": "",
"current": {
"text": "All",
"text": ["All"],
"value": "$__all"
},
"label": "HTTP status",
Expand All @@ -519,7 +519,7 @@ variables:
"query": "50",
"regex": "",
"current": {
"text": "50th",
"text": ["50th"],
"value": "50"
},
"label": "Percentile",
Expand All @@ -546,7 +546,7 @@ variables:
"query": "v1",
"regex": "",
"current": {
"text": "v1",
"text": ["v1"],
"value": "v1"
},
"label": "vX",
Expand Down
43 changes: 41 additions & 2 deletions decoder/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/K-Phoen/grabana/alert"
"github.com/K-Phoen/grabana/axis"
"github.com/K-Phoen/grabana/graph"
"github.com/K-Phoen/grabana/graph/series"
"github.com/K-Phoen/grabana/row"
)

Expand Down Expand Up @@ -86,9 +87,43 @@ func (graphPanel DashboardGraph) toOption() (row.Option, error) {
return row.WithGraph(graphPanel.Title, opts...), nil
}

type GraphSeriesOverride struct {
Alias string
Color string `yaml:",omitempty"`
Dashes *bool `yaml:",omitempty"`
Lines *bool `yaml:",omitempty"`
Fill *int `yaml:",omitempty"`
LineWidth *int `yaml:"line_width,omitempty"`
}

func (override *GraphSeriesOverride) toOption() graph.Option {
overrideOpts := []series.OverrideOption{
series.Alias(override.Alias),
}

if override.Color != "" {
overrideOpts = append(overrideOpts, series.Color(override.Color))
}
if override.Dashes != nil {
overrideOpts = append(overrideOpts, series.Dashes(*override.Dashes))
}
if override.Lines != nil {
overrideOpts = append(overrideOpts, series.Dashes(*override.Lines))
}
if override.Fill != nil {
overrideOpts = append(overrideOpts, series.Fill(*override.Fill))
}
if override.LineWidth != nil {
overrideOpts = append(overrideOpts, series.LineWidth(*override.LineWidth))
}

return graph.SeriesOverride(overrideOpts...)
}

type GraphVisualization struct {
NullValue string `yaml:",omitempty"`
Staircase bool `yaml:",omitempty"`
NullValue string `yaml:",omitempty"`
Staircase bool `yaml:",omitempty"`
Overrides []GraphSeriesOverride `yaml:"overrides,omitempty"`
}

func (graphViz *GraphVisualization) toOptions() []graph.Option {
Expand All @@ -115,6 +150,10 @@ func (graphViz *GraphVisualization) toOptions() []graph.Option {
opts = append(opts, graph.Staircase())
}

for _, override := range graphViz.Overrides {
opts = append(opts, override.toOption())
}

return opts
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ require (
github.com/stretchr/testify v1.4.0
gopkg.in/yaml.v2 v2.2.2
)

replace github.com/grafana-tools/sdk => ../sdk
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ github.com/gosimple/slug v1.1.1 h1:fRu/digW+NMwBIP+RmviTK97Ho/bEj/C9swrCspN3D4=
github.com/gosimple/slug v1.1.1/go.mod h1:ER78kgg1Mv0NQGlXiDe57DpCyfbNywXXZ9mIorhxAf0=
github.com/gosimple/slug v1.9.0 h1:r5vDcYrFz9BmfIAMC829un9hq7hKM4cHUrsv36LbEqs=
github.com/gosimple/slug v1.9.0/go.mod h1:AMZ+sOVe65uByN3kgEyf9WEBKBCSS+dJjMX9x4vDJbg=
github.com/grafana-tools/sdk v0.0.0-20210301100910-d23004341fc8 h1:f9VavpwL1aGRC9/Ka3DIYaad6y7SXRpShytH1MAcYYE=
github.com/grafana-tools/sdk v0.0.0-20210301100910-d23004341fc8/go.mod h1:uby+6hPUCRVNG/iAZKCOlaq5YhyK0oKMRke+FDesZdw=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
Expand Down
15 changes: 15 additions & 0 deletions graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package graph
import (
"github.com/K-Phoen/grabana/alert"
"github.com/K-Phoen/grabana/axis"
"github.com/K-Phoen/grabana/graph/series"
"github.com/K-Phoen/grabana/target/prometheus"
"github.com/K-Phoen/grabana/target/stackdriver"
"github.com/grafana-tools/sdk"
Expand Down Expand Up @@ -254,6 +255,20 @@ func Null(mode NullValue) Option {
}
}

// SeriesOverride configures how null values are displayed.
// See https://grafana.com/docs/grafana/latest/panels/field-options/
func SeriesOverride(opts ...series.OverrideOption) Option {
return func(graph *Graph) {
override := sdk.SeriesOverride{}

for _, opt := range opts {
opt(&override)
}

graph.Builder.GraphPanel.SeriesOverrides = append(graph.Builder.GraphPanel.SeriesOverrides, override)
}
}

// Legend defines what should be shown in the legend.
func Legend(opts ...LegendOption) Option {
return func(graph *Graph) {
Expand Down
47 changes: 47 additions & 0 deletions graph/series/override.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package series

import (
"github.com/grafana-tools/sdk"
)

// OverrideOption represents an option that can be used alter a graph panel series.
type OverrideOption func(series *sdk.SeriesOverride)

func Alias(alias string) OverrideOption {
return func(series *sdk.SeriesOverride) {
series.Alias = alias
}
}

// Color overrides the color for the matched series.
func Color(color string) OverrideOption {
return func(series *sdk.SeriesOverride) {
series.Color = &color
}
}

// Dashes enables/disables display of the series using dashes instead of lines.
func Dashes(enabled bool) OverrideOption {
return func(series *sdk.SeriesOverride) {
series.Dashes = &enabled
}
}

// Lines enables/disables display of the series using dashes instead of dashes.
func Lines(enabled bool) OverrideOption {
return func(series *sdk.SeriesOverride) {
series.Lines = &enabled
}
}

func Fill(opacity int) OverrideOption {
return func(series *sdk.SeriesOverride) {
series.Fill = &opacity
}
}

func LineWidth(width int) OverrideOption {
return func(series *sdk.SeriesOverride) {
series.LineWidth = &width
}
}
6 changes: 3 additions & 3 deletions variable/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func (values ValuesMap) asQuery() string {
return strings.Join(valuesList, ",")
}

func (values ValuesMap) labelFor(value string) string {
func (values ValuesMap) labelFor(value string) *sdk.StringSliceString {
for label, val := range values {
if val == value {
return label
return &sdk.StringSliceString{Value: []string{label}, Valid: true}
}
}

return value
return &sdk.StringSliceString{Value: []string{value}, Valid: true}
}

// Constant represents a "constant" templated variable.
Expand Down
2 changes: 1 addition & 1 deletion variable/constant/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestDefaultValueCanBeSet(t *testing.T) {

panel := New("const", Default("99th"))

req.Equal("99th", panel.Builder.Current.Text)
req.Equal([]string{"99th"}, panel.Builder.Current.Text.Value)
req.Equal("99th", panel.Builder.Current.Value)
}

Expand Down
6 changes: 3 additions & 3 deletions variable/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func (values ValuesMap) asQuery() string {
return strings.Join(valuesList, ",")
}

func (values ValuesMap) labelFor(value string) string {
func (values ValuesMap) labelFor(value string) *sdk.StringSliceString {
for label, val := range values {
if val == value {
return label
return &sdk.StringSliceString{Value: []string{label}, Valid: true}
}
}

return value
return &sdk.StringSliceString{Value: []string{value}, Valid: true}
}

// Custom represents a "custom" templated variable.
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 @@ -51,7 +51,7 @@ func TestDefaultValueCanBeSet(t *testing.T) {

panel := New("", Default("99"))

req.Equal("99", panel.Builder.Current.Text)
req.Equal([]string{"99"}, panel.Builder.Current.Text.Value)
}

func TestLabelCanBeHidden(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion variable/interval/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Values(values ValuesList) Option {
func Default(value string) Option {
return func(interval *Interval) {
interval.Builder.Current = sdk.Current{
Text: value,
Text: &sdk.StringSliceString{Value: []string{value}, Valid: true},
Value: value,
}
}
Expand Down
2 changes: 1 addition & 1 deletion variable/interval/interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestDefaultValueCanBeSet(t *testing.T) {

panel := New("", Default("99"))

req.Equal("99", panel.Builder.Current.Text)
req.Equal([]string{"99"}, panel.Builder.Current.Text.Value)
}

func TestLabelCanBeHidden(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion variable/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func IncludeAll() Option {
// DefaultAll selects "All" values by default.
func DefaultAll() Option {
return func(query *Query) {
query.Builder.Current = sdk.Current{Text: "All", Value: "$__all"}
query.Builder.Current = sdk.Current{Text: &sdk.StringSliceString{Value: []string{"All"}, Valid: true}, Value: "$__all"}
}
}

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 @@ -62,7 +62,7 @@ func TestAnAllValuesCanBeTheDefault(t *testing.T) {

panel := New("", DefaultAll())

req.Equal("All", panel.Builder.Current.Text)
req.Equal([]string{"All"}, panel.Builder.Current.Text.Value)
req.Equal("$__all", panel.Builder.Current.Value)
}

Expand Down
29 changes: 29 additions & 0 deletions vendor/github.com/grafana-tools/sdk/address.go

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

6 changes: 3 additions & 3 deletions vendor/github.com/grafana-tools/sdk/board.go

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

42 changes: 42 additions & 0 deletions vendor/github.com/grafana-tools/sdk/custom-types.go

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

5 changes: 3 additions & 2 deletions vendor/github.com/grafana-tools/sdk/org.go

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

2 changes: 2 additions & 0 deletions vendor/github.com/grafana-tools/sdk/panel.go

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

Loading

0 comments on commit 815e64e

Please sign in to comment.