Files
Joe Blubaugh 7d5cb170c6 Alerting: Sort StateHistoryItem after fetch instead of on render. (#47842)
PR #47674 attempted to sort a read-only managed async array. This change
moves the sort logic to the fetch code so sort happens once on fetch, to
a mutable array, rather than trying on each render for an immutable
array.

Signed-off-by: Joe Blubaugh <joe.blubaugh@grafana.com>
2022-04-19 16:05:28 +08:00

39 lines
842 B
TypeScript

import { getBackendSrv } from '@grafana/runtime';
import { StateHistoryItem } from 'app/types/unified-alerting';
export function fetchAnnotations(alertId: string): Promise<StateHistoryItem[]> {
return getBackendSrv()
.get('/api/annotations', {
alertId,
})
.then((result) => {
return result?.sort(sortStateHistory);
});
}
export function sortStateHistory(a: StateHistoryItem, b: StateHistoryItem): number {
const compareDesc = (a: number, b: number): number => {
// Larger numbers first.
if (a > b) {
return -1;
}
if (b > a) {
return 1;
}
return 0;
};
const endNeq = compareDesc(a.timeEnd, b.timeEnd);
if (endNeq) {
return endNeq;
}
const timeNeq = compareDesc(a.time, b.time);
if (timeNeq) {
return timeNeq;
}
return compareDesc(a.id, b.id);
}