Pin a chart to the last few runs of a flow
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useQueries } from "@tanstack/react-query"
|
||||
import { useQueries, useQuery } from "@tanstack/react-query"
|
||||
import { useEffect, useRef, useState } from "react"
|
||||
|
||||
import type { HistoryPoint } from "@/client"
|
||||
@@ -9,7 +9,12 @@ 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 {
|
||||
compareQueryOptions,
|
||||
NO_CURVE,
|
||||
runsListQueryOptions,
|
||||
shortenRunLabel,
|
||||
} from "@/components/Runs/queries"
|
||||
import { type DataContext, useDataContext } from "./dataContext"
|
||||
import { messageHistoryQueryOptions, usePublishMessage } from "./queries"
|
||||
import { usePalette } from "./settings"
|
||||
@@ -96,13 +101,86 @@ export function ChartWidget(props: WidgetProps) {
|
||||
// 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" ? (
|
||||
const source = config(props.widget).source
|
||||
if (source === "runs") return <RunsChart {...props} />
|
||||
return source === "query" ? (
|
||||
<QueryChart {...props} />
|
||||
) : (
|
||||
<LiveChart {...props} />
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The last few runs of something, side by side, always.
|
||||
*
|
||||
* A tile rather than a way of looking at the page: what a panel over a lab
|
||||
* bench shows without anyone picking runs first. The comparison endpoint
|
||||
* already answers in the shape this draws, so the widget only has to decide
|
||||
* which runs it means.
|
||||
*/
|
||||
function RunsChart({ widget }: WidgetProps) {
|
||||
const cfg = (widget.config ?? {}) as {
|
||||
runs?: {
|
||||
metric?: string
|
||||
flow?: string
|
||||
group?: string
|
||||
ids?: string[]
|
||||
latest?: number
|
||||
}
|
||||
refresh_s?: number
|
||||
}
|
||||
const pick = cfg.runs ?? {}
|
||||
const metric = pick.metric ?? ""
|
||||
const explicit = pick.ids ?? []
|
||||
const refreshMs = Math.max(15, Number(cfg.refresh_s) || 60) * 1000
|
||||
|
||||
// Named runs need no lookup. Otherwise the newest of a sweep, or of a flow —
|
||||
// and of a flow only the ones that finished, since a panel comparing curves
|
||||
// wants results rather than the crash from ten minutes ago.
|
||||
const found = useQuery({
|
||||
...runsListQueryOptions(
|
||||
pick.group
|
||||
? { group: pick.group }
|
||||
: {
|
||||
flow: pick.flow,
|
||||
status: "ok",
|
||||
limit: Math.min(Math.max(Number(pick.latest) || 3, 1), MAX_SERIES),
|
||||
},
|
||||
),
|
||||
enabled: explicit.length === 0 && Boolean(pick.group || pick.flow),
|
||||
})
|
||||
|
||||
const ids = explicit.length
|
||||
? explicit.slice(0, MAX_SERIES)
|
||||
: (found.data ?? []).slice(0, MAX_SERIES).map((run) => run.id)
|
||||
|
||||
const { data, isPending } = useQuery(
|
||||
compareQueryOptions(ids, metric, refreshMs),
|
||||
)
|
||||
const palette = usePalette()
|
||||
const lines = (data?.lines ?? []).slice(0, MAX_SERIES)
|
||||
const points = lines.reduce(
|
||||
(total, line) => total + (line.points?.length ?? 0),
|
||||
0,
|
||||
)
|
||||
|
||||
if (!metric) return <p className="text-muted-foreground">Pick a metric.</p>
|
||||
|
||||
return (
|
||||
<UplotChart
|
||||
labels={lines.map((line) => shortenRunLabel(line.label))}
|
||||
plots={lines.map((line) =>
|
||||
(line.points ?? []).map(([step, value]) => ({ ts: step, value })),
|
||||
)}
|
||||
palette={palette}
|
||||
xTime={false}
|
||||
pending={isPending || found.isPending}
|
||||
empty={points === 0 ? NO_CURVE : undefined}
|
||||
{...presentation(widget.config as Record<string, unknown>)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The same chart, drawn from a data context.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user