import { useQuery } from '@tanstack/react-query'; import { supabase } from '@/integrations/supabase/client'; import { queryKeys } from '@/lib/queryKeys'; export interface DatabaseHealth { status: 'healthy' | 'warning' | 'unhealthy'; recentErrors: number; checked_at: string; } export function useDatabaseHealth() { return useQuery({ queryKey: queryKeys.monitoring.databaseHealth(), queryFn: async () => { const threshold = new Date(Date.now() - 3600000); // 1 hour // Check for recent database errors const { count, error } = await supabase .from('request_metadata') .select('*', { count: 'exact', head: true }) .eq('error_type', 'database_error') .gte('created_at', threshold.toISOString()); if (error) { return { status: 'warning' as const, recentErrors: 0, checked_at: new Date().toISOString(), }; } const errorCount = count || 0; return { status: errorCount > 10 ? 'unhealthy' : errorCount > 5 ? 'warning' : 'healthy', recentErrors: errorCount, checked_at: new Date().toISOString(), } as DatabaseHealth; }, staleTime: 60000, refetchInterval: 120000, }); }