Merge pull request #2306 from EdSchouten/sorted-alerts

Use lexicographic order to sort alerts by name.
This commit is contained in:
Julius Volz 2016-12-31 13:12:30 +01:00 committed by GitHub
commit 90dd216646

View file

@ -274,7 +274,7 @@ func (h *Handler) Run() {
func (h *Handler) alerts(w http.ResponseWriter, r *http.Request) { func (h *Handler) alerts(w http.ResponseWriter, r *http.Request) {
alerts := h.ruleManager.AlertingRules() alerts := h.ruleManager.AlertingRules()
alertsSorter := byAlertStateSorter{alerts: alerts} alertsSorter := byAlertStateAndNameSorter{alerts: alerts}
sort.Sort(alertsSorter) sort.Sort(alertsSorter)
alertStatus := AlertStatus{ alertStatus := AlertStatus{
@ -541,18 +541,20 @@ type AlertStatus struct {
AlertStateToRowClass map[rules.AlertState]string AlertStateToRowClass map[rules.AlertState]string
} }
type byAlertStateSorter struct { type byAlertStateAndNameSorter struct {
alerts []*rules.AlertingRule alerts []*rules.AlertingRule
} }
func (s byAlertStateSorter) Len() int { func (s byAlertStateAndNameSorter) Len() int {
return len(s.alerts) return len(s.alerts)
} }
func (s byAlertStateSorter) Less(i, j int) bool { func (s byAlertStateAndNameSorter) Less(i, j int) bool {
return s.alerts[i].State() > s.alerts[j].State() return s.alerts[i].State() > s.alerts[j].State() ||
(s.alerts[i].State() == s.alerts[j].State() &&
s.alerts[i].Name() < s.alerts[j].Name())
} }
func (s byAlertStateSorter) Swap(i, j int) { func (s byAlertStateAndNameSorter) Swap(i, j int) {
s.alerts[i], s.alerts[j] = s.alerts[j], s.alerts[i] s.alerts[i], s.alerts[j] = s.alerts[j], s.alerts[i]
} }