actions.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { getBackendSrv, locationService } from '@grafana/runtime';
  2. import { notifyApp } from 'app/core/actions';
  3. import { createErrorNotification, createSuccessNotification } from 'app/core/copy/appNotification';
  4. import { AlertRuleDTO, NotifierDTO, ThunkResult } from 'app/types';
  5. import { loadAlertRules, loadedAlertRules, notificationChannelLoaded, setNotificationChannels } from './reducers';
  6. export function getAlertRulesAsync(options: { state: string }): ThunkResult<void> {
  7. return async (dispatch) => {
  8. dispatch(loadAlertRules());
  9. const rules: AlertRuleDTO[] = await getBackendSrv().get('/api/alerts', options);
  10. dispatch(loadedAlertRules(rules));
  11. };
  12. }
  13. export function togglePauseAlertRule(id: number, options: { paused: boolean }): ThunkResult<void> {
  14. return async (dispatch) => {
  15. await getBackendSrv().post(`/api/alerts/${id}/pause`, options);
  16. const stateFilter = locationService.getSearchObject().state || 'all';
  17. dispatch(getAlertRulesAsync({ state: stateFilter.toString() }));
  18. };
  19. }
  20. export function createNotificationChannel(data: any): ThunkResult<Promise<void>> {
  21. return async (dispatch) => {
  22. try {
  23. await getBackendSrv().post(`/api/alert-notifications`, data);
  24. dispatch(notifyApp(createSuccessNotification('Notification created')));
  25. locationService.push('/alerting/notifications');
  26. } catch (error) {
  27. dispatch(notifyApp(createErrorNotification(error.data.error)));
  28. }
  29. };
  30. }
  31. export function updateNotificationChannel(data: any): ThunkResult<void> {
  32. return async (dispatch) => {
  33. try {
  34. await getBackendSrv().put(`/api/alert-notifications/${data.id}`, data);
  35. dispatch(notifyApp(createSuccessNotification('Notification updated')));
  36. } catch (error) {
  37. dispatch(notifyApp(createErrorNotification(error.data.error)));
  38. }
  39. };
  40. }
  41. export function testNotificationChannel(data: any): ThunkResult<void> {
  42. return async (dispatch, getState) => {
  43. const channel = getState().notificationChannel.notificationChannel;
  44. await getBackendSrv().post('/api/alert-notifications/test', { id: channel.id, ...data });
  45. };
  46. }
  47. export function loadNotificationTypes(): ThunkResult<void> {
  48. return async (dispatch) => {
  49. const alertNotifiers: NotifierDTO[] = await getBackendSrv().get(`/api/alert-notifiers`);
  50. const notificationTypes = alertNotifiers.sort((o1, o2) => {
  51. if (o1.name > o2.name) {
  52. return 1;
  53. }
  54. return -1;
  55. });
  56. dispatch(setNotificationChannels(notificationTypes));
  57. };
  58. }
  59. export function loadNotificationChannel(id: number): ThunkResult<void> {
  60. return async (dispatch) => {
  61. await dispatch(loadNotificationTypes());
  62. const notificationChannel = await getBackendSrv().get(`/api/alert-notifications/${id}`);
  63. dispatch(notificationChannelLoaded(notificationChannel));
  64. };
  65. }