monitors.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {
  2. addErrorMessage,
  3. addLoadingMessage,
  4. clearIndicators,
  5. } from 'sentry/actionCreators/indicator';
  6. import {Client} from 'sentry/api';
  7. import {t} from 'sentry/locale';
  8. import {logException} from 'sentry/utils/logging';
  9. import RequestError from 'sentry/utils/requestError/requestError';
  10. import {Monitor} from 'sentry/views/monitors/types';
  11. export async function deleteMonitor(api: Client, orgId: string, monitorSlug: string) {
  12. addLoadingMessage(t('Deleting Monitor...'));
  13. try {
  14. await api.requestPromise(`/organizations/${orgId}/monitors/${monitorSlug}/`, {
  15. method: 'DELETE',
  16. });
  17. clearIndicators();
  18. } catch {
  19. addErrorMessage(t('Unable to remove monitor.'));
  20. }
  21. }
  22. export async function deleteMonitorEnvironment(
  23. api: Client,
  24. orgId: string,
  25. monitorSlug: string,
  26. environment: string
  27. ): Promise<boolean> {
  28. addLoadingMessage(t('Deleting Environment...'));
  29. try {
  30. await api.requestPromise(`/organizations/${orgId}/monitors/${monitorSlug}/`, {
  31. method: 'DELETE',
  32. query: {
  33. environment,
  34. },
  35. });
  36. clearIndicators();
  37. return true;
  38. } catch {
  39. addErrorMessage(t('Unable to remove environment from monitor.'));
  40. }
  41. return false;
  42. }
  43. export async function updateMonitor(
  44. api: Client,
  45. orgId: string,
  46. monitorSlug: string,
  47. data: Partial<Monitor>
  48. ): Promise<Monitor | null> {
  49. addLoadingMessage();
  50. try {
  51. const resp = await api.requestPromise(
  52. `/organizations/${orgId}/monitors/${monitorSlug}/`,
  53. {method: 'PUT', data}
  54. );
  55. clearIndicators();
  56. return resp;
  57. } catch (err) {
  58. const respError: RequestError = err;
  59. const updateKeys = Object.keys(data);
  60. // If we are updating a single value in the monitor we can read the
  61. // validation error for that key, otherwise fallback to the default error
  62. const validationError =
  63. updateKeys.length === 1 ? respError.responseJSON?.[updateKeys[0]]?.[0] : undefined;
  64. logException(err);
  65. addErrorMessage(validationError ?? t('Unable to update monitor.'));
  66. }
  67. return null;
  68. }
  69. export async function setEnvironmentIsMuted(
  70. api: Client,
  71. orgId: string,
  72. monitorSlug: string,
  73. environment: string,
  74. isMuted: boolean
  75. ) {
  76. addLoadingMessage();
  77. try {
  78. const resp = await api.requestPromise(
  79. `/organizations/${orgId}/monitors/${monitorSlug}/environments/${environment}`,
  80. {method: 'PUT', data: {isMuted}}
  81. );
  82. clearIndicators();
  83. return resp;
  84. } catch (err) {
  85. logException(err);
  86. addErrorMessage(
  87. isMuted ? t('Unable to mute environment.') : t('Unable to unmute environment.')
  88. );
  89. }
  90. return null;
  91. }