add loading older logs
This commit is contained in:
1 parent
7dbc7b3064
commit
e012910332
4 files changed
+219
-42
No files matched your search
@@ -1,16 +1,69 @@
|
|||||||
import Stack from "@mui/material/Stack";
|
import Stack from "@mui/material/Stack";
|
||||||
import Typography from "@mui/material/Typography";
|
import Typography from "@mui/material/Typography";
|
||||||
export const HeaderLogs = ({
|
import { Dot } from "@/Components/design-elements";
|
||||||
lines,
|
|
||||||
isValidating,
|
// Hooks
|
||||||
}: {
|
import { useEffect, useState } from "react";
|
||||||
|
import { useTheme } from "@mui/material";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
import { LAYOUT, SPACING } from "@/Utils/Theme/constants";
|
||||||
|
import { MS_PER_SECOND, MS_PER_MINUTE } from "@/Utils/TimeUtils";
|
||||||
|
|
||||||
|
interface HeaderLogsProps {
|
||||||
lines: number;
|
lines: number;
|
||||||
isValidating: boolean;
|
loading: boolean;
|
||||||
}) => {
|
lastUpdatedAt: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const HeaderLogs = ({ lines, loading, lastUpdatedAt }: HeaderLogsProps) => {
|
||||||
|
const theme = useTheme();
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const [now, setNow] = useState(() => Date.now());
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (lastUpdatedAt === null) return;
|
||||||
|
setNow(Date.now());
|
||||||
|
const id = window.setInterval(() => setNow(Date.now()), MS_PER_SECOND);
|
||||||
|
return () => window.clearInterval(id);
|
||||||
|
}, [lastUpdatedAt]);
|
||||||
|
|
||||||
|
const renderStatus = () => {
|
||||||
|
if (loading) return t("pages.docker.container.logs.loading");
|
||||||
|
if (lastUpdatedAt === null) return null;
|
||||||
|
const ageMs = Math.max(0, now - lastUpdatedAt);
|
||||||
|
return ageMs < MS_PER_MINUTE
|
||||||
|
? t("pages.docker.container.logs.updatedSecondsAgo", {
|
||||||
|
count: Math.floor(ageMs / MS_PER_SECOND),
|
||||||
|
})
|
||||||
|
: t("pages.docker.container.logs.updatedMinutesAgo", {
|
||||||
|
count: Math.floor(ageMs / MS_PER_MINUTE),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack direction="row">
|
<Stack
|
||||||
<Typography>{`${lines} recent lines`}</Typography>
|
direction="row"
|
||||||
{isValidating && "Refreshing..."}
|
alignItems="center"
|
||||||
|
gap={LAYOUT.MD}
|
||||||
|
>
|
||||||
|
<Typography>
|
||||||
|
{t("pages.docker.container.logs.recentLines", { count: lines })}
|
||||||
|
</Typography>
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
alignItems="center"
|
||||||
|
gap={SPACING.SM}
|
||||||
|
>
|
||||||
|
{lastUpdatedAt !== null && !loading && (
|
||||||
|
<Dot
|
||||||
|
color={theme.palette.success.main}
|
||||||
|
size="md"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<Typography color={theme.palette.text.secondary}>{renderStatus()}</Typography>
|
||||||
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -8,6 +8,7 @@ import { Fragment } from "react";
|
|||||||
// Hooks
|
// Hooks
|
||||||
import { useTheme } from "@mui/material";
|
import { useTheme } from "@mui/material";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import type { DockerLogRow } from "@/Utils/MonitorUtils";
|
import type { DockerLogRow } from "@/Utils/MonitorUtils";
|
||||||
@@ -18,6 +19,7 @@ import type { RootState } from "@/store";
|
|||||||
export const RowLog = ({ row }: { row: DockerLogRow }) => {
|
export const RowLog = ({ row }: { row: DockerLogRow }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const uiTimezone = useSelector((state: RootState) => state.ui.timezone);
|
const uiTimezone = useSelector((state: RootState) => state.ui.timezone);
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
@@ -63,7 +65,9 @@ export const RowLog = ({ row }: { row: DockerLogRow }) => {
|
|||||||
alignItems={"center"}
|
alignItems={"center"}
|
||||||
py={SPACING.MD}
|
py={SPACING.MD}
|
||||||
>
|
>
|
||||||
<Typography pr={SPACING.MD}>Some log entries were skipped</Typography>
|
<Typography pr={SPACING.MD}>
|
||||||
|
{t("pages.docker.container.logs.skipped")}
|
||||||
|
</Typography>
|
||||||
<Divider sx={{ flex: 1 }} />
|
<Divider sx={{ flex: 1 }} />
|
||||||
</Stack>
|
</Stack>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
@@ -1,28 +1,33 @@
|
|||||||
// Components
|
// Components
|
||||||
import Stack from "@mui/material/Stack";
|
import Stack from "@mui/material/Stack";
|
||||||
|
import Typography from "@mui/material/Typography";
|
||||||
import { BaseBox } from "@/Components/design-elements";
|
import { BaseBox } from "@/Components/design-elements";
|
||||||
|
import { Button } from "@/Components/inputs";
|
||||||
import { HeaderLogs } from "@/Pages/Docker/ContainerDetails/Components/HeaderLogs";
|
import { HeaderLogs } from "@/Pages/Docker/ContainerDetails/Components/HeaderLogs";
|
||||||
|
import { RowLog } from "@/Pages/Docker/ContainerDetails/Components/RowLog";
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
import type { DockerContainerLogsResponse } from "@/Types/Monitor";
|
import type { DockerContainerLogsResponse } from "@/Types/Monitor";
|
||||||
|
import type { DockerLog } from "@/Types/Check";
|
||||||
|
import type { ApiResponse } from "@/Hooks/UseApi";
|
||||||
|
|
||||||
// Hooks
|
// Hooks
|
||||||
import { useGet } from "@/Hooks/UseApi";
|
|
||||||
import {
|
import {
|
||||||
useState,
|
useState,
|
||||||
useEffect,
|
useEffect,
|
||||||
useLayoutEffect,
|
useLayoutEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
Fragment,
|
|
||||||
useRef,
|
useRef,
|
||||||
useCallback,
|
useCallback,
|
||||||
} from "react";
|
} from "react";
|
||||||
import type { DockerLog } from "@/Types/Check";
|
|
||||||
import { useTheme } from "@mui/material";
|
import { useTheme } from "@mui/material";
|
||||||
// Util
|
import { useTranslation } from "react-i18next";
|
||||||
import { LAYOUT, SPACING } from "@/Utils/Theme/constants";
|
import { useToast } from "@/Hooks/UseToast";
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
import { get } from "@/Utils/ApiClient";
|
||||||
|
import { LAYOUT } from "@/Utils/Theme/constants";
|
||||||
import { flattenDockerLogs } from "@/Utils/MonitorUtils";
|
import { flattenDockerLogs } from "@/Utils/MonitorUtils";
|
||||||
import { RowLog } from "@/Pages/Docker/ContainerDetails/Components/RowLog";
|
|
||||||
|
|
||||||
interface TabLogsProps {
|
interface TabLogsProps {
|
||||||
monitorId?: string;
|
monitorId?: string;
|
||||||
@@ -30,74 +35,177 @@ interface TabLogsProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const SCROLL_PIN_THRESHOLD_PX = 8;
|
const SCROLL_PIN_THRESHOLD_PX = 8;
|
||||||
|
const TAIL_POLL_MS = 10_000;
|
||||||
|
|
||||||
const isScrolledToBottom = (el: HTMLElement) =>
|
const isScrolledToBottom = (el: HTMLElement) =>
|
||||||
el.scrollHeight - el.scrollTop - el.clientHeight <= SCROLL_PIN_THRESHOLD_PX;
|
el.scrollHeight - el.scrollTop - el.clientHeight <= SCROLL_PIN_THRESHOLD_PX;
|
||||||
|
|
||||||
|
const fetchLogs = async (url: string, params: { before?: string; after?: string }) => {
|
||||||
|
const res = await get<ApiResponse<DockerContainerLogsResponse>>(url, { params });
|
||||||
|
return res.data.data;
|
||||||
|
};
|
||||||
|
|
||||||
export const TabLogs = ({ monitorId, containerName }: TabLogsProps) => {
|
export const TabLogs = ({ monitorId, containerName }: TabLogsProps) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const { toastError } = useToast();
|
||||||
|
|
||||||
// State
|
// logs is newest-first, matching the API and what flattenDockerLogs expects.
|
||||||
const [olderLogs, setOlderLogs] = useState<DockerLog[]>([]);
|
const [logs, setLogs] = useState<DockerLog[]>([]);
|
||||||
const [olderCursor, setOlderCursor] = useState<string | null | undefined>(undefined); // undefined -> no older requested, string -> cursor, null -> no more data
|
const [olderExhausted, setOlderExhausted] = useState(false);
|
||||||
|
const [loadingInitial, setLoadingInitial] = useState(false);
|
||||||
const [loadingOlder, setLoadingOlder] = useState(false);
|
const [loadingOlder, setLoadingOlder] = useState(false);
|
||||||
const [odlerError, setOlderError] = useState<unknown>(null);
|
const [lastUpdatedAt, setLastUpdatedAt] = useState<number | null>(null);
|
||||||
|
|
||||||
const baseLogsUrl =
|
const logsUrl =
|
||||||
monitorId && containerName
|
monitorId && containerName
|
||||||
? `/monitors/docker/details/${encodeURIComponent(monitorId)}/containers/${encodeURIComponent(containerName)}/logs`
|
? `/monitors/docker/details/${encodeURIComponent(monitorId)}/containers/${encodeURIComponent(containerName)}/logs`
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
const {
|
const newestCursor = logs[0]?.checkedAt;
|
||||||
data: head,
|
const oldestCursor = logs[logs.length - 1]?.checkedAt;
|
||||||
isLoading,
|
|
||||||
isValidating,
|
|
||||||
error: headError,
|
|
||||||
refetch,
|
|
||||||
} = useGet<DockerContainerLogsResponse>(
|
|
||||||
baseLogsUrl,
|
|
||||||
{},
|
|
||||||
{
|
|
||||||
refreshInterval: 10_000,
|
|
||||||
keepPreviousData: true,
|
|
||||||
revalidateOnFocus: true,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const rows = useMemo(() => flattenDockerLogs(head?.logs ?? []), [head?.logs]);
|
// Initial page. Resets everything when the container changes.
|
||||||
|
useEffect(() => {
|
||||||
|
setLogs([]);
|
||||||
|
setOlderExhausted(false);
|
||||||
|
if (!logsUrl) return;
|
||||||
|
let cancelled = false;
|
||||||
|
setLoadingInitial(true);
|
||||||
|
fetchLogs(logsUrl, {})
|
||||||
|
.then((page) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setLogs(page.logs);
|
||||||
|
setOlderExhausted(page.nextCursor === null);
|
||||||
|
setLastUpdatedAt(Date.now());
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
if (cancelled) return;
|
||||||
|
toastError(t("pages.docker.container.logs.loadError"));
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (!cancelled) setLoadingInitial(false);
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [logsUrl, t, toastError]);
|
||||||
|
|
||||||
|
const newestCursorRef = useRef(newestCursor);
|
||||||
|
newestCursorRef.current = newestCursor;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!logsUrl) return;
|
||||||
|
let inFlight = false;
|
||||||
|
const tick = async () => {
|
||||||
|
if (inFlight || document.hidden) return;
|
||||||
|
inFlight = true;
|
||||||
|
try {
|
||||||
|
const cursor = newestCursorRef.current;
|
||||||
|
const page = await fetchLogs(logsUrl, cursor ? { after: cursor } : {});
|
||||||
|
if (page.logs.length > 0) setLogs((prev) => [...page.logs, ...prev]);
|
||||||
|
setLastUpdatedAt(Date.now());
|
||||||
|
} catch (err: unknown) {
|
||||||
|
console.error(err);
|
||||||
|
} finally {
|
||||||
|
inFlight = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const id = window.setInterval(tick, TAIL_POLL_MS);
|
||||||
|
document.addEventListener("visibilitychange", tick);
|
||||||
|
return () => {
|
||||||
|
window.clearInterval(id);
|
||||||
|
document.removeEventListener("visibilitychange", tick);
|
||||||
|
};
|
||||||
|
}, [logsUrl]);
|
||||||
|
|
||||||
|
const rows = useMemo(() => flattenDockerLogs(logs), [logs]);
|
||||||
|
|
||||||
const logBoxRef = useRef<HTMLDivElement>(null);
|
const logBoxRef = useRef<HTMLDivElement>(null);
|
||||||
const pinnedToBottom = useRef(true);
|
const pinnedToBottom = useRef(true);
|
||||||
|
const prependAnchor = useRef<{ scrollHeight: number; scrollTop: number } | null>(null);
|
||||||
|
|
||||||
// Always pin to bottom when container/monitor changes
|
// Always pin to bottom when container/monitor changes
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
pinnedToBottom.current = true;
|
pinnedToBottom.current = true;
|
||||||
}, [monitorId, containerName]);
|
}, [monitorId, containerName]);
|
||||||
|
|
||||||
// New log rows
|
// Rows changed: restore position after a prepend, otherwise honour the pin
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
const el = logBoxRef.current;
|
const el = logBoxRef.current;
|
||||||
if (!el || !pinnedToBottom.current) return;
|
if (!el) return;
|
||||||
el.scrollTop = el.scrollHeight;
|
const anchor = prependAnchor.current;
|
||||||
|
if (anchor) {
|
||||||
|
prependAnchor.current = null;
|
||||||
|
el.scrollTop = el.scrollHeight - anchor.scrollHeight + anchor.scrollTop;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pinnedToBottom.current) el.scrollTop = el.scrollHeight;
|
||||||
}, [rows]);
|
}, [rows]);
|
||||||
|
|
||||||
const handleScroll = useCallback((event: React.UIEvent<HTMLDivElement>) => {
|
const handleScroll = useCallback((event: React.UIEvent<HTMLDivElement>) => {
|
||||||
pinnedToBottom.current = isScrolledToBottom(event.currentTarget);
|
pinnedToBottom.current = isScrolledToBottom(event.currentTarget);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const loadOlder = useCallback(async () => {
|
||||||
|
if (!logsUrl || !oldestCursor || loadingOlder) return;
|
||||||
|
setLoadingOlder(true);
|
||||||
|
try {
|
||||||
|
const page = await fetchLogs(logsUrl, { before: oldestCursor });
|
||||||
|
const el = logBoxRef.current;
|
||||||
|
if (el)
|
||||||
|
prependAnchor.current = {
|
||||||
|
scrollHeight: el.scrollHeight,
|
||||||
|
scrollTop: el.scrollTop,
|
||||||
|
};
|
||||||
|
setLogs((prev) => [...prev, ...page.logs]);
|
||||||
|
setOlderExhausted(page.nextCursor === null);
|
||||||
|
} catch {
|
||||||
|
toastError(t("pages.docker.container.logs.loadOlderError"));
|
||||||
|
} finally {
|
||||||
|
setLoadingOlder(false);
|
||||||
|
}
|
||||||
|
}, [logsUrl, oldestCursor, loadingOlder, t, toastError]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack gap={LAYOUT.MD}>
|
<Stack gap={LAYOUT.MD}>
|
||||||
<HeaderLogs
|
<HeaderLogs
|
||||||
lines={rows.length}
|
lines={rows.length}
|
||||||
isValidating={isValidating}
|
loading={loadingInitial}
|
||||||
|
lastUpdatedAt={lastUpdatedAt}
|
||||||
/>
|
/>
|
||||||
<BaseBox
|
<BaseBox
|
||||||
ref={logBoxRef}
|
ref={logBoxRef}
|
||||||
onScroll={handleScroll}
|
onScroll={handleScroll}
|
||||||
minHeight={240}
|
minHeight={240}
|
||||||
maxHeight="50vh"
|
maxHeight="50vh"
|
||||||
overflow={"auto"}
|
overflow="auto"
|
||||||
padding={LAYOUT.MD}
|
padding={LAYOUT.MD}
|
||||||
|
sx={{ overflowAnchor: "none" }}
|
||||||
>
|
>
|
||||||
|
{rows.length > 0 && (
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
justifyContent="center"
|
||||||
|
pb={LAYOUT.MD}
|
||||||
|
>
|
||||||
|
{olderExhausted ? (
|
||||||
|
<Typography color={theme.palette.text.secondary}>
|
||||||
|
{t("pages.docker.container.logs.noOlder")}
|
||||||
|
</Typography>
|
||||||
|
) : (
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
disabled={loadingOlder}
|
||||||
|
onClick={loadOlder}
|
||||||
|
>
|
||||||
|
{loadingOlder
|
||||||
|
? t("pages.docker.container.logs.loadingOlder")
|
||||||
|
: t("pages.docker.container.logs.loadOlder")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
)}
|
||||||
{rows.map((row) => {
|
{rows.map((row) => {
|
||||||
return (
|
return (
|
||||||
<RowLog
|
<RowLog
|
||||||
|
|||||||
@@ -956,6 +956,18 @@
|
|||||||
"tabs": {
|
"tabs": {
|
||||||
"overview": "Overview",
|
"overview": "Overview",
|
||||||
"logs": "Logs"
|
"logs": "Logs"
|
||||||
|
},
|
||||||
|
"logs": {
|
||||||
|
"recentLines": "{{count}} recent lines",
|
||||||
|
"loading": "Loading...",
|
||||||
|
"updatedSecondsAgo": "Updated {{count}}s ago",
|
||||||
|
"updatedMinutesAgo": "Updated {{count}}m ago",
|
||||||
|
"skipped": "Some log entries were skipped",
|
||||||
|
"loadOlder": "Load older logs",
|
||||||
|
"loadingOlder": "Loading...",
|
||||||
|
"noOlder": "Beginning of retained logs",
|
||||||
|
"loadError": "Could not load logs",
|
||||||
|
"loadOlderError": "Could not load older logs"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"host": {
|
"host": {
|
||||||
|
|||||||
Reference in new issue
Block a user