details.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import sortBy from 'lodash/sortBy';
  5. import {
  6. deleteMonitorProcessingErrorByType,
  7. updateMonitor,
  8. } from 'sentry/actionCreators/monitors';
  9. import Alert from 'sentry/components/alert';
  10. import * as Layout from 'sentry/components/layouts/thirds';
  11. import LoadingError from 'sentry/components/loadingError';
  12. import LoadingIndicator from 'sentry/components/loadingIndicator';
  13. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  14. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  15. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  16. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  17. import {t} from 'sentry/locale';
  18. import {space} from 'sentry/styles/space';
  19. import {setApiQueryData, useApiQuery, useQueryClient} from 'sentry/utils/queryClient';
  20. import useApi from 'sentry/utils/useApi';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import DetailsSidebar from 'sentry/views/monitors/components/detailsSidebar';
  23. import {DetailsTimeline} from 'sentry/views/monitors/components/detailsTimeline';
  24. import MonitorProcessingErrors from 'sentry/views/monitors/components/processingErrors/monitorProcessingErrors';
  25. import {makeMonitorErrorsQueryKey} from 'sentry/views/monitors/components/processingErrors/utils';
  26. import {makeMonitorDetailsQueryKey} from 'sentry/views/monitors/utils';
  27. import MonitorCheckIns from './components/monitorCheckIns';
  28. import MonitorHeader from './components/monitorHeader';
  29. import MonitorIssues from './components/monitorIssues';
  30. import MonitorStats from './components/monitorStats';
  31. import MonitorOnboarding from './components/onboarding';
  32. import {StatusToggleButton} from './components/statusToggleButton';
  33. import type {CheckinProcessingError, Monitor, ProcessingErrorType} from './types';
  34. const DEFAULT_POLL_INTERVAL_MS = 5000;
  35. type Props = RouteComponentProps<{monitorSlug: string; projectId: string}, {}>;
  36. function hasLastCheckIn(monitor: Monitor) {
  37. return monitor.environments.some(e => e.lastCheckIn);
  38. }
  39. function MonitorDetails({params, location}: Props) {
  40. const api = useApi();
  41. const organization = useOrganization();
  42. const queryClient = useQueryClient();
  43. const queryKey = makeMonitorDetailsQueryKey(
  44. organization,
  45. params.projectId,
  46. params.monitorSlug,
  47. {
  48. environment: location.query.environment,
  49. }
  50. );
  51. const {data: monitor, isError} = useApiQuery<Monitor>(queryKey, {
  52. staleTime: 0,
  53. refetchOnWindowFocus: true,
  54. // Refetches while we are waiting for the user to send their first check-in
  55. refetchInterval: data => {
  56. if (!data) {
  57. return false;
  58. }
  59. const [monitorData] = data;
  60. return hasLastCheckIn(monitorData) ? false : DEFAULT_POLL_INTERVAL_MS;
  61. },
  62. });
  63. const {data: checkinErrors, refetch: refetchErrors} = useApiQuery<
  64. CheckinProcessingError[]
  65. >(makeMonitorErrorsQueryKey(organization, params.projectId, params.monitorSlug), {
  66. staleTime: 0,
  67. refetchOnWindowFocus: true,
  68. });
  69. function onUpdate(data: Monitor) {
  70. const updatedMonitor = {
  71. ...data,
  72. // TODO(davidenwang): This is a bit of a hack, due to the PUT request
  73. // which pauses/unpauses a monitor not returning monitor environments
  74. // we should reuse the environments retrieved from the initial request
  75. environments: monitor?.environments,
  76. };
  77. setApiQueryData(queryClient, queryKey, updatedMonitor);
  78. }
  79. const handleUpdate = async (data: Partial<Monitor>) => {
  80. if (monitor === undefined) {
  81. return;
  82. }
  83. const resp = await updateMonitor(api, organization.slug, monitor, data);
  84. if (resp !== null) {
  85. onUpdate(resp);
  86. }
  87. };
  88. function handleDismissError(errortype: ProcessingErrorType) {
  89. deleteMonitorProcessingErrorByType(
  90. api,
  91. organization.slug,
  92. params.projectId,
  93. params.monitorSlug,
  94. errortype
  95. );
  96. refetchErrors();
  97. }
  98. if (isError) {
  99. return (
  100. <LoadingError message={t('The monitor you were looking for was not found.')} />
  101. );
  102. }
  103. if (!monitor) {
  104. return (
  105. <Layout.Page>
  106. <LoadingIndicator />
  107. </Layout.Page>
  108. );
  109. }
  110. const envsSortedByLastCheck = sortBy(monitor.environments, e => e.lastCheckIn);
  111. return (
  112. <SentryDocumentTitle title={`Crons — ${monitor.name}`}>
  113. <Layout.Page>
  114. <MonitorHeader
  115. monitor={monitor}
  116. orgSlug={organization.slug}
  117. onUpdate={onUpdate}
  118. />
  119. <Layout.Body>
  120. <Layout.Main>
  121. <StyledPageFilterBar condensed>
  122. <DatePageFilter />
  123. <EnvironmentPageFilter />
  124. </StyledPageFilterBar>
  125. {monitor.status === 'disabled' && (
  126. <Alert
  127. type="muted"
  128. showIcon
  129. trailingItems={
  130. <StatusToggleButton
  131. monitor={monitor}
  132. size="xs"
  133. onToggleStatus={status => handleUpdate({status})}
  134. >
  135. {t('Enable')}
  136. </StatusToggleButton>
  137. }
  138. >
  139. {t('This monitor is disabled and is not accepting check-ins.')}
  140. </Alert>
  141. )}
  142. {!!checkinErrors?.length && (
  143. <MonitorProcessingErrors
  144. checkinErrors={checkinErrors}
  145. onDismiss={handleDismissError}
  146. >
  147. {t('Errors were encountered while ingesting check-ins for this monitor')}
  148. </MonitorProcessingErrors>
  149. )}
  150. {!hasLastCheckIn(monitor) ? (
  151. <MonitorOnboarding monitor={monitor} />
  152. ) : (
  153. <Fragment>
  154. <DetailsTimeline organization={organization} monitor={monitor} />
  155. <MonitorStats
  156. orgSlug={organization.slug}
  157. monitor={monitor}
  158. monitorEnvs={monitor.environments}
  159. />
  160. <MonitorIssues
  161. orgSlug={organization.slug}
  162. monitor={monitor}
  163. monitorEnvs={monitor.environments}
  164. />
  165. <MonitorCheckIns
  166. orgSlug={organization.slug}
  167. monitor={monitor}
  168. monitorEnvs={monitor.environments}
  169. />
  170. </Fragment>
  171. )}
  172. </Layout.Main>
  173. <Layout.Side>
  174. <DetailsSidebar
  175. monitorEnv={envsSortedByLastCheck[envsSortedByLastCheck.length - 1]}
  176. monitor={monitor}
  177. />
  178. </Layout.Side>
  179. </Layout.Body>
  180. </Layout.Page>
  181. </SentryDocumentTitle>
  182. );
  183. }
  184. const StyledPageFilterBar = styled(PageFilterBar)`
  185. margin-bottom: ${space(2)};
  186. `;
  187. export default MonitorDetails;