Skip to content

Commit

Permalink
Merge pull request #45 from K-Phoen/heatmaps
Browse files Browse the repository at this point in the history
Initial support of heatmaps
  • Loading branch information
K-Phoen authored Mar 1, 2021
2 parents d5e25ab + ae0a9fc commit f38b018
Show file tree
Hide file tree
Showing 257 changed files with 12,404 additions and 10,948 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.11
github.com/K-Phoen/grabana v0.13.0
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-20201123153837-5fb28a7aa2ef
github.com/grafana-tools/sdk v0.0.0-20210301100910-d23004341fc8
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
28 changes: 22 additions & 6 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.11 h1:JR381d/aNGrCKe1802n5cXAtWUw80Ldl1jpFeAuC2OQ=
github.com/K-Phoen/grabana v0.12.11/go.mod h1:jAPE2R3uNda/vMMFlDjG/S7RjmtfULhd50hh5cgVi4E=
github.com/K-Phoen/grabana v0.13.0 h1:JKeqHSLmIezKZU6sjslapqoNFMudwCSlFv0AXnygkA4=
github.com/K-Phoen/grabana v0.13.0/go.mod h1:TbmPrK1gybqochDEReW/Z5AutxykwnuLkfKp/SYb7bE=
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 All @@ -33,6 +33,12 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yixi/rBrKyJs=
github.com/caarlos0/env v3.5.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/chromedp/cdproto v0.0.0-20210122124816-7a656c010d57 h1:htpyTFarq7OHx9SpkQ+7x20thTQA6JAsgnuMGoPbH4E=
github.com/chromedp/cdproto v0.0.0-20210122124816-7a656c010d57/go.mod h1:55pim6Ht4LJKdVLlyFJV/g++HsEA1hQxPbB5JyNdZC0=
github.com/chromedp/chromedp v0.6.5 h1:hPaDYBpvD2WFicln0ByzV+XRhSOtLgAgsu39O455iWY=
github.com/chromedp/chromedp v0.6.5/go.mod h1:/Q6h52DkrFuvOgmCuR6O3xT5g0bZYoPqjANKBEvQGEY=
github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic=
github.com/chromedp/sysutil v1.0.0/go.mod h1:kgWmDdq8fTzXYcKIBqIYvRRTnYb9aNS9moAV0xufSww=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down Expand Up @@ -80,6 +86,12 @@ github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTM
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.0.4 h1:5eXU1CZhpQdq5kXbKb+sECH5Ia5KiO6CYzIzdlVx6Bs=
github.com/gobwas/ws v1.0.4/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
Expand Down Expand Up @@ -118,10 +130,8 @@ 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-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/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/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 All @@ -137,6 +147,8 @@ github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
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/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.8 h1:QiWkFLKq0T7mpzwOTu6BzNDbfTE8OLrYhVKYMLF46Ok=
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand All @@ -161,6 +173,8 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
Expand Down Expand Up @@ -294,6 +308,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8 h1:de2yTH1xuxjmGB7i6Z5o2z3RCHVa0XlpSZzjd8Fe6bE=
golang.org/x/sys v0.0.0-20210122093101-04d7465088b8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
61 changes: 55 additions & 6 deletions internal/pkg/converter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (converter *JSON) convertConstVar(variable sdk.TemplateVar, dashboard *grab
constant := &grabana.VariableConst{
Name: variable.Name,
Label: variable.Label,
Default: variable.Current.Text,
Default: variable.Current.Text.(string),
ValuesMap: make(map[string]string, len(variable.Options)),
}

Expand Down Expand Up @@ -286,6 +286,8 @@ func (converter *JSON) convertDataPanel(panel sdk.Panel) (grabana.DashboardPanel
switch panel.Type {
case "graph":
return converter.convertGraph(panel), true
case "heatmap":
return converter.convertHeatmap(panel), true
case "singlestat":
return converter.convertSingleStat(panel), true
case "table":
Expand Down Expand Up @@ -314,7 +316,7 @@ func (converter *JSON) convertGraph(panel sdk.Panel) grabana.DashboardPanel {
Axes: &grabana.GraphAxes{
Bottom: converter.convertAxis(panel.Xaxis),
},
Legend: converter.convertLegend(panel.Legend),
Legend: converter.convertLegend(panel.GraphPanel.Legend),
Visualization: converter.convertVisualization(panel),
}

Expand Down Expand Up @@ -410,6 +412,50 @@ func (converter *JSON) convertAxis(sdkAxis sdk.Axis) *grabana.GraphAxis {
}
}

func (converter *JSON) convertHeatmap(panel sdk.Panel) grabana.DashboardPanel {
heatmap := &grabana.DashboardHeatmap{
Title: panel.Title,
Span: panelSpan(panel),
Transparent: panel.Transparent,
HideZeroBuckets: panel.HeatmapPanel.HideZeroBuckets,
HightlightCards: panel.HeatmapPanel.HighlightCards,
ReverseYBuckets: panel.HeatmapPanel.ReverseYBuckets,
Tooltip: &grabana.HeatmapTooltip{
Show: panel.HeatmapPanel.Tooltip.Show,
ShowHistogram: panel.HeatmapPanel.Tooltip.ShowHistogram,
Decimals: &panel.HeatmapPanel.TooltipDecimals,
},
}

if panel.Height != nil {
heatmap.Height = *panel.Height
}
if panel.Datasource != nil {
heatmap.Datasource = *panel.Datasource
}
if panel.HeatmapPanel.DataFormat != "" {
switch panel.HeatmapPanel.DataFormat {
case "tsbuckets":
heatmap.DataFormat = "time_series_buckets"
case "time_series":
heatmap.DataFormat = "time_series"
default:
converter.logger.Warn("unknown data format: skipping heatmap", zap.String("data_format", panel.HeatmapPanel.DataFormat), zap.String("heatmap_title", panel.Title))
}
}

for _, target := range panel.HeatmapPanel.Targets {
heatmapTarget := converter.convertTarget(target)
if heatmapTarget == nil {
continue
}

heatmap.Targets = append(heatmap.Targets, *heatmapTarget)
}

return grabana.DashboardPanel{Heatmap: heatmap}
}

func (converter *JSON) convertSingleStat(panel sdk.Panel) grabana.DashboardPanel {
singleStat := &grabana.DashboardSingleStat{
Title: panel.Title,
Expand Down Expand Up @@ -554,10 +600,13 @@ func (converter *JSON) convertTarget(target sdk.Target) *grabana.Target {
func (converter *JSON) convertPrometheusTarget(target sdk.Target) *grabana.Target {
return &grabana.Target{
Prometheus: &grabana.PrometheusTarget{
Query: target.Expr,
Legend: target.LegendFormat,
Ref: target.RefID,
Hidden: target.Hide,
Query: target.Expr,
Legend: target.LegendFormat,
Ref: target.RefID,
Hidden: target.Hide,
Format: target.Format,
Instant: target.Instant,
IntervalFactor: &target.IntervalFactor,
},
}
}
Expand Down
151 changes: 151 additions & 0 deletions internal/pkg/converter/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ func TestConvertCustomVar(t *testing.T) {
}))
}

func TestConvertDatasourceVar(t *testing.T) {
req := require.New(t)

variable := defaultVar("datasource")
variable.Name = "var_datasource"
variable.Label = "Label datasource"

converter := NewJSON(zap.NewNop())

dashboard := &grabana.DashboardModel{}
converter.convertVariables([]sdk.TemplateVar{variable}, dashboard)

req.Len(dashboard.Variables, 1)
req.NotNil(dashboard.Variables[0].Datasource)

dsVar := dashboard.Variables[0].Datasource

req.Equal("var_datasource", dsVar.Name)
req.Equal("Label datasource", dsVar.Label)
req.False(dsVar.IncludeAll)
}

func TestConvertConstVar(t *testing.T) {
req := require.New(t)

Expand Down Expand Up @@ -493,3 +515,132 @@ func TestConvertAxis(t *testing.T) {
req.EqualValues(42, *axis.Max)
req.False(*axis.Hidden)
}

func TestConvertTextPanelWithMarkdown(t *testing.T) {
req := require.New(t)

converter := NewJSON(zap.NewNop())
height := "200px"

textPanel := sdk.Panel{
CommonPanel: sdk.CommonPanel{
Title: "Text panel",
Transparent: true,
Height: &height,
Type: "text",
},
TextPanel: &sdk.TextPanel{
Mode: "markdown",
Content: "# hello world",
},
}

converted, ok := converter.convertDataPanel(textPanel)

req.True(ok)
req.True(converted.Text.Transparent)
req.Equal("Text panel", converted.Text.Title)
req.Equal("# hello world", converted.Text.Markdown)
req.Equal(height, converted.Text.Height)
}

func TestConvertTextPanelWithHTML(t *testing.T) {
req := require.New(t)

converter := NewJSON(zap.NewNop())

textPanel := sdk.Panel{
CommonPanel: sdk.CommonPanel{
Title: "Text panel html",
Type: "text",
},
TextPanel: &sdk.TextPanel{
Mode: "html",
Content: "<h1>hello world</h1>",
},
}

converted, ok := converter.convertDataPanel(textPanel)

req.True(ok)
req.False(converted.Text.Transparent)
req.Equal("Text panel html", converted.Text.Title)
req.Equal("<h1>hello world</h1>", converted.Text.HTML)
}

func TestConvertSinglePanel(t *testing.T) {
req := require.New(t)

converter := NewJSON(zap.NewNop())
height := "200px"
datasource := "prometheus"

singlestatPanel := sdk.Panel{
CommonPanel: sdk.CommonPanel{
Title: "Singlestat panel",
Type: "singlestat",
Transparent: true,
Height: &height,
Datasource: &datasource,
},
SinglestatPanel: &sdk.SinglestatPanel{
Format: "none",
ValueName: "current",
Colors: []string{"blue", "red", "green"},
ColorBackground: true,
ColorValue: true,
},
}

converted, ok := converter.convertDataPanel(singlestatPanel)

req.True(ok)
req.True(converted.SingleStat.Transparent)
req.Equal("Singlestat panel", converted.SingleStat.Title)
req.Equal("none", converted.SingleStat.Unit)
req.Equal("current", converted.SingleStat.ValueType)
req.Equal(height, converted.SingleStat.Height)
req.Equal(datasource, converted.SingleStat.Datasource)
req.True(reflect.DeepEqual(converted.SingleStat.Colors, [3]string{
"blue", "red", "green",
}))
req.True(reflect.DeepEqual(converted.SingleStat.Color, []string{
"background", "value",
}))
}

func TestConvertHeatmapPanel(t *testing.T) {
req := require.New(t)

converter := NewJSON(zap.NewNop())
height := "400px"
datasource := "prometheus"

heatmapPanel := sdk.Panel{
CommonPanel: sdk.CommonPanel{
Title: "heatmap panel",
Type: "heatmap",
Transparent: true,
Height: &height,
Datasource: &datasource,
},
HeatmapPanel: &sdk.HeatmapPanel{
HideZeroBuckets: true,
HighlightCards: true,
ReverseYBuckets: true,
DataFormat: "tsbuckets",
},
}

converted, ok := converter.convertDataPanel(heatmapPanel)

req.True(ok)
req.True(converted.Heatmap.Transparent)
req.Equal("heatmap panel", converted.Heatmap.Title)
req.Equal(height, converted.Heatmap.Height)
req.Equal(datasource, converted.Heatmap.Datasource)
req.True(converted.Heatmap.ReverseYBuckets)
req.True(converted.Heatmap.HideZeroBuckets)
req.True(converted.Heatmap.HightlightCards)
req.Equal("time_series_buckets", converted.Heatmap.DataFormat)
}
4 changes: 4 additions & 0 deletions vendor/github.com/K-Phoen/grabana/decoder/dashboard.go

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

Loading

0 comments on commit f38b018

Please sign in to comment.