Skip to content

Commit

Permalink
Merge pull request #41 from K-Phoen/converter-improvements
Browse files Browse the repository at this point in the history
Converter improvements
  • Loading branch information
K-Phoen authored Dec 3, 2020
2 parents 1c7c4a3 + a82972c commit 0de2ebd
Show file tree
Hide file tree
Showing 25 changed files with 404 additions and 137 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/K-Phoen/dark
go 1.13

require (
github.com/K-Phoen/grabana v0.12.6
github.com/K-Phoen/grabana v0.12.9
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-20200713194907-007f486b53df
github.com/grafana-tools/sdk v0.0.0-20201123153837-5fb28a7aa2ef
github.com/leodido/go-urn v1.2.0 // indirect
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.4.0
Expand Down
6 changes: 4 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.12.6 h1:ghgnS92PwTHoT1Bj+q9D1kAvqsm+BCVn6RpCjqvOQuE=
github.com/K-Phoen/grabana v0.12.6/go.mod h1:jAPE2R3uNda/vMMFlDjG/S7RjmtfULhd50hh5cgVi4E=
github.com/K-Phoen/grabana v0.12.9 h1:IRb+SxafmjEAV8oepn/Qs2mQoKmENzHoOhSQ+gaU1nQ=
github.com/K-Phoen/grabana v0.12.9/go.mod h1:jAPE2R3uNda/vMMFlDjG/S7RjmtfULhd50hh5cgVi4E=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
Expand Down Expand Up @@ -120,6 +120,8 @@ 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-20200713194907-007f486b53df h1:3xgV3MoAL5QpTP/8tz+IjGPYaDhp35tLNBo/mfu00hw=
github.com/grafana-tools/sdk v0.0.0-20200713194907-007f486b53df/go.mod h1:aqBqJVTJmj0MTX9cP8wuReJPte6HyttMDzSS2u8nJwo=
github.com/grafana-tools/sdk v0.0.0-20201123153837-5fb28a7aa2ef h1:pBtpdcXGO5VLXzmOvlnqfxofkw1Ce8CyoV6mAQUJmm4=
github.com/grafana-tools/sdk v0.0.0-20201123153837-5fb28a7aa2ef/go.mod h1:aqBqJVTJmj0MTX9cP8wuReJPte6HyttMDzSS2u8nJwo=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down
91 changes: 74 additions & 17 deletions internal/pkg/converter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ func (converter *JSON) convertVariable(variable sdk.TemplateVar, dashboard *grab
converter.convertQueryVar(variable, dashboard)
case "const":
converter.convertConstVar(variable, dashboard)
case "datasource":
converter.convertDatasourceVar(variable, dashboard)
default:
converter.logger.Warn("unhandled variable type found: skipped", zap.String("type", variable.Type), zap.String("name", variable.Name))
}
Expand Down Expand Up @@ -207,13 +209,27 @@ func (converter *JSON) convertQueryVar(variable sdk.TemplateVar, dashboard *grab
Label: variable.Label,
Datasource: datasource,
Request: variable.Query,
Regex: variable.Regex,
IncludeAll: variable.IncludeAll,
DefaultAll: variable.Current.Value == "$__all",
AllValue: variable.AllValue,
}

dashboard.Variables = append(dashboard.Variables, grabana.DashboardVariable{Query: query})
}

func (converter *JSON) convertDatasourceVar(variable sdk.TemplateVar, dashboard *grabana.DashboardModel) {
datasource := &grabana.VariableDatasource{
Name: variable.Name,
Label: variable.Label,
Type: variable.Query,
Regex: variable.Regex,
IncludeAll: variable.IncludeAll,
}

dashboard.Variables = append(dashboard.Variables, grabana.DashboardVariable{Datasource: datasource})
}

func (converter *JSON) convertConstVar(variable sdk.TemplateVar, dashboard *grabana.DashboardModel) {
constant := &grabana.VariableConst{
Name: variable.Name,
Expand All @@ -239,22 +255,23 @@ func (converter *JSON) convertPanels(panels []*sdk.Panel, dashboard *grabana.Das
}

currentRow = converter.convertRow(*panel)

for _, rowPanel := range panel.Panels {
convertedPanel, ok := converter.convertDataPanel(rowPanel)
if ok {
currentRow.Panels = append(currentRow.Panels, convertedPanel)
}
}
continue
}

if currentRow == nil {
currentRow = &grabana.DashboardRow{Name: "Overview"}
}

switch panel.Type {
case "graph":
currentRow.Panels = append(currentRow.Panels, converter.convertGraph(*panel))
case "singlestat":
currentRow.Panels = append(currentRow.Panels, converter.convertSingleStat(*panel))
case "table":
currentRow.Panels = append(currentRow.Panels, converter.convertTable(*panel))
default:
converter.logger.Warn("unhandled panel type: skipped", zap.String("type", panel.Type), zap.String("title", panel.Title))
convertedPanel, ok := converter.convertDataPanel(*panel)
if ok {
currentRow.Panels = append(currentRow.Panels, convertedPanel)
}
}

Expand All @@ -263,6 +280,23 @@ func (converter *JSON) convertPanels(panels []*sdk.Panel, dashboard *grabana.Das
}
}

