details.tsx 5.4 KB

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