Skip to content

Commit

Permalink
no idea whats going on here, this is an old branch
Browse files Browse the repository at this point in the history
  • Loading branch information
themarcelor committed Mar 12, 2024
1 parent c54fa57 commit 4bfb6da
Show file tree
Hide file tree
Showing 34 changed files with 692 additions and 1 deletion.
7 changes: 7 additions & 0 deletions alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,10 @@ func Tags(tags map[string]string) Option {
alert.Builder.Rules[0].Labels = tags
}
}

// DataSource sets the data source associated to the alert.
func DataSource(dataSource string) Option {
return func(alert *Alert) {
alert.HookDatasourceUID(dataSource)
}
}
28 changes: 28 additions & 0 deletions decoder/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestUnmarshalYAML(t *testing.T) {
logsPanel(),
statPanel(),
gaugePanel(),
graphPanelWithCloudwatchTarget(),
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -569,6 +570,33 @@ rows:
}
}

func graphPanelWithCloudwatchTarget() testCase {
yaml := `title: Awesome dashboard
rows:
- name: Test row
panels:
- graph:
title: Dummy
datasource: cloudwatch-test
targets:
- cloudwatch:
queryparams:
dimensions:
QueueName: "test-queue"
namespace: "AWS/SQS"
metricname: "NumberofMessagesReceived"
statistics:
- "Sum"
period: "30"
region: "us-east-1"
`

return testCase{
name: "single row with single graph panel and cloudwatch target",
yaml: yaml,
expectedGrafanaJSON: "graph_panel_cloudwatch_target.json"}
}

