Let a dashboard be read against runs instead of the live engine

This commit is contained in:
2026-08-25 11:48:15 +02:00
parent deffd1623e
commit c9880cf543
13 changed files with 438 additions and 32 deletions
@@ -9,6 +9,8 @@ import {
} from "@/components/Common/RangePicker"
import { MAX_SERIES, UplotChart } from "@/components/Common/UplotChart"
import { useLiveValue, useLiveValues } from "@/components/Flow/liveStore"
import { NO_CURVE } from "@/components/Runs/queries"
import { type DataContext, useDataContext } from "./dataContext"
import { messageHistoryQueryOptions, usePublishMessage } from "./queries"
import { usePalette } from "./settings"
import { useUi } from "./ui"
@@ -90,6 +92,10 @@ function merge(fetched: HistoryPoint[], tail: HistoryPoint[], cap: number) {
* different set of hooks, and React counts them.
*/
export function ChartWidget(props: WidgetProps) {
// A dashboard opened against something — runs today, a replayed day later —
// draws from that instead, whichever source it was pointed at otherwise.
const context = useDataContext()
if (context) return <ContextChart {...props} context={context} />
return config(props.widget).source === "query" ? (
<QueryChart {...props} />
) : (
@@ -97,6 +103,62 @@ export function ChartWidget(props: WidgetProps) {
)
}
/**
* The same chart, drawn from a data context.
*
* One line per member per bound message: three runs of a two-series chart is
* six curves, capped like any other chart at what the ramp can tell apart.
*/
function ContextChart({
widget,
context,
}: WidgetProps & { context: DataContext }) {
const cfg = widget.config as {
series?: Series[]
runs?: { metric?: string }
}
const bound = [
...(cfg.series ?? []).map((entry) => entry.message),
cfg.runs?.metric,
].filter((name): name is string => Boolean(name))
const palette = usePalette()
const drawn = bound.flatMap((name) => {
const lines = context.lines(name) ?? []
// The message name is only worth writing when the chart draws more than
// one of them; otherwise the member's own label is the whole story.
return lines.map((line) => ({
label: bound.length > 1 ? `${name} · ${line.label}` : line.label,
points: line.points,
}))
})
const shown = drawn.slice(0, MAX_SERIES)
const points = shown.reduce((total, line) => total + line.points.length, 0)
if (bound.length === 0) {
return <p className="text-muted-foreground">Pick a message.</p>
}
return (
<div className="flex h-full flex-col">
<UplotChart
labels={shown.map((line) => line.label)}
plots={shown.map((line) => line.points)}
palette={palette}
xTime={context.xTime}
pending={context.pending}
empty={points === 0 ? NO_CURVE : undefined}
{...presentation(widget.config as Record<string, unknown>)}
/>
{drawn.length > MAX_SERIES && (
<p className="text-muted-foreground text-xs">
Showing {MAX_SERIES} of {drawn.length} lines.
</p>
)}
</div>
)
}
/**
* Several messages over time, drawn on one axis.
*