diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/MetricsView.test.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/MetricsView.test.tsx index d3f1fc1dd..8e7829ed5 100644 --- a/packages/system/dashboard/images/console/apps/console/src/components/observability/MetricsView.test.tsx +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/MetricsView.test.tsx @@ -1,5 +1,5 @@ import { describe, it, expect, vi } from "vitest" -import { screen, waitFor } from "@testing-library/react" +import { screen, waitFor, within } from "@testing-library/react" import userEvent from "@testing-library/user-event" import { K8sClient } from "@cozystack/k8s-client" import { renderWithK8sProvider } from "../../test-utils/render.tsx" @@ -98,6 +98,31 @@ describe("MetricsView", () => { expect(screen.queryByText("Try a longer range.")).toBeNull() }) + it("shows a panel's samples as a table on request", async () => { + const { client } = clientWith(() => matrix("db-0")) + renderWithK8sProvider(, { client }) + await waitFor(() => expect(screen.getAllByTestId("chart")).toHaveLength(3)) + const toggle = screen.getByRole("button", { name: "Show data for CPU (cores)" }) + expect(toggle).toHaveAttribute("aria-expanded", "false") + + await userEvent.click(toggle) + + const table = screen.getByRole("region", { name: "CPU (cores) data" }) + expect(toggle).toHaveAttribute("aria-expanded", "true") + expect(toggle).toHaveAttribute("aria-controls", table.id) + expect(toggle).toHaveTextContent("Hide data") + expect(within(table).getByRole("columnheader", { name: "db-0" })).toBeInTheDocument() + expect(screen.queryByRole("region", { name: "Memory (working set) data" })).toBeNull() + }) + + it("offers no table while a panel has no samples", () => { + const { client } = clientWith(() => new Promise(() => {})) + + renderWithK8sProvider(, { client }) + + expect(screen.getByRole("button", { name: "Show data for Network" })).toBeDisabled() + }) + it("holds each chart's place while it loads", () => { const { client } = clientWith(() => new Promise(() => {})) diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/MetricsView.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/MetricsView.tsx index fac2a6b91..4663b2ae3 100644 --- a/packages/system/dashboard/images/console/apps/console/src/components/observability/MetricsView.tsx +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/MetricsView.tsx @@ -1,7 +1,7 @@ -import { useState } from "react" +import { useId, useState } from "react" import { useQueries } from "@tanstack/react-query" import { useK8sClient } from "@cozystack/k8s-client" -import { Section } from "@cozystack/ui" +import { Button, Section } from "@cozystack/ui" import { METRIC_PANELS, RANGES, @@ -14,6 +14,7 @@ import { ProxyNotice } from "./ProxyNotice.tsx" import { TimeSeriesChart } from "./TimeSeriesChart.tsx" import { RangePicker } from "./RangePicker.tsx" import { EmptyState } from "./EmptyState.tsx" +import { SeriesTable } from "./SeriesTable.tsx" const REFRESH_MS = 60_000 // The uPlot canvas plus its one-row legend, so a placeholder takes the chart's place without a jump. @@ -23,6 +24,8 @@ export function MetricsView({ scope }: { scope: ObservabilityScope }) { const client = useK8sClient() const root = metricsRoot(scope) const [range, setRange] = useState("1h") + const [tables, setTables] = useState>(new Set()) + const idBase = useId() const rangeKeys = Object.keys(RANGES) as RangeKey[] const longestRange = range === rangeKeys[rangeKeys.length - 1] @@ -57,8 +60,30 @@ export function MetricsView({ scope }: { scope: ObservabilityScope }) { ) : ( METRIC_PANELS.map((panel, i) => { const { data, error, isLoading } = results[i] + const hasSeries = !error && !isLoading && !!data && data.series.length > 0 + const tableOpen = hasSeries && tables.has(panel.id) + const tableId = `${idBase}-${panel.id}-data` + const toggle = ( + + ) return ( -
+
{error ? ( ) : isLoading || !data ? ( @@ -66,7 +91,10 @@ export function MetricsView({ scope }: { scope: ObservabilityScope }) { ) : data.series.length === 0 ? ( ) : ( - + <> + + {tableOpen && } + )}
) diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/SeriesTable.test.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/SeriesTable.test.tsx new file mode 100644 index 000000000..0b3364268 --- /dev/null +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/SeriesTable.test.tsx @@ -0,0 +1,37 @@ +import { describe, it, expect } from "vitest" +import { render, screen, within } from "@testing-library/react" +import { SeriesTable } from "./SeriesTable.tsx" + +const data = { + timestamps: [1_700_000_000, 1_700_000_060], + series: [ + { label: "db-0", values: [1, 2] }, + { label: "db-1", values: [null, 3] }, + ], +} + +describe("SeriesTable", () => { + it("is a named region the keyboard can reach", () => { + render() + + const region = screen.getByRole("region", { name: "Memory data" }) + expect(region).toHaveAttribute("id", "t") + expect(region).toHaveAttribute("tabindex", "0") + }) + + it("has a time column and one column per series", () => { + render() + + expect(screen.getAllByRole("columnheader").map((h) => h.textContent)).toEqual(["Time", "db-0", "db-1"]) + }) + + it("lists every sample newest first in the chart's units", () => { + render( `${v} B`} />) + + const rows = screen.getAllByRole("row").slice(1) + expect(rows).toHaveLength(2) + expect(within(rows[0]).getAllByRole("cell").map((c) => c.textContent)).toEqual(["2 B", "3 B"]) + expect(within(rows[1]).getAllByRole("cell").map((c) => c.textContent)).toEqual(["1 B", "–"]) + expect(within(rows[0]).getByRole("rowheader")).toHaveTextContent(new Date(1_700_000_060_000).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })) + }) +}) diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/SeriesTable.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/SeriesTable.tsx new file mode 100644 index 000000000..57cf755a0 --- /dev/null +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/SeriesTable.tsx @@ -0,0 +1,47 @@ +import type { ChartData } from "../../lib/observability/metrics.ts" + +interface SeriesTableProps { + id: string + title: string + data: ChartData + format: (value: number) => string +} + +const formatTime = (seconds: number) => + new Date(seconds * 1000).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" }) + +export function SeriesTable({ id, title, data, format }: SeriesTableProps) { + const newestFirst = data.timestamps.map((t, i) => ({ t, i })).reverse() + return ( +
+ + + + + + {data.series.map((s, j) => ( + + ))} + + + + {newestFirst.map(({ t, i }) => ( + + + {data.series.map((s, j) => { + const v = s.values[i] + return + })} + + ))} + +
{`${title}, newest first`}
Time{s.label}
{formatTime(t)}{v === null ? "–" : format(v)}
+
+ ) +} diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.test.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.test.tsx index 57cf6fb99..3df8125e3 100644 --- a/packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.test.tsx +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.test.tsx @@ -1,12 +1,15 @@ import { describe, it, expect, vi, beforeEach } from "vitest" import { render } from "@testing-library/react" -const created: { opts: { series: { label?: string }[] }; data: unknown[]; destroy: ReturnType }[] = [] +type LegendValue = (u: unknown, v: number | null, sidx: number, idx: number | null) => string +type Opts = { series: { label?: string; value?: LegendValue }[] } + +const created: { opts: Opts; data: unknown[]; destroy: ReturnType }[] = [] vi.mock("uplot", () => ({ default: class { destroy = vi.fn() - constructor(opts: { series: { label?: string }[] }, data: unknown[]) { + constructor(opts: Opts, data: unknown[]) { created.push({ opts, data, destroy: this.destroy }) } setSize() {} @@ -36,6 +39,16 @@ describe("TimeSeriesChart", () => { expect(created[0].opts.series.slice(1).map((s) => s.label)).toEqual(["db-0", "db-1"]) }) + it("shows each series' latest value in the legend while nothing is hovered", () => { + render( `${v} B`} />) + + const legend = created[0].opts.series[1].value! + + expect(legend(null, null, 1, null)).toBe("2 B") + expect(legend(null, 1, 1, 0)).toBe("1 B") + expect(legend(null, null, 1, 2)).toBe("–") + }) + it("destroys the chart on unmount", () => { const { unmount } = render() diff --git a/packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.tsx b/packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.tsx index 683df2dff..a6251d40f 100644 --- a/packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.tsx +++ b/packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.tsx @@ -18,6 +18,7 @@ export function TimeSeriesChart({ data, format }: TimeSeriesChartProps) { const el = ref.current if (!el) return const fmt = (v: number | null) => (v === null ? "–" : format(v)) + const latest = data.series.map((s) => s.values.findLast((v) => v !== null) ?? null) const chart = new uPlot( { width: el.clientWidth || 600, @@ -30,7 +31,8 @@ export function TimeSeriesChart({ data, format }: TimeSeriesChartProps) { label: s.label, stroke: COLORS[i % COLORS.length], width: 1.5, - value: (_: uPlot, v: number | null) => fmt(v), + // Without a hovered point the legend shows the latest sample instead of a dash. + value: (_: uPlot, v: number | null, sidx: number, idx: number | null) => fmt(idx === null ? latest[sidx - 1] : v), })), ], axes: [{}, { size: 84, values: (_: uPlot, splits: number[]) => splits.map(format) }],