fix(dashboard): show sample times the same way in legend and table

The legend used a fixed US date format while the data table used the
reader's locale, so one panel showed the same instant two ways. Both
now use one locale-aware format. A test also pins that the data
toggle stays disabled for a panel with no series or an error.

Assisted-by: LLM
Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
ZverGuy committed 2026-11-05 00:56:26 +07:00
1 parent 046df7fe96
commit bfa85e57bf
5 files changed
+25 -10

No files matched your search

@@ -123,6 +123,21 @@ describe("MetricsView", () => {
expect(screen.getByRole("button", { name: "Show data for Network" })).toBeDisabled()
})
it("offers no table for a panel with no series or an error", async () => {
const { client } = clientWith((url) =>
url.searchParams.get("query")?.includes("memory")
? Response.json({ status: "error", error: "too many series" }, { status: 422 })
: Response.json({ status: "success", data: { resultType: "matrix", result: [] } }),
)
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
await screen.findByText("too many series. Choose a shorter range.")
await waitFor(() => expect(screen.getAllByText("No data for this range.")).toHaveLength(2))
for (const title of ["CPU (cores)", "Memory (working set)", "Network"]) {
expect(screen.getByRole("button", { name: `Show data for ${title}` })).toBeDisabled()
}
})
it("holds each chart's place while it loads", () => {
const { client } = clientWith(() => new Promise<Response>(() => {}))
@@ -1,4 +1,4 @@
import type { ChartData } from "../../lib/observability/metrics.ts"
import { formatSampleTime, type ChartData } from "../../lib/observability/metrics.ts"
interface SeriesTableProps {
id: string
@@ -7,9 +7,6 @@ interface SeriesTableProps {
format: (value: number) => string
}
const formatTime = (seconds: number) =>
new Date(seconds * 1000).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" })
export function SeriesTable({ id, title, data, format }: SeriesTableProps) {
const newestFirst = data.timestamps.map((t, i) => ({ t, i })).reverse()
return (
@@ -33,7 +30,7 @@ export function SeriesTable({ id, title, data, format }: SeriesTableProps) {
<tbody className="divide-y divide-slate-100 text-slate-700">
{newestFirst.map(({ t, i }) => (
<tr key={t}>
<th scope="row" className="px-3 py-1 text-left font-normal whitespace-nowrap">{formatTime(t)}</th>
<th scope="row" className="px-3 py-1 text-left font-normal whitespace-nowrap">{formatSampleTime(t)}</th>
{data.series.map((s, j) => {
const v = s.values[i]
return <td key={j} className="px-3 py-1 text-right">{v === null ? "–" : format(v)}</td>
@@ -13,11 +13,11 @@ vi.mock("uplot", () => ({
created.push({ opts, data, destroy: this.destroy })
}
setSize() {}
static fmtDate = () => (d: Date) => d.toISOString()
},
}))
const { TimeSeriesChart } = await import("./TimeSeriesChart.tsx")
const { formatSampleTime } = await import("../../lib/observability/metrics.ts")
beforeEach(() => {
created.length = 0
@@ -56,7 +56,7 @@ describe("TimeSeriesChart", () => {
const time = created[0].opts.series[0].value!
expect(time(null, null, 0, null)).toBe("latest")
expect(time(null, 2, 0, 1)).toBe(new Date(2000).toISOString())
expect(time(null, 2, 0, 1)).toBe(formatSampleTime(2))
})
it("destroys the chart on unmount", () => {
@@ -1,7 +1,7 @@
import { useEffect, useRef } from "react"
import uPlot from "uplot"
import "uplot/dist/uPlot.min.css"
import type { ChartData } from "../../lib/observability/metrics.ts"
import { formatSampleTime, type ChartData } from "../../lib/observability/metrics.ts"
const COLORS = ["#2563eb", "#16a34a", "#d97706", "#9333ea", "#dc2626", "#0891b2", "#4b5563", "#db2777"]
const HEIGHT = 200
@@ -19,7 +19,6 @@ export function TimeSeriesChart({ data, format }: TimeSeriesChartProps) {
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 stamp = uPlot.fmtDate("{M}/{D}/{YY} {h}:{mm}:{ss} {AA}")
const chart = new uPlot(
{
width: el.clientWidth || 600,
@@ -27,7 +26,7 @@ export function TimeSeriesChart({ data, format }: TimeSeriesChartProps) {
scales: { x: { time: true } },
legend: { live: true },
series: [
{ value: (_: uPlot, v: number | null, _sidx: number, idx: number | null) => (idx === null || v === null ? "latest" : stamp(new Date(v * 1000))) },
{ value: (_: uPlot, v: number | null, _sidx: number, idx: number | null) => (idx === null || v === null ? "latest" : formatSampleTime(v)) },
...data.series.map((s, i) => ({
label: s.label,
stroke: COLORS[i % COLORS.length],
@@ -93,6 +93,10 @@ export const METRIC_PANELS: MetricPanel[] = [
},
]
export function formatSampleTime(seconds: number): string {
return new Date(seconds * 1000).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" })
}
export interface ChartSeries {
label: string
values: (number | null)[]