details.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 DatePageFilter from 'sentry/components/datePageFilter';
  6. import * as Layout from 'sentry/components/layouts/thirds';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  9. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  10. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  11. import {space} from 'sentry/styles/space';
  12. import {setApiQueryData, useApiQuery, useQueryClient} from 'sentry/utils/queryClient';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. import usePageFilters from 'sentry/utils/usePageFilters';
  15. import {CronDetailsTimeline} from 'sentry/views/monitors/components/cronDetailsTimeline';
  16. import DetailsSidebar from 'sentry/views/monitors/components/detailsSidebar';
  17. import MonitorCheckIns from './components/monitorCheckIns';
  18. import MonitorHeader from './components/monitorHeader';
  19. import MonitorIssues from './components/monitorIssues';
  20. import MonitorStats from './components/monitorStats';
  21. import MonitorOnboarding from './components/onboarding';
  22. import {Monitor} from './types';
  23. const DEFAULT_POLL_INTERVAL_MS = 5000;
  24. type Props = RouteComponentProps<{monitorSlug: string}, {}>;
  25. function hasLastCheckIn(monitor: Monitor) {
  26. return monitor.environments.some(e => e.lastCheckIn);
  27. }
  28. function MonitorDetails({params, location}: Props) {
  29. const {selection} = usePageFilters();
  30. const organization = useOrganization();
  31. const queryClient = useQueryClient();
  32. // TODO(epurkhiser): For now we just use the fist environment OR production
  33. // if we have all environments selected
  34. const environment = selection.environments[0];
  35. const queryKey = [
  36. `/organizations/${organization.slug}/monitors/${params.monitorSlug}/`,
  37. {query: {...location.query, environment}},
  38. ] as const;
  39. const {data: monitor} = useApiQuery<Monitor>(queryKey, {
  40. staleTime: 0,
  41. // Refetches while we are waiting for the user to send their first check-in
  42. refetchInterval: data => {
  43. if (!data) {
  44. return false;
  45. }
  46. const [monitorData] = data;
  47. return hasLastCheckIn(monitorData) ? false : DEFAULT_POLL_INTERVAL_MS;
  48. },
  49. });
  50. function onUpdate(data: Monitor) {
  51. const updatedMonitor = {
  52. ...data,
  53. // TODO(davidenwang): This is a bit of a hack, due to the PUT request
  54. // which pauses/unpauses a monitor not returning monitor environments
  55. // we should reuse the environments retrieved from the initial request
  56. environments: monitor?.environments,
  57. };
  58. setApiQueryData(queryClient, queryKey, updatedMonitor);
  59. }
  60. if (!monitor) {
  61. return (
  62. <Layout.Page>
  63. <LoadingIndicator />
  64. </Layout.Page>
  65. );
  66. }
  67. const envsSortedByLastCheck = sortBy(monitor.environments, e => e.lastCheckIn);
  68. return (
  69. <SentryDocumentTitle title={`Crons — ${monitor.name}`}>
  70. <Layout.Page>
  71. <MonitorHeader monitor={monitor} orgId={organization.slug} onUpdate={onUpdate} />
  72. <Layout.Body>
  73. <Layout.Main>
  74. <StyledPageFilterBar condensed>
  75. <DatePageFilter alignDropdown="left" />
  76. <EnvironmentPageFilter />
  77. </StyledPageFilterBar>
  78. {!hasLastCheckIn(monitor) ? (
  79. <MonitorOnboarding monitor={monitor} />
  80. ) : (
  81. <Fragment>
  82. <CronDetailsTimeline organization={organization} monitor={monitor} />
  83. <MonitorStats
  84. orgSlug={organization.slug}
  85. monitor={monitor}
  86. monitorEnvs={monitor.environments}
  87. />
  88. <MonitorIssues
  89. orgSlug={organization.slug}
  90. monitor={monitor}
  91. monitorEnvs={monitor.environments}
  92. />
  93. <MonitorCheckIns
  94. orgSlug={organization.slug}
  95. monitor={monitor}
  96. monitorEnvs={monitor.environments}
  97. />
  98. </Fragment>
  99. )}
  100. </Layout.Main>
  101. <Layout.Side>
  102. <DetailsSidebar
  103. monitorEnv={envsSortedByLastCheck[envsSortedByLastCheck.length - 1]}
  104. monitor={monitor}
  105. />
  106. </Layout.Side>
  107. </Layout.Body>
  108. </Layout.Page>
  109. </SentryDocumentTitle>
  110. );
  111. }
  112. const StyledPageFilterBar = styled(PageFilterBar)`
  113. margin-bottom: ${space(2)};
  114. `;
  115. export default MonitorDetails;