monitors.tsx 3.4 KB

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