func (converter *JSON) convertDataPanel(panel sdk.Panel) (grabana.DashboardPanel, bool) {
switch panel.Type {
case "graph":
return converter.convertGraph(panel), true
case "singlestat":
return converter.convertSingleStat(panel), true
case "table":
return converter.convertTable(panel), true
case "text":
return converter.convertText(panel), true
default:
converter.logger.Warn("unhandled panel type: skipped", zap.String("type", panel.Type), zap.String("title", panel.Title))
}

return grabana.DashboardPanel{}, false
}

func (converter *JSON) convertRow(panel sdk.Panel) *grabana.DashboardRow {
return &grabana.DashboardRow{
Name: panel.Title,
Expand All @@ -272,8 +306,9 @@ func (converter *JSON) convertRow(panel sdk.Panel) *grabana.DashboardRow {

func (converter *JSON) convertGraph(panel sdk.Panel) grabana.DashboardPanel {
graph := &grabana.DashboardGraph{
Title: panel.Title,
Span: panelSpan(panel),
Title: panel.Title,
Span: panelSpan(panel),
Transparent: panel.Transparent,
Axes: &grabana.GraphAxes{
Bottom: converter.convertAxis(panel.Xaxis),
},
Expand Down Expand Up @@ -365,10 +400,11 @@ func (converter *JSON) convertAxis(sdkAxis sdk.Axis) *grabana.GraphAxis {

func (converter *JSON) convertSingleStat(panel sdk.Panel) grabana.DashboardPanel {
singleStat := &grabana.DashboardSingleStat{
Title: panel.Title,
Span: panelSpan(panel),
Unit: panel.SinglestatPanel.Format,
ValueType: panel.SinglestatPanel.ValueName,
Title: panel.Title,
Span: panelSpan(panel),
Unit: panel.SinglestatPanel.Format,
ValueType: panel.SinglestatPanel.ValueName,
Transparent: panel.Transparent,
}

if panel.Height != nil {
Expand Down Expand Up @@ -423,8 +459,9 @@ func (converter *JSON) convertSingleStat(panel sdk.Panel) grabana.DashboardPanel

func (converter *JSON) convertTable(panel sdk.Panel) grabana.DashboardPanel {
table := &grabana.DashboardTable{
Title: panel.Title,
Span: panelSpan(panel),
Title: panel.Title,
Span: panelSpan(panel),
Transparent: panel.Transparent,
}

if panel.Height != nil {
Expand Down Expand Up @@ -467,6 +504,26 @@ func (converter *JSON) convertTable(panel sdk.Panel) grabana.DashboardPanel {
return grabana.DashboardPanel{Table: table}
}

func (converter *JSON) convertText(panel sdk.Panel) grabana.DashboardPanel {
text := &grabana.DashboardText{
Title: panel.Title,
Span: panelSpan(panel),
Transparent: panel.Transparent,
}

if panel.Height != nil {
text.Height = *panel.Height
}

if panel.TextPanel.Mode == "markdown" {
text.Markdown = panel.TextPanel.Content
} else {
text.HTML = panel.TextPanel.Content
}

return grabana.DashboardPanel{Text: text}
}

func (converter *JSON) convertTarget(target sdk.Target) *grabana.Target {
// looks like a prometheus target
if target.Expr != "" {
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/K-Phoen/grabana/README.md

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

11 changes: 11 additions & 0 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.

20 changes: 12 additions & 8 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.

5 changes: 5 additions & 0 deletions vendor/github.com/K-Phoen/grabana/decoder/row.go

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

26 changes: 15 additions & 11 deletions vendor/github.com/K-Phoen/grabana/decoder/singlestat.go

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

4 changes: 4 additions & 0 deletions 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.

14 changes: 9 additions & 5 deletions vendor/github.com/K-Phoen/grabana/decoder/text.go

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

Loading

0 comments on commit 0de2ebd

Please sign in to comment.