diff --git a/web/e2e/specs/explore.spec.ts b/web/e2e/specs/explore.spec.ts index 4886a719fa..d545b9ebfb 100644 --- a/web/e2e/specs/explore.spec.ts +++ b/web/e2e/specs/explore.spec.ts @@ -342,4 +342,37 @@ test.describe("Explore — Frigate+ submission (desktop) @high", () => { dialog.getByRole("button", { name: "Yes", exact: true }), ).toBeVisible(); }); + + test("a rate limited submission says how long to wait", async ({ + frigateApp, + }) => { + await frigateApp.installDefaults({ config: { plus: { enabled: true } } }); + const page = frigateApp.page; + await page.route("**/api/events/*/plus", (route) => + route.fulfill({ + status: 429, + headers: { "Retry-After": "90" }, + json: { success: false, retry_after: 90 }, + }), + ); + + await frigateApp.goto("/explore?labels=person"); + + const firstResult = page.locator("[data-start]").first(); + await expect(firstResult).toBeVisible({ timeout: 10_000 }); + await firstResult.click(); + + const dialog = page.getByRole("dialog"); + await dialog.getByRole("button", { name: "Yes", exact: true }).click(); + + await expect( + page.getByText( + "Frigate+ is rate limiting requests. Try again in 1 minute, 30 seconds.", + ), + ).toBeVisible(); + // the object can be submitted again once the wait is over + await expect( + dialog.getByRole("button", { name: "Yes", exact: true }), + ).toBeVisible(); + }); }); diff --git a/web/e2e/specs/settings/detection-models.spec.ts b/web/e2e/specs/settings/detection-models.spec.ts index 2d4b1c0c6c..d3da34232d 100644 --- a/web/e2e/specs/settings/detection-models.spec.ts +++ b/web/e2e/specs/settings/detection-models.spec.ts @@ -459,6 +459,36 @@ test.describe("Detection models settings @high", () => { ); }); + test("a Frigate+ rate limit says how long to wait", async ({ + frigateApp, + }) => { + await installRoutes( + frigateApp.page, + [ + { + scene: "default", + devices: ["openvino:GPU.0"], + path: "plus://abc123", + plus: PLUS_MODEL, + }, + ], + true, + ); + // registered last, so it takes precedence over the models route above + await frigateApp.page.route("**/api/plus/models", (route) => + route.fulfill({ + status: 429, + headers: { "Retry-After": "120" }, + json: { success: false, retry_after: 120 }, + }), + ); + await openPage(frigateApp); + + await expect(frigateApp.page.locator("#pageRoot")).toContainText( + "Frigate+ is rate limiting requests. Try again in 2 minutes.", + ); + }); + test("picking a Frigate+ model stays on the tab and saves a plus path", async ({ frigateApp, }) => { diff --git a/web/public/locales/en/common.json b/web/public/locales/en/common.json index 2ae848ee81..a7479b4b4b 100644 --- a/web/public/locales/en/common.json +++ b/web/public/locales/en/common.json @@ -327,6 +327,10 @@ "copyDetails": "Copy details", "copyFailed": "Could not copy details to clipboard" }, + "plusRateLimited": { + "retryIn": "Frigate+ is rate limiting requests. Try again in {{duration}}.", + "retryLater": "Frigate+ is rate limiting requests. Try again later." + }, "selectItem": "Select {{item}}", "readTheDocumentation": "Read the documentation", "information": { diff --git a/web/src/components/config-form/theme/fields/ModelSourcePicker.tsx b/web/src/components/config-form/theme/fields/ModelSourcePicker.tsx index 3fabf83138..f5f11fed65 100644 --- a/web/src/components/config-form/theme/fields/ModelSourcePicker.tsx +++ b/web/src/components/config-form/theme/fields/ModelSourcePicker.tsx @@ -14,6 +14,7 @@ import { Switch } from "@/components/ui/switch"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import type { FrigateConfig } from "@/types/frigateConfig"; import { describeSupportedDetectors } from "@/utils/plusModels"; +import { usePlusRateLimit } from "@/hooks/use-plus-rate-limit"; export type FrigatePlusModel = { id: string; @@ -65,20 +66,26 @@ export function ModelSourcePicker({ // metadata of whatever model was saved before it const selectedId = plusModelId(path) ?? plus?.id; - const { data: availableModels, isLoading } = useSWR< - Record - >(plusEnabled ? "/plus/models" : null, { - fetcher: async (url) => { - const res = await axios.get(url, { withCredentials: true }); - return res.data.reduce( - (obj: Record, model: FrigatePlusModel) => { - obj[model.id] = model; - return obj; - }, - {}, - ); + const { getRateLimitMessage } = usePlusRateLimit(); + const { + data: availableModels, + error: modelsError, + isLoading, + } = useSWR>( + plusEnabled ? "/plus/models" : null, + { + fetcher: async (url) => { + const res = await axios.get(url, { withCredentials: true }); + return res.data.reduce( + (obj: Record, model: FrigatePlusModel) => { + obj[model.id] = model; + return obj; + }, + {}, + ); + }, }, - }); + ); const [showIncompatible, setShowIncompatible] = useState(false); const showIncompatibleId = useId(); @@ -101,6 +108,7 @@ export function ModelSourcePicker({ const isCompatible = (model: FrigatePlusModel) => !detector || model.supportedDetectors.includes(detector); + const rateLimitMessage = getRateLimitMessage(modelsError); const allEntries = Object.entries(availableModels ?? {}); const hasIncompatible = allEntries.some(([, model]) => !isCompatible(model)); @@ -163,7 +171,8 @@ export function ModelSourcePicker({ {entries.length === 0 ? (
- {t("frigatePlus.modelInfo.noModelsAvailable")} + {rateLimitMessage ?? + t("frigatePlus.modelInfo.noModelsAvailable")}
) : ( entries.map(([id, model]) => ( @@ -184,6 +193,9 @@ export function ModelSourcePicker({
+ {rateLimitMessage && ( +

{rateLimitMessage}

+ )}

frigatePlus.modelInfo.modelSelect

diff --git a/web/src/components/overlay/detail/SearchDetailDialog.tsx b/web/src/components/overlay/detail/SearchDetailDialog.tsx index 7d6b7c465c..8bd4316458 100644 --- a/web/src/components/overlay/detail/SearchDetailDialog.tsx +++ b/web/src/components/overlay/detail/SearchDetailDialog.tsx @@ -87,6 +87,7 @@ import TextEntryDialog from "@/components/overlay/dialog/TextEntryDialog"; import AttributeSelectDialog from "@/components/overlay/dialog/AttributeSelectDialog"; import { Trans, useTranslation } from "react-i18next"; import { useIsAdmin } from "@/hooks/use-is-admin"; +import { usePlusRateLimit } from "@/hooks/use-plus-rate-limit"; import { getTranslatedLabel } from "@/utils/i18n"; import { CameraNameLabel } from "@/components/camera/FriendlyNameLabel"; import { DialogPortal } from "@radix-ui/react-dialog"; @@ -699,6 +700,7 @@ function ObjectDetailsTab({ "views/faceLibrary", "components/dialog", ]); + const { getRateLimitMessage } = usePlusRateLimit(); const apiHost = useApiHost(); const hasCustomClassificationModels = useMemo( @@ -1286,7 +1288,7 @@ function ObjectDetailsTab({ revalidate: false, }, ); - } catch { + } catch (error) { if (displayedIdRef.current === eventId) { setState("reviewing"); } @@ -1294,12 +1296,13 @@ function ObjectDetailsTab({ // the toast is not object specific, so it is always shown to avoid // silently dropping a failed submission toast.error( - t("explore.plus.review.toast.error", { ns: "components/dialog" }), + getRateLimitMessage(error) ?? + t("explore.plus.review.toast.error", { ns: "components/dialog" }), { position: "top-center" }, ); } }, - [search, mutate, mapSearchResults, isEventsKey, t], + [search, mutate, mapSearchResults, isEventsKey, t, getRateLimitMessage], ); const popoverContainerRef = useRef(null); diff --git a/web/src/components/overlay/detail/TrackingDetails.tsx b/web/src/components/overlay/detail/TrackingDetails.tsx index 21066471fe..958fba2ba2 100644 --- a/web/src/components/overlay/detail/TrackingDetails.tsx +++ b/web/src/components/overlay/detail/TrackingDetails.tsx @@ -54,6 +54,7 @@ import { useApiHost } from "@/api"; import ImageLoadingIndicator from "@/components/indicators/ImageLoadingIndicator"; import ObjectTrackOverlay from "../ObjectTrackOverlay"; import { useIsAdmin } from "@/hooks/use-is-admin"; +import { usePlusRateLimit } from "@/hooks/use-plus-rate-limit"; import { VideoResolutionType } from "@/types/live"; import { VodManifest } from "@/types/playback"; @@ -934,6 +935,7 @@ function LifecycleIconRow({ annotationOffset, }: LifecycleIconRowProps) { const { t } = useTranslation(["views/explore", "components/player"]); + const { getRateLimitMessage } = usePlusRateLimit(); const { data: config } = useSWR("config"); const [isOpen, setIsOpen] = useState(false); const [showAdvancedScores, setShowAdvancedScores] = useState(false); @@ -1251,11 +1253,12 @@ function LifecycleIconRow({ position: "top-center", }, ); - } catch { + } catch (error) { toast.error( - t("toast.error.submitFrigatePlusFailed", { - ns: "components/player", - }), + getRateLimitMessage(error) ?? + t("toast.error.submitFrigatePlusFailed", { + ns: "components/player", + }), { position: "top-center", }, diff --git a/web/src/components/overlay/dialog/FrigatePlusDialog.tsx b/web/src/components/overlay/dialog/FrigatePlusDialog.tsx index 347d5c10bc..d1495ea30d 100644 --- a/web/src/components/overlay/dialog/FrigatePlusDialog.tsx +++ b/web/src/components/overlay/dialog/FrigatePlusDialog.tsx @@ -22,6 +22,7 @@ import { baseUrl } from "@/api/baseUrl"; import { getTranslatedLabel } from "@/utils/i18n"; import useImageLoaded from "@/hooks/use-image-loaded"; import { useIsAdmin } from "@/hooks/use-is-admin"; +import { usePlusRateLimit } from "@/hooks/use-plus-rate-limit"; export type FrigatePlusDialogProps = { upload?: Event; @@ -37,6 +38,7 @@ export function FrigatePlusDialog({ onEventUploaded, }: FrigatePlusDialogProps) { const { t, i18n } = useTranslation(["components/dialog"]); + const { getRateLimitMessage } = usePlusRateLimit(); type SubmissionState = "reviewing" | "uploading" | "submitted"; const [state, setState] = useState( @@ -63,14 +65,15 @@ export function FrigatePlusDialog({ setState("submitted"); onEventUploaded(); - } catch { + } catch (error) { setState("reviewing"); - toast.error(t("explore.plus.review.toast.error"), { - position: "top-center", - }); + toast.error( + getRateLimitMessage(error) ?? t("explore.plus.review.toast.error"), + { position: "top-center" }, + ); } }, - [upload, onEventUploaded, t], + [upload, onEventUploaded, t, getRateLimitMessage], ); const [imgRef, imgLoaded, onImgLoad] = useImageLoaded(); diff --git a/web/src/components/player/HlsVideoPlayer.tsx b/web/src/components/player/HlsVideoPlayer.tsx index 7b0a66004e..68f0c310b3 100644 --- a/web/src/components/player/HlsVideoPlayer.tsx +++ b/web/src/components/player/HlsVideoPlayer.tsx @@ -22,6 +22,7 @@ import { ASPECT_VERTICAL_LAYOUT, RecordingPlayerError } from "@/types/record"; import { useTranslation } from "react-i18next"; import ObjectTrackOverlay from "@/components/overlay/ObjectTrackOverlay"; import { useIsAdmin } from "@/hooks/use-is-admin"; +import { usePlusRateLimit } from "@/hooks/use-plus-rate-limit"; // Android native hls does not seek correctly const USE_NATIVE_HLS = false; @@ -109,6 +110,7 @@ export default function HlsVideoPlayer({ transformedOverlay, }: HlsVideoPlayerProps) { const { t } = useTranslation("components/player"); + const { getRateLimitMessage } = usePlusRateLimit(); const { data: config } = useSWR("config"); const isAdmin = useIsAdmin(); @@ -525,18 +527,26 @@ export default function HlsVideoPlayer({ onUploadFrame={async () => { const frameTime = getVideoTime(); - if (frameTime && onUploadFrame) { + if (!frameTime || !onUploadFrame) { + return; + } + + try { const resp = await onUploadFrame(frameTime); - if (resp && resp.status == 200) { - toast.success(t("toast.success.submittedFrigatePlus"), { - position: "top-center", - }); - } else { - toast.success(t("toast.error.submitFrigatePlusFailed"), { - position: "top-center", - }); + if (resp?.status !== 200) { + throw new Error(); } + + toast.success(t("toast.success.submittedFrigatePlus"), { + position: "top-center", + }); + } catch (error) { + toast.error( + getRateLimitMessage(error) ?? + t("toast.error.submitFrigatePlusFailed"), + { position: "top-center" }, + ); } }} onSnapshot={onSnapshot ? handleSnapshot : undefined} diff --git a/web/src/components/setup/SetupDetector.tsx b/web/src/components/setup/SetupDetector.tsx index 15f24cdf11..0232087b61 100644 --- a/web/src/components/setup/SetupDetector.tsx +++ b/web/src/components/setup/SetupDetector.tsx @@ -10,6 +10,7 @@ import { SelectValue, } from "@/components/ui/select"; import { useDocDomain } from "@/hooks/use-doc-domain"; +import { usePlusRateLimit } from "@/hooks/use-plus-rate-limit"; import type { FrigateConfig } from "@/types/frigateConfig"; import type { DetectionHardware } from "@/types/hardware"; import { recommendedDetectorCount } from "@/utils/detectionHardware"; @@ -79,15 +80,16 @@ export default function SetupDetector({ const needsModel = MODEL_REQUIRED_DETECTORS.includes(selected.detector); - const { data: plusModels } = useSWR( - plusEnabled && needsModel ? "/plus/models" : null, - { - fetcher: async (url) => { - const res = await axios.get(url, { withCredentials: true }); - return res.data; - }, + const { getRateLimitMessage } = usePlusRateLimit(); + const { data: plusModels, error: plusModelsError } = useSWR< + FrigatePlusModel[] + >(plusEnabled && needsModel ? "/plus/models" : null, { + fetcher: async (url) => { + const res = await axios.get(url, { withCredentials: true }); + return res.data; }, - ); + }); + const plusRateLimitMessage = getRateLimitMessage(plusModelsError); const [plusModelId, setPlusModelId] = useState(""); const compatiblePlusModels = useMemo( @@ -217,6 +219,9 @@ export default function SetupDetector({

{t("setupWizard.detector.modelRequired", { name: selected.name })}

+ {plusEnabled && plusRateLimitMessage && ( +

{plusRateLimitMessage}

+ )} {plusEnabled ? (