Files
frigate/web/src/hooks/use-health-problems.ts
T
Josh HawkinsandGitHub 64b07889af Tweaks and fixes (#24659)
* add last time to active notices

* Stop retrying the review lookup in the explore detail menu when it 404s

* don't let a slow websocket client block publishers

A websocket client that stopped reading its socket (for example a suspended workstation with a Frigate tab open) blocked ws4py's sendall on whatever thread was publishing, including the single inter-process responder thread, so the recording maintainer and stats hung until nginx dropped the client after 5 minutes. Each client now gets its own bounded send queue and writer thread, and a client that overflows the queue is shut down. ws4py writes close and pong frames directly on its shared manager thread, bypassing the queue, so a 10s socket send timeout keeps a stalled client from pinning that thread. The jsmpeg and birdseye outputs share the class, so their unreachable send error handling is removed.

* don't let a lost object enter a zone

When the detector lost an object, the tracker kept it at its last box and every held frame still counted toward the zone's inertia. A car detected in a zone once or twice as it drove out of view could reach an inertia of 8 and be marked as entering, which sent it to GenAI and any other required zones check even though Review never showed the zone. Inertia and loitering still count on frames without a detection, but an object can only newly enter a zone on a frame where it was actually detected.

* add test

* limit retry stop to 404 only

* add docstrings
2026-10-15 06:54:59 -06:00

224 lines
6.4 KiB
TypeScript

import { useCallback, useMemo } from "react";
import axios from "axios";
import type { TFunction } from "i18next";
import useSWR from "swr";
import { useDateLocale } from "@/hooks/use-date-locale";
import { useTimezone } from "@/hooks/use-date-utils";
import { useHealthChecks } from "@/hooks/use-health-checks";
import { hiddenAt, useNotices } from "@/hooks/use-notices";
import { evaluateConfigHealth } from "@/utils/configHealth";
import { formatUnixTimestampToDateTime } from "@/utils/dateUtil";
import { sortHealthProblems } from "@/utils/healthSort";
import { streamHealth } from "@/utils/streamHealth";
import type { FrigateConfig } from "@/types/frigateConfig";
import type { HealthProblem } from "@/types/health";
import type { MutedCheck, Notice } from "@/types/notice";
const EXTERNAL_LINK = /^https?:\/\//;
type HealthProblems = {
/** shown rows, most severe first */
problems: HealthProblem[];
/** acknowledged and muted rows, most recently hidden first; undefined until loaded */
hidden?: HealthProblem[];
loading: boolean;
/** show every hidden row again */
unhideAll: () => Promise<void>;
};
/**
* Every row of the Health tab's Notices list: registry notices plus the config
* and stream checks the browser builds. The Notices pane and the status bar
* both read it, so they always agree. `t` only fills in the text; row ids and
* counts never depend on it.
*/
export function useHealthProblems(
t: TFunction,
showHidden = false,
): HealthProblems {
const { data: config } = useSWR<FrigateConfig>("config", {
revalidateOnFocus: false,
});
const timezone = useTimezone(config);
const locale = useDateLocale();
const {
notices,
hidden: hiddenNotices,
acknowledge,
mute,
unhide,
mutateHidden,
} = useNotices(showHidden);
const { data: mutedChecks, mutate: mutateMutedChecks } = useSWR<MutedCheck[]>(
"notices/muted_checks",
);
const {
stream: { results },
} = useHealthChecks();
const muteCheck = useCallback(
async (id: string) => {
await axios.post(`notices/${id}/mute`);
mutateMutedChecks();
},
[mutateMutedChecks],
);
const unmuteCheck = useCallback(
async (id: string) => {
await axios.delete(`notices/${id}/hidden`);
mutateMutedChecks();
},
[mutateMutedChecks],
);
const unhideAll = useCallback(async () => {
await axios.delete("notices/hidden");
mutateHidden();
mutateMutedChecks();
}, [mutateHidden, mutateMutedChecks]);
const formatTime = useCallback(
(timestamp: number) =>
formatUnixTimestampToDateTime(timestamp, {
timezone,
date_format: "MMM d, h:mm a",
locale,
}),
[timezone, locale],
);
const noticeRow = useCallback(
(notice: Notice): HealthProblem => {
const link = notice.link ?? undefined;
const external = link !== undefined && EXTERNAL_LINK.test(link);
const isCamera = notice.category === "camera";
return {
id: `notice:${notice.id}`,
source: "registry",
severity: notice.severity,
// only a camera scope is a name; other scopes are ids like a release
scope: isCamera ? (notice.scope ?? undefined) : undefined,
scopeIsCamera: isCamera,
// replace keeps backend params out of i18next's own option names
text: t(`health.notices.kinds.${notice.kind}`, {
ns: "views/system",
replace: notice.params,
count: notice.count,
}),
meta:
notice.muted_at !== null
? t("health.notices.mutedAt", {
ns: "views/system",
time: formatTime(notice.muted_at),
})
: notice.acknowledged_at !== null
? t("health.notices.acknowledgedAt", {
ns: "views/system",
time: formatTime(notice.acknowledged_at),
})
: t("health.notices.occurred", {
ns: "views/system",
time: formatTime(notice.first_seen),
lastTime: formatTime(notice.last_seen),
count: notice.count,
}),
link: external ? undefined : link,
externalLink: external ? link : undefined,
...(hiddenAt(notice) > 0
? {
hidden: notice.muted_at !== null ? "muted" : "acknowledged",
onUnhide: () => unhide(notice.id),
}
: {
onAcknowledge: notice.acknowledgeable
? () => acknowledge(notice.id)
: undefined,
onMute: () => mute(notice.id),
}),
};
},
[acknowledge, mute, unhide, formatTime, t],
);
const checks = useMemo<HealthProblem[]>(
() =>
config
? [
...evaluateConfigHealth(config, t),
...streamHealth(config, results, t).problems,
]
: [],
[config, results, t],
);
const mutedAt = useMemo(
() =>
new Map((mutedChecks ?? []).map((check) => [check.id, check.muted_at])),
[mutedChecks],
);
const problems = useMemo(
() =>
sortHealthProblems([
...(notices ?? []).map(noticeRow),
...checks
.filter((check) => !mutedAt.has(check.id))
.map((check) => ({
...check,
onMute: () => muteCheck(check.id),
})),
]),
[notices, noticeRow, checks, mutedAt, muteCheck],
);
const hidden = useMemo(() => {
if (!showHidden || hiddenNotices === undefined) {
return undefined;
}
const rows = [
...hiddenNotices.map((notice) => ({
at: hiddenAt(notice),
row: noticeRow(notice),
})),
...checks.flatMap((check) => {
const at = mutedAt.get(check.id);
return at === undefined
? []
: [
{
at,
row: {
...check,
meta: t("health.notices.mutedAt", {
ns: "views/system",
time: formatTime(at),
}),
hidden: "muted" as const,
onUnhide: () => unmuteCheck(check.id),
},
},
];
}),
];
return rows.sort((a, b) => b.at - a.at).map(({ row }) => row);
}, [
showHidden,
hiddenNotices,
noticeRow,
checks,
mutedAt,
formatTime,
unmuteCheck,
t,
]);
const loading = notices === undefined || mutedChecks === undefined || !config;
return { problems, hidden, loading, unhideAll };
}