mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Fix panic when limit_alerts=0. (#86640)
Oversight in the TopK function meant if k=0, then we'd panic when checking element zero in the heap, because no items are ever allowed into the heap.
This commit is contained in:
parent
45a7f649fe
commit
f07f48616a
@ -250,6 +250,11 @@ func (by AlertsBy) TopK(alerts []Alert, k int) []Alert {
|
|||||||
// which is important for sorting alerts, as the comparison function is
|
// which is important for sorting alerts, as the comparison function is
|
||||||
// very expensive.
|
// very expensive.
|
||||||
|
|
||||||
|
// If k is zero or less, return nothing.
|
||||||
|
if k < 1 {
|
||||||
|
return []Alert{}
|
||||||
|
}
|
||||||
|
|
||||||
// The heap must be in ascending order, so that the root of the heap is
|
// The heap must be in ascending order, so that the root of the heap is
|
||||||
// the current smallest element.
|
// the current smallest element.
|
||||||
byAscending := func(a1, a2 *Alert) bool { return by(a2, a1) }
|
byAscending := func(a1, a2 *Alert) bool { return by(a2, a1) }
|
||||||
|
@ -113,6 +113,11 @@ func TestTopKAlertsByImportance(t *testing.T) {
|
|||||||
input []Alert
|
input []Alert
|
||||||
expected []Alert
|
expected []Alert
|
||||||
}{{
|
}{{
|
||||||
|
name: "no alerts are returned (k=0)",
|
||||||
|
k: 0,
|
||||||
|
input: []Alert{{State: "normal"}, {State: "nodata"}, {State: "error"}, {State: "pending"}, {State: "alerting"}},
|
||||||
|
expected: []Alert{},
|
||||||
|
}, {
|
||||||
name: "alerts are ordered in expected importance (k=1)",
|
name: "alerts are ordered in expected importance (k=1)",
|
||||||
k: 1,
|
k: 1,
|
||||||
input: []Alert{{State: "normal"}, {State: "nodata"}, {State: "error"}, {State: "pending"}, {State: "alerting"}},
|
input: []Alert{{State: "normal"}, {State: "nodata"}, {State: "error"}, {State: "pending"}, {State: "alerting"}},
|
||||||
|
Loading…
Reference in New Issue
Block a user