details.tsx 4.4 KB

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