Skip to content

Commit

Permalink
Remove redundant alert name assignment
Browse files Browse the repository at this point in the history
Removed the redundant line where alert name was being assigned with the Builder title in both graph.go and timeseries.go. Also updated the alert creation test in alert_test.go to reflect this change.
  • Loading branch information
lueurxax committed Apr 30, 2024
1 parent 2e021d1 commit ccb6596
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
5 changes: 0 additions & 5 deletions alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ func (client *Client) AddAlert(ctx context.Context, namespace string, alertDefin

alertDefinition.HookDatasourceUID(datasourceUID)

// Before we can add this alert, we need to delete any other alert that might exist for this dashboard and panel
if err := client.DeleteAlertGroup(ctx, namespace, alertDefinition.Builder.Name); err != nil && !errors.Is(err, ErrAlertNotFound) {
return fmt.Errorf("could not delete existing alerts: %w", err)
}

buf, err := json.Marshal(alertDefinition.Builder)
if err != nil {
return err
Expand Down
13 changes: 13 additions & 0 deletions dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ func (client *Client) UpsertDashboard(ctx context.Context, folder *Folder, build
return nil, err
}

alertPanelNames := make(map[string]struct{})
for _, al := range alerts {
alertPanelNames[al.PanelName] = struct{}{}
}

// Clean alerts by panel name
for panelName := range alertPanelNames {
// Before we can add this alert, we need to delete any other alert that might exist for this dashboard and panel
if err := client.DeleteAlertGroup(ctx, folder.Title, panelName); err != nil && !errors.Is(err, ErrAlertNotFound) {
return nil, fmt.Errorf("could not delete existing alerts: %w", err)
}
}

for i := range alerts {
alert := *alerts[i]

Expand Down
2 changes: 1 addition & 1 deletion dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ func TestDashboardsCanBeCreatedWithNewAlertsAndDeletesPreviousAlerts(t *testing.
req.NoError(err)

req.JSONEq(`{
"name": "Heap allocations",
"name": "Too many heap allocations",
"interval": "1m",
"rules": [
{
Expand Down
9 changes: 6 additions & 3 deletions graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ func TestAlertsCanBeConfigured(t *testing.T) {
req.NoError(err)
req.NotNil(panel.Alerts)
req.Len(panel.Alerts, 1)
req.Equal("panel title", panel.Alerts[0].Builder.Name)
req.Equal("some alert", panel.Alerts[0].Builder.Name)
req.Equal("panel title", panel.Alerts[0].PanelName)
}

func TestTwoAlertsCanBeConfigured(t *testing.T) {
Expand All @@ -171,8 +172,10 @@ func TestTwoAlertsCanBeConfigured(t *testing.T) {
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)
req.Equal("panel title", panel.Alerts[0].PanelName)
req.Equal("panel title", panel.Alerts[1].PanelName)
req.Equal("some alert", panel.Alerts[0].Builder.Name)
req.Equal("other alert", panel.Alerts[1].Builder.Name)
}

func TestDrawModeCanBeConfigured(t *testing.T) {
Expand Down
9 changes: 6 additions & 3 deletions timeseries/timeseries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func TestAlertsCanBeConfigured(t *testing.T) {
req.NoError(err)
req.NotNil(panel.Alerts)
req.Len(panel.Alerts, 1)
req.Equal("panel title", panel.Alerts[0].Builder.Name)
req.Equal("panel title", panel.Alerts[0].PanelName)
req.Equal("some alert", panel.Alerts[0].Builder.Name)
}

func TestTwoAlertsCanBeConfigured(t *testing.T) {
Expand All @@ -157,8 +158,10 @@ func TestTwoAlertsCanBeConfigured(t *testing.T) {
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)
req.Equal("panel title", panel.Alerts[0].PanelName)
req.Equal("panel title", panel.Alerts[1].PanelName)
req.Equal("some alert", panel.Alerts[0].Builder.Name)
req.Equal("other alert", panel.Alerts[1].Builder.Name)
}

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

0 comments on commit ccb6596

Please sign in to comment.