details.tsx 4.6 KB

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