apis.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { getBackendSrv } from 'app/core/services/backend_srv';
  2. import alertDef from './alertDef';
  3. import moment from 'moment';
  4. export interface AlertRule {
  5. id: number;
  6. dashboardId: number;
  7. panelId: number;
  8. name: string;
  9. state: string;
  10. stateText: string;
  11. stateIcon: string;
  12. stateClass: string;
  13. stateAge: string;
  14. info?: string;
  15. url: string;
  16. }
  17. export function setStateFields(rule, state) {
  18. const stateModel = alertDef.getStateDisplayModel(state);
  19. rule.state = state;
  20. rule.stateText = stateModel.text;
  21. rule.stateIcon = stateModel.iconClass;
  22. rule.stateClass = stateModel.stateClass;
  23. rule.stateAge = moment(rule.newStateDate)
  24. .fromNow()
  25. .replace(' ago', '');
  26. }
  27. export const getAlertRules = async (): Promise<AlertRule[]> => {
  28. try {
  29. const rules = await getBackendSrv().get('/api/alerts', {});
  30. for (const rule of rules) {
  31. setStateFields(rule, rule.state);
  32. if (rule.state !== 'paused') {
  33. if (rule.executionError) {
  34. rule.info = 'Execution Error: ' + rule.executionError;
  35. }
  36. if (rule.evalData && rule.evalData.noData) {
  37. rule.info = 'Query returned no data';
  38. }
  39. }
  40. }
  41. return rules;
  42. } catch (error) {
  43. console.error(error);
  44. throw error;
  45. }
  46. };