diff --git a/alerts/alerts.go b/alerts/alerts.go index a14d460b6..1ebebbc80 100644 --- a/alerts/alerts.go +++ b/alerts/alerts.go @@ -72,16 +72,21 @@ type ( } AlertsResponse struct { - Alerts []Alert `json:"alerts"` - HasMore bool `json:"hasMore"` - Total int `json:"total"` - TotalInfo int `json:"totalInfo"` - TotalWarning int `json:"totalWarning"` - TotalError int `json:"totalError"` - TotalCritical int `json:"totalCritical"` + Alerts []Alert `json:"alerts"` + HasMore bool `json:"hasMore"` + Totals struct { + Info int `json:"info"` + Warning int `json:"warning"` + Error int `json:"error"` + Critical int `json:"critical"` + } `json:"total"` } ) +func (ar AlertsResponse) Total() int { + return ar.Totals.Info + ar.Totals.Warning + ar.Totals.Error + ar.Totals.Critical +} + // String implements the fmt.Stringer interface. func (s Severity) String() string { switch s { @@ -194,15 +199,14 @@ func (m *Manager) Alerts(_ context.Context, opts AlertsOpts) (AlertsResponse, er alerts := make([]Alert, 0, len(m.alerts)) for _, a := range m.alerts { - resp.Total++ if a.Severity == SeverityInfo { - resp.TotalInfo++ + resp.Totals.Info++ } else if a.Severity == SeverityWarning { - resp.TotalWarning++ + resp.Totals.Warning++ } else if a.Severity == SeverityError { - resp.TotalError++ + resp.Totals.Error++ } else if a.Severity == SeverityCritical { - resp.TotalCritical++ + resp.Totals.Critical++ } if opts.Severity != 0 && a.Severity != opts.Severity { continue // filter by severity diff --git a/internal/testing/cluster_test.go b/internal/testing/cluster_test.go index 69b318f66..6b5f88769 100644 --- a/internal/testing/cluster_test.go +++ b/internal/testing/cluster_test.go @@ -1992,24 +1992,24 @@ func TestAlerts(t *testing.T) { for severity := alerts.SeverityInfo; severity <= alerts.SeverityCritical; severity++ { ar, err = b.Alerts(context.Background(), alerts.AlertsOpts{Severity: severity}) tt.OK(err) - if ar.Total != 32 { - t.Fatal("expected 32 alerts", ar.Total) - } else if ar.TotalInfo != 3 { - t.Fatal("expected 3 info alerts", ar.TotalInfo) - } else if ar.TotalWarning != 6 { - t.Fatal("expected 6 warning alerts", ar.TotalWarning) - } else if ar.TotalError != 9 { - t.Fatal("expected 9 error alerts", ar.TotalError) - } else if ar.TotalCritical != 14 { - t.Fatal("expected 14 critical alerts", ar.TotalCritical) - } else if severity == alerts.SeverityInfo && len(ar.Alerts) != ar.TotalInfo { - t.Fatalf("expected %v info alerts, got %v", ar.TotalInfo, len(ar.Alerts)) - } else if severity == alerts.SeverityWarning && len(ar.Alerts) != ar.TotalWarning { - t.Fatalf("expected %v warning alerts, got %v", ar.TotalWarning, len(ar.Alerts)) - } else if severity == alerts.SeverityError && len(ar.Alerts) != ar.TotalError { - t.Fatalf("expected %v error alerts, got %v", ar.TotalError, len(ar.Alerts)) - } else if severity == alerts.SeverityCritical && len(ar.Alerts) != ar.TotalCritical { - t.Fatalf("expected %v critical alerts, got %v", ar.TotalCritical, len(ar.Alerts)) + if ar.Total() != 32 { + t.Fatal("expected 32 alerts", ar.Total()) + } else if ar.Totals.Info != 3 { + t.Fatal("expected 3 info alerts", ar.Totals.Info) + } else if ar.Totals.Warning != 6 { + t.Fatal("expected 6 warning alerts", ar.Totals.Warning) + } else if ar.Totals.Error != 9 { + t.Fatal("expected 9 error alerts", ar.Totals.Error) + } else if ar.Totals.Critical != 14 { + t.Fatal("expected 14 critical alerts", ar.Totals.Critical) + } else if severity == alerts.SeverityInfo && len(ar.Alerts) != ar.Totals.Info { + t.Fatalf("expected %v info alerts, got %v", ar.Totals.Info, len(ar.Alerts)) + } else if severity == alerts.SeverityWarning && len(ar.Alerts) != ar.Totals.Warning { + t.Fatalf("expected %v warning alerts, got %v", ar.Totals.Warning, len(ar.Alerts)) + } else if severity == alerts.SeverityError && len(ar.Alerts) != ar.Totals.Error { + t.Fatalf("expected %v error alerts, got %v", ar.Totals.Error, len(ar.Alerts)) + } else if severity == alerts.SeverityCritical && len(ar.Alerts) != ar.Totals.Critical { + t.Fatalf("expected %v critical alerts, got %v", ar.Totals.Critical, len(ar.Alerts)) } } }