annotations.ts 842 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { getBackendSrv } from '@grafana/runtime';
  2. import { StateHistoryItem } from 'app/types/unified-alerting';
  3. export function fetchAnnotations(alertId: string): Promise<StateHistoryItem[]> {
  4. return getBackendSrv()
  5. .get('/api/annotations', {
  6. alertId,
  7. })
  8. .then((result) => {
  9. return result?.sort(sortStateHistory);
  10. });
  11. }
  12. export function sortStateHistory(a: StateHistoryItem, b: StateHistoryItem): number {
  13. const compareDesc = (a: number, b: number): number => {
  14. // Larger numbers first.
  15. if (a > b) {
  16. return -1;
  17. }
  18. if (b > a) {
  19. return 1;
  20. }
  21. return 0;
  22. };
  23. const endNeq = compareDesc(a.timeEnd, b.timeEnd);
  24. if (endNeq) {
  25. return endNeq;
  26. }
  27. const timeNeq = compareDesc(a.time, b.time);
  28. if (timeNeq) {
  29. return timeNeq;
  30. }
  31. return compareDesc(a.id, b.id);
  32. }