details.tsx 5.5 KB

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