()
| 27 | ]; |
| 28 | |
| 29 | export function useServiceHealth() { |
| 30 | const [services, setServices] = useState<ServiceStatus[]>( |
| 31 | SERVICE_CONFIGS.map((s) => ({ ...s, status: "checking" as const })) |
| 32 | ); |
| 33 | |
| 34 | const checkHealth = useCallback(async () => { |
| 35 | const updated = await Promise.all( |
| 36 | SERVICE_CONFIGS.map(async (config) => { |
| 37 | // Services without HTTP health endpoints - mark as unknown |
| 38 | if (config.noHealthCheck) { |
| 39 | return { ...config, status: "unknown" as const }; |
| 40 | } |
| 41 | |
| 42 | // Services with HTTP health endpoints |
| 43 | if (config.url) { |
| 44 | try { |
| 45 | const response = await fetch(config.url, { |
| 46 | method: "GET", |
| 47 | mode: "no-cors", |
| 48 | }); |
| 49 | return { |
| 50 | ...config, |
| 51 | status: response.ok || response.type === "opaque" ? "healthy" as const : "unhealthy" as const, |
| 52 | }; |
| 53 | } catch { |
| 54 | return { ...config, status: "unhealthy" as const }; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Fallback - no URL and not marked noHealthCheck |
| 59 | return { ...config, status: "unknown" as const }; |
| 60 | }) |
| 61 | ); |
| 62 | setServices(updated); |
| 63 | }, []); |
| 64 | |
| 65 | useEffect(() => { |
| 66 | checkHealth(); |
| 67 | const interval = setInterval(checkHealth, 30000); |
| 68 | return () => clearInterval(interval); |
| 69 | }, [checkHealth]); |
| 70 | |
| 71 | const getStatus = useCallback( |
| 72 | (id: string): "healthy" | "unhealthy" | "checking" | "unknown" => { |
| 73 | return services.find((s) => s.id === id)?.status ?? "checking"; |
| 74 | }, |
| 75 | [services] |
| 76 | ); |
| 77 | |
| 78 | // Only count services that have health checks in the totals |
| 79 | const checkableServices = services.filter((s) => !s.noHealthCheck); |
| 80 | const healthyCount = checkableServices.filter((s) => s.status === "healthy").length; |
| 81 | |
| 82 | return { services, getStatus, healthyCount, totalCount: checkableServices.length }; |
| 83 | } |
no test coverage detected