From c6103e764dda3f5593cd67c4eaf1103d53735f92 Mon Sep 17 00:00:00 2001 From: Jordi Enric Date: Thu, 17 Oct 2024 14:25:10 +0200 Subject: [PATCH] handle state checking project events --- .../interfaces/Home/SecurityStatus.tsx | 10 +-- .../interfaces/Home/ServiceStatus.tsx | 61 ++++++++++--------- apps/studio/data/project-events/keys.ts | 9 +++ .../project-events/project-events-query.ts | 46 ++++++++++++++ 4 files changed, 92 insertions(+), 34 deletions(-) create mode 100644 apps/studio/data/project-events/keys.ts create mode 100644 apps/studio/data/project-events/project-events-query.ts diff --git a/apps/studio/components/interfaces/Home/SecurityStatus.tsx b/apps/studio/components/interfaces/Home/SecurityStatus.tsx index eeba9e89523..c8c5edb915d 100644 --- a/apps/studio/components/interfaces/Home/SecurityStatus.tsx +++ b/apps/studio/components/interfaces/Home/SecurityStatus.tsx @@ -82,11 +82,11 @@ export const SecurityStatus = () => { return ( lints.length > 0 && ( <> - -
+ +
diff --git a/apps/studio/components/interfaces/Home/ServiceStatus.tsx b/apps/studio/components/interfaces/Home/ServiceStatus.tsx index 146c58ef5a0..46f466f1ec4 100644 --- a/apps/studio/components/interfaces/Home/ServiceStatus.tsx +++ b/apps/studio/components/interfaces/Home/ServiceStatus.tsx @@ -17,6 +17,7 @@ import { Popover_Shadcn_, } from 'ui' import { PopoverSeparator } from '@ui/components/shadcn/ui/popover' +import { useProjectEventsQuery } from 'data/project-events/project-events-query' const SERVICE_STATUS_THRESHOLD = 5 // minutes @@ -30,7 +31,7 @@ const StatusMessage = ({ isProjectNew: boolean }) => { if (isLoading) return 'Checking status' - if (isProjectNew) return 'Coming up...' + if (isProjectNew && !isSuccess) return 'Coming up...' if (isSuccess) return 'No issues' return 'Unable to connect' } @@ -44,7 +45,9 @@ const StatusIcon = ({ isSuccess: boolean isProjectNew: boolean }) => { - if (isLoading || isProjectNew) return + if (isLoading) return + if (isProjectNew && !isSuccess) + return if (isSuccess) return return } @@ -168,40 +171,40 @@ const ServiceStatus = () => { const isLoadingChecks = services.some((service) => service.isLoading) const allServicesOperational = services.every((service) => service.isSuccess) - // If the project is less than 5 minutes old, and status is not operational, then it's likely the service is still starting up - const isProjectNew = + const [dateRange] = useState<{ startDate: string; endDate: string }>({ + startDate: dayjs().subtract(30, 'minute').toISOString(), + endDate: dayjs().toISOString(), + }) + + const { data: restoreProjectEvents, isLoading: projectEventsLoading } = useProjectEventsQuery({ + ref, + startDate: dateRange.startDate, + endDate: dateRange.endDate, + eventTypes: [ + 'project.infra_restart_or_resize_initiated', + 'project.restore_initiated', + 'project.restored_from_logical_backup', + 'project.restored_from_pitr', + ], + }) + + console.log('events', restoreProjectEvents) + + const projectWasRestoredRecently = !projectEventsLoading && !!restoreProjectEvents?.length + + // If the project is less than SERVICE_STATUS_THRESHOLD minutes old, and status is not operational, then it's likely the service is still starting up + const isProjectNewOrRestoredRecently = + projectWasRestoredRecently || dayjs.utc().diff(dayjs.utc(project?.inserted_at), 'minute') < SERVICE_STATUS_THRESHOLD || project?.status === 'COMING_UP' - useEffect(() => { - let timer: any - - if (isProjectNew) { - const secondsSinceProjectCreated = dayjs - .utc() - .diff(dayjs.utc(project?.inserted_at), 'seconds') - const remainingTimeTillNextCheck = SERVICE_STATUS_THRESHOLD * 60 - secondsSinceProjectCreated - - timer = setTimeout(() => { - refetchServiceStatus() - refetchPostgresServiceStatus() - refetchEdgeFunctionServiceStatus() - }, remainingTimeTillNextCheck * 1000) - } - - return () => { - clearTimeout(timer) - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [isProjectNew]) - return (