Skip to content

Commit

Permalink
Merge pull request #179 from K-Phoen/links
Browse files Browse the repository at this point in the history
Links
  • Loading branch information
K-Phoen authored Jun 5, 2022
2 parents 08eb45d + cdc735a commit 092b4d3
Show file tree
Hide file tree
Showing 30 changed files with 406 additions and 46 deletions.
2 changes: 2 additions & 0 deletions decoder/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ rows:
title: Total Request per Second
description: Does it perform?
span: 12
links:
- { title: linky, url: http://linky }
targets:
- prometheus:
query: "go_memstats_heap_alloc_bytes"
Expand Down
4 changes: 4 additions & 0 deletions decoder/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type DashboardGraph struct {
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Targets []Target
Links DashboardPanelLinks `yaml:",omitempty"`
Axes *GraphAxes `yaml:",omitempty"`
Legend []string `yaml:",omitempty,flow"`
Alert *Alert `yaml:",omitempty"`
Expand Down Expand Up @@ -47,6 +48,9 @@ func (graphPanel DashboardGraph) toOption() (row.Option, error) {
if graphPanel.Repeat != "" {
opts = append(opts, graph.Repeat(graphPanel.Repeat))
}
if len(graphPanel.Links) != 0 {
opts = append(opts, graph.Links(graphPanel.Links.toModel()...))
}
if graphPanel.Axes != nil && graphPanel.Axes.Right != nil {
opts = append(opts, graph.RightYAxis(graphPanel.Axes.Right.toOptions()...))
}
Expand Down
22 changes: 13 additions & 9 deletions decoder/heatmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ var ErrInvalidDataFormat = fmt.Errorf("invalid data format")
// DashboardHeatmap represents a heatmap panel.
type DashboardHeatmap struct {
Title string
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
DataFormat string `yaml:"data_format,omitempty"`
HideZeroBuckets bool `yaml:"hide_zero_buckets"`
HighlightCards bool `yaml:"highlight_cards"`
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
DataFormat string `yaml:"data_format,omitempty"`
HideZeroBuckets bool `yaml:"hide_zero_buckets"`
HighlightCards bool `yaml:"highlight_cards"`
Links DashboardPanelLinks `yaml:",omitempty"`
Targets []Target
ReverseYBuckets bool `yaml:"reverse_y_buckets,omitempty"`
Tooltip *HeatmapTooltip `yaml:",omitempty"`
Expand Down Expand Up @@ -108,6 +109,9 @@ func (heatmapPanel DashboardHeatmap) toOption() (row.Option, error) {
if heatmapPanel.Repeat != "" {
opts = append(opts, heatmap.Repeat(heatmapPanel.Repeat))
}
if len(heatmapPanel.Links) != 0 {
opts = append(opts, heatmap.Links(heatmapPanel.Links.toModel()...))
}
if heatmapPanel.DataFormat != "" {
switch heatmapPanel.DataFormat {
case "time_series_buckets":
Expand Down
20 changes: 12 additions & 8 deletions decoder/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ var ErrInvalidDeduplicationStrategy = fmt.Errorf("invalid deduplication strategy

type DashboardLogs struct {
Title string
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Targets []LogsTarget `yaml:",omitempty"`
Visualization *LogsVisualization `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Links DashboardPanelLinks `yaml:",omitempty"`
Targets []LogsTarget `yaml:",omitempty"`
Visualization *LogsVisualization `yaml:",omitempty"`
}

type LogsTarget struct {
Expand Down Expand Up @@ -47,6 +48,9 @@ func (panel DashboardLogs) toOption() (row.Option, error) {
if panel.Repeat != "" {
opts = append(opts, logs.Repeat(panel.Repeat))
}
if len(panel.Links) != 0 {
opts = append(opts, logs.Links(panel.Links.toModel()...))
}
for _, t := range panel.Targets {
opt, err := panel.target(t)
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions decoder/panellink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package decoder

import (
"github.com/K-Phoen/grabana/links"
)

type DashboardPanelLinks []DashboardPanelLink

func (collection DashboardPanelLinks) toModel() []links.Link {
models := make([]links.Link, 0, len(collection))

for _, link := range collection {
models = append(models, link.toModel())
}

return models
}

type DashboardPanelLink struct {
Title string
URL string `yaml:"url"`
OpenInNewTab bool `yaml:"open_in_new_tab,omitempty"`
}

func (l DashboardPanelLink) toModel() links.Link {
if l.OpenInNewTab {
return links.New(l.Title, l.URL, links.OpenBlank())
}

return links.New(l.Title, l.URL)
}
36 changes: 36 additions & 0 deletions decoder/panellink_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package decoder

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestPanelLink(t *testing.T) {
req := require.New(t)

yamlLink := DashboardPanelLink{
Title: "joe",
URL: "http://foo",
OpenInNewTab: true,
}

model := yamlLink.toModel()

req.Equal("joe", model.Builder.Title)
req.Equal("http://foo", *model.Builder.URL)
req.True(*model.Builder.TargetBlank)
}

func TestPanelLinks(t *testing.T) {
req := require.New(t)

yamlLinks := DashboardPanelLinks{
{Title: "joe", URL: "http://foo"},
{Title: "bar", URL: "http://baz"},
}

model := yamlLinks.toModel()

req.Len(model, 2)
}
16 changes: 10 additions & 6 deletions decoder/singlestat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ var ErrInvalidSingleStatValueType = fmt.Errorf("invalid single stat value type")

type DashboardSingleStat struct {
Title string
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Links DashboardPanelLinks `yaml:",omitempty"`
Unit string
Decimals *int `yaml:",omitempty"`
ValueType string `yaml:"value_type"`
Expand Down Expand Up @@ -54,6 +55,9 @@ func (singleStatPanel DashboardSingleStat) toOption() (row.Option, error) {
if singleStatPanel.Repeat != "" {
opts = append(opts, singlestat.Repeat(singleStatPanel.Repeat))
}
if len(singleStatPanel.Links) != 0 {
opts = append(opts, singlestat.Links(singleStatPanel.Links.toModel()...))
}
if singleStatPanel.Unit != "" {
opts = append(opts, singlestat.Unit(singleStatPanel.Unit))
}
Expand Down
16 changes: 10 additions & 6 deletions decoder/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ type StatThresholdStep struct {

type DashboardStat struct {
Title string
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Links DashboardPanelLinks `yaml:",omitempty"`
Targets []Target

Unit string `yaml:",omitempty"`
Expand Down Expand Up @@ -64,6 +65,9 @@ func (statPanel DashboardStat) toOption() (row.Option, error) {
if statPanel.Repeat != "" {
opts = append(opts, stat.Repeat(statPanel.Repeat))
}
if len(statPanel.Links) != 0 {
opts = append(opts, stat.Links(statPanel.Links.toModel()...))
}
if statPanel.Unit != "" {
opts = append(opts, stat.Unit(statPanel.Unit))
}
Expand Down
14 changes: 9 additions & 5 deletions decoder/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
// DashboardTable represents a table panel.
type DashboardTable struct {
Title string
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Links DashboardPanelLinks `yaml:",omitempty"`
Targets []Target
HiddenColumns []string `yaml:"hidden_columns,flow"`
TimeSeriesAggregations []table.Aggregation `yaml:"time_series_aggregations"`
Expand All @@ -36,6 +37,9 @@ func (tablePanel DashboardTable) toOption() (row.Option, error) {
if tablePanel.Datasource != "" {
opts = append(opts, table.DataSource(tablePanel.Datasource))
}
if len(tablePanel.Links) != 0 {
opts = append(opts, table.Links(tablePanel.Links.toModel()...))
}

for _, t := range tablePanel.Targets {
opt, err := tablePanel.target(t)
Expand Down
8 changes: 8 additions & 0 deletions decoder/testdata/timeseries_panel.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@
"description": "Does it perform?",
"transparent": false,
"type": "timeseries",
"links": [
{
"includeVars": false,
"title": "linky",
"url": "http://linky",
"type": ""
}
],
"targets": [
{
"refId": "",
Expand Down
16 changes: 10 additions & 6 deletions decoder/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (

type DashboardText struct {
Title string
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
HTML string `yaml:",omitempty"`
Markdown string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Links DashboardPanelLinks `yaml:",omitempty"`
HTML string `yaml:",omitempty"`
Markdown string `yaml:",omitempty"`
}

func (textPanel DashboardText) toOption() row.Option {
Expand All @@ -24,6 +25,9 @@ func (textPanel DashboardText) toOption() row.Option {
if textPanel.Span != 0 {
opts = append(opts, text.Span(textPanel.Span))
}
if len(textPanel.Links) != 0 {
opts = append(opts, text.Links(textPanel.Links.toModel()...))
}
if textPanel.Height != "" {
opts = append(opts, text.Height(textPanel.Height))
}
Expand Down
16 changes: 10 additions & 6 deletions decoder/timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ var ErrInvalidAxisScale = fmt.Errorf("invalid axis scale")

type DashboardTimeSeries struct {
Title string
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Span float32 `yaml:",omitempty"`
Height string `yaml:",omitempty"`
Transparent bool `yaml:",omitempty"`
Datasource string `yaml:",omitempty"`
Repeat string `yaml:",omitempty"`
Links DashboardPanelLinks `yaml:",omitempty"`
Targets []Target
Legend []string `yaml:",omitempty,flow"`
Alert *Alert `yaml:",omitempty"`
Expand Down Expand Up @@ -50,6 +51,9 @@ func (timeseriesPanel DashboardTimeSeries) toOption() (row.Option, error) {
if timeseriesPanel.Repeat != "" {
opts = append(opts, timeseries.Repeat(timeseriesPanel.Repeat))
}
if len(timeseriesPanel.Links) != 0 {
opts = append(opts, timeseries.Links(timeseriesPanel.Links.toModel()...))
}
if len(timeseriesPanel.Legend) != 0 {
legendOpts, err := timeseriesPanel.legend()
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/K-Phoen/grabana/axis"
"github.com/K-Phoen/grabana/errors"
"github.com/K-Phoen/grabana/graph/series"
"github.com/K-Phoen/grabana/links"
"github.com/K-Phoen/grabana/target/graphite"
"github.com/K-Phoen/grabana/target/influxdb"
"github.com/K-Phoen/grabana/target/prometheus"
Expand Down Expand Up @@ -120,6 +121,19 @@ func defaultAxes() Option {
}
}

// Links adds links to be displayed on this panel.
func Links(panelLinks ...links.Link) Option {
return func(graph *Graph) error {
graph.Builder.Links = make([]sdk.Link, 0, len(panelLinks))

for _, link := range panelLinks {
graph.Builder.Links = append(graph.Builder.Links, link.Builder)
}

return nil
}
}

// WithPrometheusTarget adds a prometheus query to the graph.
func WithPrometheusTarget(query string, options ...prometheus.Option) Option {
target := prometheus.New(query, options...)
Expand Down
10 changes: 10 additions & 0 deletions graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/K-Phoen/grabana/axis"
"github.com/K-Phoen/grabana/errors"
"github.com/K-Phoen/grabana/graph/series"
"github.com/K-Phoen/grabana/links"
"github.com/K-Phoen/grabana/target/stackdriver"
"github.com/stretchr/testify/require"
)
Expand All @@ -21,6 +22,15 @@ func TestNewGraphPanelsCanBeCreated(t *testing.T) {
req.Equal(float32(6), panel.Builder.Span)
}

func TestGraphPanelCanHaveLinks(t *testing.T) {
req := require.New(t)

panel, err := New("", Links(links.New("", "")))

req.NoError(err)
req.Len(panel.Builder.Links, 1)
}

func TestGraphPanelCanHavePrometheusTargets(t *testing.T) {
req := require.New(t)

Expand Down
Loading

0 comments on commit 092b4d3

Please sign in to comment.