diff --git a/panel.go b/panel.go index 862e901..5aa2be5 100644 --- a/panel.go +++ b/panel.go @@ -41,6 +41,7 @@ const ( BarGaugeType HeatmapType TimeseriesType + PieChartType LogsType GaugeType ) @@ -54,6 +55,11 @@ const ( RepeatDirectionHorizontal RepeatDirection = "h" ) +const ( + PieChartLegendValuesValue PieChartLegendValue = "value" + PieChartLegendValuesPercent PieChartLegendValue = "percent" +) + type ( // Panel represents panels of different types defined in Grafana. Panel struct { @@ -72,6 +78,7 @@ type ( *BarGaugePanel *HeatmapPanel *TimeseriesPanel + *PieChartPanel *LogsPanel *GaugePanel *CustomPanel @@ -335,6 +342,39 @@ type ( Options TimeseriesOptions `json:"options"` FieldConfig FieldConfig `json:"fieldConfig"` } + + PieChartPanel struct { + Targets []Target `json:"targets,omitempty"` + Options PieChartOptions `json:"options"` + FieldConfig FieldConfig `json:"fieldConfig"` + } + + PieChartOptions struct { + ReduceOptions PieChartReduceOptions `json:"reduceOptions"` + PieType string `json:"pieType"` + Tooltip PieChartTooltip `json:"tooltip"` + Legend PieChartLegend `json:"legend"` + } + PieChartLegendValue string + PieChartLegend struct { + ShowLegend bool `json:"showLegend"` + DisplayMode string `json:"displayMode"` + Placement string `json:"placement"` + Values []PieChartLegendValue `json:"values"` + Calcs []string `json:"calcs"` + } + + PieChartReduceOptions struct { + Values bool `json:"values"` + Calcs []string `json:"calcs"` + Fields string `json:"fields"` + } + + PieChartTooltip struct { + Mode string `json:"mode"` // todo types + Sort string `json:"sort"` // todo types + } + TimeseriesOptions struct { Legend TimeseriesLegendOptions `json:"legend,omitempty"` Tooltip TimeseriesTooltipOptions `json:"tooltip,omitempty"` @@ -725,6 +765,34 @@ func NewTimeseries(title string) *Panel { } } +// NewPieChart initializes panel with a piechart panel. +func NewPieChart(title string) *Panel { + if title == "" { + title = "Panel Title" + } + + return &Panel{ + CommonPanel: CommonPanel{ + OfType: PieChartType, + Title: title, + Type: "piechart", + Span: 12, + IsNew: true, + }, + PieChartPanel: &PieChartPanel{ + FieldConfig: FieldConfig{ + Defaults: FieldConfigDefaults{ + Color: FieldConfigColor{ + Mode: "palette-classic", + FixedColor: "green", + SeriesBy: "last", + }, + }, + }, + }, + } +} + // NewLogs initializes a new panel as a Logs panel. func NewLogs(title string) *Panel { if title == "" { @@ -940,6 +1008,8 @@ func (p *Panel) ResetTargets() { p.HeatmapPanel.Targets = nil case TimeseriesType: p.TimeseriesPanel.Targets = nil + case PieChartType: + p.PieChartPanel.Targets = nil case LogsType: p.LogsPanel.Targets = nil case GaugeType: @@ -965,6 +1035,8 @@ func (p *Panel) AddTarget(t *Target) { p.HeatmapPanel.Targets = append(p.HeatmapPanel.Targets, *t) case TimeseriesType: p.TimeseriesPanel.Targets = append(p.TimeseriesPanel.Targets, *t) + case PieChartType: + p.PieChartPanel.Targets = append(p.PieChartPanel.Targets, *t) case LogsType: p.LogsPanel.Targets = append(p.LogsPanel.Targets, *t) case GaugeType: @@ -998,6 +1070,8 @@ func (p *Panel) SetTarget(t *Target) { setTarget(t, &p.HeatmapPanel.Targets) case TimeseriesType: setTarget(t, &p.TimeseriesPanel.Targets) + case PieChartType: + setTarget(t, &p.PieChartPanel.Targets) case LogsType: setTarget(t, &p.LogsPanel.Targets) case GaugeType: @@ -1035,6 +1109,8 @@ func (p *Panel) RepeatDatasourcesForEachTarget(dsNames ...string) { repeatDS(dsNames, &p.HeatmapPanel.Targets) case TimeseriesType: repeatDS(dsNames, &p.TimeseriesPanel.Targets) + case PieChartType: + repeatDS(dsNames, &p.PieChartPanel.Targets) case LogsType: repeatDS(dsNames, &p.LogsPanel.Targets) case GaugeType: @@ -1075,6 +1151,8 @@ func (p *Panel) RepeatTargetsForDatasources(dsNames ...string) { repeatTarget(dsNames, &p.HeatmapPanel.Targets) case TimeseriesType: repeatTarget(dsNames, &p.TimeseriesPanel.Targets) + case PieChartType: + repeatTarget(dsNames, &p.PieChartPanel.Targets) case LogsType: repeatTarget(dsNames, &p.LogsPanel.Targets) case GaugeType: @@ -1100,6 +1178,8 @@ func (p *Panel) GetTargets() *[]Target { return &p.HeatmapPanel.Targets case TimeseriesType: return &p.TimeseriesPanel.Targets + case PieChartType: + return &p.PieChartPanel.Targets case LogsType: return &p.LogsPanel.Targets case GaugeType: @@ -1178,6 +1258,12 @@ func (p *Panel) UnmarshalJSON(b []byte) error { if err = json.Unmarshal(b, ×eries); err == nil { p.TimeseriesPanel = ×eries } + case "piechart": + var piechart PieChartPanel + p.OfType = PieChartType + if err = json.Unmarshal(b, &piechart); err == nil { + p.PieChartPanel = &piechart + } case "logs": var logs LogsPanel p.OfType = LogsType @@ -1285,6 +1371,12 @@ func (p *Panel) MarshalJSON() ([]byte, error) { TimeseriesPanel }{p.CommonPanel, *p.TimeseriesPanel} return json.Marshal(outTimeseries) + case PieChartType: + var outPieChart = struct { + CommonPanel + PieChartPanel + }{p.CommonPanel, *p.PieChartPanel} + return json.Marshal(outPieChart) case LogsType: var outLogs = struct { CommonPanel diff --git a/panel_test.go b/panel_test.go index 1c1730f..3a52d89 100644 --- a/panel_test.go +++ b/panel_test.go @@ -25,6 +25,7 @@ import ( "testing" "github.com/K-Phoen/sdk" + "github.com/stretchr/testify/assert" ) func TestStackVal_UnmarshalJSON_GotTrue(t *testing.T) { @@ -266,6 +267,19 @@ func TestNewGraph(t *testing.T) { } } +func TestNewPieChart(t *testing.T) { + assert := assert.New(t) + var title = "Sample Title" + piechart := sdk.NewPieChart(title) + assert.NotNil(piechart.PieChartPanel) + assert.Nil(piechart.GraphPanel) + assert.Nil(piechart.TextPanel) + assert.Nil(piechart.DashlistPanel) + assert.Nil(piechart.SinglestatPanel) + assert.Nil(piechart.LogsPanel) + assert.Equal(piechart.Title, title) +} + func TestNewTimeseries(t *testing.T) { var title = "Sample Title" @@ -797,4 +811,4 @@ func TestCustomPanelOutput_MarshalJSON(t *testing.T) { t.Fatalf("wrong value of %s: got %s, expected %s", titleKey, val, titleValue) } -} \ No newline at end of file +}