2024-04-04 22:55:04 +02:00
|
|
|
import { useFrigateStats } from "@/api/ws";
|
2024-05-29 20:05:39 +02:00
|
|
|
import { CameraLineGraph } from "@/components/graph/CameraGraph";
|
2024-04-04 22:55:04 +02:00
|
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
|
|
|
import { FrigateStats } from "@/types/stats";
|
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
type CameraMetricsProps = {
|
|
|
|
lastUpdated: number;
|
|
|
|
setLastUpdated: (last: number) => void;
|
|
|
|
};
|
|
|
|
export default function CameraMetrics({
|
|
|
|
lastUpdated,
|
|
|
|
setLastUpdated,
|
|
|
|
}: CameraMetricsProps) {
|
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
|
|
|
|
|
|
|
// stats
|
|
|
|
|
|
|
|
const { data: initialStats } = useSWR<FrigateStats[]>(
|
2024-04-10 01:08:11 +02:00
|
|
|
["stats/history", { keys: "cpu_usages,cameras,detection_fps,service" }],
|
2024-04-04 22:55:04 +02:00
|
|
|
{
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const [statsHistory, setStatsHistory] = useState<FrigateStats[]>([]);
|
|
|
|
const { payload: updatedStats } = useFrigateStats();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (initialStats == undefined || initialStats.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (statsHistory.length == 0) {
|
|
|
|
setStatsHistory(initialStats);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!updatedStats) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updatedStats.service.last_updated > lastUpdated) {
|
2024-05-29 20:05:39 +02:00
|
|
|
setStatsHistory([...statsHistory.slice(1), updatedStats]);
|
2024-04-04 22:55:04 +02:00
|
|
|
setLastUpdated(Date.now() / 1000);
|
|
|
|
}
|
|
|
|
}, [initialStats, updatedStats, statsHistory, lastUpdated, setLastUpdated]);
|
|
|
|
|
|
|
|
// timestamps
|
|
|
|
|
|
|
|
const updateTimes = useMemo(
|
|
|
|
() => statsHistory.map((stats) => stats.service.last_updated),
|
|
|
|
[statsHistory],
|
|
|
|
);
|
|
|
|
|
|
|
|
// stats data
|
|
|
|
|
2024-04-10 01:08:11 +02:00
|
|
|
const overallFpsSeries = useMemo(() => {
|
|
|
|
if (!statsHistory) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const series: {
|
|
|
|
[key: string]: { name: string; data: { x: number; y: number }[] };
|
|
|
|
} = {};
|
|
|
|
|
2024-04-23 16:32:46 +02:00
|
|
|
series["overall_fps"] = { name: "overall frames per second", data: [] };
|
2024-04-10 01:08:11 +02:00
|
|
|
series["overall_dps"] = { name: "overall detections per second", data: [] };
|
|
|
|
series["overall_skipped_dps"] = {
|
|
|
|
name: "overall skipped detections per second",
|
|
|
|
data: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
statsHistory.forEach((stats, statsIdx) => {
|
|
|
|
if (!stats) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-23 16:32:46 +02:00
|
|
|
let frames = 0;
|
|
|
|
Object.values(stats.cameras).forEach(
|
|
|
|
(camStat) => (frames += camStat.camera_fps),
|
|
|
|
);
|
|
|
|
|
|
|
|
series["overall_fps"].data.push({
|
|
|
|
x: statsIdx,
|
|
|
|
y: Math.round(frames),
|
|
|
|
});
|
|
|
|
|
2024-04-10 01:08:11 +02:00
|
|
|
series["overall_dps"].data.push({
|
|
|
|
x: statsIdx,
|
|
|
|
y: stats.detection_fps,
|
|
|
|
});
|
|
|
|
|
|
|
|
let skipped = 0;
|
|
|
|
Object.values(stats.cameras).forEach(
|
|
|
|
(camStat) => (skipped += camStat.skipped_fps),
|
|
|
|
);
|
|
|
|
|
|
|
|
series["overall_skipped_dps"].data.push({
|
|
|
|
x: statsIdx,
|
|
|
|
y: skipped,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return Object.values(series);
|
|
|
|
}, [statsHistory]);
|
|
|
|
|
2024-04-04 22:55:04 +02:00
|
|
|
const cameraCpuSeries = useMemo(() => {
|
|
|
|
if (!statsHistory || statsHistory.length == 0) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const series: {
|
|
|
|
[cam: string]: {
|
|
|
|
[key: string]: { name: string; data: { x: number; y: string }[] };
|
|
|
|
};
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
statsHistory.forEach((stats, statsIdx) => {
|
|
|
|
if (!stats) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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: statsIdx,
|
|
|
|
y: stats.cpu_usages[camStats.ffmpeg_pid.toString()]?.cpu ?? 0.0,
|
|
|
|
});
|
|
|
|
series[key]["capture"].data.push({
|
|
|
|
x: statsIdx,
|
|
|
|
y: stats.cpu_usages[camStats.capture_pid?.toString()]?.cpu ?? 0,
|
|
|
|
});
|
|
|
|
series[key]["detect"].data.push({
|
|
|
|
x: statsIdx,
|
|
|
|
y: stats.cpu_usages[camStats.pid.toString()].cpu,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return series;
|
|
|
|
}, [config, statsHistory]);
|
|
|
|
|
|
|
|
const cameraFpsSeries = useMemo(() => {
|
|
|
|
if (!statsHistory) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const series: {
|
|
|
|
[cam: string]: {
|
|
|
|
[key: string]: { name: string; data: { x: number; y: number }[] };
|
|
|
|
};
|
|
|
|
} = {};
|
|
|
|
|
|
|
|
statsHistory.forEach((stats, statsIdx) => {
|
|
|
|
if (!stats) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.entries(stats.cameras).forEach(([key, camStats]) => {
|
|
|
|
if (!(key in series)) {
|
|
|
|
const camName = key.replaceAll("_", " ");
|
|
|
|
series[key] = {};
|
2024-04-23 16:32:46 +02:00
|
|
|
series[key]["fps"] = {
|
|
|
|
name: `${camName} frames per second`,
|
|
|
|
data: [],
|
|
|
|
};
|
2024-04-04 22:55:04 +02:00
|
|
|
series[key]["det"] = {
|
|
|
|
name: `${camName} detections per second`,
|
|
|
|
data: [],
|
|
|
|
};
|
|
|
|
series[key]["skip"] = {
|
|
|
|
name: `${camName} skipped detections per second`,
|
|
|
|
data: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-23 16:32:46 +02:00
|
|
|
series[key]["fps"].data.push({
|
|
|
|
x: statsIdx,
|
|
|
|
y: camStats.camera_fps,
|
|
|
|
});
|
2024-04-04 22:55:04 +02:00
|
|
|
series[key]["det"].data.push({
|
|
|
|
x: statsIdx,
|
|
|
|
y: camStats.detection_fps,
|
|
|
|
});
|
|
|
|
series[key]["skip"].data.push({
|
|
|
|
x: statsIdx,
|
|
|
|
y: camStats.skipped_fps,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return series;
|
|
|
|
}, [statsHistory]);
|
|
|
|
|
|
|
|
return (
|
2024-06-03 20:43:30 +02:00
|
|
|
<div className="scrollbar-container mt-4 flex size-full flex-col gap-3 overflow-y-auto">
|
2024-05-14 17:06:44 +02:00
|
|
|
<div className="text-sm font-medium text-muted-foreground">Overview</div>
|
2024-04-10 01:08:11 +02:00
|
|
|
<div className="grid grid-cols-1 md:grid-cols-3">
|
|
|
|
{statsHistory.length != 0 ? (
|
2024-05-14 17:06:44 +02:00
|
|
|
<div className="rounded-lg bg-background_alt p-2.5 md:rounded-2xl">
|
2024-04-23 16:32:46 +02:00
|
|
|
<div className="mb-5">Frames / Detections</div>
|
2024-04-10 01:08:11 +02:00
|
|
|
<CameraLineGraph
|
|
|
|
graphId="overall-stats"
|
2024-04-23 16:32:46 +02:00
|
|
|
unit=""
|
|
|
|
dataLabels={["camera", "detect", "skipped"]}
|
2024-04-10 01:08:11 +02:00
|
|
|
updateTimes={updateTimes}
|
|
|
|
data={overallFpsSeries}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
2024-05-14 17:06:44 +02:00
|
|
|
<Skeleton className="h-32 w-full rounded-lg md:rounded-2xl" />
|
2024-04-10 01:08:11 +02:00
|
|
|
)}
|
|
|
|
</div>
|
2024-05-14 17:06:44 +02:00
|
|
|
<div className="grid grid-cols-1 gap-6 md:grid-cols-2">
|
2024-04-04 22:55:04 +02:00
|
|
|
{config &&
|
|
|
|
Object.values(config.cameras).map((camera) => {
|
|
|
|
if (camera.enabled) {
|
|
|
|
return (
|
2024-05-14 17:06:44 +02:00
|
|
|
<div className="flex w-full flex-col gap-3">
|
|
|
|
<div className="text-sm font-medium capitalize text-muted-foreground">
|
2024-04-04 22:55:04 +02:00
|
|
|
{camera.name.replaceAll("_", " ")}
|
|
|
|
</div>
|
2024-05-14 17:06:44 +02:00
|
|
|
<div key={camera.name} className="grid gap-2 sm:grid-cols-2">
|
2024-04-04 22:55:04 +02:00
|
|
|
{Object.keys(cameraCpuSeries).includes(camera.name) ? (
|
2024-05-14 17:06:44 +02:00
|
|
|
<div className="rounded-lg bg-background_alt p-2.5 md:rounded-2xl">
|
2024-04-04 22:55:04 +02:00
|
|
|
<div className="mb-5">CPU</div>
|
|
|
|
<CameraLineGraph
|
|
|
|
graphId={`${camera.name}-cpu`}
|
|
|
|
unit="%"
|
|
|
|
dataLabels={["ffmpeg", "capture", "detect"]}
|
|
|
|
updateTimes={updateTimes}
|
|
|
|
data={Object.values(
|
|
|
|
cameraCpuSeries[camera.name] || {},
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
2024-05-14 17:06:44 +02:00
|
|
|
<Skeleton className="aspect-video size-full" />
|
2024-04-04 22:55:04 +02:00
|
|
|
)}
|
|
|
|
{Object.keys(cameraFpsSeries).includes(camera.name) ? (
|
2024-05-14 17:06:44 +02:00
|
|
|
<div className="rounded-lg bg-background_alt p-2.5 md:rounded-2xl">
|
2024-04-23 16:32:46 +02:00
|
|
|
<div className="mb-5">Frames / Detections</div>
|
2024-04-04 22:55:04 +02:00
|
|
|
<CameraLineGraph
|
|
|
|
graphId={`${camera.name}-dps`}
|
2024-04-23 16:32:46 +02:00
|
|
|
unit=""
|
|
|
|
dataLabels={["camera", "detect", "skipped"]}
|
2024-04-04 22:55:04 +02:00
|
|
|
updateTimes={updateTimes}
|
|
|
|
data={Object.values(
|
|
|
|
cameraFpsSeries[camera.name] || {},
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
2024-05-14 17:06:44 +02:00
|
|
|
<Skeleton className="aspect-video size-full" />
|
2024-04-04 22:55:04 +02:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|