|
| 1 | +import { useEffect, useState } from 'react' |
| 2 | +import { useTranslation } from 'react-i18next' |
| 3 | +import StatCard from './StatCard' |
| 4 | +import { nodesApi, p2pApi } from '../utils/api' |
| 5 | + |
| 6 | +// ClusterSummary shows merged totals across both transports. Self-contained |
| 7 | +// (own lightweight fetch) so the page composes without lifting state out of the |
| 8 | +// two large section components. |
| 9 | +export default function ClusterSummary({ distributedEnabled, p2pEnabled }) { |
| 10 | + const { t } = useTranslation('admin') |
| 11 | + const [nats, setNats] = useState({ nodes: 0, inFlight: 0 }) |
| 12 | + const [swarm, setSwarm] = useState({ online: 0, total: 0 }) |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + let active = true |
| 16 | + async function load() { |
| 17 | + if (distributedEnabled) { |
| 18 | + try { |
| 19 | + const list = await nodesApi.list() |
| 20 | + const nodes = Array.isArray(list) ? list : (list?.nodes ?? []) |
| 21 | + if (active) setNats({ nodes: nodes.length, inFlight: nodes.reduce((a, n) => a + (n.in_flight_count || 0), 0) }) |
| 22 | + } catch { /* leave zeros */ } |
| 23 | + } |
| 24 | + if (p2pEnabled) { |
| 25 | + try { |
| 26 | + const stats = await p2pApi.getStats() |
| 27 | + const online = (stats?.federated?.online || 0) + (stats?.llama_cpp_workers?.online || 0) + (stats?.mlx_workers?.online || 0) |
| 28 | + const total = (stats?.federated?.total || 0) + (stats?.llama_cpp_workers?.total || 0) + (stats?.mlx_workers?.total || 0) |
| 29 | + if (active) setSwarm({ online, total }) |
| 30 | + } catch { /* leave zeros */ } |
| 31 | + } |
| 32 | + } |
| 33 | + load() |
| 34 | + return () => { active = false } |
| 35 | + }, [distributedEnabled, p2pEnabled]) |
| 36 | + |
| 37 | + return ( |
| 38 | + <div className="stat-grid" style={{ marginBottom: 'var(--spacing-lg)' }}> |
| 39 | + {distributedEnabled && <StatCard icon="fas fa-network-wired" label={t('cluster.summary.nodes', 'Distributed nodes')} value={nats.nodes} />} |
| 40 | + {distributedEnabled && <StatCard icon="fas fa-bolt" label={t('cluster.summary.inFlight', 'In-flight requests')} value={nats.inFlight} />} |
| 41 | + {p2pEnabled && <StatCard icon="fas fa-circle-nodes" label={t('cluster.summary.peers', 'Swarm peers online')} value={`${swarm.online}/${swarm.total}`} />} |
| 42 | + </div> |
| 43 | + ) |
| 44 | +} |
0 commit comments