File size: 2,457 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
};