add null check to HostSystemPage

This commit is contained in:
NlightN22 2024-11-27 16:19:12 +07:00
parent 9bbd36c434
commit 2e1a8bebae
2 changed files with 37 additions and 31 deletions

View File

@ -50,15 +50,15 @@ const HostSystemPage = () => {
}) })
const mapCameraData = useCallback(() => { const mapCameraData = useCallback(() => {
if (!data) return [] if (!data || !data.cameras) return []
return Object.entries(data.cameras).flatMap(([name, stats]) => { return Object.entries(data.cameras).flatMap(([name, stats]) => {
return (['Ffmpeg', 'Capture', 'Detect'] as ProcessType[]).map(type => { return (['Ffmpeg', 'Capture', 'Detect'] as ProcessType[]).map(type => {
const pid = type === ProcessType.Ffmpeg ? stats.ffmpeg_pid : const pid = type === ProcessType.Ffmpeg ? stats.ffmpeg_pid :
type === ProcessType.Capture ? stats.capture_pid : stats.pid; type === ProcessType.Capture ? stats.capture_pid : stats.pid;
const fps = type === ProcessType.Ffmpeg ? stats.camera_fps : const fps = type === ProcessType.Ffmpeg ? stats.camera_fps :
type === ProcessType.Capture ? stats.process_fps : stats.detection_fps; type === ProcessType.Capture ? stats.process_fps : stats.detection_fps;
const cpu = data.cpu_usages[pid]?.cpu; const cpu = data.cpu_usages ? data.cpu_usages[pid]?.cpu : '0'
const mem = data.cpu_usages[pid]?.mem; const mem = data.cpu_usages ? data.cpu_usages[pid]?.mem : '0'
return { return {
cameraName: name, cameraName: name,
@ -105,25 +105,30 @@ const HostSystemPage = () => {
} }
}) })
const gpuStats = Object.entries(data.gpu_usages).map(([name, stats]) => { const gpuStats = () => {
return ( if (!data?.gpu_usages) return null
<Grid.Col key={name + stats.gpu} xs={7} sm={6} md={5} lg={4} p='0.2rem'>
return Object.entries(data.gpu_usages).map(([name, stats]) => (
<Grid.Col key={`${name}-${stats.gpu}`} xs={7} sm={6} md={5} lg={4} p="0.2rem">
<GpuStat <GpuStat
name={name} name={name}
decoder={stats.dec} decoder={stats.dec}
encoder={stats.enc} encoder={stats.enc}
gpu={stats.gpu} gpu={stats.gpu}
mem={stats.mem} mem={stats.mem}
onVaInfoClick={() => handleVaInfoClick()} onVaInfoClick={handleVaInfoClick}
/> />
</Grid.Col> </Grid.Col>
) ))
}) };
const detectorsStats = Object.entries(data.detectors).map(([name, stats]) => { const detectorsStats = () => {
if (!data?.detectors) return null
Object.entries(data.detectors).map(([name, stats]) => {
const pid = stats.pid const pid = stats.pid
const cpu = data.cpu_usages[pid]?.cpu; const cpu = data.cpu_usages ? data.cpu_usages[pid]?.cpu : '0'
const mem = data.cpu_usages[pid]?.mem; const mem = data.cpu_usages ? data.cpu_usages[pid]?.mem : '0'
return ( return (
<Grid.Col key={pid} xs={6} sm={5} md={4} lg={3} p='0.2rem'> <Grid.Col key={pid} xs={6} sm={5} md={4} lg={3} p='0.2rem'>
<DetectorsStat <DetectorsStat
@ -136,6 +141,7 @@ const HostSystemPage = () => {
</Grid.Col> </Grid.Col>
) )
}) })
}
const handleFfprobeClick = (cameraName: string) => openContextModal({ const handleFfprobeClick = (cameraName: string) => openContextModal({
modal: 'ffprobeModal', modal: 'ffprobeModal',
@ -164,8 +170,8 @@ const HostSystemPage = () => {
{storageStats} {storageStats}
</Grid> </Grid>
<Grid mt='sm' justify="center" mb='sm' align='stretch'> <Grid mt='sm' justify="center" mb='sm' align='stretch'>
{gpuStats} {gpuStats()}
{detectorsStats} {detectorsStats()}
</Grid> </Grid>
<SegmentedControl <SegmentedControl
value={selector} value={selector}

View File

@ -35,17 +35,17 @@ export interface Stream {
export interface FrigateStats { export interface FrigateStats {
cameras: { cameras: {
[cameraName: string]: CameraStat [cameraName: string]: CameraStat
} } | undefined
cpu_usages: { cpu_usages: {
[processId: string]: ProcessStat [processId: string]: ProcessStat
} } | undefined
detection_fps: number detection_fps: number
detectors: { detectors: {
[detectorName: string]: DetectorStat [detectorName: string]: DetectorStat
} } | undefined
gpu_usages: { gpu_usages: {
[gpuName: string]: GpuStat [gpuName: string]: GpuStat
} } | undefined
processes: Processes processes: Processes
service: Service service: Service
} }