Skip to content

Commit

Permalink
Support multiple alerts in Graph and TimeSeries panels
Browse files Browse the repository at this point in the history
The previous implementation only supported a single alert per Graph and TimeSeries. The current changes introduce the ability to configure multiple alerts per panel. Also, changes have been propagated to the corresponding tests in graph_test.go and timeseries_test.go to verify the new feature handling multiple alerts.
  • Loading branch information
lueurxax committed Apr 30, 2024
1 parent 234e2b7 commit 5a1d1be
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
9 changes: 5 additions & 4 deletions graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ const (
// Graph represents a graph panel.
type Graph struct {
Builder *sdk.Panel
Alert *alert.Alert
Alerts []*alert.Alert
}

// New creates a new graph panel.
func New(title string, options ...Option) (*Graph, error) {
panel := &Graph{Builder: sdk.NewGraph(title)}
panel := &Graph{Builder: sdk.NewGraph(title), Alerts: make([]*alert.Alert, 0)}

panel.Builder.AliasColors = make(map[string]interface{})
panel.Builder.IsNew = false
Expand Down Expand Up @@ -266,8 +266,9 @@ func XAxis(opts ...axis.Option) Option {
// Alert creates an alert for this graph.
func Alert(name string, opts ...alert.Option) Option {
return func(graph *Graph) error {
graph.Alert = alert.New(graph.Builder.Title, append(opts, alert.Summary(name))...)
graph.Alert.Builder.Name = graph.Builder.Title
al := alert.New(graph.Builder.Title, append(opts, alert.Summary(name))...)
al.Builder.Name = graph.Builder.Title
graph.Alerts = append(graph.Alerts, al)

return nil
}
Expand Down
17 changes: 15 additions & 2 deletions graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,21 @@ func TestAlertsCanBeConfigured(t *testing.T) {
panel, err := New("panel title", Alert("some alert"))

req.NoError(err)
req.NotNil(panel.Alert)
req.Equal("panel title", panel.Alert.Builder.Name)
req.NotNil(panel.Alerts)
req.Len(panel.Alerts, 1)
req.Equal("panel title", panel.Alerts[0].Builder.Name)
}

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

panel, err := New("panel title", Alert("some alert"), Alert("other alert"))

req.NoError(err)
req.NotNil(panel.Alerts)
req.Len(panel.Alerts, 2)
req.Equal("panel title", panel.Alerts[0].Builder.Name)
req.Equal("panel title", panel.Alerts[1].Builder.Name)
}

func TestDrawModeCanBeConfigured(t *testing.T) {
Expand Down
24 changes: 14 additions & 10 deletions row/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ func WithGraph(title string, options ...graph.Option) Option {

row.builder.Add(panel.Builder)

if panel.Alert == nil {
if panel.Alerts == nil || len(panel.Alerts) == 0 {
return nil
}

if panel.Builder.Datasource != nil {
panel.Alert.Datasource = panel.Builder.Datasource.LegacyName
}
for _, al := range panel.Alerts {
if panel.Builder.Datasource != nil {
al.Datasource = panel.Builder.Datasource.LegacyName
}

row.alerts = append(row.alerts, panel.Alert)
row.alerts = append(row.alerts, al)
}

return nil
}
Expand All @@ -82,15 +84,17 @@ func WithTimeSeries(title string, options ...timeseries.Option) Option {

row.builder.Add(panel.Builder)

if panel.Alert == nil {
if panel.Alerts == nil || len(panel.Alerts) == 0 {
return nil
}

if panel.Builder.Datasource != nil {
panel.Alert.Datasource = panel.Builder.Datasource.LegacyName
}
for _, al := range panel.Alerts {
if panel.Builder.Datasource != nil {
al.Datasource = panel.Builder.Datasource.LegacyName
}

row.alerts = append(row.alerts, panel.Alert)
row.alerts = append(row.alerts, al)
}

return nil
}
Expand Down
9 changes: 5 additions & 4 deletions timeseries/timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ const (
// TimeSeries represents a time series panel.
type TimeSeries struct {
Builder *sdk.Panel
Alert *alert.Alert
Alerts []*alert.Alert
}

// New creates a new time series panel.
func New(title string, options ...Option) (*TimeSeries, error) {
panel := &TimeSeries{Builder: sdk.NewTimeseries(title)}
panel := &TimeSeries{Builder: sdk.NewTimeseries(title), Alerts: make([]*alert.Alert, 0)}
panel.Builder.IsNew = false

for _, opt := range append(defaults(), options...) {
Expand Down Expand Up @@ -409,8 +409,9 @@ func Transparent() Option {
// Alert creates an alert for this graph.
func Alert(name string, opts ...alert.Option) Option {
return func(timeseries *TimeSeries) error {
timeseries.Alert = alert.New(timeseries.Builder.Title, append(opts, alert.Summary(name))...)
timeseries.Alert.Builder.Name = timeseries.Builder.Title
al := alert.New(timeseries.Builder.Title, append(opts, alert.Summary(name))...)
al.Builder.Name = timeseries.Builder.Title
timeseries.Alerts = append(timeseries.Alerts, al)

return nil
}
Expand Down
17 changes: 15 additions & 2 deletions timeseries/timeseries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,21 @@ func TestAlertsCanBeConfigured(t *testing.T) {
panel, err := New("panel title", Alert("some alert"))

req.NoError(err)
req.NotNil(panel.Alert)
req.Equal("panel title", panel.Alert.Builder.Name)
req.NotNil(panel.Alerts)
req.Len(panel.Alerts, 1)
req.Equal("panel title", panel.Alerts[0].Builder.Name)
}

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

panel, err := New("panel title", Alert("some alert"), Alert("other alert"))

req.NoError(err)
req.NotNil(panel.Alerts)
req.Len(panel.Alerts, 2)
req.Equal("panel title", panel.Alerts[0].Builder.Name)
req.Equal("panel title", panel.Alerts[1].Builder.Name)
}

func TestLineWidthCanBeConfigured(t *testing.T) {
Expand Down

0 comments on commit 5a1d1be

Please sign in to comment.