Skip to content

Commit

Permalink
Merge pull request #14 from K-Phoen/graph-axes-legend
Browse files Browse the repository at this point in the history
Support graph legends and axes
  • Loading branch information
K-Phoen committed Mar 29, 2020
2 parents c8f80aa + 1b23903 commit 4e6e631
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 18 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.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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
83 changes: 83 additions & 0 deletions internal/pkg/converter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions internal/pkg/converter/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions vendor/github.com/K-Phoen/grabana/.golangci.yaml

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

4 changes: 2 additions & 2 deletions vendor/github.com/K-Phoen/grabana/dashboard/dashboard.go

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

68 changes: 59 additions & 9 deletions vendor/github.com/K-Phoen/grabana/decoder/graph.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/decoder/table.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,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
Expand Down

0 comments on commit 4e6e631

Please sign in to comment.