mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 05:51:12 -05:00
Add comprehensive monitoring dashboard scaffolding: - Extend queryKeys with monitoring keys - Create hooks: useCombinedAlerts, useRecentActivity, useDatabaseHealth, useModerationHealth - Build UI components: SystemHealthStatus, CriticalAlertsPanel, MonitoringQuickStats, RecentActivityTimeline, MonitoringNavCards - Create MonitoringOverview page and integrate routing (MonitoringOverview route) - Wire AdminSidebar to include Monitoring navigation - Introduce supporting routing and admin layout hooks/utilities - Align with TanStack Query patterns and plan for auto-refresh and optimistic updates
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
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,
|
|
});
|
|
}
|