Show camera graphs

This commit is contained in:
Nick Mowen 2024-01-26 10:20:04 -07:00 committed by Nicolas Mowen
parent a9b9c21cb6
commit b9d8cc14de
2 changed files with 212 additions and 38 deletions

View File

@ -2,7 +2,7 @@ import Chart from "react-apexcharts";
type SystemGraphProps = { type SystemGraphProps = {
graphId: string; graphId: string;
title: string; title?: string;
unit: string; unit: string;
data: ApexAxisChartSeries; data: ApexAxisChartSeries;
}; };
@ -14,7 +14,7 @@ export default function SystemGraph({
}: SystemGraphProps) { }: SystemGraphProps) {
return ( return (
<Chart <Chart
type="line" type="bar"
options={{ options={{
chart: { chart: {
id: graphId, id: graphId,
@ -32,27 +32,23 @@ export default function SystemGraph({
show: true, show: true,
showForSingleSeries: true, showForSingleSeries: true,
position: "top", position: "top",
horizontalAlign: "left",
onItemClick: { onItemClick: {
toggleDataSeries: true, toggleDataSeries: true,
}, },
}, },
title: { dataLabels: {
text: title, enabled: false,
}, },
xaxis: { xaxis: {
type: "datetime", type: "datetime",
}, },
yaxis: { yaxis: {
tickAmount: 3, show: false,
labels: {
formatter: (value) => {
return `${value.toFixed(2)} ${unit}`;
},
},
}, },
}} }}
series={data} series={data}
height="100%" height="120"
/> />
); );
} }

View File

@ -5,15 +5,18 @@ import { useEffect, useMemo, useState } from "react";
import SystemGraph from "@/components/graph/SystemGraph"; import SystemGraph from "@/components/graph/SystemGraph";
import { useFrigateStats } from "@/api/ws"; import { useFrigateStats } from "@/api/ws";
import TimeAgo from "@/components/dynamic/TimeAgo"; import TimeAgo from "@/components/dynamic/TimeAgo";
import { FrigateConfig } from "@/types/frigateConfig";
function System() { function System() {
const { data: config } = useSWR<FrigateConfig>("config");
// stats // stats
const { data: initialStats } = useSWR<FrigateStats[]>("stats/history", { const { data: initialStats } = useSWR<FrigateStats[]>("stats/history", {
revalidateOnFocus: false, revalidateOnFocus: false,
}); });
const { payload: updatedStats } = useFrigateStats(); const { payload: updatedStats } = useFrigateStats();
const [statsHistory, setStatsHistory] = useState<FrigateStats[]>( const [statsHistory, setStatsHistory] = useState<FrigateStats[]>(
initialStats || [] initialStats || [],
); );
const lastUpdated = useMemo(() => { const lastUpdated = useMemo(() => {
@ -39,7 +42,7 @@ function System() {
} }
setStatsHistory([...statsHistory, updatedStats]); setStatsHistory([...statsHistory, updatedStats]);
}, [initialStats, updatedStats]); }, [initialStats, updatedStats, statsHistory]);
// stats data pieces // stats data pieces
const detInferenceTimeSeries = useMemo(() => { const detInferenceTimeSeries = useMemo(() => {
@ -48,10 +51,14 @@ function System() {
} }
const series: { const series: {
[key: string]: { name: string; data: { x: any; y: any }[] }; [key: string]: { name: string; data: { x: object; y: number }[] };
} = {}; } = {};
statsHistory.forEach((stats) => { statsHistory.forEach((stats) => {
if (!stats) {
return;
}
const statTime = new Date(stats.service.last_updated * 1000); const statTime = new Date(stats.service.last_updated * 1000);
Object.entries(stats.detectors).forEach(([key, stats]) => { Object.entries(stats.detectors).forEach(([key, stats]) => {
@ -70,10 +77,14 @@ function System() {
} }
const series: { const series: {
[key: string]: { name: string; data: { x: any; y: any }[] }; [key: string]: { name: string; data: { x: object; y: string }[] };
} = {}; } = {};
statsHistory.forEach((stats) => { statsHistory.forEach((stats) => {
if (!stats) {
return;
}
const statTime = new Date(stats.service.last_updated * 1000); const statTime = new Date(stats.service.last_updated * 1000);
Object.entries(stats.detectors).forEach(([key, detStats]) => { Object.entries(stats.detectors).forEach(([key, detStats]) => {
@ -95,10 +106,14 @@ function System() {
} }
const series: { const series: {
[key: string]: { name: string; data: { x: any; y: any }[] }; [key: string]: { name: string; data: { x: object; y: string }[] };
} = {}; } = {};
statsHistory.forEach((stats) => { statsHistory.forEach((stats) => {
if (!stats) {
return;
}
const statTime = new Date(stats.service.last_updated * 1000); const statTime = new Date(stats.service.last_updated * 1000);
Object.entries(stats.detectors).forEach(([key, detStats]) => { Object.entries(stats.detectors).forEach(([key, detStats]) => {
@ -120,10 +135,14 @@ function System() {
} }
const series: { const series: {
[key: string]: { name: string; data: { x: any; y: any }[] }; [key: string]: { name: string; data: { x: object; y: string }[] };
} = {}; } = {};
statsHistory.forEach((stats) => { statsHistory.forEach((stats) => {
if (!stats) {
return;
}
const statTime = new Date(stats.service.last_updated * 1000); const statTime = new Date(stats.service.last_updated * 1000);
Object.entries(stats.gpu_usages || []).forEach(([key, stats]) => { Object.entries(stats.gpu_usages || []).forEach(([key, stats]) => {
@ -142,10 +161,14 @@ function System() {
} }
const series: { const series: {
[key: string]: { name: string; data: { x: any; y: any }[] }; [key: string]: { name: string; data: { x: object; y: string }[] };
} = {}; } = {};
statsHistory.forEach((stats) => { statsHistory.forEach((stats) => {
if (!stats) {
return;
}
const statTime = new Date(stats.service.last_updated * 1000); const statTime = new Date(stats.service.last_updated * 1000);
Object.entries(stats.gpu_usages || {}).forEach(([key, stats]) => { Object.entries(stats.gpu_usages || {}).forEach(([key, stats]) => {
@ -158,16 +181,104 @@ function System() {
}); });
return Object.values(series); return Object.values(series);
}, [statsHistory]); }, [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 (!(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,
});
series[key]["capture"].data.push({
x: statTime,
y: stats.cpu_usages[camStats.capture_pid.toString()].cpu,
});
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(() => { const otherProcessCpuSeries = useMemo(() => {
if (!statsHistory) { if (!statsHistory) {
return []; return [];
} }
const series: { const series: {
[key: string]: { name: string; data: { x: any; y: any }[] }; [key: string]: { name: string; data: { x: object; y: string }[] };
} = {}; } = {};
statsHistory.forEach((stats) => { statsHistory.forEach((stats) => {
if (!stats) {
return;
}
const statTime = new Date(stats.service.last_updated * 1000); const statTime = new Date(stats.service.last_updated * 1000);
Object.entries(stats.processes).forEach(([key, procStats]) => { Object.entries(stats.processes).forEach(([key, procStats]) => {
@ -191,10 +302,14 @@ function System() {
} }
const series: { const series: {
[key: string]: { name: string; data: { x: any; y: any }[] }; [key: string]: { name: string; data: { x: object; y: string }[] };
} = {}; } = {};
statsHistory.forEach((stats) => { statsHistory.forEach((stats) => {
if (!stats) {
return;
}
const statTime = new Date(stats.service.last_updated * 1000); const statTime = new Date(stats.service.last_updated * 1000);
Object.entries(stats.processes).forEach(([key, procStats]) => { Object.entries(stats.processes).forEach(([key, procStats]) => {
@ -214,20 +329,61 @@ function System() {
}, [statsHistory]); }, [statsHistory]);
return ( return (
<> <div className="size-full p-2">
<div className="flex items-center"> <div className="w-full h-8 flex justify-between items-center">
<Heading as="h2">System</Heading> <div className="h-full flex items-center gap-2">
{initialStats && ( <div className="h-full font-medium content-center">System</div>
<div className="ml-2 text-sm">{initialStats[0].service.version}</div> {initialStats && (
)} <div className="h-full text-muted-foreground text-sm content-center">
</div> {initialStats[0].service.version}
{lastUpdated && ( </div>
<div className="text-xs mb-2"> )}
Last refreshed: <TimeAgo time={lastUpdated * 1000} dense />
</div> </div>
)} <div className="h-full flex items-center">
{lastUpdated && (
<div className="h-full text-muted-foreground text-sm content-center">
Last refreshed: <TimeAgo time={lastUpdated * 1000} dense />
</div>
)}
</div>
</div>
<Heading as="h4">Detectors</Heading> <div className="grid grid-cols-1 sm:grid-cols-3 gap-2">
<div className="p-2.5 bg-primary rounded-2xl flex-col">
<div>Inference Speed</div>
{detInferenceTimeSeries.map((series) => (
<SystemGraph
graphId="detector-inference"
unit="ms"
data={[series]}
/>
))}
</div>
<div className="bg-primary rounded-2xl flex-col">
<SystemGraph
graphId="detector-usages"
title="CPU"
unit="%"
data={detCpuSeries}
/>
</div>
<div className="bg-primary rounded-2xl flex-col">
<SystemGraph
graphId="detector-usages"
title="Memory"
unit="%"
data={detMemSeries}
/>
</div>
</div>
</div>
);
}
export default System;
/**
* <Heading as="h4">Detectors</Heading>
<div className="grid grid-cols-1 sm:grid-cols-3"> <div className="grid grid-cols-1 sm:grid-cols-3">
<SystemGraph <SystemGraph
graphId="detector-inference" graphId="detector-inference"
@ -251,7 +407,7 @@ function System() {
{gpuSeries.length > 0 && ( {gpuSeries.length > 0 && (
<> <>
<Heading as="h4">GPUs</Heading> <Heading as="h4">GPUs</Heading>
<div className="grid grid-cols-1 sm:grid-cols-2"> <div className="mt-2 grid grid-cols-1 sm:grid-cols-2">
<SystemGraph <SystemGraph
graphId="detector-inference" graphId="detector-inference"
title="GPU Usage" title="GPU Usage"
@ -267,6 +423,32 @@ function System() {
</div> </div>
</> </>
)} )}
<Heading as="h4">Cameras</Heading>
<div className="grid grid-cols-1 sm:grid-cols-2">
{config &&
Object.values(config.cameras).map((camera) => {
if (camera.enabled) {
return (
<div key={camera.name} className="grid grid-cols-2">
<SystemGraph
graphId={`${camera.name}-cpu`}
title={`${camera.name.replaceAll("_", " ")} CPU`}
unit="%"
data={Object.values(cameraCpuSeries[camera.name] || {})}
/>
<SystemGraph
graphId={`${camera.name}-fps`}
title={`${camera.name.replaceAll("_", " ")} FPS`}
unit=""
data={Object.values(cameraFpsSeries[camera.name] || {})}
/>
</div>
);
}
return null;
})}
</div>
<Heading as="h4">Other Processes</Heading> <Heading as="h4">Other Processes</Heading>
<div className="grid grid-cols-1 sm:grid-cols-2"> <div className="grid grid-cols-1 sm:grid-cols-2">
<SystemGraph <SystemGraph
@ -282,8 +464,4 @@ function System() {
data={otherProcessMemSeries} data={otherProcessMemSeries}
/> />
</div> </div>
</> */
);
}
export default System;