Skip to content

Commit bfa85e5

Browse files
committed
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 <[email protected]>
1 parent 046df7f commit bfa85e5

5 files changed

Lines changed: 25 additions & 10 deletions

File tree

‎packages/system/dashboard/images/console/apps/console/src/components/observability/MetricsView.test.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ describe("MetricsView", () => {
123123
expect(screen.getByRole("button", { name: "Show data for Network" })).toBeDisabled()
124124
})
125125

126+
it("offers no table for a panel with no series or an error", async () => {
127+
const { client } = clientWith((url) =>
128+
url.searchParams.get("query")?.includes("memory")
129+
? Response.json({ status: "error", error: "too many series" }, { status: 422 })
130+
: Response.json({ status: "success", data: { resultType: "matrix", result: [] } }),
131+
)
132+
renderWithK8sProvider(<MetricsView scope={scope} />, { client })
133+
134+
await screen.findByText("too many series. Choose a shorter range.")
135+
await waitFor(() => expect(screen.getAllByText("No data for this range.")).toHaveLength(2))
136+
for (const title of ["CPU (cores)", "Memory (working set)", "Network"]) {
137+
expect(screen.getByRole("button", { name: `Show data for ${title}` })).toBeDisabled()
138+
}
139+
})
140+
126141
it("holds each chart's place while it loads", () => {
127142
const { client } = clientWith(() => new Promise<Response>(() => {}))
128143

‎packages/system/dashboard/images/console/apps/console/src/components/observability/SeriesTable.tsx‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ChartData } from "../../lib/observability/metrics.ts"
1+
import { formatSampleTime, type ChartData } from "../../lib/observability/metrics.ts"
22

33
interface SeriesTableProps {
44
id: string
@@ -7,9 +7,6 @@ interface SeriesTableProps {
77
format: (value: number) => string
88
}
99

10-
const formatTime = (seconds: number) =>
11-
new Date(seconds * 1000).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" })
12-
1310
export function SeriesTable({ id, title, data, format }: SeriesTableProps) {
1411
const newestFirst = data.timestamps.map((t, i) => ({ t, i })).reverse()
1512
return (
@@ -33,7 +30,7 @@ export function SeriesTable({ id, title, data, format }: SeriesTableProps) {
3330
<tbody className="divide-y divide-slate-100 text-slate-700">
3431
{newestFirst.map(({ t, i }) => (
3532
<tr key={t}>
36-
<th scope="row" className="px-3 py-1 text-left font-normal whitespace-nowrap">{formatTime(t)}</th>
33+
<th scope="row" className="px-3 py-1 text-left font-normal whitespace-nowrap">{formatSampleTime(t)}</th>
3734
{data.series.map((s, j) => {
3835
const v = s.values[i]
3936
return <td key={j} className="px-3 py-1 text-right">{v === null ? "–" : format(v)}</td>

‎packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.test.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ vi.mock("uplot", () => ({
1313
created.push({ opts, data, destroy: this.destroy })
1414
}
1515
setSize() {}
16-
static fmtDate = () => (d: Date) => d.toISOString()
1716
},
1817
}))
1918

2019
const { TimeSeriesChart } = await import("./TimeSeriesChart.tsx")
20+
const { formatSampleTime } = await import("../../lib/observability/metrics.ts")
2121

2222
beforeEach(() => {
2323
created.length = 0
@@ -56,7 +56,7 @@ describe("TimeSeriesChart", () => {
5656
const time = created[0].opts.series[0].value!
5757

5858
expect(time(null, null, 0, null)).toBe("latest")
59-
expect(time(null, 2, 0, 1)).toBe(new Date(2000).toISOString())
59+
expect(time(null, 2, 0, 1)).toBe(formatSampleTime(2))
6060
})
6161

6262
it("destroys the chart on unmount", () => {

‎packages/system/dashboard/images/console/apps/console/src/components/observability/TimeSeriesChart.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useRef } from "react"
22
import uPlot from "uplot"
33
import "uplot/dist/uPlot.min.css"
4-
import type { ChartData } from "../../lib/observability/metrics.ts"
4+
import { formatSampleTime, type ChartData } from "../../lib/observability/metrics.ts"
55

66
const COLORS = ["#2563eb", "#16a34a", "#d97706", "#9333ea", "#dc2626", "#0891b2", "#4b5563", "#db2777"]
77
const HEIGHT = 200
@@ -19,15 +19,14 @@ export function TimeSeriesChart({ data, format }: TimeSeriesChartProps) {
1919
if (!el) return
2020
const fmt = (v: number | null) => (v === null ? "–" : format(v))
2121
const latest = data.series.map((s) => s.values.findLast((v) => v !== null) ?? null)
22-
const stamp = uPlot.fmtDate("{M}/{D}/{YY} {h}:{mm}:{ss} {AA}")
2322
const chart = new uPlot(
2423
{
2524
width: el.clientWidth || 600,
2625
height: HEIGHT,
2726
scales: { x: { time: true } },
2827
legend: { live: true },
2928
series: [
30-
{ value: (_: uPlot, v: number | null, _sidx: number, idx: number | null) => (idx === null || v === null ? "latest" : stamp(new Date(v * 1000))) },
29+
{ value: (_: uPlot, v: number | null, _sidx: number, idx: number | null) => (idx === null || v === null ? "latest" : formatSampleTime(v)) },
3130
...data.series.map((s, i) => ({
3231
label: s.label,
3332
stroke: COLORS[i % COLORS.length],

‎packages/system/dashboard/images/console/apps/console/src/lib/observability/metrics.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export const METRIC_PANELS: MetricPanel[] = [
9393
},
9494
]
9595

96+
export function formatSampleTime(seconds: number): string {
97+
return new Date(seconds * 1000).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" })
98+
}
99+
96100
export interface ChartSeries {
97101
label: string
98102
values: (number | null)[]

0 commit comments

Comments
 (0)