details.tsx 5.5 KB

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