File size: 2,018 Bytes
0ba2011
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
import { useState, useEffect } from "react";

export interface SystemStatusData {
    timestamp: string;
    uptime: {
        process: number;
        system: number;
    };
    memory: {
        rss: string;
        heapTotal: string;
        heapUsed: string;
    };
    system: {
        loadAvg: number[];
        freeMem: string;
        totalMem: string;
    };
}

export interface DiagnosticsData {
    timestamp: string;
    protocol: string;
    host: string;
    forwardedHost: string | null;
    ip: string | null;
    userAgent: string;
    env_flags: {
        AUTH_TRUST_HOST: string;
        SPACE_ID: string;
        NODE_ENV: string;
        HOSTNAME: string;
    };
    headers: Record<string, string>;
}

export function useSystemStatus() {
    const [status, setStatus] = useState<SystemStatusData | null>(null);
    const [diagnostics, setDiagnostics] = useState<DiagnosticsData | null>(null);
    const [loading, setLoading] = useState(true);
    const [error, setError] = useState<string | null>(null);

    const fetchData = async () => {
        try {
            const [healthRes, diagRes] = await Promise.all([
                fetch("/api/health"),
                fetch("/api/diag")
            ]);

            if (!healthRes.ok || !diagRes.ok) {
                throw new Error("Failed to fetch system data");
            }

            const [healthData, diagData] = await Promise.all([
                healthRes.json(),
                diagRes.json()
            ]);

            setStatus(healthData);
            setDiagnostics(diagData);
            setError(null);
        } catch (err) {
            setError(err instanceof Error ? err.message : "Unknown error");
        } finally {
            setLoading(false);
        }
    };

    useEffect(() => {
        fetchData();
        const interval = setInterval(fetchData, 10000); // 10s refresh
        return () => clearInterval(interval);
    }, []);

    return { status, diagnostics, loading, error, refetch: fetchData };
}