Spaces:
Paused
Paused
| import { useState, useEffect } from 'react'; | |
| import { API_URL } from '../../config/api'; | |
| export interface ReactorState { | |
| health: { | |
| status: string; | |
| score: number; | |
| services: Record<string, { healthy: boolean; optional?: boolean }>; | |
| }; | |
| stats: { | |
| nodes: number; | |
| relationships: number; | |
| }; | |
| logs: any[]; | |
| system: { | |
| cpu: number; | |
| memory: number; | |
| uptime: number; | |
| }; | |
| loading: boolean; | |
| } | |
| export const useReactorData = () => { | |
| const [data, setData] = useState<ReactorState>({ | |
| health: { status: 'UNKNOWN', score: 0, services: {} }, | |
| stats: { nodes: 0, relationships: 0 }, | |
| logs: [], | |
| system: { cpu: 0, memory: 0, uptime: 0 }, | |
| loading: true | |
| }); | |
| const fetchData = async () => { | |
| try { | |
| // Parallel fetch for speed | |
| const [healthRes, graphRes, logsRes, sysRes] = await Promise.all([ | |
| fetch(`${API_URL}/health`).catch(e => { console.error('Health fetch failed:', e); return { json: () => ({ status: 'OFFLINE', score: 0, services: {} }) }; }), | |
| fetch(`${API_URL}/api/evolution/graph/stats`).catch(e => { console.error('Graph stats fetch failed:', e); return { json: () => ({ stats: { nodes: 0, relationships: 0 } }) }; }), | |
| fetch(`${API_URL}/api/hyper/events`).catch(e => { console.error('Hyper events fetch failed:', e); return { json: () => ({ events: [] }) }; }), | |
| fetch(`${API_URL}/api/sys/stats`).catch(e => { console.error('Sys stats fetch failed:', e); return { json: () => ({ cpu: { load: 0 }, memory: { usedPercent: 0 }, uptime: 0 }) }; }) | |
| ]); | |
| const health = await healthRes.json(); | |
| const graph = await graphRes.json(); | |
| const logs = await logsRes.json(); | |
| const sys = await sysRes.json(); | |
| setData({ | |
| health: health, | |
| stats: { | |
| nodes: graph.stats?.nodes || 0, | |
| relationships: graph.stats?.relationships || 0 | |
| }, | |
| logs: logs.events || [], | |
| system: { | |
| cpu: Math.round(sys.cpu?.load || 0), | |
| memory: Math.round(sys.memory?.usedPercent || 0), | |
| uptime: Math.round(sys.uptime || 0) | |
| }, | |
| loading: false | |
| }); | |
| } catch (err) { | |
| console.error('Reactor Data Fetch Error:', err); | |
| setData(prev => ({ ...prev, loading: false })); | |
| } | |
| }; | |
| useEffect(() => { | |
| fetchData(); | |
| const interval = setInterval(fetchData, 5000); // Update every 5s | |
| return () => clearInterval(interval); | |
| }, []); | |
| return data; | |
| }; | |