monitors.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/core';
  9. import {logException} from 'sentry/utils/logging';
  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 ? 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. ids: 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, ids},
  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. }
  129. export async function deleteMonitorProcessingErrorByType(
  130. api: Client,
  131. orgId: string,
  132. projectId: string,
  133. monitorSlug: string,
  134. errortype: ProcessingErrorType
  135. ) {
  136. addLoadingMessage();
  137. try {
  138. await api.requestPromise(
  139. `/projects/${orgId}/${projectId}/monitors/${monitorSlug}/processing-errors/`,
  140. {
  141. method: 'DELETE',
  142. query: {errortype},
  143. }
  144. );
  145. clearIndicators();
  146. } catch (err) {
  147. logException(err);
  148. if (err.status === 403) {
  149. addErrorMessage(t('You do not have permission to dismiss these processing errors'));
  150. } else {
  151. addErrorMessage(t('Unable to dismiss the processing errors'));
  152. }
  153. }
  154. }
  155. export async function deleteProjectProcessingErrorByType(
  156. api: Client,
  157. orgId: string,
  158. projectId: string,
  159. errortype: ProcessingErrorType
  160. ) {
  161. addLoadingMessage();
  162. try {
  163. await api.requestPromise(`/projects/${orgId}/${projectId}/processing-errors/`, {
  164. method: 'DELETE',
  165. query: {errortype},
  166. });
  167. clearIndicators();
  168. } catch (err) {
  169. logException(err);
  170. if (err.status === 403) {
  171. addErrorMessage(t('You do not have permission to dismiss these processing errors'));
  172. } else {
  173. addErrorMessage(t('Unable to dismiss the processing errors'));
  174. }
  175. }
  176. }