diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/LogLines.test.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogLines.test.tsx new file mode 100644 index 000000000..775c5fcc6 --- /dev/null +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogLines.test.tsx @@ -0,0 +1,34 @@ +import { describe, it, expect } from "vitest" +import { fireEvent, render, screen } from "@testing-library/react" +import { LogLines } from "./LogLines.tsx" + +const entry = (n: number) => ({ _time: `2026-11-04T10:00:0${n}Z`, _msg: `line ${n}`, _stream_id: "s" }) + +function sized(list: HTMLElement, scrollHeight: number) { + Object.defineProperty(list, "scrollHeight", { configurable: true, value: scrollHeight }) + Object.defineProperty(list, "clientHeight", { configurable: true, value: 100 }) +} + +describe("LogLines", () => { + it("keeps following the newest line while the reader is at the bottom", () => { + const { rerender } = render() + const list = screen.getByRole("list") + sized(list, 500) + + rerender() + + expect(list.scrollTop).toBe(500) + }) + + it("leaves the position alone once the reader scrolls up", () => { + const { rerender } = render() + const list = screen.getByRole("list") + sized(list, 500) + list.scrollTop = 120 + fireEvent.scroll(list) + + rerender() + + expect(list.scrollTop).toBe(120) + }) +}) diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/LogLines.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogLines.tsx new file mode 100644 index 000000000..a6f4b5861 --- /dev/null +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogLines.tsx @@ -0,0 +1,34 @@ +import { useEffect, useRef } from "react" +import { CONTAINER_FIELD, POD_FIELD } from "../../lib/observability/logs.ts" +import type { LogEntry } from "../../lib/observability/proxy.ts" + +const BOTTOM_SLACK_PX = 24 + +export function LogLines({ entries }: { entries: LogEntry[] }) { + const ref = useRef(null) + const pinned = useRef(true) + + useEffect(() => { + if (ref.current && pinned.current) ref.current.scrollTop = ref.current.scrollHeight + }, [entries]) + + if (entries.length === 0) return

No log lines for this range and filter.

+ return ( +
    { + const el = e.currentTarget + pinned.current = el.scrollHeight - el.scrollTop - el.clientHeight <= BOTTOM_SLACK_PX + }} + className="max-h-[60vh] overflow-auto rounded-md bg-slate-950 p-3 font-mono text-xs leading-5 text-slate-100" + > + {entries.map((e, i) => ( +
  1. + {new Date(e._time).toLocaleTimeString()}{" "} + {`${e[POD_FIELD] ?? ""}/${e[CONTAINER_FIELD] ?? ""}`}{" "} + {e._msg} +
  2. + ))} +
+ ) +} diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.test.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.test.tsx index fae3f6a4b..526efeb23 100644 --- a/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.test.tsx +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.test.tsx @@ -81,6 +81,18 @@ describe("LogsView", () => { expect(screen.getByRole("button", { name: "Follow" })).toHaveAttribute("aria-pressed", "true") }) + it("says the tail stopped when the proxy refuses the query", async () => { + const { client } = clientWith((endpoint, params) => + endpoint.endsWith("tail") ? new Response("pipe not allowed in tail", { status: 400 }) : defaultHandler(endpoint, params), + ) + renderWithK8sProvider(, { client }) + + await userEvent.click(screen.getByRole("button", { name: "Follow" })) + + expect(await screen.findByText(/Stopped/)).toBeInTheDocument() + expect(screen.getByText("pipe not allowed in tail")).toBeInTheDocument() + }) + it("renders a cluster without the proxy as not available", async () => { const { client } = clientWith(() => new Response("404 page not found", { status: 404 })) diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.tsx index 707b037b9..f21f2eb86 100644 --- a/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.tsx +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/LogsView.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState } from "react" +import { useState } from "react" import { useQuery } from "@tanstack/react-query" import { useK8sClient } from "@cozystack/k8s-client" import { Button, Spinner } from "@cozystack/ui" @@ -14,15 +14,16 @@ import { isScopeWide, logsRoot, queryLogs, - type LogEntry, type ObservabilityScope, } from "../../lib/observability/proxy.ts" import { useLogTail } from "../../lib/observability/use-log-tail.ts" import { ProxyNotice } from "./ProxyNotice.tsx" import { RangePicker } from "./RangePicker.tsx" +import { LogLines } from "./LogLines.tsx" const HISTORY_LIMIT = 500 const FOLLOW_CAP = 1000 +const TAIL_STATE_LABEL = { connecting: "Connecting…", live: "Live", reconnecting: "Reconnecting…", stopped: "Stopped" } function windowFor(range: LogRangeKey) { const end = Date.now() @@ -50,25 +51,6 @@ function Picker({ label, value, options, onChange }: { label: string; value: str ) } -function LogLines({ entries }: { entries: LogEntry[] }) { - const ref = useRef(null) - useEffect(() => { - if (ref.current) ref.current.scrollTop = ref.current.scrollHeight - }, [entries]) - if (entries.length === 0) return

No log lines for this range and filter.

- return ( -
    - {entries.map((e, i) => ( -
  1. - {new Date(e._time).toLocaleTimeString()}{" "} - {`${e[POD_FIELD] ?? ""}/${e[CONTAINER_FIELD] ?? ""}`}{" "} - {e._msg} -
  2. - ))} -
- ) -} - export function LogsView({ scope }: { scope: ObservabilityScope }) { const client = useK8sClient() const root = logsRoot(scope) @@ -77,7 +59,8 @@ export function LogsView({ scope }: { scope: ObservabilityScope }) { const [filter, setFilter] = useState("") const [pod, setPod] = useState("") const [container, setContainer] = useState("") - const [follow, setFollow] = useState(false) + const [followRun, setFollowRun] = useState(null) + const follow = followRun !== null const query = buildLogsQuery({ filter, pod, container }) const pickerQuery = (field: string, narrowTo?: string) => ({ @@ -100,7 +83,7 @@ export function LogsView({ scope }: { scope: ObservabilityScope }) { enabled: !follow, retry: false, }) - const tail = useLogTail(root, query, follow, { cap: FOLLOW_CAP }) + const tail = useLogTail(root, query, followRun, { cap: FOLLOW_CAP }) const wide = [pods.error, history.error, tail.error].find(isScopeWide) if (wide) { @@ -135,13 +118,13 @@ export function LogsView({ scope }: { scope: ObservabilityScope }) { { setPod(v); setContainer("") }} /> {!follow && } - {follow && (

- {tail.state === "live" ? "Live" : tail.state === "reconnecting" ? "Reconnecting…" : "Connecting…"} + {TAIL_STATE_LABEL[tail.state]} {` · newest ${FOLLOW_CAP} lines kept`}

)}