Browse Source

feat(crons): Poll for first check-in (#49270)

Polls every 5 seconds for a first check-in if we have not received any
check-ins on the monitor yet

Fixes: https://github.com/getsentry/sentry/issues/46901
David Wang 1 year ago
parent
commit
bb3b499a7b
1 changed files with 18 additions and 3 deletions
  1. 18 3
      static/app/views/monitors/details.tsx

+ 18 - 3
static/app/views/monitors/details.tsx

@@ -21,8 +21,14 @@ import MonitorStats from './components/monitorStats';
 import MonitorOnboarding from './components/onboarding';
 import {Monitor} from './types';
 
+const DEFAULT_POLL_INTERVAL_MS = 5000;
+
 type Props = RouteComponentProps<{monitorSlug: string}, {}>;
 
+function hasLastCheckIn(monitor: Monitor) {
+  return monitor.environments.some(e => e.lastCheckIn);
+}
+
 function MonitorDetails({params, location}: Props) {
   const {selection} = usePageFilters();
 
@@ -38,7 +44,17 @@ function MonitorDetails({params, location}: Props) {
     {query: {...location.query, environment}},
   ] as const;
 
-  const {data: monitor} = useApiQuery<Monitor>(queryKey, {staleTime: 0});
+  const {data: monitor} = useApiQuery<Monitor>(queryKey, {
+    staleTime: 0,
+    // Refetches while we are waiting for the user to send their first check-in
+    refetchInterval: data => {
+      if (!data) {
+        return false;
+      }
+      const [monitorData] = data;
+      return hasLastCheckIn(monitorData) ? false : DEFAULT_POLL_INTERVAL_MS;
+    },
+  });
 
   function onUpdate(data: Monitor) {
     const updatedMonitor = {
@@ -59,7 +75,6 @@ function MonitorDetails({params, location}: Props) {
     );
   }
 
-  const hasLastCheckIn = monitor.environments.some(e => e.lastCheckIn);
   const envsSortedByLastCheck = monitor.environments.sort((a, b) =>
     a.lastCheckIn.localeCompare(b.lastCheckIn)
   );
@@ -74,7 +89,7 @@ function MonitorDetails({params, location}: Props) {
               <DatePageFilter alignDropdown="left" />
               <EnvironmentPageFilter />
             </StyledPageFilterBar>
-            {!hasLastCheckIn ? (
+            {!hasLastCheckIn(monitor) ? (
               <MonitorOnboarding orgId={organization.slug} monitor={monitor} />
             ) : (
               <Fragment>