import useSWR from "swr"; import { FrigateStats } from "@/types/stats"; import { useEffect, useMemo, useState } from "react"; import SystemGraph from "@/components/graph/SystemGraph"; import { useFrigateStats } from "@/api/ws"; import TimeAgo from "@/components/dynamic/TimeAgo"; import { FrigateConfig } from "@/types/frigateConfig"; import { DetectorCpuThreshold, DetectorMemThreshold, InferenceThreshold, } from "@/types/graph"; function System() { const { data: config } = useSWR("config"); // stats chunks // stats collection const { data: initialStats } = useSWR("stats/history", { revalidateOnFocus: false, }); const { payload: updatedStats } = useFrigateStats(); const [statsHistory, setStatsHistory] = useState( initialStats || [], ); const lastUpdated = useMemo(() => { if (updatedStats) { return updatedStats.service.last_updated; } if (initialStats) { return initialStats.at(-1)?.service?.last_updated; } return undefined; }, [initialStats, updatedStats]); useEffect(() => { if (initialStats == undefined) { return; } if (statsHistory.length < initialStats.length) { setStatsHistory(initialStats); return; } setStatsHistory([...statsHistory, updatedStats]); }, [initialStats, updatedStats]); // stats data pieces const detInferenceTimeSeries = useMemo(() => { if (!statsHistory) { return []; } const series: { [key: string]: { name: string; data: { x: object; y: number }[] }; } = {}; statsHistory.forEach((stats) => { if (!stats) { return; } const statTime = new Date(stats.service.last_updated * 1000); Object.entries(stats.detectors).forEach(([key, stats]) => { if (!(key in series)) { series[key] = { name: `${key} (${stats.pid})`, data: [] }; } series[key].data.push({ x: statTime, y: stats.inference_speed }); }); }); return Object.values(series); }, [statsHistory]); const detCpuSeries = useMemo(() => { if (!statsHistory) { return []; } const series: { [key: string]: { name: string; data: { x: object; y: string }[] }; } = {}; statsHistory.forEach((stats) => { if (!stats) { return; } const statTime = new Date(stats.service.last_updated * 1000); Object.entries(stats.detectors).forEach(([key, detStats]) => { if (!(key in series)) { series[key] = { name: key, data: [] }; } series[key].data.push({ x: statTime, y: stats.cpu_usages[detStats.pid.toString()].cpu, }); }); }); return Object.values(series); }, [statsHistory]); const detMemSeries = useMemo(() => { if (!statsHistory) { return []; } const series: { [key: string]: { name: string; data: { x: object; y: string }[] }; } = {}; statsHistory.forEach((stats) => { if (!stats) { return; } const statTime = new Date(stats.service.last_updated * 1000); Object.entries(stats.detectors).forEach(([key, detStats]) => { if (!(key in series)) { series[key] = { name: key, data: [] }; } series[key].data.push({ x: statTime, y: stats.cpu_usages[detStats.pid.toString()].mem, }); }); }); return Object.values(series); }, [statsHistory]); const gpuSeries = useMemo(() => { if (!statsHistory) { return []; } const series: { [key: string]: { name: string; data: { x: object; y: string }[] }; } = {}; statsHistory.forEach((stats) => { if (!stats) { return; } const statTime = new Date(stats.service.last_updated * 1000); Object.entries(stats.gpu_usages || []).forEach(([key, stats]) => { if (!(key in series)) { series[key] = { name: key, data: [] }; } series[key].data.push({ x: statTime, y: stats.gpu }); }); }); return Object.keys(series).length > 0 ? Object.values(series) : []; }, [statsHistory]); const gpuMemSeries = useMemo(() => { if (!statsHistory) { return []; } const series: { [key: string]: { name: string; data: { x: object; y: string }[] }; } = {}; statsHistory.forEach((stats) => { if (!stats) { return; } const statTime = new Date(stats.service.last_updated * 1000); Object.entries(stats.gpu_usages || {}).forEach(([key, stats]) => { if (!(key in series)) { series[key] = { name: key, data: [] }; } series[key].data.push({ x: statTime, y: stats.mem }); }); }); return Object.values(series); }, [statsHistory]); const cameraCpuSeries = useMemo(() => { if (!statsHistory || statsHistory.length == 0) { return {}; } const series: { [cam: string]: { [key: string]: { name: string; data: { x: object; y: string }[] }; }; } = {}; statsHistory.forEach((stats) => { if (!stats) { return; } const statTime = new Date(stats.service.last_updated * 1000); Object.entries(stats.cameras).forEach(([key, camStats]) => { if (!config?.cameras[key].enabled) { return; } if (!(key in series)) { const camName = key.replaceAll("_", " "); series[key] = {}; series[key]["ffmpeg"] = { name: `${camName} ffmpeg`, data: [] }; series[key]["capture"] = { name: `${camName} capture`, data: [] }; series[key]["detect"] = { name: `${camName} detect`, data: [] }; } series[key]["ffmpeg"].data.push({ x: statTime, y: stats.cpu_usages[camStats.ffmpeg_pid.toString()]?.cpu ?? 0.0, }); series[key]["capture"].data.push({ x: statTime, y: stats.cpu_usages[camStats.capture_pid?.toString()]?.cpu ?? 0, }); series[key]["detect"].data.push({ x: statTime, y: stats.cpu_usages[camStats.pid.toString()].cpu, }); }); }); return series; }, [statsHistory]); const cameraFpsSeries = useMemo(() => { if (!statsHistory) { return {}; } const series: { [cam: string]: { [key: string]: { name: string; data: { x: object; y: number }[] }; }; } = {}; statsHistory.forEach((stats) => { if (!stats) { return; } const statTime = new Date(stats.service.last_updated * 1000); Object.entries(stats.cameras).forEach(([key, camStats]) => { if (!(key in series)) { const camName = key.replaceAll("_", " "); series[key] = {}; series[key]["det"] = { name: `${camName} detections`, data: [] }; series[key]["skip"] = { name: `${camName} skipped detections`, data: [], }; } series[key]["det"].data.push({ x: statTime, y: camStats.detection_fps, }); series[key]["skip"].data.push({ x: statTime, y: camStats.skipped_fps, }); }); }); return series; }, [statsHistory]); const otherProcessCpuSeries = useMemo(() => { if (!statsHistory) { return []; } const series: { [key: string]: { name: string; data: { x: object; y: string }[] }; } = {}; statsHistory.forEach((stats) => { if (!stats) { return; } const statTime = new Date(stats.service.last_updated * 1000); Object.entries(stats.processes).forEach(([key, procStats]) => { if (procStats.pid.toString() in stats.cpu_usages) { if (!(key in series)) { series[key] = { name: `${key} (${procStats.pid})`, data: [] }; } series[key].data.push({ x: statTime, y: stats.cpu_usages[procStats.pid.toString()].cpu, }); } }); }); return Object.keys(series).length > 0 ? Object.values(series) : []; }, [statsHistory]); const otherProcessMemSeries = useMemo(() => { if (!statsHistory) { return []; } const series: { [key: string]: { name: string; data: { x: object; y: string }[] }; } = {}; statsHistory.forEach((stats) => { if (!stats) { return; } const statTime = new Date(stats.service.last_updated * 1000); Object.entries(stats.processes).forEach(([key, procStats]) => { if (procStats.pid.toString() in stats.cpu_usages) { if (!(key in series)) { series[key] = { name: key, data: [] }; } series[key].data.push({ x: statTime, y: stats.cpu_usages[procStats.pid.toString()].mem, }); } }); }); return Object.values(series); }, [statsHistory]); return (
System
{initialStats && (
{initialStats[0].service.version}
)}
{lastUpdated && (
Last refreshed:
)}
Detector Inference Speed
{detInferenceTimeSeries.map((series) => ( ))}
Detector CPU Usage
{detCpuSeries.map((series) => ( ))}
Detector Memory Usage
{detMemSeries.map((series) => ( ))}
); } export default System; /** *
* * Detectors
{gpuSeries.length > 0 && ( <> GPUs
)} Cameras
{config && Object.values(config.cameras).map((camera) => { if (camera.enabled) { return (
); } return null; })}
Other Processes
*/