Skip to content

Commit

Permalink
bus: breakdown totals
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Feb 27, 2024
1 parent 0ef1757 commit 7c5b3c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
28 changes: 16 additions & 12 deletions alerts/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
36 changes: 18 additions & 18 deletions internal/testing/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down

0 comments on commit 7c5b3c9

Please sign in to comment.