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

Initial support for timeseries panels #181

Merged
merged 1 commit into from
Feb 3, 2022
Merged
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
140 changes: 129 additions & 11 deletions panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
RowType
BarGaugeType
HeatmapType
TimeseriesType
)

const MixedSource = "-- Mixed --"
Expand All @@ -60,6 +61,7 @@ type (
*AlertlistPanel
*BarGaugePanel
*HeatmapPanel
*TimeseriesPanel
*CustomPanel
}
panelType int8
Expand Down Expand Up @@ -164,17 +166,7 @@ type (
FieldConfig *FieldConfig `json:"fieldConfig,omitempty"`
}
FieldConfig struct {
Defaults struct {
Unit string `json:"unit"`
Threshold struct {
Mode string `json:"mode"`
Steps []struct {
Color string `json:"color"`
Value string `json:"value"`
} `json:"steps"`
} `json:"threshold"`
Links []Link `json:"links,omitempty"`
} `json:"defaults"`
Defaults FieldConfigDefaults `json:"defaults"`
}
Options struct {
Orientation string `json:"orientation"`
Expand Down Expand Up @@ -361,6 +353,80 @@ type (
YBucketNumber *float64 `json:"yBucketNumber"`
YBucketSize *float64 `json:"yBucketSize"`
}
TimeseriesPanel struct {
Targets []Target `json:"targets,omitempty"`
Options TimeseriesOptions `json:"options"`
FieldConfig FieldConfig `json:"fieldConfig"`
}
TimeseriesOptions struct {
Legend TimeseriesLegendOptions `json:"legend,omitempty"`
Tooltip TimeseriesTooltipOptions `json:"tooltip,omitempty"`
}
TimeseriesLegendOptions struct {
Calcs []string `json:"calcs"`
DisplayMode string `json:"displayMode"`
Placement string `json:"placement"`
}
TimeseriesTooltipOptions struct {
Mode string `json:"mode"`
}
FieldConfigDefaults struct {
Unit string `json:"unit"`
Decimals *int `json:"decimals,omitempty"`
Min *int `json:"min,omitempty"`
Max *int `json:"max,omitempty"`
Color FieldConfigColor `json:"color"`
Thresholds Thresholds `json:"thresholds"`
Custom FieldConfigCustom `json:"custom"`
Links []Link `json:"links,omitempty"`
}
FieldConfigCustom struct {
AxisLabel string `json:"axisLabel,omitempty"`
AxisPlacement string `json:"axisPlacement"`
AxisSoftMin *int `json:"axisSoftMin,omitempty"`
AxisSoftMax *int `json:"axisSoftMax,omitempty"`
BarAlignment int `json:"barAlignment"`
DrawStyle string `json:"drawStyle"`
FillOpacity int `json:"fillOpacity"`
GradientMode string `json:"gradientMode"`
LineInterpolation string `json:"lineInterpolation"`
LineWidth int `json:"lineWidth"`
PointSize int `json:"pointSize"`
ShowPoints string `json:"showPoints"`
SpanNulls bool `json:"spanNulls"`
HideFrom struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an example dashboard I get:

            "hideFrom": {
              "graph": false,
              "legend": false,
              "tooltip": false
            },

On 7.5.1. Could you please point me to an example dashboard that has all of these settings? Or the typescript file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see this in 8.3.1

        "hideFrom": {
          "tooltip": false,
          "viz": false,
          "legend": false
        },

Legend bool `json:"legend"`
Tooltip bool `json:"tooltip"`
Viz bool `json:"viz"`
} `json:"hideFrom"`
LineStyle struct {
Fill string `json:"fill"`
} `json:"lineStyle"`
ScaleDistribution struct {
Type string `json:"type"`
Log int `json:"log,omitempty"`
} `json:"scaleDistribution"`
Stacking struct {
Group string `json:"group"`
Mode string `json:"mode"`
} `json:"stacking"`
ThresholdsStyle struct {
Mode string `json:"mode"`
} `json:"thresholdsStyle"`
}
Thresholds struct {
Mode string `json:"mode"`
Steps []ThresholdStep `json:"steps"`
}
ThresholdStep struct {
Color string `json:"color"`
Value *int `json:"value"`
}
FieldConfigColor struct {
Mode string `json:"mode"`
FixedColor string `json:"fixedColor,omitempty"`
SeriesBy string `json:"seriesBy,omitempty"`
}
CustomPanel map[string]interface{}
)

Expand Down Expand Up @@ -641,6 +707,34 @@ func NewGraph(title string) *Panel {
}}
}

