details.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 usePageFilters from 'sentry/utils/usePageFilters';
  19. import {CronDetailsTimeline} from 'sentry/views/monitors/components/cronDetailsTimeline';
  20. import DetailsSidebar from 'sentry/views/monitors/components/detailsSidebar';
  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 {selection} = usePageFilters();
  36. const organization = useOrganization();
  37. const queryClient = useQueryClient();
  38. // TODO(epurkhiser): For now we just use the fist environment OR production
  39. // if we have all environments selected
  40. const environment = selection.environments[0];
  41. const queryKey = [
  42. `/organizations/${organization.slug}/monitors/${params.monitorSlug}/`,
  43. {query: {...location.query, environment}},
  44. ] as const;
  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.slug, data);
  72. onUpdate(resp);
  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 monitor={monitor} orgId={organization.slug} onUpdate={onUpdate} />
  86. <Layout.Body>
  87. <Layout.Main>
  88. <StyledPageFilterBar condensed>
  89. <DatePageFilter />
  90. <EnvironmentPageFilter />
  91. </StyledPageFilterBar>
  92. {monitor.status === 'disabled' && (
  93. <Alert
  94. type="muted"
  95. showIcon
  96. trailingItems={
  97. <StatusToggleButton
  98. monitor={monitor}
  99. size="xs"
  100. onClick={() => handleUpdate({status: 'active'})}
  101. >
  102. {t('Reactivate')}
  103. </StatusToggleButton>
  104. }
  105. >
  106. {t('This monitor is disabled and is not accepting check-ins.')}
  107. </Alert>
  108. )}
  109. {!hasLastCheckIn(monitor) ? (
  110. <MonitorOnboarding monitor={monitor} />
  111. ) : (
  112. <Fragment>
  113. <CronDetailsTimeline organization={organization} monitor={monitor} />
  114. <MonitorStats
  115. orgSlug={organization.slug}
  116. monitor={monitor}
  117. monitorEnvs={monitor.environments}
  118. />
  119. <MonitorIssues
  120. orgSlug={organization.slug}
  121. monitor={monitor}
  122. monitorEnvs={monitor.environments}
  123. />
  124. <MonitorCheckIns
  125. orgSlug={organization.slug}
  126. monitor={monitor}
  127. monitorEnvs={monitor.environments}
  128. />
  129. </Fragment>
  130. )}
  131. </Layout.Main>
  132. <Layout.Side>
  133. <DetailsSidebar
  134. monitorEnv={envsSortedByLastCheck[envsSortedByLastCheck.length - 1]}
  135. monitor={monitor}
  136. />
  137. </Layout.Side>
  138. </Layout.Body>
  139. </Layout.Page>
  140. </SentryDocumentTitle>
  141. );
  142. }
  143. const StyledPageFilterBar = styled(PageFilterBar)`
  144. margin-bottom: ${space(2)};
  145. `;
  146. export default MonitorDetails;