details.tsx 5.7 KB

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