mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-09-27 18:38:58 +03:00
* update date-fns, i18next, react-dropzone, react-markdown and @types/node react-i18next 17 peers `i18next >= 26.2.0`, so the two move together. react-day-picker already depends on date-fns 4, so date-fns now dedupes to a single copy. react-dropzone 20 declares `node >=22` in `engines`, but npm only warns on Node 20 and nothing in the build needs Node 22. * update js-yaml, @hookform/resolvers and prettier js-yaml 5 has no default export, so `DictAsYamlField` imports `dump`, `load` and `YAMLException` by name. @hookform/resolvers 5 types `zodResolver` with the schema's input and output types separately, and fields with defaults are optional on input, so the zone and classification model forms pass both types to `useForm`. zod's range now starts at 3.25, which resolvers 5 requires. * format with prettier 3.9
219 lines
5.9 KiB
TypeScript
219 lines
5.9 KiB
TypeScript
import { FrigateConfig } from "@/types/frigateConfig";
|
|
import {
|
|
CameraDetectThreshold,
|
|
CameraFfmpegThreshold,
|
|
InferenceThreshold,
|
|
} from "@/types/graph";
|
|
import { FrigateStats, PotentialProblem, ProblemSeverity } from "@/types/stats";
|
|
import { useMemo } from "react";
|
|
import useSWR from "swr";
|
|
import useDeepMemo from "./use-deep-memo";
|
|
import { capitalizeAll, capitalizeFirstLetter } from "@/utils/stringUtil";
|
|
import { isReplayCamera } from "@/utils/cameraUtil";
|
|
import { useFrigateStats, useJobStatus } from "@/api/ws";
|
|
import { useIsAdmin } from "./use-is-admin";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
// the status bar has always rendered these exact classes; keep them byte for
|
|
// byte so its output does not change
|
|
const SEVERITY_COLOR: Record<ProblemSeverity, string> = {
|
|
error: "text-danger",
|
|
warning: "text-orange-400",
|
|
info: "text-selected",
|
|
};
|
|
|
|
function problem(
|
|
severity: ProblemSeverity,
|
|
text: string,
|
|
relevantLink?: string,
|
|
): PotentialProblem {
|
|
return { text, severity, color: SEVERITY_COLOR[severity], relevantLink };
|
|
}
|
|
|
|
// matches SKIPPED_DETECTIONS_PCT in frigate/stats/emitter.py
|
|
const SKIPPED_DETECTIONS_PCT = 5;
|
|
|
|
export default function useStats(stats: FrigateStats | undefined) {
|
|
const { t } = useTranslation(["views/system"]);
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
|
const isAdmin = useIsAdmin();
|
|
|
|
// Pass isAdmin as revalidateOnFocus so non-admins never send the jobState snapshot pull
|
|
const { payload: replayJob } = useJobStatus("debug_replay", isAdmin);
|
|
const replayActive = Boolean(
|
|
isAdmin &&
|
|
replayJob &&
|
|
(replayJob.status === "queued" ||
|
|
replayJob.status === "running" ||
|
|
replayJob.status === "success"),
|
|
);
|
|
|
|
const memoizedStats = useDeepMemo(stats);
|
|
|
|
const potentialProblems = useMemo<PotentialProblem[]>(() => {
|
|
const problems: PotentialProblem[] = [];
|
|
|
|
if (!memoizedStats) {
|
|
return problems;
|
|
}
|
|
|
|
// if frigate has just started
|
|
// don't look for issues
|
|
if (memoizedStats.service.uptime < 120) {
|
|
return problems;
|
|
}
|
|
|
|
if (memoizedStats.service.retention_unmet) {
|
|
problems.push(
|
|
problem("error", t("stats.retentionUnmet"), "/system#storage"),
|
|
);
|
|
}
|
|
|
|
// check detectors for high inference speeds
|
|
Object.entries(memoizedStats["detectors"]).forEach(([key, det]) => {
|
|
if (det["inference_speed"] > InferenceThreshold.error) {
|
|
problems.push(
|
|
problem(
|
|
"error",
|
|
t("stats.detectIsVerySlow", {
|
|
detect: capitalizeFirstLetter(key),
|
|
speed: det["inference_speed"],
|
|
}),
|
|
"/system#general",
|
|
),
|
|
);
|
|
} else if (det["inference_speed"] > InferenceThreshold.warning) {
|
|
problems.push(
|
|
problem(
|
|
"warning",
|
|
t("stats.detectIsSlow", {
|
|
detect: capitalizeFirstLetter(key),
|
|
speed: det["inference_speed"],
|
|
}),
|
|
"/system#general",
|
|
),
|
|
);
|
|
}
|
|
});
|
|
|
|
// check for offline cameras
|
|
Object.entries(memoizedStats["cameras"]).forEach(([name, cam]) => {
|
|
if (!config) {
|
|
return;
|
|
}
|
|
|
|
// Skip replay cameras
|
|
if (isReplayCamera(name)) {
|
|
return;
|
|
}
|
|
|
|
const cameraName = config.cameras?.[name]?.friendly_name ?? name;
|
|
if (config.cameras?.[name]?.enabled && cam["camera_fps"] == 0) {
|
|
problems.push(
|
|
problem(
|
|
"error",
|
|
t("stats.cameraIsOffline", {
|
|
camera: capitalizeFirstLetter(capitalizeAll(cameraName)),
|
|
}),
|
|
"logs",
|
|
),
|
|
);
|
|
}
|
|
});
|
|
|
|
// check camera cpu usages
|
|
Object.entries(memoizedStats["cameras"]).forEach(([name, cam]) => {
|
|
// Skip replay cameras
|
|
if (isReplayCamera(name)) {
|
|
return;
|
|
}
|
|
|
|
const ffmpegAvg = parseFloat(
|
|
memoizedStats["cpu_usages"][cam["ffmpeg_pid"]]?.cpu_average,
|
|
);
|
|
const detectAvg = parseFloat(
|
|
memoizedStats["cpu_usages"][cam["pid"]]?.cpu_average,
|
|
);
|
|
|
|
const cameraName = config?.cameras?.[name]?.friendly_name ?? name;
|
|
|
|
if (
|
|
config?.cameras?.[name]?.enabled &&
|
|
cam["skipped_pct"] >= SKIPPED_DETECTIONS_PCT
|
|
) {
|
|
problems.push(
|
|
problem(
|
|
"warning",
|
|
t("stats.cameraSkippedDetections", {
|
|
camera: capitalizeFirstLetter(capitalizeAll(cameraName)),
|
|
pct: cam["skipped_pct"],
|
|
}),
|
|
"/system#cameras",
|
|
),
|
|
);
|
|
}
|
|
|
|
if (!isNaN(ffmpegAvg) && ffmpegAvg >= CameraFfmpegThreshold.error) {
|
|
problems.push(
|
|
problem(
|
|
"error",
|
|
t("stats.ffmpegHighCpuUsage", {
|
|
camera: capitalizeFirstLetter(capitalizeAll(cameraName)),
|
|
ffmpegAvg,
|
|
}),
|
|
"/system#cameras",
|
|
),
|
|
);
|
|
}
|
|
|
|
if (!isNaN(detectAvg) && detectAvg >= CameraDetectThreshold.error) {
|
|
problems.push(
|
|
problem(
|
|
"error",
|
|
t("stats.detectHighCpuUsage", {
|
|
camera: capitalizeFirstLetter(capitalizeAll(cameraName)),
|
|
detectAvg,
|
|
}),
|
|
"/system#cameras",
|
|
),
|
|
);
|
|
}
|
|
});
|
|
|
|
// Add message if debug replay is active
|
|
if (replayActive) {
|
|
problems.push(
|
|
problem(
|
|
"info",
|
|
t("stats.debugReplayActive", {
|
|
defaultValue: "Debug replay session is active",
|
|
}),
|
|
"/replay",
|
|
),
|
|
);
|
|
}
|
|
|
|
return problems;
|
|
}, [config, memoizedStats, t, replayActive]);
|
|
|
|
return { potentialProblems };
|
|
}
|
|
|
|
export function useAutoFrigateStats() {
|
|
const { data: initialStats } = useSWR<FrigateStats>("stats", {
|
|
revalidateOnFocus: false,
|
|
});
|
|
const latestStats = useFrigateStats();
|
|
|
|
const stats = useMemo(() => {
|
|
if (latestStats) {
|
|
return latestStats;
|
|
}
|
|
|
|
return initialStats;
|
|
}, [initialStats, latestStats]);
|
|
|
|
return stats;
|
|
}
|