Skip to content

Commit

Permalink
Encode heatmap panels
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Dec 23, 2023
1 parent 92c0691 commit 0342774
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 5 deletions.
8 changes: 4 additions & 4 deletions encoder/golang/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ func (encoder *Encoder) encodeDataPanel(panel sdk.Panel) (jen.Code, bool) {
return encoder.encodeStat(panel), true
case "text":
return encoder.encodeText(panel), true
case "heatmap":
return encoder.encodeHeatmap(panel), true
/*
case "heatmap":
return converter.convertHeatmap(panel), true
case "singlestat":
return converter.convertSingleStat(panel), true
return encoder.encodeSingleStat(panel), true
case "table":
return converter.convertTable(panel), true
return encoder.encodeTable(panel), true
*/
default:
encoder.logger.Warn("unhandled panel type: skipped", zap.String("type", panel.Type), zap.String("title", panel.Title))
Expand Down
132 changes: 132 additions & 0 deletions encoder/golang/heatmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package golang

import (
"strconv"

"github.com/K-Phoen/jennifer/jen"
"github.com/K-Phoen/sdk"
"go.uber.org/zap"
)

func (encoder *Encoder) encodeHeatmap(panel sdk.Panel) jen.Code {
settings := encoder.encodeCommonPanelProperties(panel, "heatmap")

settings = append(
settings,
encoder.encodeTargets(panel.HeatmapPanel.Targets, "heatmap")...,
)

settings = append(settings, encoder.encodeHeatmapTooltip(panel.HeatmapPanel)...)
settings = append(settings, encoder.encodeHeatmapYAxis(panel.HeatmapPanel))

// DataFormat
if panel.HeatmapPanel.DataFormat != "" {
switch panel.HeatmapPanel.DataFormat {
case "tsbuckets":
settings = append(settings, heatmapQual("DataFormat").Call(heatmapQual("TimeSeriesBuckets")))
case "timeseries":
settings = append(settings, heatmapQual("DataFormat").Call(heatmapQual("TimeSeries")))
default:
encoder.logger.Warn("unknown heatmap data format", zap.String("data_format", panel.HeatmapPanel.DataFormat))
}
}

// ShowZeroBuckets/HideZeroBuckets
if !panel.HeatmapPanel.HideZeroBuckets {
settings = append(settings, heatmapQual("ShowZeroBuckets").Call())
} else {
settings = append(settings, heatmapQual("HideZeroBuckets").Call())
}

// HighlightCards/NoHighlightCards
if !panel.HeatmapPanel.HighlightCards {
settings = append(settings, heatmapQual("NoHighlightCards").Call())
} else {
settings = append(settings, heatmapQual("HighlightCards").Call())
}

// ReverseYBuckets
if panel.HeatmapPanel.ReverseYBuckets {
settings = append(settings, heatmapQual("ReverseYBuckets").Call())
}

// HideXAxis
if !panel.HeatmapPanel.XAxis.Show {
settings = append(settings, heatmapQual("HideXAxis").Call())
}

// Legend()
if !panel.HeatmapPanel.Legend.Show {
settings = append(settings, heatmapQual("Legend").Call(heatmapQual("Hide")))
}

return qual("row", "WithHeatmap").MultiLineCall(
settings...,
)
}

func (encoder *Encoder) encodeHeatmapYAxis(panel *sdk.HeatmapPanel) jen.Code {
var settings []jen.Code

// Unit
if panel.YAxis.Format != "" {
settings = append(settings, heatmapAxisQual("Unit").Call(lit(panel.YAxis.Format)))
}

// Decimals
if panel.YAxis.Decimals != nil {
settings = append(settings, heatmapAxisQual("Decimals").Call(lit(*panel.YAxis.Decimals)))
}

// Min
if panel.YAxis.Min != nil {
asFloat, err := strconv.ParseFloat(*panel.YAxis.Min, 64)
if err != nil {
encoder.logger.Warn("could not parse heatmap YAxis min as float", zap.Error(err), zap.String("min", *panel.YAxis.Min))
} else {
settings = append(settings, heatmapAxisQual("Min").Call(lit(asFloat)))
}
}

// Max
if panel.YAxis.Max != nil {
asFloat, err := strconv.ParseFloat(*panel.YAxis.Max, 64)
if err != nil {
encoder.logger.Warn("could not parse heatmap YAxis max as float", zap.Error(err), zap.String("min", *panel.YAxis.Max))
} else {
settings = append(settings, heatmapAxisQual("Max").Call(lit(asFloat)))
}
}

if len(settings) == 0 {
return nil
}

return heatmapQual("YAxis").Call(settings...)
}

func (encoder *Encoder) encodeHeatmapTooltip(panel *sdk.HeatmapPanel) []jen.Code {
settings := []jen.Code{
heatmapQual("HideTooltipHistogram").Call(lit(panel.TooltipDecimals)),
}

// HideTooltip
if !panel.Tooltip.Show {
settings = append(settings, heatmapQual("HideTooltip").Call())
}

// HideTooltipHistogram
if !panel.Tooltip.ShowHistogram {
settings = append(settings, heatmapQual("HideTooltipHistogram").Call())
}

return settings
}

func heatmapAxisQual(name string) *jen.Statement {
return qual("heatmap/axis", name)
}

func heatmapQual(name string) *jen.Statement {
return qual("heatmap", name)
}
2 changes: 1 addition & 1 deletion heatmap/axis/axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type SDKAxis struct {
SplitFactor *float64 `json:"splitFactor"`
}

// YAxis represents a the Y axis of a heatmap.
// YAxis represents the Y axis of a heatmap.
type YAxis struct {
Builder *SDKAxis
}
Expand Down

0 comments on commit 0342774

Please sign in to comment.