Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: start adding piechart sdk json information #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
BarGaugeType
HeatmapType
TimeseriesType
PieChartType
LogsType
GaugeType
)
Expand All @@ -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 {
Expand All @@ -72,6 +78,7 @@ type (
*BarGaugePanel
*HeatmapPanel
*TimeseriesPanel
*PieChartPanel
*LogsPanel
*GaugePanel
*CustomPanel
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -1178,6 +1258,12 @@ func (p *Panel) UnmarshalJSON(b []byte) error {
if err = json.Unmarshal(b, &timeseries); err == nil {
p.TimeseriesPanel = &timeseries
}
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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion panel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"

"github.com/K-Phoen/sdk"
"github.com/stretchr/testify/assert"
)

func TestStackVal_UnmarshalJSON_GotTrue(t *testing.T) {
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -797,4 +811,4 @@ func TestCustomPanelOutput_MarshalJSON(t *testing.T) {
t.Fatalf("wrong value of %s: got %s, expected %s", titleKey, val, titleValue)
}

}
}
Loading