2023-12-31 16:31:33 +03:00
|
|
|
import { Button } from "@/components/ui/button";
|
2024-03-24 20:23:39 +03:00
|
|
|
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
|
2024-04-03 00:25:48 +03:00
|
|
|
import { LogLine, LogSeverity } from "@/types/log";
|
2023-12-31 16:31:33 +03:00
|
|
|
import copy from "copy-to-clipboard";
|
2024-02-24 03:25:00 +03:00
|
|
|
import { useCallback, useMemo, useRef, useState } from "react";
|
2024-04-03 00:25:48 +03:00
|
|
|
import { IoIosAlert } from "react-icons/io";
|
|
|
|
|
import { GoAlertFill } from "react-icons/go";
|
2024-03-24 20:23:39 +03:00
|
|
|
import { LuCopy } from "react-icons/lu";
|
2023-12-31 16:31:33 +03:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
|
|
const logTypes = ["frigate", "go2rtc", "nginx"] as const;
|
|
|
|
|
type LogType = (typeof logTypes)[number];
|
2023-12-08 16:33:22 +03:00
|
|
|
|
2024-04-03 00:25:48 +03:00
|
|
|
const datestamp = /\[[\d\s-:]*]/;
|
|
|
|
|
const severity = /(DEBUG)|(INFO)|(WARNING)|(ERROR)/;
|
|
|
|
|
const section = /[\w.]*/;
|
|
|
|
|
|
2023-12-08 16:33:22 +03:00
|
|
|
function Logs() {
|
2023-12-31 16:31:33 +03:00
|
|
|
const [logService, setLogService] = useState<LogType>("frigate");
|
|
|
|
|
|
2024-04-03 00:25:48 +03:00
|
|
|
const { data: frigateLogs } = useSWR<string>("logs/frigate", {
|
2023-12-31 16:31:33 +03:00
|
|
|
refreshInterval: 1000,
|
|
|
|
|
});
|
|
|
|
|
const { data: go2rtcLogs } = useSWR("logs/go2rtc", { refreshInterval: 1000 });
|
|
|
|
|
const { data: nginxLogs } = useSWR("logs/nginx", { refreshInterval: 1000 });
|
2024-04-03 00:25:48 +03:00
|
|
|
|
|
|
|
|
// convert to log data
|
|
|
|
|
|
2023-12-31 16:31:33 +03:00
|
|
|
const logs = useMemo(() => {
|
|
|
|
|
if (logService == "frigate") {
|
|
|
|
|
return frigateLogs;
|
|
|
|
|
} else if (logService == "go2rtc") {
|
|
|
|
|
return go2rtcLogs;
|
|
|
|
|
} else if (logService == "nginx") {
|
|
|
|
|
return nginxLogs;
|
|
|
|
|
} else {
|
|
|
|
|
return "unknown logs";
|
|
|
|
|
}
|
|
|
|
|
}, [logService, frigateLogs, go2rtcLogs, nginxLogs]);
|
|
|
|
|
|
2024-04-03 00:25:48 +03:00
|
|
|
const logLines = useMemo<LogLine[]>(() => {
|
|
|
|
|
if (logService == "frigate") {
|
|
|
|
|
if (!frigateLogs) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return frigateLogs
|
|
|
|
|
.split("\n")
|
|
|
|
|
.map((line) => {
|
|
|
|
|
const match = datestamp.exec(line);
|
|
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sectionMatch = section.exec(
|
|
|
|
|
line.substring(match.index + match[0].length).trim(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!sectionMatch) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
dateStamp: match.toString().slice(1, -1),
|
|
|
|
|
severity: severity
|
|
|
|
|
.exec(line)
|
|
|
|
|
?.at(0)
|
|
|
|
|
?.toString()
|
|
|
|
|
?.toLowerCase() as LogSeverity,
|
|
|
|
|
section: sectionMatch.toString(),
|
|
|
|
|
content: line
|
|
|
|
|
.substring(line.indexOf(":", match.index + match[0].length) + 2)
|
|
|
|
|
.trim(),
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
.filter((value) => value != null) as LogLine[];
|
|
|
|
|
} else if (logService == "go2rtc") {
|
|
|
|
|
return [];
|
|
|
|
|
} else if (logService == "nginx") {
|
|
|
|
|
return [];
|
|
|
|
|
} else {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}, [logService, frigateLogs, go2rtcLogs, nginxLogs]);
|
|
|
|
|
|
|
|
|
|
//console.log(`the logs are ${JSON.stringify(logLines)}`);
|
|
|
|
|
|
2023-12-31 16:31:33 +03:00
|
|
|
const handleCopyLogs = useCallback(() => {
|
|
|
|
|
copy(logs);
|
|
|
|
|
}, [logs]);
|
|
|
|
|
|
2024-02-24 03:25:00 +03:00
|
|
|
// scroll to bottom button
|
|
|
|
|
|
|
|
|
|
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
const [endVisible, setEndVisible] = useState(true);
|
|
|
|
|
const observer = useRef<IntersectionObserver | null>(null);
|
|
|
|
|
const endLogRef = useCallback(
|
|
|
|
|
(node: HTMLElement | null) => {
|
|
|
|
|
if (observer.current) observer.current.disconnect();
|
|
|
|
|
try {
|
|
|
|
|
observer.current = new IntersectionObserver((entries) => {
|
|
|
|
|
setEndVisible(entries[0].isIntersecting);
|
|
|
|
|
});
|
|
|
|
|
if (node) observer.current.observe(node);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// no op
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-02-29 01:23:56 +03:00
|
|
|
[setEndVisible],
|
2024-02-24 03:25:00 +03:00
|
|
|
);
|
|
|
|
|
|
2023-12-08 16:33:22 +03:00
|
|
|
return (
|
2024-03-30 21:44:12 +03:00
|
|
|
<div className="size-full p-2 flex flex-col">
|
2023-12-31 16:31:33 +03:00
|
|
|
<div className="flex justify-between items-center">
|
2024-03-24 20:23:39 +03:00
|
|
|
<ToggleGroup
|
|
|
|
|
className="*:px-3 *:py-4 *:rounded-2xl"
|
|
|
|
|
type="single"
|
|
|
|
|
size="sm"
|
|
|
|
|
value={logService}
|
|
|
|
|
onValueChange={(value: LogType) =>
|
|
|
|
|
value ? setLogService(value) : null
|
|
|
|
|
} // don't allow the severity to be unselected
|
|
|
|
|
>
|
|
|
|
|
{Object.values(logTypes).map((item) => (
|
|
|
|
|
<ToggleGroupItem
|
|
|
|
|
key={item}
|
|
|
|
|
className={`flex items-center justify-between gap-2 ${logService == item ? "" : "text-gray-500"}`}
|
|
|
|
|
value={item}
|
|
|
|
|
aria-label={`Select ${item}`}
|
|
|
|
|
>
|
|
|
|
|
<div className="capitalize">{`${item} Logs`}</div>
|
|
|
|
|
</ToggleGroupItem>
|
|
|
|
|
))}
|
|
|
|
|
</ToggleGroup>
|
2023-12-31 16:31:33 +03:00
|
|
|
<div>
|
2024-03-24 20:23:39 +03:00
|
|
|
<Button
|
|
|
|
|
className="flex justify-between items-center gap-2"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={handleCopyLogs}
|
|
|
|
|
>
|
|
|
|
|
<LuCopy />
|
|
|
|
|
<div className="hidden md:block">Copy to Clipboard</div>
|
|
|
|
|
</Button>
|
2023-12-31 16:31:33 +03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-02-24 03:25:00 +03:00
|
|
|
{!endVisible && (
|
2024-02-28 17:16:32 +03:00
|
|
|
<Button
|
|
|
|
|
className="absolute bottom-8 left-[50%] -translate-x-[50%] rounded-xl bg-accent-foreground text-white bg-gray-400 z-20 p-2"
|
|
|
|
|
variant="secondary"
|
2024-02-24 03:25:00 +03:00
|
|
|
onClick={() =>
|
|
|
|
|
contentRef.current?.scrollTo({
|
|
|
|
|
top: contentRef.current?.scrollHeight,
|
|
|
|
|
behavior: "smooth",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
Jump to Bottom
|
2024-02-28 17:16:32 +03:00
|
|
|
</Button>
|
2024-02-24 03:25:00 +03:00
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
ref={contentRef}
|
2024-04-03 00:25:48 +03:00
|
|
|
className="w-full h-min my-2 font-mono text-sm rounded py-4 sm:py-2 whitespace-pre-wrap overflow-auto"
|
2024-02-24 03:25:00 +03:00
|
|
|
>
|
2024-04-03 00:25:48 +03:00
|
|
|
<div className="py-2 sticky top-0 -translate-y-1/4 grid grid-cols-5 sm:grid-cols-8 md:grid-cols-12 bg-background *:p-2">
|
|
|
|
|
<div className="p-1 flex items-center capitalize border-y border-l">
|
|
|
|
|
Severity
|
|
|
|
|
</div>
|
|
|
|
|
<div className="col-span-2 sm:col-span-1 flex items-center border-y border-l">
|
|
|
|
|
Timestamp
|
|
|
|
|
</div>
|
|
|
|
|
<div className="col-span-2 flex items-center border-y border-l border-r sm:border-r-0">
|
|
|
|
|
Tag
|
|
|
|
|
</div>
|
|
|
|
|
<div className="col-span-5 sm:col-span-4 md:col-span-8 flex items-center border">
|
|
|
|
|
Message
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{logLines.map((log, idx) => (
|
|
|
|
|
<LogLineData key={idx} offset={idx} line={log} />
|
|
|
|
|
))}
|
2024-02-24 03:25:00 +03:00
|
|
|
<div ref={endLogRef} />
|
2023-12-31 16:31:33 +03:00
|
|
|
</div>
|
2024-02-21 23:07:32 +03:00
|
|
|
</div>
|
2023-12-08 16:33:22 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-03 00:25:48 +03:00
|
|
|
type LogLineDataProps = {
|
|
|
|
|
line: LogLine;
|
|
|
|
|
offset: number;
|
|
|
|
|
};
|
|
|
|
|
function LogLineData({ line, offset }: LogLineDataProps) {
|
|
|
|
|
// long log message
|
|
|
|
|
|
|
|
|
|
const contentRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
const [expanded, setExpanded] = useState(false);
|
|
|
|
|
|
|
|
|
|
const contentOverflows = useMemo(() => {
|
|
|
|
|
if (!contentRef.current) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return contentRef.current.scrollWidth > contentRef.current.clientWidth;
|
|
|
|
|
// update on ref change
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
}, [contentRef.current]);
|
|
|
|
|
|
|
|
|
|
// severity coloring
|
|
|
|
|
|
|
|
|
|
const severityClassName = useMemo(() => {
|
|
|
|
|
switch (line.severity) {
|
|
|
|
|
case "info":
|
|
|
|
|
return "text-secondary-foreground rounded-md";
|
|
|
|
|
case "warning":
|
|
|
|
|
return "text-yellow-400 rounded-md";
|
|
|
|
|
case "error":
|
|
|
|
|
return "text-danger rounded-md";
|
|
|
|
|
}
|
|
|
|
|
}, [line]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={`py-2 grid grid-cols-5 sm:grid-cols-8 md:grid-cols-12 gap-2 ${offset % 2 == 0 ? "bg-secondary" : "bg-secondary/80"} border-t border-l`}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className={`h-full p-1 flex items-center gap-2 capitalize ${severityClassName}`}
|
|
|
|
|
>
|
|
|
|
|
{line.severity == "error" ? (
|
|
|
|
|
<GoAlertFill className="size-5" />
|
|
|
|
|
) : (
|
|
|
|
|
<IoIosAlert className="size-5" />
|
|
|
|
|
)}
|
|
|
|
|
{line.severity}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="h-full col-span-2 sm:col-span-1 flex items-center">
|
|
|
|
|
{line.dateStamp}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="h-full col-span-2 flex items-center overflow-hidden text-ellipsis">
|
|
|
|
|
{line.section}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="w-full col-span-5 sm:col-span-4 md:col-span-8 flex justify-between items-center">
|
|
|
|
|
<div
|
|
|
|
|
ref={contentRef}
|
|
|
|
|
className={`w-[94%] flex items-center" ${expanded ? "" : "overflow-hidden whitespace-nowrap text-ellipsis"}`}
|
|
|
|
|
>
|
|
|
|
|
{line.content}
|
|
|
|
|
</div>
|
|
|
|
|
{contentOverflows && (
|
|
|
|
|
<Button className="mr-4" onClick={() => setExpanded(!expanded)}>
|
|
|
|
|
...
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:33:22 +03:00
|
|
|
export default Logs;
|