monitors.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import * as Sentry from '@sentry/react';
  2. import {
  3. addErrorMessage,
  4. addLoadingMessage,
  5. clearIndicators,
  6. } from 'sentry/actionCreators/indicator';
  7. import type {Client} from 'sentry/api';
  8. import {t} from 'sentry/locale';
  9. import type {ObjectStatus} from 'sentry/types/core';
  10. import type RequestError from 'sentry/utils/requestError/requestError';
  11. import type {Monitor, ProcessingErrorType} 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
  67. ? (respError.responseJSON?.[updateKeys[0]!] as any)?.[0]
  68. : undefined;
  69. Sentry.captureException(err);
  70. addErrorMessage(validationError ?? t('Unable to update monitor.'));
  71. }
  72. return null;
  73. }
  74. export async function setEnvironmentIsMuted(
  75. api: Client,
  76. orgId: string,
  77. monitor: Monitor,
  78. environment: string,
  79. isMuted: boolean
  80. ) {
  81. addLoadingMessage();
  82. try {
  83. const resp = await api.requestPromise(
  84. `/projects/${orgId}/${monitor.project.slug}/monitors/${monitor.slug}/environments/${environment}`,
  85. {method: 'PUT', data: {isMuted}}
  86. );
  87. clearIndicators();
  88. return resp;
  89. } catch (err) {
  90. Sentry.captureException(err);
  91. addErrorMessage(
  92. isMuted ? t('Unable to mute environment.') : t('Unable to unmute environment.')
  93. );
  94. }
  95. return null;
  96. }
  97. export interface BulkEditOperation {
  98. isMuted?: boolean;
  99. status?: ObjectStatus;
  100. }
  101. interface BulkEditResponse {
  102. errored: Monitor[];
  103. updated: Monitor[];
  104. }
  105. export async function bulkEditMonitors(
  106. api: Client,
  107. orgId: string,
  108. ids: string[],
  109. operation: BulkEditOperation
  110. ): Promise<BulkEditResponse | null> {
  111. addLoadingMessage();
  112. try {
  113. const resp: BulkEditResponse = await api.requestPromise(
  114. `/organizations/${orgId}/monitors/`,
  115. {
  116. method: 'PUT',
  117. data: {...operation, ids},
  118. }
  119. );
  120. clearIndicators();
  121. if (resp.errored?.length > 0) {
  122. addErrorMessage(t('Unable to apply the changes to all monitors'));
  123. }
  124. return resp;
  125. } catch (err) {
  126. Sentry.captureException(err);
  127. addErrorMessage(t('Unable to apply the changes to all monitors'));
  128. }
  129. return null;
  130. }
  131. export async function deleteMonitorProcessingErrorByType(
  132. api: Client,
  133. orgId: string,
  134. projectId: string,
  135. monitorSlug: string,
  136. errortype: ProcessingErrorType
  137. ) {
  138. addLoadingMessage();
  139. try {
  140. await api.requestPromise(
  141. `/projects/${orgId}/${projectId}/monitors/${monitorSlug}/processing-errors/`,
  142. {
  143. method: 'DELETE',
  144. query: {errortype},
  145. }
  146. );
  147. clearIndicators();
  148. } catch (err) {
  149. Sentry.captureException(err);
  150. if (err.status === 403) {
  151. addErrorMessage(t('You do not have permission to dismiss these processing errors'));
  152. } else {
  153. addErrorMessage(t('Unable to dismiss the processing errors'));
  154. }
  155. }
  156. }
  157. export async function deleteProjectProcessingErrorByType(
  158. api: Client,
  159. orgId: string,
  160. projectId: string,
  161. errortype: ProcessingErrorType
  162. ) {
  163. addLoadingMessage();
  164. try {
  165. await api.requestPromise(`/projects/${orgId}/${projectId}/processing-errors/`, {
  166. method: 'DELETE',
  167. query: {errortype},
  168. });
  169. clearIndicators();
  170. } catch (err) {
  171. Sentry.captureException(err);
  172. if (err.status === 403) {
  173. addErrorMessage(t('You do not have permission to dismiss these processing errors'));
  174. } else {
  175. addErrorMessage(t('Unable to dismiss the processing errors'));
  176. }
  177. }
  178. }