// NewTimeseries initializes panel with a timeseries panel.
func NewTimeseries(title string) *Panel {
if title == "" {
title = "Panel Title"
}

return &Panel{
CommonPanel: CommonPanel{
OfType: TimeseriesType,
Title: title,
Type: "timeseries",
Span: 12,
IsNew: true,
},
TimeseriesPanel: &TimeseriesPanel{
FieldConfig: FieldConfig{
Defaults: FieldConfigDefaults{
Color: FieldConfigColor{
Mode: "palette-classic",
FixedColor: "green",
SeriesBy: "last",
},
},
},
},
}
}

// NewTable initializes panel with a table panel.
func NewTable(title string) *Panel {
if title == "" {
Expand Down Expand Up @@ -782,6 +876,8 @@ func (p *Panel) ResetTargets() {
p.BarGaugePanel.Targets = nil
case HeatmapType:
p.HeatmapPanel.Targets = nil
case TimeseriesType:
p.TimeseriesPanel.Targets = nil
}
}

Expand All @@ -801,6 +897,8 @@ func (p *Panel) AddTarget(t *Target) {
p.TablePanel.Targets = append(p.TablePanel.Targets, *t)
case HeatmapType:
p.HeatmapPanel.Targets = append(p.HeatmapPanel.Targets, *t)
case TimeseriesType:
p.TimeseriesPanel.Targets = append(p.TimeseriesPanel.Targets, *t)
}
// TODO check for existing refID
}
Expand Down Expand Up @@ -828,6 +926,8 @@ func (p *Panel) SetTarget(t *Target) {
setTarget(t, &p.TablePanel.Targets)
case HeatmapType:
setTarget(t, &p.HeatmapPanel.Targets)
case TimeseriesType:
setTarget(t, &p.TimeseriesPanel.Targets)
}
}

Expand Down Expand Up @@ -859,6 +959,8 @@ func (p *Panel) RepeatDatasourcesForEachTarget(dsNames ...string) {
repeatDS(dsNames, &p.TablePanel.Targets)
case HeatmapType:
repeatDS(dsNames, &p.HeatmapPanel.Targets)
case TimeseriesType:
repeatDS(dsNames, &p.TimeseriesPanel.Targets)
}
}

Expand Down Expand Up @@ -893,6 +995,8 @@ func (p *Panel) RepeatTargetsForDatasources(dsNames ...string) {
repeatTarget(dsNames, &p.TablePanel.Targets)
case HeatmapType:
repeatTarget(dsNames, &p.HeatmapPanel.Targets)
case TimeseriesType:
repeatTarget(dsNames, &p.TimeseriesPanel.Targets)
}
}

Expand All @@ -912,6 +1016,8 @@ func (p *Panel) GetTargets() *[]Target {
return &p.BarGaugePanel.Targets
case HeatmapType:
return &p.HeatmapPanel.Targets
case TimeseriesType:
return &p.TimeseriesPanel.Targets
default:
return nil
}
Expand Down Expand Up @@ -975,6 +1081,12 @@ func (p *Panel) UnmarshalJSON(b []byte) (err error) {
if err = json.Unmarshal(b, &heatmap); err == nil {
p.HeatmapPanel = &heatmap
}
case "timeseries":
var timeseries TimeseriesPanel
p.OfType = TimeseriesType
if err = json.Unmarshal(b, &timeseries); err == nil {
p.TimeseriesPanel = &timeseries
}
case "row":
var rowpanel RowPanel
p.OfType = RowType
Expand Down Expand Up @@ -1060,6 +1172,12 @@ func (p *Panel) MarshalJSON() ([]byte, error) {
HeatmapPanel
}{p.CommonPanel, *p.HeatmapPanel}
return json.Marshal(outHeatmap)
case TimeseriesType:
var outTimeseries = struct {
CommonPanel
TimeseriesPanel
}{p.CommonPanel, *p.TimeseriesPanel}
return json.Marshal(outTimeseries)
case CustomType:
var outCustom = customPanelOutput{
p.CommonPanel,
Expand Down
25 changes: 25 additions & 0 deletions panel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,31 @@ func TestNewGraph(t *testing.T) {
}
}

func TestNewTimeseries(t *testing.T) {
var title = "Sample Title"

timeseries := sdk.NewTimeseries(title)

if timeseries.TimeseriesPanel == nil {
t.Error("should be not nil")
}
if timeseries.GraphPanel != nil {
t.Error("should be nil")
}
if timeseries.TextPanel != nil {
t.Error("should be nil")
}
if timeseries.DashlistPanel != nil {
t.Error("should be nil")
}
if timeseries.SinglestatPanel != nil {
t.Error("should be nil")
}
if timeseries.Title != title {
t.Errorf("title should be %s but %s", title, timeseries.Title)
}
}

func TestGraph_AddTarget(t *testing.T) {
var target = sdk.Target{
RefID: "A",
Expand Down