details.tsx 6.4 KB

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