monitors.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {
  2. addErrorMessage,
  3. addLoadingMessage,
  4. clearIndicators,
  5. } from 'sentry/actionCreators/indicator';
  6. import type {Client} from 'sentry/api';
  7. import {t} from 'sentry/locale';
  8. import type {ObjectStatus} from 'sentry/types';
  9. import {logException} from 'sentry/utils/logging';
  10. import type RequestError from 'sentry/utils/requestError/requestError';
  11. import type {Monitor} from 'sentry/views/monitors/types';
  12. export async function deleteMonitor(api: Client, orgId: string, monitor: Monitor) {
  13. addLoadingMessage(t('Deleting Monitor...'));
  14. try {
  15. await api.requestPromise(
  16. `/projects/${orgId}/${monitor.project.slug}/monitors/${monitor.slug}/`,
  17. {method: 'DELETE'}
  18. );
  19. clearIndicators();
  20. } catch {
  21. addErrorMessage(t('Unable to remove monitor.'));
  22. }
  23. }
  24. export async function deleteMonitorEnvironment(
  25. api: Client,
  26. orgId: string,
  27. monitor: Monitor,
  28. environment: string
  29. ): Promise<boolean> {
  30. addLoadingMessage(t('Deleting Environment...'));
  31. try {
  32. await api.requestPromise(
  33. `/projects/${orgId}/${monitor.project.slug}/monitors/${monitor.slug}/`,
  34. {
  35. method: 'DELETE',
  36. query: {environment},
  37. }
  38. );
  39. clearIndicators();
  40. return true;
  41. } catch {
  42. addErrorMessage(t('Unable to remove environment from monitor.'));
  43. }
  44. return false;
  45. }
  46. export async function updateMonitor(
  47. api: Client,
  48. orgId: string,
  49. monitor: Monitor,
  50. data: Partial<Monitor>
  51. ): Promise<Monitor | null> {
  52. addLoadingMessage();
  53. try {
  54. const resp = await api.requestPromise(
  55. `/projects/${orgId}/${monitor.project.slug}/monitors/${monitor.slug}/`,
  56. {method: 'PUT', data}
  57. );
  58. clearIndicators();
  59. return resp;
  60. } catch (err) {
  61. const respError: RequestError = err;
  62. const updateKeys = Object.keys(data);
  63. // If we are updating a single value in the monitor we can read the
  64. // validation error for that key, otherwise fallback to the default error
  65. const validationError =
  66. updateKeys.length === 1 ? respError.responseJSON?.[updateKeys[0]]?.[0] : undefined;
  67. logException(err);
  68. addErrorMessage(validationError ?? t('Unable to update monitor.'));
  69. }
  70. return null;
  71. }
  72. export async function setEnvironmentIsMuted(
  73. api: Client,
  74. orgId: string,
  75. monitor: Monitor,
  76. environment: string,
  77. isMuted: boolean
  78. ) {
  79. addLoadingMessage();
  80. try {
  81. const resp = await api.requestPromise(
  82. `/projects/${orgId}/${monitor.project.slug}/monitors/${monitor.slug}/environments/${environment}`,
  83. {method: 'PUT', data: {isMuted}}
  84. );
  85. clearIndicators();
  86. return resp;
  87. } catch (err) {
  88. logException(err);
  89. addErrorMessage(
  90. isMuted ? t('Unable to mute environment.') : t('Unable to unmute environment.')
  91. );
  92. }
  93. return null;
  94. }
  95. export interface BulkEditOperation {
  96. isMuted?: boolean;
  97. status?: ObjectStatus;
  98. }
  99. interface BulkEditResponse {
  100. errored: Monitor[];
  101. updated: Monitor[];
  102. }
  103. export async function bulkEditMonitors(
  104. api: Client,
  105. orgId: string,
  106. slugs: string[],
  107. operation: BulkEditOperation
  108. ): Promise<BulkEditResponse | null> {
  109. addLoadingMessage();
  110. try {
  111. const resp: BulkEditResponse = await api.requestPromise(
  112. `/organizations/${orgId}/monitors/`,
  113. {
  114. method: 'PUT',
  115. data: {...operation, slugs},
  116. }
  117. );
  118. clearIndicators();
  119. if (resp.errored?.length > 0) {
  120. addErrorMessage(t('Unable to apply the changes to all monitors'));
  121. }
  122. return resp;
  123. } catch (err) {
  124. logException(err);
  125. addErrorMessage(t('Unable to apply the changes to all monitors'));
  126. }
  127. return null;
  128. }