From 4a1758b0aec8cdb8044da3895981418293db5dde Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Wed, 3 Apr 2024 06:47:57 -0600 Subject: [PATCH] Only load necessary logs --- frigate/api/app.py | 2 +- web/src/pages/Logs.tsx | 90 ++++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/frigate/api/app.py b/frigate/api/app.py index e8becee73..8c99fdd39 100644 --- a/frigate/api/app.py +++ b/frigate/api/app.py @@ -426,7 +426,7 @@ def logs(service: str): ) start = request.args.get("start", type=int, default=0) - end = request.args.get("start", type=int) + end = request.args.get("end", type=int) try: file = open(service_location, "r") diff --git a/web/src/pages/Logs.tsx b/web/src/pages/Logs.tsx index 4e66657d4..05fd294e5 100644 --- a/web/src/pages/Logs.tsx +++ b/web/src/pages/Logs.tsx @@ -2,15 +2,17 @@ import { Button } from "@/components/ui/button"; import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; import { LogData, LogLine, LogSeverity } from "@/types/log"; import copy from "copy-to-clipboard"; -import { useCallback, useMemo, useRef, useState } from "react"; +import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { IoIosAlert } from "react-icons/io"; import { GoAlertFill } from "react-icons/go"; import { LuCopy } from "react-icons/lu"; -import useSWR from "swr"; +import axios from "axios"; const logTypes = ["frigate", "go2rtc", "nginx"] as const; type LogType = (typeof logTypes)[number]; +type LogRange = { start: number; end: number }; + const frigateDateStamp = /\[[\d\s-:]*]/; const frigateSeverity = /(DEBUG)|(INFO)|(WARNING)|(ERROR)/; const frigateSection = /[\w.]*/; @@ -23,37 +25,49 @@ const ngSeverity = /(GET)|(POST)|(PATCH)|(DELETE)/; function Logs() { const [logService, setLogService] = useState("frigate"); - const { data: frigateLogs } = useSWR("logs/frigate", { - refreshInterval: 1000, - }); - const { data: go2rtcLogs } = useSWR("logs/go2rtc", { - refreshInterval: 1000, - }); - const { data: nginxLogs } = useSWR("logs/nginx", { - refreshInterval: 1000, - }); + // log data handling + + const [logs, setLogs] = useState([]); + + useEffect(() => { + axios.get(`logs/${logService}`).then((resp) => { + if (resp.status == 200) { + const data = resp.data as LogData; + setLogs(data.lines); + } + }); + }, [logService]); + + useEffect(() => { + if (!logs || logs.length == 0) { + return; + } + + const id = setTimeout(() => { + axios.get(`logs/${logService}?start=${logs.length}`).then((resp) => { + if (resp.status == 200) { + const data = resp.data as LogData; + setLogs([...logs, ...data.lines]); + } + }); + }, 1000); + + return () => { + if (id) { + clearTimeout(id); + } + }; + }, [logs, logService]); // convert to log data - const logs = useMemo(() => { - if (logService == "frigate") { - return frigateLogs; - } else if (logService == "go2rtc") { - return go2rtcLogs; - } else if (logService == "nginx") { - return nginxLogs; - } else { - return undefined; - } - }, [logService, frigateLogs, go2rtcLogs, nginxLogs]); - const logLines = useMemo(() => { - if (logService == "frigate") { - if (!frigateLogs) { - return []; - } + if (!logs) { + return []; + } - return frigateLogs.lines + if (logService == "frigate") { + return logs .map((line) => { const match = frigateDateStamp.exec(line); @@ -84,11 +98,7 @@ function Logs() { }) .filter((value) => value != null) as LogLine[]; } else if (logService == "go2rtc") { - if (!go2rtcLogs) { - return []; - } - - return go2rtcLogs.lines + return logs .map((line) => { if (line.length == 0) { return null; @@ -124,11 +134,7 @@ function Logs() { }) .filter((value) => value != null) as LogLine[]; } else if (logService == "nginx") { - if (!nginxLogs) { - return []; - } - - return nginxLogs.lines + return logs .map((line) => { if (line.length == 0) { return null; @@ -145,15 +151,15 @@ function Logs() { } else { return []; } - }, [logService, frigateLogs, go2rtcLogs, nginxLogs]); + }, [logs, logService]); const handleCopyLogs = useCallback(() => { if (logs) { - copy(logs.lines.join("\n")); + copy(logs.join("\n")); } }, [logs]); - // scroll to bottom button + // scroll to bottom const contentRef = useRef(null); const [endVisible, setEndVisible] = useState(true); @@ -244,7 +250,7 @@ function Logs() { {logLines.map((log, idx) => ( ))} -
+
);