From c6d937cec083d7b713b98093ef529b5de99ad2dd Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Wed, 3 Apr 2024 06:13:55 -0600 Subject: [PATCH] Support log data format --- frigate/api/app.py | 16 ++++++++++++---- web/src/pages/Logs.tsx | 21 +++++++++------------ web/src/types/log.ts | 5 +++++ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/frigate/api/app.py b/frigate/api/app.py index 6fdedab90..e8becee73 100644 --- a/frigate/api/app.py +++ b/frigate/api/app.py @@ -159,9 +159,9 @@ def config(): config["plus"] = {"enabled": current_app.plus_api.is_active()} for detector, detector_config in config["detectors"].items(): - detector_config["model"]["labelmap"] = ( - current_app.frigate_config.model.merged_labelmap - ) + detector_config["model"][ + "labelmap" + ] = current_app.frigate_config.model.merged_labelmap return jsonify(config) @@ -425,11 +425,19 @@ def logs(service: str): 404, ) + start = request.args.get("start", type=int, default=0) + end = request.args.get("start", type=int) + try: file = open(service_location, "r") contents = file.read() file.close() - return contents, 200 + + lines = contents.splitlines() + return make_response( + jsonify({"totalLines": len(lines), "lines": lines[start:end]}), + 200, + ) except FileNotFoundError as e: logger.error(e) return make_response( diff --git a/web/src/pages/Logs.tsx b/web/src/pages/Logs.tsx index 5396b3fff..4e66657d4 100644 --- a/web/src/pages/Logs.tsx +++ b/web/src/pages/Logs.tsx @@ -1,6 +1,6 @@ import { Button } from "@/components/ui/button"; import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; -import { LogLine, LogSeverity } from "@/types/log"; +import { LogData, LogLine, LogSeverity } from "@/types/log"; import copy from "copy-to-clipboard"; import { useCallback, useMemo, useRef, useState } from "react"; import { IoIosAlert } from "react-icons/io"; @@ -23,13 +23,13 @@ const ngSeverity = /(GET)|(POST)|(PATCH)|(DELETE)/; function Logs() { const [logService, setLogService] = useState("frigate"); - const { data: frigateLogs } = useSWR("logs/frigate", { + const { data: frigateLogs } = useSWR("logs/frigate", { refreshInterval: 1000, }); - const { data: go2rtcLogs } = useSWR("logs/go2rtc", { + const { data: go2rtcLogs } = useSWR("logs/go2rtc", { refreshInterval: 1000, }); - const { data: nginxLogs } = useSWR("logs/nginx", { + const { data: nginxLogs } = useSWR("logs/nginx", { refreshInterval: 1000, }); @@ -43,7 +43,7 @@ function Logs() { } else if (logService == "nginx") { return nginxLogs; } else { - return "unknown logs"; + return undefined; } }, [logService, frigateLogs, go2rtcLogs, nginxLogs]); @@ -53,8 +53,7 @@ function Logs() { return []; } - return frigateLogs - .split("\n") + return frigateLogs.lines .map((line) => { const match = frigateDateStamp.exec(line); @@ -89,8 +88,7 @@ function Logs() { return []; } - return go2rtcLogs - .split("\n") + return go2rtcLogs.lines .map((line) => { if (line.length == 0) { return null; @@ -130,8 +128,7 @@ function Logs() { return []; } - return nginxLogs - .split("\n") + return nginxLogs.lines .map((line) => { if (line.length == 0) { return null; @@ -152,7 +149,7 @@ function Logs() { const handleCopyLogs = useCallback(() => { if (logs) { - copy(logs); + copy(logs.lines.join("\n")); } }, [logs]); diff --git a/web/src/types/log.ts b/web/src/types/log.ts index 445e79c83..faf8115f2 100644 --- a/web/src/types/log.ts +++ b/web/src/types/log.ts @@ -1,3 +1,8 @@ +export type LogData = { + lineCount: number; + lines: string[]; +}; + export type LogSeverity = "info" | "warning" | "error" | "debug"; export type LogLine = {