-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from K-Phoen/gauge-panel
Support "gauge" panels
- Loading branch information
Showing
16 changed files
with
920 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package converter | ||
|
||
import ( | ||
grabana "github.com/K-Phoen/grabana/decoder" | ||
"github.com/K-Phoen/sdk" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (converter *JSON) convertGauge(panel sdk.Panel) grabana.DashboardPanel { | ||
gauge := &grabana.DashboardGauge{ | ||
Title: panel.Title, | ||
Span: panelSpan(panel), | ||
Unit: panel.GaugePanel.FieldConfig.Defaults.Unit, | ||
Decimals: panel.GaugePanel.FieldConfig.Defaults.Decimals, | ||
Transparent: panel.Transparent, | ||
Orientation: converter.convertGaugeOrientation(panel), | ||
ValueType: converter.convertGaugeValueType(panel), | ||
ThresholdMode: converter.convertGaugeThresholdMode(panel), | ||
Thresholds: converter.convertGaugeThresholds(panel), | ||
} | ||
|
||
if panel.GaugePanel.Options.Text != nil { | ||
gauge.ValueFontSize = panel.GaugePanel.Options.Text.ValueSize | ||
gauge.TitleFontSize = panel.GaugePanel.Options.Text.TitleSize | ||
} | ||
|
||
if panel.Description != nil { | ||
gauge.Description = *panel.Description | ||
} | ||
if panel.Repeat != nil { | ||
gauge.Repeat = *panel.Repeat | ||
} | ||
if panel.Height != nil { | ||
gauge.Height = *(panel.Height).(*string) | ||
} | ||
if panel.Datasource != nil { | ||
gauge.Datasource = panel.Datasource.LegacyName | ||
} | ||
if len(panel.Links) != 0 { | ||
gauge.Links = converter.convertPanelLinks(panel.Links) | ||
} | ||
|
||
for _, target := range panel.GaugePanel.Targets { | ||
graphTarget := converter.convertTarget(target) | ||
if graphTarget == nil { | ||
continue | ||
} | ||
|
||
gauge.Targets = append(gauge.Targets, *graphTarget) | ||
} | ||
|
||
return grabana.DashboardPanel{Gauge: gauge} | ||
} | ||
|
||
func (converter *JSON) convertGaugeValueType(panel sdk.Panel) string { | ||
if len(panel.GaugePanel.Options.ReduceOptions.Calcs) != 1 { | ||
return "last_non_null" | ||
} | ||
|
||
valueType := panel.GaugePanel.Options.ReduceOptions.Calcs[0] | ||
|
||
switch valueType { | ||
case "first": | ||
return "first" | ||
case "firstNotNull": | ||
return "first_non_null" | ||
case "last": | ||
return "last" | ||
case "lastNotNull": | ||
return "last_non_null" | ||
|
||
case "min": | ||
return "min" | ||
case "max": | ||
return "max" | ||
case "mean": | ||
return "avg" | ||
|
||
case "count": | ||
return "count" | ||
case "sum": | ||
return "total" | ||
case "range": | ||
return "range" | ||
|
||
default: | ||
converter.logger.Warn("unknown value type", zap.String("value type", valueType)) | ||
return "last_non_null" | ||
} | ||
} | ||
|
||
func (converter *JSON) convertGaugeOrientation(panel sdk.Panel) string { | ||
switch panel.GaugePanel.Options.Orientation { | ||
case "": | ||
return "auto" | ||
case "horizontal": | ||
return "horizontal" | ||
case "vertical": | ||
return "vertical" | ||
default: | ||
converter.logger.Warn("unknown orientation", zap.String("orientation", panel.StatPanel.Options.Orientation)) | ||
return "auto" | ||
} | ||
} | ||
|
||
func (converter *JSON) convertGaugeThresholdMode(panel sdk.Panel) string { | ||
switch panel.GaugePanel.FieldConfig.Defaults.Thresholds.Mode { | ||
case "": | ||
return "absolute" | ||
case "absolute": | ||
return "absolute" | ||
case "percentage": | ||
return "relative" | ||
default: | ||
converter.logger.Warn("unknown threshold mode", zap.String("mode", panel.GaugePanel.FieldConfig.Defaults.Thresholds.Mode)) | ||
return "absolute" | ||
} | ||
} | ||
|
||
func (converter *JSON) convertGaugeThresholds(panel sdk.Panel) []grabana.GaugeThresholdStep { | ||
steps := make([]grabana.GaugeThresholdStep, 0, len(panel.GaugePanel.FieldConfig.Defaults.Thresholds.Steps)) | ||
|
||
for _, step := range panel.GaugePanel.FieldConfig.Defaults.Thresholds.Steps { | ||
steps = append(steps, grabana.GaugeThresholdStep{ | ||
Color: step.Color, | ||
Value: step.Value, | ||
}) | ||
} | ||
|
||
return steps | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package converter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/K-Phoen/sdk" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestConvertGaugePanel(t *testing.T) { | ||
req := require.New(t) | ||
|
||
converter := NewJSON(zap.NewNop()) | ||
height := "200px" | ||
datasource := "prometheus" | ||
|
||
gaugePanel := sdk.Panel{ | ||
CommonPanel: sdk.CommonPanel{ | ||
Title: "Gauge panel", | ||
Description: strPtr("panel desc"), | ||
Type: "gauge", | ||
Transparent: true, | ||
Height: &height, | ||
Datasource: &sdk.DatasourceRef{LegacyName: datasource}, | ||
}, | ||
GaugePanel: &sdk.GaugePanel{ | ||
Targets: []sdk.Target{ | ||
{ | ||
Expr: "sum(kube_pod_info{}) / sum(kube_node_status_allocatable{resource=\"pods\"})", | ||
}, | ||
}, | ||
Options: sdk.StatOptions{ | ||
Orientation: "horizontal", | ||
ReduceOptions: sdk.ReduceOptions{ | ||
Calcs: []string{"last"}, | ||
}, | ||
Text: &sdk.TextOptions{ | ||
ValueSize: 10, | ||
TitleSize: 20, | ||
}, | ||
}, | ||
FieldConfig: sdk.FieldConfig{ | ||
Defaults: sdk.FieldConfigDefaults{ | ||
Unit: "none", | ||
Thresholds: sdk.Thresholds{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
converted, ok := converter.convertDataPanel(gaugePanel) | ||
|
||
req.True(ok) | ||
req.True(converted.Gauge.Transparent) | ||
req.Equal("Gauge panel", converted.Gauge.Title) | ||
req.Equal("panel desc", converted.Gauge.Description) | ||
req.Equal("none", converted.Gauge.Unit) | ||
req.Equal("last", converted.Gauge.ValueType) | ||
req.Equal("horizontal", converted.Gauge.Orientation) | ||
req.Equal(10, converted.Gauge.ValueFontSize) | ||
req.Equal(20, converted.Gauge.TitleFontSize) | ||
req.Equal(height, converted.Gauge.Height) | ||
req.Equal(datasource, converted.Gauge.Datasource) | ||
} | ||
|
||
func TestConvertGaugeLinks(t *testing.T) { | ||
req := require.New(t) | ||
|
||
converter := NewJSON(zap.NewNop()) | ||
sdkPanel := sdk.NewGauge("") | ||
sdkPanel.Links = []sdk.Link{ | ||
{Title: "stat title", URL: strPtr("stat url")}, | ||
} | ||
|
||
converted, ok := converter.convertDataPanel(*sdkPanel) | ||
|
||
req.True(ok) | ||
req.NotNil(converted.Gauge) | ||
|
||
panel := converted.Gauge | ||
req.Len(panel.Links, 1) | ||
req.Equal("stat title", panel.Links[0].Title) | ||
req.Equal("stat url", panel.Links[0].URL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.