diff --git a/go.mod b/go.mod index a6222295..0ed42390 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/K-Phoen/dark go 1.13 require ( - github.com/K-Phoen/grabana v0.6.1 + github.com/K-Phoen/grabana v0.6.3 github.com/caarlos0/env v3.5.0+incompatible github.com/go-playground/universal-translator v0.17.0 // indirect github.com/grafana-tools/sdk v0.0.0-20200127194913-bdcab199ffde diff --git a/go.sum b/go.sum index c10dfd06..aaabcffa 100644 --- a/go.sum +++ b/go.sum @@ -15,8 +15,8 @@ github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6L github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/K-Phoen/grabana v0.6.1 h1:Efl04O7P8or2eBE0Earb1rd6GDHmqVOAarJEZi2s2yA= -github.com/K-Phoen/grabana v0.6.1/go.mod h1:kOnSP/jusHkwX2YPnR3dkQZHMnKvaNEaSz6uTeXYlkc= +github.com/K-Phoen/grabana v0.6.3 h1:Rk54mMPPuSo8Oe5N9pC8hHhgHT3nnhRw2Q47P08JwPE= +github.com/K-Phoen/grabana v0.6.3/go.mod h1:kOnSP/jusHkwX2YPnR3dkQZHMnKvaNEaSz6uTeXYlkc= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= diff --git a/internal/pkg/converter/json.go b/internal/pkg/converter/json.go index 3a14d8cb..1f7203f3 100644 --- a/internal/pkg/converter/json.go +++ b/internal/pkg/converter/json.go @@ -18,6 +18,21 @@ type JSON struct { logger *zap.Logger } +type sdkLegend struct { + AlignAsTable bool `json:"alignAsTable"` + Avg bool `json:"avg"` + Current bool `json:"current"` + HideEmpty bool `json:"hideEmpty"` + HideZero bool `json:"hideZero"` + Max bool `json:"max"` + Min bool `json:"min"` + RightSide bool `json:"rightSide"` + Show bool `json:"show"` + SideWidth *uint `json:"sideWidth,omitempty"` + Total bool `json:"total"` + Values bool `json:"values"` +} + func NewJSON(logger *zap.Logger) *JSON { return &JSON{ logger: logger, @@ -226,6 +241,10 @@ func (converter *JSON) convertGraph(panel sdk.Panel) grabana.DashboardPanel { graph := &grabana.DashboardGraph{ Title: panel.Title, Span: panelSpan(panel), + Axes: &grabana.GraphAxes{ + Bottom: converter.convertAxis(panel.Xaxis), + }, + Legend: converter.convertLegend(panel.Legend), } if panel.Height != nil { @@ -235,6 +254,11 @@ func (converter *JSON) convertGraph(panel sdk.Panel) grabana.DashboardPanel { graph.Datasource = *panel.Datasource } + if len(panel.Yaxes) == 2 { + graph.Axes.Left = converter.convertAxis(panel.Yaxes[0]) + graph.Axes.Right = converter.convertAxis(panel.Yaxes[1]) + } + for _, target := range panel.GraphPanel.Targets { graphTarget := converter.convertTarget(target) if graphTarget == nil { @@ -247,6 +271,65 @@ func (converter *JSON) convertGraph(panel sdk.Panel) grabana.DashboardPanel { return grabana.DashboardPanel{Graph: graph} } +func (converter *JSON) convertLegend(sdkLegend sdkLegend) []string { + var legend []string + + if !sdkLegend.Show { + legend = append(legend, "hide") + } + if sdkLegend.AlignAsTable { + legend = append(legend, "as_table") + } + if sdkLegend.RightSide { + legend = append(legend, "to_the_right") + } + if sdkLegend.Min { + legend = append(legend, "min") + } + if sdkLegend.Max { + legend = append(legend, "max") + } + if sdkLegend.Avg { + legend = append(legend, "avg") + } + if sdkLegend.Current { + legend = append(legend, "current") + } + if sdkLegend.Total { + legend = append(legend, "total") + } + if sdkLegend.HideEmpty { + legend = append(legend, "no_null_series") + } + if sdkLegend.HideZero { + legend = append(legend, "no_zero_series") + } + + return legend +} + +func (converter *JSON) convertAxis(sdkAxis sdk.Axis) *grabana.GraphAxis { + hidden := !sdkAxis.Show + var min *float64 + var max *float64 + + if sdkAxis.Min != nil { + min = &sdkAxis.Min.Value + } + if sdkAxis.Max != nil { + max = &sdkAxis.Max.Value + } + + return &grabana.GraphAxis{ + Hidden: &hidden, + Label: sdkAxis.Label, + Unit: &sdkAxis.Format, + Min: min, + Max: max, + LogBase: sdkAxis.LogBase, + } +} + func (converter *JSON) convertSingleStat(panel sdk.Panel) grabana.DashboardPanel { singleStat := &grabana.DashboardSingleStat{ Title: panel.Title, diff --git a/internal/pkg/converter/json_test.go b/internal/pkg/converter/json_test.go index 4e35518d..2633b086 100644 --- a/internal/pkg/converter/json_test.go +++ b/internal/pkg/converter/json_test.go @@ -271,3 +271,52 @@ func TestConvertTagAnnotation(t *testing.T) { req.Equal("#5794F2", dashboard.TagsAnnotation[0].IconColor) req.Equal(datasource, dashboard.TagsAnnotation[0].Datasource) } + +func TestConvertLegend(t *testing.T) { + req := require.New(t) + + converter := NewJSON(zap.NewNop()) + + rawLegend := sdkLegend{ + AlignAsTable: true, + Avg: true, + Current: true, + HideEmpty: true, + HideZero: true, + Max: true, + Min: true, + RightSide: true, + Show: true, + Total: true, + } + + legend := converter.convertLegend(rawLegend) + + req.ElementsMatch( + []string{"as_table", "to_the_right", "min", "max", "avg", "current", "total", "no_null_series", "no_zero_series"}, + legend, + ) +} + +func TestConvertAxis(t *testing.T) { + req := require.New(t) + + converter := NewJSON(zap.NewNop()) + + rawAxis := sdk.Axis{ + Format: "bytes", + LogBase: 2, + Min: &sdk.FloatString{Value: 0}, + Max: &sdk.FloatString{Value: 42}, + Show: true, + Label: "Axis", + } + + axis := converter.convertAxis(rawAxis) + + req.Equal("bytes", *axis.Unit) + req.Equal("Axis", axis.Label) + req.EqualValues(0, *axis.Min) + req.EqualValues(42, *axis.Max) + req.False(*axis.Hidden) +} diff --git a/vendor/github.com/K-Phoen/grabana/.golangci.yaml b/vendor/github.com/K-Phoen/grabana/.golangci.yaml index da863b3a..5987273d 100644 --- a/vendor/github.com/K-Phoen/grabana/.golangci.yaml +++ b/vendor/github.com/K-Phoen/grabana/.golangci.yaml @@ -13,7 +13,6 @@ linters: - depguard - dupl - gochecknoinits - - goconst - gocritic - gocyclo - gofmt @@ -32,10 +31,11 @@ linters: disable: - lll - maligned + - goconst linters-settings: errcheck: ignore: fmt:.* run: - modules-download-mode: vendor \ No newline at end of file + modules-download-mode: vendor diff --git a/vendor/github.com/K-Phoen/grabana/dashboard/dashboard.go b/vendor/github.com/K-Phoen/grabana/dashboard/dashboard.go index 848737a2..740f967c 100644 --- a/vendor/github.com/K-Phoen/grabana/dashboard/dashboard.go +++ b/vendor/github.com/K-Phoen/grabana/dashboard/dashboard.go @@ -16,8 +16,8 @@ import ( type TagAnnotation struct { Name string Datasource string - IconColor string `yaml:"color"` - Tags []string + IconColor string `yaml:"color"` + Tags []string `yaml:",flow"` } // Option represents an option that can be used to configure a diff --git a/vendor/github.com/K-Phoen/grabana/decoder/graph.go b/vendor/github.com/K-Phoen/grabana/decoder/graph.go index f4a332c9..a0c42359 100644 --- a/vendor/github.com/K-Phoen/grabana/decoder/graph.go +++ b/vendor/github.com/K-Phoen/grabana/decoder/graph.go @@ -11,6 +11,7 @@ import ( var ErrNoAlertThresholdDefined = fmt.Errorf("no threshold defined") var ErrInvalidAlertValueFunc = fmt.Errorf("invalid alert value function") +var ErrInvalidLegendAttribute = fmt.Errorf("invalid legend attribute") type DashboardGraph struct { Title string @@ -19,6 +20,7 @@ type DashboardGraph struct { Datasource string `yaml:",omitempty"` Targets []Target Axes *GraphAxes `yaml:",omitempty"` + Legend []string `yaml:",omitempty,flow"` Alert *GraphAlert `yaml:",omitempty"` } @@ -43,6 +45,14 @@ func (graphPanel DashboardGraph) toOption() (row.Option, error) { if graphPanel.Axes != nil && graphPanel.Axes.Bottom != nil { opts = append(opts, graph.XAxis(graphPanel.Axes.Bottom.toOptions()...)) } + if len(graphPanel.Legend) != 0 { + legendOpts, err := graphPanel.legend() + if err != nil { + return nil, err + } + + opts = append(opts, graph.Legend(legendOpts...)) + } if graphPanel.Alert != nil { alertOpts, err := graphPanel.Alert.toOptions() if err != nil { @@ -64,6 +74,43 @@ func (graphPanel DashboardGraph) toOption() (row.Option, error) { return row.WithGraph(graphPanel.Title, opts...), nil } +func (graphPanel *DashboardGraph) legend() ([]graph.LegendOption, error) { + opts := make([]graph.LegendOption, 0, len(graphPanel.Legend)) + + for _, attribute := range graphPanel.Legend { + var opt graph.LegendOption + + switch attribute { + case "hide": + opt = graph.Hide + case "as_table": + opt = graph.AsTable + case "to_the_right": + opt = graph.ToTheRight + case "min": + opt = graph.Min + case "max": + opt = graph.Max + case "avg": + opt = graph.Avg + case "current": + opt = graph.Current + case "total": + opt = graph.Total + case "no_null_series": + opt = graph.NoNullSeries + case "no_zero_series": + opt = graph.NoZeroSeries + default: + return nil, ErrInvalidLegendAttribute + } + + opts = append(opts, opt) + } + + return opts, nil +} + func (graphPanel *DashboardGraph) target(t Target) (graph.Option, error) { if t.Prometheus != nil { return graph.WithPrometheusTarget(t.Prometheus.Query, t.Prometheus.toOptions()...), nil @@ -73,12 +120,12 @@ func (graphPanel *DashboardGraph) target(t Target) (graph.Option, error) { } type GraphAxis struct { - Hidden *bool - Label string - Unit *string - Min *float64 - Max *float64 - LogBase int `yaml:"log_base"` + Hidden *bool `yaml:",omitempty"` + Label string `yaml:",omitempty"` + Unit *string `yaml:",omitempty"` + Min *float64 `yaml:",omitempty"` + Max *float64 `yaml:",omitempty"` + LogBase int `yaml:"log_base"` } func (a GraphAxis) toOptions() []axis.Option { @@ -99,14 +146,17 @@ func (a GraphAxis) toOptions() []axis.Option { if a.Max != nil { opts = append(opts, axis.Max(*a.Max)) } + if a.LogBase != 0 { + opts = append(opts, axis.LogBase(a.LogBase)) + } return opts } type GraphAxes struct { - Left *GraphAxis - Right *GraphAxis - Bottom *GraphAxis + Left *GraphAxis `yaml:",omitempty"` + Right *GraphAxis `yaml:",omitempty"` + Bottom *GraphAxis `yaml:",omitempty"` } type GraphAlert struct { diff --git a/vendor/github.com/K-Phoen/grabana/decoder/table.go b/vendor/github.com/K-Phoen/grabana/decoder/table.go index c4fa6116..67ac5336 100644 --- a/vendor/github.com/K-Phoen/grabana/decoder/table.go +++ b/vendor/github.com/K-Phoen/grabana/decoder/table.go @@ -11,7 +11,7 @@ type DashboardTable struct { Height string `yaml:",omitempty"` Datasource string `yaml:",omitempty"` Targets []Target - HiddenColumns []string `yaml:"hidden_columns"` + HiddenColumns []string `yaml:"hidden_columns,flow"` TimeSeriesAggregations []table.Aggregation `yaml:"time_series_aggregations"` } diff --git a/vendor/modules.txt b/vendor/modules.txt index 6914aeac..6c463739 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,6 +1,6 @@ # cloud.google.com/go v0.38.0 cloud.google.com/go/compute/metadata -# github.com/K-Phoen/grabana v0.6.1 +# github.com/K-Phoen/grabana v0.6.3 github.com/K-Phoen/grabana github.com/K-Phoen/grabana/alert github.com/K-Phoen/grabana/axis