func singleStatPanel() testCase {
yaml := `title: Awesome dashboard
Expand Down
5 changes: 4 additions & 1 deletion decoder/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func (gaugePanel DashboardGauge) target(t Target) (gauge.Option, error) {

return gauge.WithStackdriverTarget(stackdriverTarget), nil
}

if t.Cloudwatch != nil {
return gauge.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
24 changes: 24 additions & 0 deletions decoder/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/K-Phoen/grabana/gauge"
"github.com/stretchr/testify/require"
"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/stretchr/testify/assert"
)

func TestGaugeValidValueTypes(t *testing.T) {
Expand Down Expand Up @@ -45,3 +47,25 @@ func TestGaugeValidValueTypes(t *testing.T) {
})
}
}

func TestGaugeCloudwatchTarget(t *testing.T) {

panel := DashboardGauge{
Title: "cloudwatch target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
3 changes: 3 additions & 0 deletions decoder/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ func (graphPanel *DashboardGraph) target(t Target) (graph.Option, error) {

return graph.WithStackdriverTarget(stackdriverTarget), nil
}
if t.Cloudwatch != nil {
return graph.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
Expand Down
3 changes: 3 additions & 0 deletions decoder/heatmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ func (heatmapPanel DashboardHeatmap) target(t Target) (heatmap.Option, error) {

return heatmap.WithStackdriverTarget(stackdriverTarget), nil
}
if t.Cloudwatch != nil {
return heatmap.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
23 changes: 23 additions & 0 deletions decoder/heatmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (

"github.com/K-Phoen/grabana/heatmap/axis"
"github.com/K-Phoen/grabana/row"
"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/K-Phoen/sdk"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)

func TestHeatmapCanBeDecoded(t *testing.T) {
Expand Down Expand Up @@ -123,3 +125,24 @@ func TestHeatmapCanNotBeDecodedIfTargetIsInvalid(t *testing.T) {
req.Error(err)
req.Equal(ErrTargetNotConfigured, err)
}

func TestHeatmapCloudwatchTarget(t *testing.T) {
panel := DashboardHeatmap{
Title: "heatmap target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
3 changes: 3 additions & 0 deletions decoder/singlestat.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ func (singleStatPanel DashboardSingleStat) target(t Target) (singlestat.Option,

return singlestat.WithStackdriverTarget(stackdriverTarget), nil
}
if t.Cloudwatch != nil {
return singlestat.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
23 changes: 23 additions & 0 deletions decoder/singlestat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/K-Phoen/grabana/singlestat"
"github.com/stretchr/testify/require"
"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/stretchr/testify/assert"
)

func TestValidValueTypes(t *testing.T) {
Expand Down Expand Up @@ -74,3 +76,24 @@ func TestSparkLineModes(t *testing.T) {
})
}
}

func TestSinglestatCloudwatchTarget(t *testing.T) {
panel := DashboardSingleStat{
Title: "Singlestat target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
3 changes: 3 additions & 0 deletions decoder/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ func (statPanel DashboardStat) target(t Target) (stat.Option, error) {

return stat.WithStackdriverTarget(stackdriverTarget), nil
}
if t.Cloudwatch != nil {
return stat.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
23 changes: 23 additions & 0 deletions decoder/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/K-Phoen/grabana/stat"
"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -77,3 +79,24 @@ func TestStatValidColorMode(t *testing.T) {
})
}
}

func TestStatCloudwatchTarget(t *testing.T) {
panel := DashboardStat{
Title: "Stat target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
3 changes: 3 additions & 0 deletions decoder/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func (tablePanel *DashboardTable) target(t Target) (table.Option, error) {
if t.InfluxDB != nil {
return table.WithInfluxDBTarget(t.InfluxDB.Query, t.InfluxDB.toOptions()...), nil
}
if t.Cloudwatch != nil {
return table.WithCloudwatchTarget(t.Cloudwatch.QueryParams, t.Cloudwatch.toOptions()...), nil
}

return nil, ErrTargetNotConfigured
}
29 changes: 29 additions & 0 deletions decoder/table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package decoder

import (
"testing"

"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/stretchr/testify/assert"
)

func TestTableCloudwatchTarget(t *testing.T) {
panel := DashboardTable{
Title: "Table target test",
Targets: []Target{
{
Cloudwatch: &CloudwatchTarget{
QueryParams: cloudwatch.QueryParams{
Dimensions: map[string]string{
"Name": "test",
},
},
},
},
},
}

option, err := panel.target(panel.Targets[0])
assert.NoError(t, err)
assert.NotNil(t, option)
}
20 changes: 20 additions & 0 deletions decoder/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package decoder
import (
"fmt"

"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/K-Phoen/grabana/target/graphite"
"github.com/K-Phoen/grabana/target/influxdb"
"github.com/K-Phoen/grabana/target/loki"
Expand All @@ -22,6 +23,7 @@ type Target struct {
InfluxDB *InfluxDBTarget `yaml:"influxdb,omitempty"`
Stackdriver *StackdriverTarget `yaml:",omitempty"`
Loki *LokiTarget `yaml:",omitempty"`
Cloudwatch *CloudwatchTarget `yaml:",omitempty"`
}

type PrometheusTarget struct {
Expand Down Expand Up @@ -325,3 +327,21 @@ func (t StackdriverAlignment) toOption() (stackdriver.Option, error) {
return nil, ErrInvalidStackdriverAlignment
}
}

type CloudwatchTarget struct {
QueryParams cloudwatch.QueryParams `yaml:",omitempty"`
Ref string `yaml:",omitempty"`
Hidden bool `yaml:",omitempty"`
}

func (t CloudwatchTarget) toOptions() []cloudwatch.Option {
opts := []cloudwatch.Option{
cloudwatch.Ref(t.Ref),
}

if t.Hidden {
opts = append(opts, cloudwatch.Hide())
}

return opts
}
24 changes: 24 additions & 0 deletions decoder/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package decoder
import (
"testing"

"github.com/K-Phoen/grabana/target/cloudwatch"
"github.com/K-Phoen/grabana/target/influxdb"

"github.com/K-Phoen/grabana/target/graphite"
Expand Down Expand Up @@ -341,3 +342,26 @@ func TestGraphiteHiddenTarget(t *testing.T) {

req.True(target.Builder.Hide)
}

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

query := cloudwatch.QueryParams{}
opts := CloudwatchTarget{
QueryParams: query,
}.toOptions()
target := cloudwatch.New(query, opts...)

req.False(target.Builder.Hide)
}

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

query := cloudwatch.QueryParams{}

opts := CloudwatchTarget{Hidden: true}.toOptions()
target := cloudwatch.New(query, opts...)

req.True(target.Builder.Hide)
}
Loading

0 comments on commit 4bfb6da

Please sign in to comment.