Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Split interactive sparkline runtime owners

rcourtman6801690ec2de
diff --git a/docs/release-control/v6/internal/subsystems/frontend-primitives.md b/docs/release-control/v6/internal/subsystems/frontend-primitives.md
index 5e04b1af63..524f7a74a7 100644
--- a/docs/release-control/v6/internal/subsystems/frontend-primitives.md
+++ b/docs/release-control/v6/internal/subsystems/frontend-primitives.md
@@ -179,6 +179,14 @@ owns sorting, count, identity-alias, and linked-agent derivation, and
owns the per-row render/runtime surface. Future work should extend those
owners instead of pushing websocket, alert, or identity plumbing back into the
shared table shell.
+The shared interactive sparkline now follows that same split.
+`frontend-modern/src/components/shared/InteractiveSparkline.tsx` stays the
+render shell, `frontend-modern/src/components/shared/useInteractiveSparklineState.ts`
+owns hover state, RAF throttling, canvas draw scheduling, and resize lifecycle,
+and `frontend-modern/src/components/shared/interactiveSparklineModel.ts` owns
+sparkline downsampling, gap segmentation, axis-tick math, and hover-selection
+policy. Future sparkline work should extend those owners instead of pushing
+canvas scheduling or chart-shape math back into the shared component shell.
The audit log settings surface now follows that same owner split.
`frontend-modern/src/components/Settings/AuditLogPanel.tsx` stays the canonical
diff --git a/frontend-modern/src/components/shared/InteractiveSparkline.tsx b/frontend-modern/src/components/shared/InteractiveSparkline.tsx
index cf378a7781..fdfbfa61f6 100644
--- a/frontend-modern/src/components/shared/InteractiveSparkline.tsx
+++ b/frontend-modern/src/components/shared/InteractiveSparkline.tsx
@@ -1,830 +1,29 @@
-import { Component, Show, For, createEffect, createMemo, createSignal, onCleanup } from 'solid-js';
+import { Component, For, Show } from 'solid-js';
import { Portal } from 'solid-js/web';
-import type { MetricPoint, TimeRange } from '@/api/charts';
-import { downsampleLTTB, calculateOptimalPoints } from '@/utils/downsample';
-import { timeRangeToMs } from '@/utils/timeRange';
-import { scheduleSparkline, setupCanvasDPR } from '@/utils/canvasRenderQueue';
+import { formatInteractiveSparklineHoverTime, type InteractiveSparklineProps } from './interactiveSparklineModel';
+import { useInteractiveSparklineState } from './useInteractiveSparklineState';
-export interface InteractiveSparklineSeries {
- id?: string;
- data: MetricPoint[];
- color: string;
- name?: string;
-}
-
-interface InteractiveSparklineProps {
- series: InteractiveSparklineSeries[];
- rangeLabel?: string;
- timeRange?: TimeRange;
- renderMode?: 'auto' | 'svg' | 'canvas';
- yMode?: 'percent' | 'auto';
- size?: 'sm' | 'md' | 'lg';
- /** When true, keep a synthetic window-start anchor connected to first real point. */
- bridgeLeadingGap?: boolean;
- formatValue?: (value: number) => string;
- formatTopLabel?: (maxValue: number) => string;
- sortTooltipByValue?: boolean;
- maxTooltipRows?: number;
- highlightNearestSeriesOnHover?: boolean;
- highlightSeriesId?: string | null;
-}
-
-const formatHoverTime = (timestamp: number): string =>
- new Date(timestamp).toLocaleString([], {
- year: 'numeric',
- month: 'short',
- day: '2-digit',
- hour: '2-digit',
- minute: '2-digit',
- second: '2-digit',
- hour12: false,
- });
-
-const clamp = (value: number, min: number, max: number): number =>
- Math.max(min, Math.min(value, max));
-
-const formatRelativeOffset = (offsetMs: number): string => {
- if (offsetMs <= 0) return 'now';
- const dayMs = 24 * 60 * 60_000;
- const hourMs = 60 * 60_000;
- const minuteMs = 60_000;
-
- if (offsetMs >= dayMs) {
- return `-${Math.round(offsetMs / dayMs)}d`;
- }
- if (offsetMs >= hourMs) {
- return `-${Math.round(offsetMs / hourMs)}h`;
- }
- return `-${Math.max(1, Math.round(offsetMs / minuteMs))}m`;
-};
-
-const findNearestMetricPoint = (
- points: MetricPoint[],
- targetTimestamp: number,
-): { point: MetricPoint; index: number } | null => {
- if (points.length === 0) return null;
-
- let low = 0;
- let high = points.length - 1;
- while (low < high) {
- const mid = Math.floor((low + high) / 2);
- if (points[mid].timestamp < targetTimestamp) {
- low = mid + 1;
- } else {
- high = mid;
- }
- }
-
- const candidate = points[low];
- const previous = low > 0 ? points[low - 1] : candidate;
- if (
- Math.abs(previous.timestamp - targetTimestamp) <=
- Math.abs(candidate.timestamp - targetTimestamp)
- ) {
- return { point: previous, index: low > 0 ? low - 1 : low };
- }
- return { point: candidate, index: low };
-};
-
-const ensureRenderablePoints = (points: MetricPoint[], windowStart: number): MetricPoint[] => {
- if (points.length === 0) return [];
- if (points.length >= 2) return points;
- const point = points[0];
- return [
- {
- timestamp: Math.max(windowStart, point.timestamp - 1_000),
- value: point.value,
- },
- point,
- ];
-};
-
-const splitPointsOnGaps = (
- points: MetricPoint[],
- gapThresholdMs: number,
- windowStart: number,
- bridgeLeadingGap: boolean,
-): MetricPoint[][] => {
- if (points.length === 0) return [];
- const segments: MetricPoint[][] = [];
- let currentSegment: MetricPoint[] = [points[0]];
-
- for (let i = 1; i < points.length; i++) {
- const previous = points[i - 1];
- const current = points[i];
- const hasGap = current.timestamp - previous.timestamp > gapThresholdMs;
- const isLeadingBridgeGap =
- bridgeLeadingGap &&
- segments.length === 0 &&
- currentSegment.length === 1 &&
- currentSegment[0].timestamp <= windowStart + 1_000;
-
- if (hasGap && !isLeadingBridgeGap) {
- segments.push(ensureRenderablePoints(currentSegment, windowStart));
- currentSegment = [current];
- continue;
- }
- currentSegment.push(current);
- }
-
- segments.push(ensureRenderablePoints(currentSegment, windowStart));
- return segments;
-};
-
-const inferGapThresholdMs = (points: MetricPoint[], rangeMs: number): number => {
- if (points.length < 3) return rangeMs + 1;
-
- const deltas: number[] = [];
- for (let i = 1; i < points.length; i++) {
- const delta = points[i].timestamp - points[i - 1].timestamp;
- if (delta > 0) {
- deltas.push(delta);
- }
- }
- if (deltas.length < 2) return rangeMs + 1;
-
- deltas.sort((a, b) => a - b);
- // P90 captures upper normal variation; 3x multiplier only breaks on genuine outages.
- const p90Delta = deltas[Math.floor(deltas.length * 0.9)];
- const minThreshold = Math.max(15_000, Math.floor(rangeMs / 120));
- const maxThreshold = Math.max(minThreshold, Math.floor(rangeMs / 2));
- return clamp(p90Delta * 3, minThreshold, maxThreshold);
-};
-
-interface HoverSeriesValue {
- name: string;
- color: string;
- value: number;
- timestamp: number;
- seriesIndex: number;
-}
-
-const selectTopValuesByValue = (values: HoverSeriesValue[], limit: number): HoverSeriesValue[] => {
- if (limit <= 0 || values.length === 0) return [];
- if (values.length <= limit) {
- return [...values].sort((a, b) => b.value - a.value);
- }
-
- const top: HoverSeriesValue[] = [];
- for (const value of values) {
- let insertAt = top.length;
- for (let i = 0; i < top.length; i++) {
- if (value.value > top[i].value) {
- insertAt = i;
- break;
- }
- }
- if (insertAt >= limit && top.length >= limit) continue;
- top.splice(insertAt, 0, value);
- if (top.length > limit) {
- top.pop();
- }
- }
- return top;
-};
+export type {
+ InteractiveSparklineProps,
+ InteractiveSparklineSeries,
+} from './interactiveSparklineModel';
export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props) => {
let chartSurfaceRef: Element | undefined;
let canvasRef: HTMLCanvasElement | undefined;
let canvasHostRef: HTMLDivElement | undefined;
- const vbH = 100;
- const vbW = 200;
- const canvasSeriesThreshold = 120;
- const canvasPointThreshold = 15_000;
- const tooltipPadding = 8;
- const tooltipEstimatedWidth = 190;
- const maxRows = () => props.maxTooltipRows ?? 6;
- const yMode = () => props.yMode ?? 'percent';
- const shouldUseCanvas = createMemo(() => {
- const renderMode = props.renderMode ?? 'auto';
- if (renderMode === 'svg') return false;
- if (renderMode === 'canvas') return true;
- const totalPoints = props.series.reduce((sum, series) => sum + series.data.length, 0);
- return props.series.length >= canvasSeriesThreshold || totalPoints >= canvasPointThreshold;
- });
- const formatValue = (value: number) =>
- props.formatValue ? props.formatValue(value) : `${value.toFixed(1)}%`;
-
- const [hoveredState, setHoveredState] = createSignal<{
- x: number;
- tooltipX: number;
- tooltipY: number;
- timestamp: number;
- totalValues: number;
- minY: number;
- nearestSeriesIndex: number | null;
- highlightedSeriesIndex: number | null;
- focusedTooltip: boolean;
- values: { name: string; color: string; value: number; seriesIndex: number }[];
- } | null>(null);
- const [lockedSeriesIndex, setLockedSeriesIndex] = createSignal<number | null>(null);
-
- const chartData = createMemo(() => {
- const range = props.timeRange || '1h';
- const rangeMs = timeRangeToMs(range);
- const windowEnd = Date.now();
- const windowStart = windowEnd - rangeMs;
- const targetPoints = calculateOptimalPoints(vbW, 'sparkline');
- const bridgeLeadingGap = props.bridgeLeadingGap === true;
- const validSeries = props.series
- .map((series) => {
- const inWindow = series.data.filter(
- (point) => point.timestamp >= windowStart && point.timestamp <= windowEnd,
- );
- // Need at least 2 real points to render a meaningful line.
- // Single-point series create tiny artifacts at the chart edge.
- if (inWindow.length < 2) return null;
- const renderable = ensureRenderablePoints(inWindow, windowStart);
- if (renderable.length === 0) return null;
- // Downsample to target resolution — LTTB preserves visual shape (peaks, valleys, trends).
- const drawData =
- renderable.length > targetPoints * 1.5
- ? downsampleLTTB(renderable, targetPoints)
- : renderable;
- const segments = splitPointsOnGaps(
- drawData,
- inferGapThresholdMs(drawData, rangeMs),
- windowStart,
- bridgeLeadingGap,
- );
- return {
- ...series,
- hoverData: renderable,
- drawData,
- segments,
- };
- })
- .filter(
- (
- series,
- ): series is {
- id?: string;
- data: MetricPoint[];
- color: string;
- name?: string;
- hoverData: MetricPoint[];
- drawData: MetricPoint[];
- segments: MetricPoint[][];
- } => series !== null,
- );
-
- if (validSeries.length === 0) {
- return {
- validSeries: [] as Array<{
- id?: string;
- data: MetricPoint[];
- color: string;
- name?: string;
- hoverData: MetricPoint[];
- drawData: MetricPoint[];
- segments: MetricPoint[][];
- }>,
- paths: [] as { path: string; areaPath?: string; color: string; seriesIndex: number }[],
- windowStart,
- rangeMs,
- scaleMax: 0,
- };
- }
-
- let scaleMax = 100;
- if (yMode() === 'auto') {
- scaleMax = 0;
- for (const series of validSeries) {
- for (const point of series.hoverData) {
- if (point.value > scaleMax) scaleMax = point.value;
- }
- }
- if (scaleMax <= 0) {
- return {
- validSeries,
- paths: [] as { path: string; areaPath?: string; color: string; seriesIndex: number }[],
- windowStart,
- rangeMs,
- scaleMax: 0,
- };
- }
- }
-
- // Break lines across large gaps so we don't imply continuity through missing data.
- // Skip SVG path generation in canvas mode to avoid expensive string allocation.
- const paths = shouldUseCanvas()
- ? []
- : (() => {
- return validSeries.flatMap((series, index) => {
- return series.segments.map((segment) => {
- const coords = segment.map((point) => {
- const x = clamp(((point.timestamp - windowStart) / rangeMs) * vbW, 0, vbW);
- const normalized =
- yMode() === 'auto'
- ? Math.max(0, point.value) / scaleMax
- : Math.min(Math.max(point.value, 0), 100) / 100;
- const y = vbH - normalized * vbH;
- return { x, y };
- });
- const pathStrings = coords.map((c) => `${c.x.toFixed(1)},${c.y.toFixed(1)}`);
- const path = `M${pathStrings.join('L')}`;
- let areaPath = '';
- if (validSeries.length === 1 && coords.length > 1) {
- areaPath = `${path} L${coords[coords.length - 1].x.toFixed(1)},${vbH} L${coords[0].x.toFixed(1)},${vbH} Z`;
- }
- return { path, areaPath, color: series.color, seriesIndex: index };
- });
- });
- })();
-
- return {
- validSeries,
- paths,
- windowStart,
- rangeMs,
- scaleMax,
- };
- });
-
- let pendingHoverPosition: { clientX: number; clientY: number } | null = null;
- let hoverRafId: number | null = null;
-
- const computeHoverState = (clientX: number, clientY: number) => {
- const computed = chartData();
- if (!chartSurfaceRef || computed.validSeries.length === 0 || computed.rangeMs <= 0) return;
- const rect = chartSurfaceRef.getBoundingClientRect();
- if (rect.width <= 0) return;
-
- const mouseX = Math.max(0, Math.min(clientX - rect.left, rect.width));
- const chartX = (mouseX / rect.width) * vbW;
- const targetTimestamp = computed.windowStart + (chartX / vbW) * computed.rangeMs;
- const shouldTrackNearest = props.highlightNearestSeriesOnHover === true;
- const chartY = shouldTrackNearest
- ? (Math.max(0, Math.min(clientY - rect.top, rect.height)) / rect.height) * vbH
- : 0;
- const valueToChartY = (value: number): number => {
- if (yMode() === 'auto') {
- if (computed.scaleMax <= 0) return vbH;
- return vbH - (Math.max(0, value) / computed.scaleMax) * vbH;
- }
- return vbH - (Math.min(Math.max(value, 0), 100) / 100) * vbH;
- };
-
- let minY = vbH;
- let nearestSeriesIndex: number | null = null;
- let nearestDistance = Number.POSITIVE_INFINITY;
- const values: HoverSeriesValue[] = [];
- for (let seriesIndex = 0; seriesIndex < computed.validSeries.length; seriesIndex++) {
- const series = computed.validSeries[seriesIndex];
- const nearest = findNearestMetricPoint(series.hoverData, targetTimestamp);
- if (!nearest) continue;
- const point = nearest.point;
-
- const pointY = valueToChartY(point.value);
- if (pointY < minY) minY = pointY;
-
- if (shouldTrackNearest) {
- const distance = Math.abs(pointY - chartY);
- if (distance < nearestDistance) {
- nearestDistance = distance;
- nearestSeriesIndex = seriesIndex;
- }
- }
-
- values.push({
- name: series.name || 'Series',
- color: series.color,
- value: point.value,
- timestamp: point.timestamp,
- seriesIndex,
- });
- }
-
- if (values.length === 0) {
- setHoveredState(null);
- return;
- }
-
- const nearLineThresholdPx = 6;
- const nearLineThresholdChartUnits = (nearLineThresholdPx / Math.max(1, rect.height)) * vbH;
- const lockedIndex = shouldTrackNearest ? lockedSeriesIndex() : null;
- const effectiveSeriesIndex =
- lockedIndex !== null
- ? lockedIndex
- : shouldTrackNearest &&
- nearestSeriesIndex !== null &&
- nearestDistance <= nearLineThresholdChartUnits
- ? nearestSeriesIndex
- : null;
- const focusedTooltip = effectiveSeriesIndex !== null;
-
- let groupedValues = values;
- if (!focusedTooltip) {
- const byValue = new Map<number, HoverSeriesValue[]>();
- for (const v of values) {
- let key = Math.round(v.value * 1000) / 1000;
- if (!byValue.has(key)) byValue.set(key, []);
- byValue.get(key)!.push(v);
- }
- groupedValues = [];
- for (const arr of byValue.values()) {
- if (arr.length > 1) {
- groupedValues.push({
- name: `${arr.length} Series`,
- color: 'currentColor',
- value: arr[0].value,
- timestamp: arr[0].timestamp,
- seriesIndex: -1,
- });
- } else {
- groupedValues.push(arr[0]);
- }
- }
- }
-
- const tooltipValues: HoverSeriesValue[] =
- focusedTooltip && effectiveSeriesIndex !== null
- ? values.filter((value) => value.seriesIndex === effectiveSeriesIndex)
- : props.sortTooltipByValue
- ? selectTopValuesByValue(groupedValues, maxRows())
- : groupedValues.slice(0, maxRows());
- if (tooltipValues.length === 0) {
- setHoveredState(null);
- return;
- }
- const totalValues = focusedTooltip ? tooltipValues.length : values.length;
-
- let tooltipX = clientX;
- let tooltipY = rect.top - 6;
- if (typeof window !== 'undefined') {
- const activeTooltipWidth = focusedTooltip ? 150 : tooltipEstimatedWidth;
- const minTooltipX = tooltipPadding + activeTooltipWidth / 2;
- const maxTooltipX = Math.max(
- minTooltipX,
- window.innerWidth - tooltipPadding - activeTooltipWidth / 2,
- );
- tooltipX = clamp(tooltipX, minTooltipX, maxTooltipX);
-
- const shownRows = tooltipValues.length;
- const tooltipEstimatedHeight = 22 + shownRows * 16 + (totalValues > shownRows ? 14 : 0);
- const minTooltipY = tooltipEstimatedHeight + tooltipPadding;
- tooltipY = Math.max(tooltipY, minTooltipY);
- }
-
- setHoveredState({
- x: chartX,
- tooltipX,
- tooltipY,
- timestamp: tooltipValues[0].timestamp,
- totalValues,
- minY,
- nearestSeriesIndex,
- highlightedSeriesIndex: shouldTrackNearest && focusedTooltip ? effectiveSeriesIndex : null,
- focusedTooltip,
- values: tooltipValues.map((value) => ({
- name: value.name,
- color: value.color,
- value: value.value,
- seriesIndex: value.seriesIndex,
- })),
- });
- };
-
- const flushHoverState = () => {
- hoverRafId = null;
- if (!pendingHoverPosition) return;
- const position = pendingHoverPosition;
- pendingHoverPosition = null;
- computeHoverState(position.clientX, position.clientY);
- };
-
- const handleMouseMove = (e: MouseEvent) => {
- const shouldThrottle = chartData().validSeries.length > 80;
- if (typeof window === 'undefined' || !shouldThrottle) {
- computeHoverState(e.clientX, e.clientY);
- return;
- }
- pendingHoverPosition = { clientX: e.clientX, clientY: e.clientY };
- if (hoverRafId !== null) return;
- hoverRafId = window.requestAnimationFrame(flushHoverState);
- };
-
- const handleMouseLeave = () => {
- pendingHoverPosition = null;
- if (typeof window !== 'undefined' && hoverRafId !== null) {
- window.cancelAnimationFrame(hoverRafId);
- hoverRafId = null;
- }
- setHoveredState(null);
- };
-
- const handleClick = () => {
- if (!props.highlightNearestSeriesOnHover) return;
- const locked = lockedSeriesIndex();
- if (locked !== null) {
- setLockedSeriesIndex(null);
- return;
- }
- const hovered = hoveredState();
- if (!hovered) return;
- const candidateSeriesIndex = hovered.highlightedSeriesIndex ?? hovered.nearestSeriesIndex;
- if (candidateSeriesIndex === null) return;
- setLockedSeriesIndex(candidateSeriesIndex);
- };
-
- createEffect(() => {
- const locked = lockedSeriesIndex();
- if (locked === null) return;
- if (locked < 0 || locked >= chartData().validSeries.length) {
- setLockedSeriesIndex(null);
- }
- });
-
- const topLabel = createMemo(() => {
- if (yMode() === 'percent') return '100%';
- const maxValue = chartData().scaleMax;
- if (maxValue <= 0) return '0';
- return props.formatTopLabel ? props.formatTopLabel(maxValue) : maxValue.toFixed(1);
- });
-
- const axisTicks = createMemo(() => {
- if (yMode() === 'percent') {
- return [
- { label: '100%', top: '0%', anchor: 'top' as const },
- { label: '80%', top: '20%', anchor: 'middle' as const },
- { label: '60%', top: '40%', anchor: 'middle' as const },
- { label: '40%', top: '60%', anchor: 'middle' as const },
- { label: '20%', top: '80%', anchor: 'middle' as const },
- { label: '0%', top: '100%', anchor: 'bottom' as const },
- ];
- }
-
- return [
- { label: topLabel(), top: '0%', anchor: 'top' as const },
- { label: '0', top: '100%', anchor: 'bottom' as const },
- ];
- });
-
- const gridLineY = createMemo(() => {
- if (yMode() === 'percent') {
- return [vbH * 0.2, vbH * 0.4, vbH * 0.6, vbH * 0.8];
- }
- return [vbH * 0.25, vbH * 0.5, vbH * 0.75];
- });
-
- const gridLineX = createMemo(() => [vbW * 0.5]);
-
- const xAxisTicks = createMemo(() => {
- const computed = chartData();
- const range = props.timeRange || '1h';
- const rangeToken = props.rangeLabel || range;
- return [
- { left: 0, label: `-${rangeToken}`, anchor: 'start' as const },
- { left: 50, label: formatRelativeOffset(computed.rangeMs / 2), anchor: 'middle' as const },
- { left: 100, label: 'now', anchor: 'end' as const },
- ];
- });
- const xAxisBandPx = 16;
-
- const externallyHighlightedSeriesIndex = createMemo(() => {
- const highlightId = props.highlightSeriesId;
- if (!highlightId) return null;
- const index = chartData().validSeries.findIndex((series) => series.id === highlightId);
- return index >= 0 ? index : null;
- });
-
- const activeEmphasisSeriesIndex = createMemo(() => {
- const locked = lockedSeriesIndex();
- if (props.highlightNearestSeriesOnHover && locked !== null) {
- return locked;
- }
- const hovered = hoveredState();
- if (
- props.highlightNearestSeriesOnHover &&
- hovered?.highlightedSeriesIndex !== null &&
- hovered
- ) {
- return hovered.highlightedSeriesIndex;
- }
- return externallyHighlightedSeriesIndex();
- });
-
- const drawCanvas = () => {
- if (!canvasRef) return;
- const computed = chartData();
- const ctx = canvasRef.getContext('2d');
- if (!ctx) return;
-
- const rect = canvasRef.getBoundingClientRect();
- const width = rect.width;
- const height = rect.height;
- if (width <= 0 || height <= 0) return;
-
- setupCanvasDPR(canvasRef, ctx, width, height);
-
- const isDark =
- typeof document !== 'undefined' && document.documentElement.classList.contains('dark');
- const gridColor = isDark ? 'rgba(255, 255, 255, 0.06)' : 'rgba(17, 24, 39, 0.06)';
- const gridColorStrong = isDark ? 'rgba(255, 255, 255, 0.10)' : 'rgba(17, 24, 39, 0.10)';
- const hoverLineColor = isDark ? 'rgba(255, 255, 255, 0.45)' : 'rgba(17, 24, 39, 0.45)';
-
- const yLines = yMode() === 'percent' ? [0.2, 0.4, 0.6, 0.8] : [0.25, 0.5, 0.75];
- ctx.save();
- ctx.lineWidth = 0.5;
- for (let i = 0; i < yLines.length; i++) {
- const y = yLines[i] * height;
- ctx.strokeStyle = i === 1 ? gridColorStrong : gridColor;
- ctx.beginPath();
- ctx.moveTo(0, y);
- ctx.lineTo(width, y);
- ctx.stroke();
- }
- ctx.strokeStyle = isDark ? 'rgba(255, 255, 255, 0.04)' : 'rgba(17, 24, 39, 0.04)';
- ctx.beginPath();
- ctx.moveTo(width * 0.5, 0);
- ctx.lineTo(width * 0.5, height);
- ctx.stroke();
- ctx.restore();
-
- if (computed.validSeries.length === 0 || computed.rangeMs <= 0) {
- return;
- }
-
- const active = activeEmphasisSeriesIndex();
- const valueToY = (value: number): number => {
- if (yMode() === 'auto') {
- if (computed.scaleMax <= 0) return height;
- return height - (Math.max(0, value) / computed.scaleMax) * height;
- }
- return height - (Math.min(Math.max(value, 0), 100) / 100) * height;
- };
-
- for (let seriesIndex = 0; seriesIndex < computed.validSeries.length; seriesIndex++) {
- const series = computed.validSeries[seriesIndex];
- const isLg = props.size === 'lg';
- const lineWidth =
- active === null
- ? isLg
- ? 2
- : 1.5
- : active === seriesIndex
- ? isLg
- ? 3.5
- : 2.8
- : isLg
- ? 1
- : 0.9;
- const opacity = active === null ? 0.75 : active === seriesIndex ? 1 : 0.1;
-
- ctx.save();
- ctx.globalAlpha = opacity;
- ctx.strokeStyle = series.color;
- ctx.lineWidth = lineWidth;
- ctx.lineCap = 'round';
- ctx.lineJoin = 'round';
-
- for (const segment of series.segments) {
- if (segment.length === 0) continue;
-
- if (computed.validSeries.length === 1) {
- ctx.beginPath();
- for (let i = 0; i < segment.length; i++) {
- const point = segment[i];
- const x = clamp(
- ((point.timestamp - computed.windowStart) / computed.rangeMs) * width,
- 0,
- width,
- );
- const y = valueToY(point.value);
- if (i === 0) ctx.moveTo(x, y);
- else ctx.lineTo(x, y);
- }
- const lastPoint = segment[segment.length - 1];
- const firstPoint = segment[0];
- const lastX = clamp(
- ((lastPoint.timestamp - computed.windowStart) / computed.rangeMs) * width,
- 0,
- width,
- );
- const firstX = clamp(
- ((firstPoint.timestamp - computed.windowStart) / computed.rangeMs) * width,
- 0,
- width,
- );
-
- ctx.lineTo(lastX, height);
- ctx.lineTo(firstX, height);
- ctx.closePath();
-
- const areaGrad = ctx.createLinearGradient(0, 0, 0, height);
- // basic hex convert #xxx to rgba for 25% opacity
- const baseColor = series.color;
- if (baseColor.startsWith('#') && (baseColor.length === 7 || baseColor.length === 4)) {
- areaGrad.addColorStop(0, baseColor + '40');
- areaGrad.addColorStop(1, baseColor + '00');
- } else {
- areaGrad.addColorStop(0, 'rgba(255, 255, 255, 0.15)');
- areaGrad.addColorStop(1, 'rgba(255, 255, 255, 0)');
- }
- ctx.fillStyle = areaGrad;
- ctx.fill();
- }
-
- ctx.beginPath();
- for (let i = 0; i < segment.length; i++) {
- const point = segment[i];
- const x = clamp(
- ((point.timestamp - computed.windowStart) / computed.rangeMs) * width,
- 0,
- width,
- );
- const y = valueToY(point.value);
- if (i === 0) {
- ctx.moveTo(x, y);
- } else {
- ctx.lineTo(x, y);
- }
- }
- ctx.stroke();
- }
- ctx.restore();
- }
-
- const hover = hoveredState();
- if (hover) {
- ctx.save();
- ctx.strokeStyle = hoverLineColor;
- ctx.lineWidth = 1;
- ctx.setLineDash([3, 3]);
- const x = (hover.x / vbW) * width;
- const grad = ctx.createLinearGradient(
- 0,
- Math.max(0, hover.minY - 4),
- 0,
- Math.max(0, hover.minY - 4) + 20,
- );
- grad.addColorStop(0, 'rgba(255, 255, 255, 0)');
- grad.addColorStop(1, hoverLineColor);
- ctx.strokeStyle = hoverLineColor; // Fallback to basic hover line if complex gradient logic adds noise
-
- // Let's cap the hover line using the existing gradient or direct styling.
- const hoverLineGrad = ctx.createLinearGradient(0, Math.max(0, hover.minY - 4), 0, height);
- hoverLineGrad.addColorStop(0, 'transparent');
- hoverLineGrad.addColorStop(0.1, hoverLineColor);
- hoverLineGrad.addColorStop(1, hoverLineColor);
- ctx.strokeStyle = hoverLineGrad;
-
- ctx.beginPath();
- ctx.moveTo(x, Math.max(0, hover.minY - 4));
- ctx.lineTo(x, height);
- ctx.stroke();
- ctx.restore();
- }
- };
-
- let unregisterCanvasDraw: (() => void) | null = null;
- const queueCanvasDraw = () => {
- if (!shouldUseCanvas()) return;
- if (unregisterCanvasDraw) {
- unregisterCanvasDraw();
- }
- unregisterCanvasDraw = scheduleSparkline(drawCanvas);
- };
-
- createEffect(() => {
- if (!shouldUseCanvas()) {
- if (unregisterCanvasDraw) {
- unregisterCanvasDraw();
- unregisterCanvasDraw = null;
- }
- return;
- }
-
- void chartData();
- void activeEmphasisSeriesIndex();
- void hoveredState();
- queueCanvasDraw();
- });
-
- createEffect(() => {
- if (!shouldUseCanvas() || !canvasHostRef) return;
- const observer = new ResizeObserver(() => queueCanvasDraw());
- observer.observe(canvasHostRef);
- onCleanup(() => observer.disconnect());
- });
- onCleanup(() => {
- pendingHoverPosition = null;
- if (typeof window !== 'undefined' && hoverRafId !== null) {
- window.cancelAnimationFrame(hoverRafId);
- hoverRafId = null;
- }
- if (unregisterCanvasDraw) {
- unregisterCanvasDraw();
- unregisterCanvasDraw = null;
- }
+ const sparkline = useInteractiveSparklineState(props, {
+ getCanvas: () => canvasRef,
+ getCanvasHost: () => canvasHostRef,
+ getChartSurface: () => chartSurfaceRef,
});
return (
<div class="w-full h-full min-h-[88px] flex flex-col">
<div class="relative flex-1 min-h-0">
<div class="absolute inset-y-0 left-0 w-7 pointer-events-none">
- <For each={axisTicks()}>
+ <For each={sparkline.axisTicks()}>
{(tick) => (
<span
class="absolute left-0 text-[8px] leading-none text-muted transition-all duration-300 ease-out"
@@ -845,41 +44,41 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
</div>
<div class="h-full ml-7 mr-3" ref={canvasHostRef}>
<Show
- when={shouldUseCanvas()}
+ when={sparkline.shouldUseCanvas()}
fallback={
<svg
- ref={(el) => {
- chartSurfaceRef = el;
+ ref={(element) => {
+ chartSurfaceRef = element;
}}
class="w-full h-full cursor-crosshair"
- viewBox={`0 0 ${vbW} ${vbH}`}
+ viewBox={`0 0 ${sparkline.vbW} ${sparkline.vbH}`}
preserveAspectRatio="none"
- onMouseMove={handleMouseMove}
- onMouseLeave={handleMouseLeave}
- onClick={handleClick}
+ onMouseMove={sparkline.handleMouseMove}
+ onMouseLeave={sparkline.handleMouseLeave}
+ onClick={sparkline.handleClick}
>
- <Show when={chartData().validSeries.length === 1}>
+ <Show when={sparkline.chartData().validSeries.length === 1}>
<defs>
<linearGradient id="single-series-area" x1="0" y1="0" x2="0" y2="1">
<stop
offset="0%"
- stop-color={chartData().validSeries[0].color}
+ stop-color={sparkline.chartData().validSeries[0].color}
stop-opacity="0.25"
/>
<stop
offset="100%"
- stop-color={chartData().validSeries[0].color}
+ stop-color={sparkline.chartData().validSeries[0].color}
stop-opacity="0"
/>
</linearGradient>
</defs>
</Show>
- <For each={gridLineY()}>
+ <For each={sparkline.gridLineY()}>
{(y, index) => (
<line
x1="0"
y1={y}
- x2={vbW}
+ x2={sparkline.vbW}
y2={y}
stroke="currentColor"
stroke-opacity={index() === 1 ? '0.1' : '0.06'}
@@ -888,13 +87,13 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
)}
</For>
- <For each={gridLineX()}>
+ <For each={sparkline.gridLineX()}>
{(x) => (
<line
x1={x}
y1="0"
x2={x}
- y2={vbH}
+ y2={sparkline.vbH}
stroke="currentColor"
stroke-opacity="0.04"
stroke-width="0.5"
@@ -902,13 +101,13 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
)}
</For>
- <Show when={hoveredState()}>
+ <Show when={sparkline.hoveredState()}>
{(hover) => (
<line
x1={hover().x}
y1={Math.max(0, hover().minY - 4)}
x2={hover().x}
- y2={vbH}
+ y2={sparkline.vbH}
stroke="currentColor"
stroke-opacity="0.45"
stroke-width="1"
@@ -918,7 +117,7 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
)}
</Show>
- <For each={chartData().paths}>
+ <For each={sparkline.chartData().paths}>
{(pathData) => (
<g>
<Show when={pathData.areaPath}>
@@ -929,7 +128,7 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
fill="none"
stroke={pathData.color}
stroke-width={(() => {
- const active = activeEmphasisSeriesIndex();
+ const active = sparkline.activeEmphasisSeriesIndex();
if (active === null) {
return '1.5';
}
@@ -938,7 +137,7 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
stroke-linecap="round"
stroke-linejoin="round"
opacity={(() => {
- const active = activeEmphasisSeriesIndex();
+ const active = sparkline.activeEmphasisSeriesIndex();
if (active === null) {
return '0.75';
}
@@ -954,20 +153,23 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
}
>
<canvas
- ref={(el) => {
- canvasRef = el;
- chartSurfaceRef = el;
+ ref={(element) => {
+ canvasRef = element;
+ chartSurfaceRef = element;
}}
class="w-full h-full cursor-crosshair block"
- onMouseMove={handleMouseMove}
- onMouseLeave={handleMouseLeave}
- onClick={handleClick}
+ onMouseMove={sparkline.handleMouseMove}
+ onMouseLeave={sparkline.handleMouseLeave}
+ onClick={sparkline.handleClick}
/>
</Show>
</div>
</div>
- <div class="relative pointer-events-none ml-7 mr-3" style={{ height: `${xAxisBandPx}px` }}>
- <For each={xAxisTicks()}>
+ <div
+ class="relative pointer-events-none ml-7 mr-3"
+ style={{ height: `${sparkline.xAxisBandPx}px` }}
+ >
+ <For each={sparkline.xAxisTicks()}>
{(tick) => (
<span
class="absolute top-[2px] text-[9px] font-medium leading-none text-muted transition-all duration-300 ease-out"
@@ -988,7 +190,7 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
</div>
<Portal>
- <Show when={hoveredState()}>
+ <Show when={sparkline.hoveredState()}>
{(hover) => (
<div
class="fixed pointer-events-none text-xs rounded px-2 py-1.5 shadow-lg border border-slate-600"
@@ -1001,7 +203,9 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
color: 'rgb(248, 250, 252)',
}}
>
- <div class="font-medium text-center mb-1">{formatHoverTime(hover().timestamp)}</div>
+ <div class="font-medium text-center mb-1">
+ {formatInteractiveSparklineHoverTime(hover().timestamp)}
+ </div>
<For each={hover().values}>
{(entry) => (
<div
@@ -1025,7 +229,7 @@ export const InteractiveSparkline: Component<InteractiveSparklineProps> = (props
<span class="w-1.5 h-1.5 rounded-full" style={{ background: entry.color }} />
<span style={{ color: 'rgb(203, 213, 225)' }}>{entry.name}</span>
<span class="ml-auto font-medium" style={{ color: 'rgb(248, 250, 252)' }}>
- {formatValue(entry.value)}
+ {sparkline.formatValue(entry.value)}
</span>
</div>
)}
diff --git a/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts b/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts
index d592051473..5a8ab3e722 100644
--- a/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts
+++ b/frontend-modern/src/components/shared/SharedPrimitives.guardrails.test.ts
@@ -1,6 +1,8 @@
import { describe, expect, it } from 'vitest';
import calloutCardSource from '@/components/shared/CalloutCard.tsx?raw';
import filterButtonGroupSource from '@/components/shared/FilterButtonGroup.tsx?raw';
+import interactiveSparklineSource from '@/components/shared/InteractiveSparkline.tsx?raw';
+import interactiveSparklineModelSource from '@/components/shared/interactiveSparklineModel.ts?raw';
import infrastructureSummaryTableSource from '@/components/shared/InfrastructureSummaryTable.tsx?raw';
import infrastructureSummaryTableRowSource from '@/components/shared/InfrastructureSummaryTableRow.tsx?raw';
import infrastructureSummaryTableModelSource from '@/components/shared/infrastructureSummaryTableModel.ts?raw';
@@ -8,6 +10,7 @@ import infrastructureSummaryTableStateSource from '@/components/shared/useInfras
import monitoredSystemLimitWarningBannerSource from '@/components/shared/MonitoredSystemLimitWarningBanner.tsx?raw';
import selectionCardGroupSource from '@/components/shared/SelectionCardGroup.tsx?raw';
import tagBadgesSource from '@/components/shared/TagBadges.tsx?raw';
+import interactiveSparklineStateSource from '@/components/shared/useInteractiveSparklineState.ts?raw';
import guestRowSource from '@/components/Dashboard/GuestRow.tsx?raw';
import resourceDetailDrawerOverviewSource from '@/components/Infrastructure/ResourceDetailDrawerOverviewTab.tsx?raw';
import aiSettingsDialogsSource from '@/components/Settings/AISettingsDialogs.tsx?raw';
@@ -139,4 +142,22 @@ describe('shared primitive guardrails', () => {
expect(infrastructureSummaryTableRowSource).not.toContain('useWebSocket');
expect(infrastructureSummaryTableRowSource).not.toContain('useAlertsActivation');
});
+
+ it('keeps interactive sparkline on shell, runtime, and model owners', () => {
+ expect(interactiveSparklineSource).toContain('useInteractiveSparklineState');
+ expect(interactiveSparklineSource).not.toContain('createEffect');
+ expect(interactiveSparklineSource).not.toContain('createSignal');
+ expect(interactiveSparklineSource).not.toContain('scheduleSparkline');
+ expect(interactiveSparklineSource).not.toContain('downsampleLTTB');
+
+ expect(interactiveSparklineStateSource).toContain('export function useInteractiveSparklineState');
+ expect(interactiveSparklineStateSource).toContain('createSignal');
+ expect(interactiveSparklineStateSource).toContain('scheduleSparkline');
+ expect(interactiveSparklineStateSource).toContain('computeInteractiveSparklineHoverState');
+
+ expect(interactiveSparklineModelSource).toContain('buildInteractiveSparklineChartData');
+ expect(interactiveSparklineModelSource).toContain('computeInteractiveSparklineHoverState');
+ expect(interactiveSparklineModelSource).toContain('downsampleLTTB');
+ expect(interactiveSparklineModelSource).toContain('findNearestMetricPoint');
+ });
});
diff --git a/frontend-modern/src/components/shared/__tests__/InteractiveSparkline.test.tsx b/frontend-modern/src/components/shared/__tests__/InteractiveSparkline.test.tsx
index 3944b6b96b..8473a13f2e 100644
--- a/frontend-modern/src/components/shared/__tests__/InteractiveSparkline.test.tsx
+++ b/frontend-modern/src/components/shared/__tests__/InteractiveSparkline.test.tsx
@@ -1,6 +1,9 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
import { InteractiveSparkline } from '@/components/shared/InteractiveSparkline';
+import interactiveSparklineSource from '@/components/shared/InteractiveSparkline.tsx?raw';
+import interactiveSparklineModelSource from '@/components/shared/interactiveSparklineModel.ts?raw';
+import interactiveSparklineStateSource from '@/components/shared/useInteractiveSparklineState.ts?raw';
describe('InteractiveSparkline hover behavior', () => {
afterEach(() => {
@@ -8,6 +11,17 @@ describe('InteractiveSparkline hover behavior', () => {
cleanup();
});
+ it('keeps the sparkline on shell, runtime, and model owners', () => {
+ expect(interactiveSparklineSource).toContain('useInteractiveSparklineState');
+ expect(interactiveSparklineSource).not.toContain('createEffect');
+ expect(interactiveSparklineSource).not.toContain('createSignal');
+ expect(interactiveSparklineSource).not.toContain('scheduleSparkline');
+ expect(interactiveSparklineStateSource).toContain('scheduleSparkline');
+ expect(interactiveSparklineStateSource).toContain('createSignal');
+ expect(interactiveSparklineModelSource).toContain('buildInteractiveSparklineChartData');
+ expect(interactiveSparklineModelSource).toContain('computeInteractiveSparklineHoverState');
+ });
+
it('shows a vertical dashed hover line and a tooltip', async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2024-01-01T12:00:00Z'));
diff --git a/frontend-modern/src/components/shared/interactiveSparklineModel.ts b/frontend-modern/src/components/shared/interactiveSparklineModel.ts
new file mode 100644
index 0000000000..052f691abc
--- /dev/null
+++ b/frontend-modern/src/components/shared/interactiveSparklineModel.ts
@@ -0,0 +1,617 @@
+import type { MetricPoint, TimeRange } from '@/api/charts';
+import { downsampleLTTB, calculateOptimalPoints } from '@/utils/downsample';
+import { timeRangeToMs } from '@/utils/timeRange';
+
+export interface InteractiveSparklineSeries {
+ id?: string;
+ data: MetricPoint[];
+ color: string;
+ name?: string;
+}
+
+export interface InteractiveSparklineProps {
+ series: InteractiveSparklineSeries[];
+ rangeLabel?: string;
+ timeRange?: TimeRange;
+ renderMode?: 'auto' | 'svg' | 'canvas';
+ yMode?: 'percent' | 'auto';
+ size?: 'sm' | 'md' | 'lg';
+ bridgeLeadingGap?: boolean;
+ formatValue?: (value: number) => string;
+ formatTopLabel?: (maxValue: number) => string;
+ sortTooltipByValue?: boolean;
+ maxTooltipRows?: number;
+ highlightNearestSeriesOnHover?: boolean;
+ highlightSeriesId?: string | null;
+}
+
+export interface InteractiveSparklineChartSeries extends InteractiveSparklineSeries {
+ hoverData: MetricPoint[];
+ drawData: MetricPoint[];
+ segments: MetricPoint[][];
+}
+
+export interface InteractiveSparklinePathData {
+ path: string;
+ areaPath?: string;
+ color: string;
+ seriesIndex: number;
+}
+
+export interface InteractiveSparklineChartData {
+ validSeries: InteractiveSparklineChartSeries[];
+ paths: InteractiveSparklinePathData[];
+ windowStart: number;
+ rangeMs: number;
+ scaleMax: number;
+}
+
+export interface InteractiveSparklineDisplayValue {
+ name: string;
+ color: string;
+ value: number;
+ seriesIndex: number;
+}
+
+export interface InteractiveSparklineHoverState {
+ x: number;
+ tooltipX: number;
+ tooltipY: number;
+ timestamp: number;
+ totalValues: number;
+ minY: number;
+ nearestSeriesIndex: number | null;
+ highlightedSeriesIndex: number | null;
+ focusedTooltip: boolean;
+ values: InteractiveSparklineDisplayValue[];
+}
+
+interface HoverSeriesValue extends InteractiveSparklineDisplayValue {
+ timestamp: number;
+}
+
+export const formatInteractiveSparklineHoverTime = (timestamp: number): string =>
+ new Date(timestamp).toLocaleString([], {
+ year: 'numeric',
+ month: 'short',
+ day: '2-digit',
+ hour: '2-digit',
+ minute: '2-digit',
+ second: '2-digit',
+ hour12: false,
+ });
+
+export const clampInteractiveSparklineValue = (value: number, min: number, max: number): number =>
+ Math.max(min, Math.min(value, max));
+
+export const formatInteractiveSparklineRelativeOffset = (offsetMs: number): string => {
+ if (offsetMs <= 0) return 'now';
+ const dayMs = 24 * 60 * 60_000;
+ const hourMs = 60 * 60_000;
+ const minuteMs = 60_000;
+
+ if (offsetMs >= dayMs) {
+ return `-${Math.round(offsetMs / dayMs)}d`;
+ }
+ if (offsetMs >= hourMs) {
+ return `-${Math.round(offsetMs / hourMs)}h`;
+ }
+ return `-${Math.max(1, Math.round(offsetMs / minuteMs))}m`;
+};
+
+const findNearestMetricPoint = (
+ points: MetricPoint[],
+ targetTimestamp: number,
+): { point: MetricPoint; index: number } | null => {
+ if (points.length === 0) return null;
+
+ let low = 0;
+ let high = points.length - 1;
+ while (low < high) {
+ const mid = Math.floor((low + high) / 2);
+ if (points[mid].timestamp < targetTimestamp) {
+ low = mid + 1;
+ } else {
+ high = mid;
+ }
+ }
+
+ const candidate = points[low];
+ const previous = low > 0 ? points[low - 1] : candidate;
+ if (
+ Math.abs(previous.timestamp - targetTimestamp) <=
+ Math.abs(candidate.timestamp - targetTimestamp)
+ ) {
+ return { point: previous, index: low > 0 ? low - 1 : low };
+ }
+ return { point: candidate, index: low };
+};
+
+const ensureRenderablePoints = (points: MetricPoint[], windowStart: number): MetricPoint[] => {
+ if (points.length === 0) return [];
+ if (points.length >= 2) return points;
+ const point = points[0];
+ return [
+ {
+ timestamp: Math.max(windowStart, point.timestamp - 1_000),
+ value: point.value,
+ },
+ point,
+ ];
+};
+
+const splitPointsOnGaps = (
+ points: MetricPoint[],
+ gapThresholdMs: number,
+ windowStart: number,
+ bridgeLeadingGap: boolean,
+): MetricPoint[][] => {
+ if (points.length === 0) return [];
+ const segments: MetricPoint[][] = [];
+ let currentSegment: MetricPoint[] = [points[0]];
+
+ for (let index = 1; index < points.length; index++) {
+ const previous = points[index - 1];
+ const current = points[index];
+ const hasGap = current.timestamp - previous.timestamp > gapThresholdMs;
+ const isLeadingBridgeGap =
+ bridgeLeadingGap &&
+ segments.length === 0 &&
+ currentSegment.length === 1 &&
+ currentSegment[0].timestamp <= windowStart + 1_000;
+
+ if (hasGap && !isLeadingBridgeGap) {
+ segments.push(ensureRenderablePoints(currentSegment, windowStart));
+ currentSegment = [current];
+ continue;
+ }
+ currentSegment.push(current);
+ }
+
+ segments.push(ensureRenderablePoints(currentSegment, windowStart));
+ return segments;
+};
+
+const inferGapThresholdMs = (points: MetricPoint[], rangeMs: number): number => {
+ if (points.length < 3) return rangeMs + 1;
+
+ const deltas: number[] = [];
+ for (let index = 1; index < points.length; index++) {
+ const delta = points[index].timestamp - points[index - 1].timestamp;
+ if (delta > 0) {
+ deltas.push(delta);
+ }
+ }
+ if (deltas.length < 2) return rangeMs + 1;
+
+ deltas.sort((left, right) => left - right);
+ const p90Delta = deltas[Math.floor(deltas.length * 0.9)];
+ const minThreshold = Math.max(15_000, Math.floor(rangeMs / 120));
+ const maxThreshold = Math.max(minThreshold, Math.floor(rangeMs / 2));
+ return clampInteractiveSparklineValue(p90Delta * 3, minThreshold, maxThreshold);
+};
+
+const selectTopValuesByValue = (values: HoverSeriesValue[], limit: number): HoverSeriesValue[] => {
+ if (limit <= 0 || values.length === 0) return [];
+ if (values.length <= limit) {
+ return [...values].sort((left, right) => right.value - left.value);
+ }
+
+ const top: HoverSeriesValue[] = [];
+ for (const value of values) {
+ let insertAt = top.length;
+ for (let index = 0; index < top.length; index++) {
+ if (value.value > top[index].value) {
+ insertAt = index;
+ break;
+ }
+ }
+ if (insertAt >= limit && top.length >= limit) continue;
+ top.splice(insertAt, 0, value);
+ if (top.length > limit) {
+ top.pop();
+ }
+ }
+ return top;
+};
+
+export const getInteractiveSparklineShouldUseCanvas = (
+ series: InteractiveSparklineSeries[],
+ renderMode: InteractiveSparklineProps['renderMode'],
+ canvasSeriesThreshold = 120,
+ canvasPointThreshold = 15_000,
+) => {
+ if (renderMode === 'svg') return false;
+ if (renderMode === 'canvas') return true;
+ const totalPoints = series.reduce((sum, item) => sum + item.data.length, 0);
+ return series.length >= canvasSeriesThreshold || totalPoints >= canvasPointThreshold;
+};
+
+export const buildInteractiveSparklineChartData = ({
+ series,
+ timeRange,
+ yMode,
+ vbW,
+ vbH,
+ shouldUseCanvas,
+ bridgeLeadingGap,
+}: {
+ series: InteractiveSparklineSeries[];
+ timeRange: TimeRange;
+ yMode: 'percent' | 'auto';
+ vbW: number;
+ vbH: number;
+ shouldUseCanvas: boolean;
+ bridgeLeadingGap: boolean;
+}): InteractiveSparklineChartData => {
+ const rangeMs = timeRangeToMs(timeRange);
+ const windowEnd = Date.now();
+ const windowStart = windowEnd - rangeMs;
+ const targetPoints = calculateOptimalPoints(vbW, 'sparkline');
+ const validSeries = series
+ .map((item) => {
+ const inWindow = item.data.filter(
+ (point) => point.timestamp >= windowStart && point.timestamp <= windowEnd,
+ );
+ if (inWindow.length < 2) return null;
+ const renderable = ensureRenderablePoints(inWindow, windowStart);
+ if (renderable.length === 0) return null;
+ const drawData =
+ renderable.length > targetPoints * 1.5
+ ? downsampleLTTB(renderable, targetPoints)
+ : renderable;
+ const segments = splitPointsOnGaps(
+ drawData,
+ inferGapThresholdMs(drawData, rangeMs),
+ windowStart,
+ bridgeLeadingGap,
+ );
+ return {
+ ...item,
+ hoverData: renderable,
+ drawData,
+ segments,
+ };
+ })
+ .filter((item): item is InteractiveSparklineChartSeries => item !== null);
+
+ if (validSeries.length === 0) {
+ return {
+ validSeries: [],
+ paths: [],
+ windowStart,
+ rangeMs,
+ scaleMax: 0,
+ };
+ }
+
+ let scaleMax = 100;
+ if (yMode === 'auto') {
+ scaleMax = 0;
+ for (const item of validSeries) {
+ for (const point of item.hoverData) {
+ if (point.value > scaleMax) scaleMax = point.value;
+ }
+ }
+ if (scaleMax <= 0) {
+ return {
+ validSeries,
+ paths: [],
+ windowStart,
+ rangeMs,
+ scaleMax: 0,
+ };
+ }
+ }
+
+ const paths = shouldUseCanvas
+ ? []
+ : validSeries.flatMap((item, seriesIndex) =>
+ item.segments.map((segment) => {
+ const coords = segment.map((point) => {
+ const x = clampInteractiveSparklineValue(
+ ((point.timestamp - windowStart) / rangeMs) * vbW,
+ 0,
+ vbW,
+ );
+ const normalized =
+ yMode === 'auto'
+ ? Math.max(0, point.value) / scaleMax
+ : Math.min(Math.max(point.value, 0), 100) / 100;
+ const y = vbH - normalized * vbH;
+ return { x, y };
+ });
+ const pathStrings = coords.map((coord) => `${coord.x.toFixed(1)},${coord.y.toFixed(1)}`);
+ const path = `M${pathStrings.join('L')}`;
+ let areaPath = '';
+ if (validSeries.length === 1 && coords.length > 1) {
+ areaPath = `${path} L${coords[coords.length - 1].x.toFixed(1)},${vbH} L${coords[0].x.toFixed(1)},${vbH} Z`;
+ }
+ return { path, areaPath, color: item.color, seriesIndex };
+ }),
+ );
+
+ return {
+ validSeries,
+ paths,
+ windowStart,
+ rangeMs,
+ scaleMax,
+ };
+};
+
+export const createInteractiveSparklineValueToY = (
+ yMode: 'percent' | 'auto',
+ scaleMax: number,
+ vbH: number,
+) => {
+ return (value: number): number => {
+ if (yMode === 'auto') {
+ if (scaleMax <= 0) return vbH;
+ return vbH - (Math.max(0, value) / scaleMax) * vbH;
+ }
+ return vbH - (Math.min(Math.max(value, 0), 100) / 100) * vbH;
+ };
+};
+
+export const buildInteractiveSparklineTopLabel = ({
+ yMode,
+ scaleMax,
+ formatTopLabel,
+}: {
+ yMode: 'percent' | 'auto';
+ scaleMax: number;
+ formatTopLabel?: (maxValue: number) => string;
+}) => {
+ if (yMode === 'percent') return '100%';
+ if (scaleMax <= 0) return '0';
+ return formatTopLabel ? formatTopLabel(scaleMax) : scaleMax.toFixed(1);
+};
+
+export const buildInteractiveSparklineAxisTicks = (
+ yMode: 'percent' | 'auto',
+ topLabel: string,
+) => {
+ if (yMode === 'percent') {
+ return [
+ { label: '100%', top: '0%', anchor: 'top' as const },
+ { label: '80%', top: '20%', anchor: 'middle' as const },
+ { label: '60%', top: '40%', anchor: 'middle' as const },
+ { label: '40%', top: '60%', anchor: 'middle' as const },
+ { label: '20%', top: '80%', anchor: 'middle' as const },
+ { label: '0%', top: '100%', anchor: 'bottom' as const },
+ ];
+ }
+
+ return [
+ { label: topLabel, top: '0%', anchor: 'top' as const },
+ { label: '0', top: '100%', anchor: 'bottom' as const },
+ ];
+};
+
+export const buildInteractiveSparklineGridLineY = (
+ yMode: 'percent' | 'auto',
+ vbH: number,
+) => {
+ if (yMode === 'percent') {
+ return [vbH * 0.2, vbH * 0.4, vbH * 0.6, vbH * 0.8];
+ }
+ return [vbH * 0.25, vbH * 0.5, vbH * 0.75];
+};
+
+export const buildInteractiveSparklineGridLineX = (vbW: number) => [vbW * 0.5];
+
+export const buildInteractiveSparklineXAxisTicks = ({
+ rangeMs,
+ rangeLabel,
+ timeRange,
+}: {
+ rangeMs: number;
+ rangeLabel?: string;
+ timeRange: TimeRange;
+}) => {
+ const rangeToken = rangeLabel || timeRange;
+ return [
+ { left: 0, label: `-${rangeToken}`, anchor: 'start' as const },
+ {
+ left: 50,
+ label: formatInteractiveSparklineRelativeOffset(rangeMs / 2),
+ anchor: 'middle' as const,
+ },
+ { left: 100, label: 'now', anchor: 'end' as const },
+ ];
+};
+
+export const getInteractiveSparklineExternalSeriesIndex = (
+ chartData: InteractiveSparklineChartData,
+ highlightSeriesId?: string | null,
+) => {
+ if (!highlightSeriesId) return null;
+ const index = chartData.validSeries.findIndex((series) => series.id === highlightSeriesId);
+ return index >= 0 ? index : null;
+};
+
+export const getInteractiveSparklineActiveEmphasisSeriesIndex = ({
+ highlightNearestSeriesOnHover,
+ lockedSeriesIndex,
+ hoveredState,
+ externalSeriesIndex,
+}: {
+ highlightNearestSeriesOnHover: boolean;
+ lockedSeriesIndex: number | null;
+ hoveredState: InteractiveSparklineHoverState | null;
+ externalSeriesIndex: number | null;
+}) => {
+ if (highlightNearestSeriesOnHover && lockedSeriesIndex !== null) {
+ return lockedSeriesIndex;
+ }
+ if (
+ highlightNearestSeriesOnHover &&
+ hoveredState?.highlightedSeriesIndex !== null &&
+ hoveredState
+ ) {
+ return hoveredState.highlightedSeriesIndex;
+ }
+ return externalSeriesIndex;
+};
+
+export const computeInteractiveSparklineHoverState = ({
+ chartData,
+ chartRect,
+ clientX,
+ clientY,
+ vbW,
+ vbH,
+ yMode,
+ maxRows,
+ sortTooltipByValue,
+ highlightNearestSeriesOnHover,
+ lockedSeriesIndex,
+ tooltipPadding,
+ tooltipEstimatedWidth,
+}: {
+ chartData: InteractiveSparklineChartData;
+ chartRect: DOMRect;
+ clientX: number;
+ clientY: number;
+ vbW: number;
+ vbH: number;
+ yMode: 'percent' | 'auto';
+ maxRows: number;
+ sortTooltipByValue?: boolean;
+ highlightNearestSeriesOnHover?: boolean;
+ lockedSeriesIndex: number | null;
+ tooltipPadding: number;
+ tooltipEstimatedWidth: number;
+}): InteractiveSparklineHoverState | null => {
+ if (chartData.validSeries.length === 0 || chartData.rangeMs <= 0 || chartRect.width <= 0) {
+ return null;
+ }
+
+ const mouseX = Math.max(0, Math.min(clientX - chartRect.left, chartRect.width));
+ const chartX = (mouseX / chartRect.width) * vbW;
+ const targetTimestamp = chartData.windowStart + (chartX / vbW) * chartData.rangeMs;
+ const shouldTrackNearest = highlightNearestSeriesOnHover === true;
+ const chartY = shouldTrackNearest
+ ? (Math.max(0, Math.min(clientY - chartRect.top, chartRect.height)) / chartRect.height) * vbH
+ : 0;
+ const valueToChartY = createInteractiveSparklineValueToY(yMode, chartData.scaleMax, vbH);
+
+ let minY = vbH;
+ let nearestSeriesIndex: number | null = null;
+ let nearestDistance = Number.POSITIVE_INFINITY;
+ const values: HoverSeriesValue[] = [];
+
+ for (let seriesIndex = 0; seriesIndex < chartData.validSeries.length; seriesIndex++) {
+ const series = chartData.validSeries[seriesIndex];
+ const nearest = findNearestMetricPoint(series.hoverData, targetTimestamp);
+ if (!nearest) continue;
+ const point = nearest.point;
+ const pointY = valueToChartY(point.value);
+ if (pointY < minY) minY = pointY;
+
+ if (shouldTrackNearest) {
+ const distance = Math.abs(pointY - chartY);
+ if (distance < nearestDistance) {
+ nearestDistance = distance;
+ nearestSeriesIndex = seriesIndex;
+ }
+ }
+
+ values.push({
+ name: series.name || 'Series',
+ color: series.color,
+ value: point.value,
+ timestamp: point.timestamp,
+ seriesIndex,
+ });
+ }
+
+ if (values.length === 0) {
+ return null;
+ }
+
+ const nearLineThresholdPx = 6;
+ const nearLineThresholdChartUnits = (nearLineThresholdPx / Math.max(1, chartRect.height)) * vbH;
+ const effectiveSeriesIndex =
+ lockedSeriesIndex !== null
+ ? lockedSeriesIndex
+ : shouldTrackNearest &&
+ nearestSeriesIndex !== null &&
+ nearestDistance <= nearLineThresholdChartUnits
+ ? nearestSeriesIndex
+ : null;
+ const focusedTooltip = effectiveSeriesIndex !== null;
+
+ let groupedValues = values;
+ if (!focusedTooltip) {
+ const byValue = new Map<number, HoverSeriesValue[]>();
+ for (const value of values) {
+ const key = Math.round(value.value * 1000) / 1000;
+ if (!byValue.has(key)) byValue.set(key, []);
+ byValue.get(key)!.push(value);
+ }
+ groupedValues = [];
+ for (const group of byValue.values()) {
+ if (group.length > 1) {
+ groupedValues.push({
+ name: `${group.length} Series`,
+ color: 'currentColor',
+ value: group[0].value,
+ timestamp: group[0].timestamp,
+ seriesIndex: -1,
+ });
+ } else {
+ groupedValues.push(group[0]);
+ }
+ }
+ }
+
+ const tooltipValues =
+ focusedTooltip && effectiveSeriesIndex !== null
+ ? values.filter((value) => value.seriesIndex === effectiveSeriesIndex)
+ : sortTooltipByValue
+ ? selectTopValuesByValue(groupedValues, maxRows)
+ : groupedValues.slice(0, maxRows);
+ if (tooltipValues.length === 0) {
+ return null;
+ }
+
+ const totalValues = focusedTooltip ? tooltipValues.length : values.length;
+ let tooltipX = clientX;
+ let tooltipY = chartRect.top - 6;
+
+ if (typeof window !== 'undefined') {
+ const activeTooltipWidth = focusedTooltip ? 150 : tooltipEstimatedWidth;
+ const minTooltipX = tooltipPadding + activeTooltipWidth / 2;
+ const maxTooltipX = Math.max(
+ minTooltipX,
+ window.innerWidth - tooltipPadding - activeTooltipWidth / 2,
+ );
+ tooltipX = clampInteractiveSparklineValue(tooltipX, minTooltipX, maxTooltipX);
+
+ const shownRows = tooltipValues.length;
+ const tooltipEstimatedHeight = 22 + shownRows * 16 + (totalValues > shownRows ? 14 : 0);
+ const minTooltipY = tooltipEstimatedHeight + tooltipPadding;
+ tooltipY = Math.max(tooltipY, minTooltipY);
+ }
+
+ return {
+ x: chartX,
+ tooltipX,
+ tooltipY,
+ timestamp: tooltipValues[0].timestamp,
+ totalValues,
+ minY,
+ nearestSeriesIndex,
+ highlightedSeriesIndex:
+ highlightNearestSeriesOnHover && focusedTooltip ? effectiveSeriesIndex : null,
+ focusedTooltip,
+ values: tooltipValues.map((value) => ({
+ name: value.name,
+ color: value.color,
+ value: value.value,
+ seriesIndex: value.seriesIndex,
+ })),
+ };
+};
diff --git a/frontend-modern/src/components/shared/useInteractiveSparklineState.ts b/frontend-modern/src/components/shared/useInteractiveSparklineState.ts
new file mode 100644
index 0000000000..426a0b5221
--- /dev/null
+++ b/frontend-modern/src/components/shared/useInteractiveSparklineState.ts
@@ -0,0 +1,379 @@
+import { createEffect, createMemo, createSignal, onCleanup } from 'solid-js';
+import { scheduleSparkline, setupCanvasDPR } from '@/utils/canvasRenderQueue';
+import {
+ buildInteractiveSparklineAxisTicks,
+ buildInteractiveSparklineChartData,
+ buildInteractiveSparklineGridLineX,
+ buildInteractiveSparklineGridLineY,
+ buildInteractiveSparklineTopLabel,
+ buildInteractiveSparklineXAxisTicks,
+ clampInteractiveSparklineValue,
+ computeInteractiveSparklineHoverState,
+ createInteractiveSparklineValueToY,
+ getInteractiveSparklineActiveEmphasisSeriesIndex,
+ getInteractiveSparklineExternalSeriesIndex,
+ getInteractiveSparklineShouldUseCanvas,
+ type InteractiveSparklineHoverState,
+ type InteractiveSparklineProps,
+} from './interactiveSparklineModel';
+
+interface InteractiveSparklineRefs {
+ getCanvas: () => HTMLCanvasElement | undefined;
+ getCanvasHost: () => HTMLDivElement | undefined;
+ getChartSurface: () => Element | undefined;
+}
+
+export function useInteractiveSparklineState(
+ props: InteractiveSparklineProps,
+ refs: InteractiveSparklineRefs,
+) {
+ const vbH = 100;
+ const vbW = 200;
+ const xAxisBandPx = 16;
+ const tooltipPadding = 8;
+ const tooltipEstimatedWidth = 190;
+ const maxRows = () => props.maxTooltipRows ?? 6;
+ const yMode = () => props.yMode ?? 'percent';
+ const shouldUseCanvas = createMemo(() =>
+ getInteractiveSparklineShouldUseCanvas(props.series, props.renderMode),
+ );
+ const formatValue = (value: number) =>
+ props.formatValue ? props.formatValue(value) : `${value.toFixed(1)}%`;
+
+ const [hoveredState, setHoveredState] = createSignal<InteractiveSparklineHoverState | null>(null);
+ const [lockedSeriesIndex, setLockedSeriesIndex] = createSignal<number | null>(null);
+
+ const chartData = createMemo(() =>
+ buildInteractiveSparklineChartData({
+ series: props.series,
+ timeRange: props.timeRange || '1h',
+ yMode: yMode(),
+ vbW,
+ vbH,
+ shouldUseCanvas: shouldUseCanvas(),
+ bridgeLeadingGap: props.bridgeLeadingGap === true,
+ }),
+ );
+
+ let pendingHoverPosition: { clientX: number; clientY: number } | null = null;
+ let hoverRafId: number | null = null;
+ let unregisterCanvasDraw: (() => void) | null = null;
+
+ const computeHoverState = (clientX: number, clientY: number) => {
+ const chartSurface = refs.getChartSurface();
+ if (!chartSurface) return;
+ const computed = computeInteractiveSparklineHoverState({
+ chartData: chartData(),
+ chartRect: chartSurface.getBoundingClientRect(),
+ clientX,
+ clientY,
+ vbW,
+ vbH,
+ yMode: yMode(),
+ maxRows: maxRows(),
+ sortTooltipByValue: props.sortTooltipByValue,
+ highlightNearestSeriesOnHover: props.highlightNearestSeriesOnHover,
+ lockedSeriesIndex: props.highlightNearestSeriesOnHover ? lockedSeriesIndex() : null,
+ tooltipPadding,
+ tooltipEstimatedWidth,
+ });
+ setHoveredState(computed);
+ };
+
+ const flushHoverState = () => {
+ hoverRafId = null;
+ if (!pendingHoverPosition) return;
+ const position = pendingHoverPosition;
+ pendingHoverPosition = null;
+ computeHoverState(position.clientX, position.clientY);
+ };
+
+ const handleMouseMove = (event: MouseEvent) => {
+ const shouldThrottle = chartData().validSeries.length > 80;
+ if (typeof window === 'undefined' || !shouldThrottle) {
+ computeHoverState(event.clientX, event.clientY);
+ return;
+ }
+ pendingHoverPosition = { clientX: event.clientX, clientY: event.clientY };
+ if (hoverRafId !== null) return;
+ hoverRafId = window.requestAnimationFrame(flushHoverState);
+ };
+
+ const handleMouseLeave = () => {
+ pendingHoverPosition = null;
+ if (typeof window !== 'undefined' && hoverRafId !== null) {
+ window.cancelAnimationFrame(hoverRafId);
+ hoverRafId = null;
+ }
+ setHoveredState(null);
+ };
+
+ const handleClick = () => {
+ if (!props.highlightNearestSeriesOnHover) return;
+ const locked = lockedSeriesIndex();
+ if (locked !== null) {
+ setLockedSeriesIndex(null);
+ return;
+ }
+ const hovered = hoveredState();
+ if (!hovered) return;
+ const candidateSeriesIndex = hovered.highlightedSeriesIndex ?? hovered.nearestSeriesIndex;
+ if (candidateSeriesIndex === null) return;
+ setLockedSeriesIndex(candidateSeriesIndex);
+ };
+
+ createEffect(() => {
+ const locked = lockedSeriesIndex();
+ if (locked === null) return;
+ if (locked < 0 || locked >= chartData().validSeries.length) {
+ setLockedSeriesIndex(null);
+ }
+ });
+
+ const topLabel = createMemo(() =>
+ buildInteractiveSparklineTopLabel({
+ yMode: yMode(),
+ scaleMax: chartData().scaleMax,
+ formatTopLabel: props.formatTopLabel,
+ }),
+ );
+
+ const axisTicks = createMemo(() => buildInteractiveSparklineAxisTicks(yMode(), topLabel()));
+ const gridLineY = createMemo(() => buildInteractiveSparklineGridLineY(yMode(), vbH));
+ const gridLineX = createMemo(() => buildInteractiveSparklineGridLineX(vbW));
+ const xAxisTicks = createMemo(() =>
+ buildInteractiveSparklineXAxisTicks({
+ rangeMs: chartData().rangeMs,
+ rangeLabel: props.rangeLabel,
+ timeRange: props.timeRange || '1h',
+ }),
+ );
+
+ const externalSeriesIndex = createMemo(() =>
+ getInteractiveSparklineExternalSeriesIndex(chartData(), props.highlightSeriesId),
+ );
+ const activeEmphasisSeriesIndex = createMemo(() =>
+ getInteractiveSparklineActiveEmphasisSeriesIndex({
+ highlightNearestSeriesOnHover: props.highlightNearestSeriesOnHover === true,
+ lockedSeriesIndex: lockedSeriesIndex(),
+ hoveredState: hoveredState(),
+ externalSeriesIndex: externalSeriesIndex(),
+ }),
+ );
+
+ const drawCanvas = () => {
+ const canvas = refs.getCanvas();
+ if (!canvas) return;
+ const computed = chartData();
+ const ctx = canvas.getContext('2d');
+ if (!ctx) return;
+
+ const rect = canvas.getBoundingClientRect();
+ const width = rect.width;
+ const height = rect.height;
+ if (width <= 0 || height <= 0) return;
+
+ setupCanvasDPR(canvas, ctx, width, height);
+
+ const isDark =
+ typeof document !== 'undefined' && document.documentElement.classList.contains('dark');
+ const gridColor = isDark ? 'rgba(255, 255, 255, 0.06)' : 'rgba(17, 24, 39, 0.06)';
+ const gridColorStrong = isDark ? 'rgba(255, 255, 255, 0.10)' : 'rgba(17, 24, 39, 0.10)';
+ const hoverLineColor = isDark ? 'rgba(255, 255, 255, 0.45)' : 'rgba(17, 24, 39, 0.45)';
+
+ const yLines = yMode() === 'percent' ? [0.2, 0.4, 0.6, 0.8] : [0.25, 0.5, 0.75];
+ ctx.save();
+ ctx.clearRect(0, 0, width, height);
+ ctx.lineWidth = 0.5;
+ for (let index = 0; index < yLines.length; index++) {
+ const y = yLines[index] * height;
+ ctx.strokeStyle = index === 1 ? gridColorStrong : gridColor;
+ ctx.beginPath();
+ ctx.moveTo(0, y);
+ ctx.lineTo(width, y);
+ ctx.stroke();
+ }
+ ctx.strokeStyle = isDark ? 'rgba(255, 255, 255, 0.04)' : 'rgba(17, 24, 39, 0.04)';
+ ctx.beginPath();
+ ctx.moveTo(width * 0.5, 0);
+ ctx.lineTo(width * 0.5, height);
+ ctx.stroke();
+ ctx.restore();
+
+ if (computed.validSeries.length === 0 || computed.rangeMs <= 0) {
+ return;
+ }
+
+ const active = activeEmphasisSeriesIndex();
+ const valueToY = createInteractiveSparklineValueToY(yMode(), computed.scaleMax, height);
+
+ for (let seriesIndex = 0; seriesIndex < computed.validSeries.length; seriesIndex++) {
+ const series = computed.validSeries[seriesIndex];
+ const isLg = props.size === 'lg';
+ const lineWidth =
+ active === null
+ ? isLg
+ ? 2
+ : 1.5
+ : active === seriesIndex
+ ? isLg
+ ? 3.5
+ : 2.8
+ : isLg
+ ? 1
+ : 0.9;
+ const opacity = active === null ? 0.75 : active === seriesIndex ? 1 : 0.1;
+
+ ctx.save();
+ ctx.globalAlpha = opacity;
+ ctx.strokeStyle = series.color;
+ ctx.lineWidth = lineWidth;
+ ctx.lineCap = 'round';
+ ctx.lineJoin = 'round';
+
+ for (const segment of series.segments) {
+ if (segment.length === 0) continue;
+
+ if (computed.validSeries.length === 1) {
+ ctx.beginPath();
+ for (let index = 0; index < segment.length; index++) {
+ const point = segment[index];
+ const x = clampInteractiveSparklineValue(
+ ((point.timestamp - computed.windowStart) / computed.rangeMs) * width,
+ 0,
+ width,
+ );
+ const y = valueToY(point.value);
+ if (index === 0) ctx.moveTo(x, y);
+ else ctx.lineTo(x, y);
+ }
+ const lastPoint = segment[segment.length - 1];
+ const firstPoint = segment[0];
+ const lastX = clampInteractiveSparklineValue(
+ ((lastPoint.timestamp - computed.windowStart) / computed.rangeMs) * width,
+ 0,
+ width,
+ );
+ const firstX = clampInteractiveSparklineValue(
+ ((firstPoint.timestamp - computed.windowStart) / computed.rangeMs) * width,
+ 0,
+ width,
+ );
+
+ ctx.lineTo(lastX, height);
+ ctx.lineTo(firstX, height);
+ ctx.closePath();
+
+ const areaGradient = ctx.createLinearGradient(0, 0, 0, height);
+ if (
+ series.color.startsWith('#') &&
+ (series.color.length === 7 || series.color.length === 4)
+ ) {
+ areaGradient.addColorStop(0, `${series.color}40`);
+ areaGradient.addColorStop(1, `${series.color}00`);
+ } else {
+ areaGradient.addColorStop(0, 'rgba(255, 255, 255, 0.15)');
+ areaGradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
+ }
+ ctx.fillStyle = areaGradient;
+ ctx.fill();
+ }
+
+ ctx.beginPath();
+ for (let index = 0; index < segment.length; index++) {
+ const point = segment[index];
+ const x = clampInteractiveSparklineValue(
+ ((point.timestamp - computed.windowStart) / computed.rangeMs) * width,
+ 0,
+ width,
+ );
+ const y = valueToY(point.value);
+ if (index === 0) ctx.moveTo(x, y);
+ else ctx.lineTo(x, y);
+ }
+ ctx.stroke();
+ }
+ ctx.restore();
+ }
+
+ const hover = hoveredState();
+ if (!hover) return;
+ ctx.save();
+ const x = (hover.x / vbW) * width;
+ const hoverLineGradient = ctx.createLinearGradient(0, Math.max(0, hover.minY - 4), 0, height);
+ hoverLineGradient.addColorStop(0, 'transparent');
+ hoverLineGradient.addColorStop(0.1, hoverLineColor);
+ hoverLineGradient.addColorStop(1, hoverLineColor);
+ ctx.strokeStyle = hoverLineGradient;
+ ctx.lineWidth = 1;
+ ctx.setLineDash([3, 3]);
+ ctx.beginPath();
+ ctx.moveTo(x, Math.max(0, hover.minY - 4));
+ ctx.lineTo(x, height);
+ ctx.stroke();
+ ctx.restore();
+ };
+
+ const queueCanvasDraw = () => {
+ if (!shouldUseCanvas()) return;
+ if (unregisterCanvasDraw) {
+ unregisterCanvasDraw();
+ }
+ unregisterCanvasDraw = scheduleSparkline(drawCanvas);
+ };
+
+ createEffect(() => {
+ if (!shouldUseCanvas()) {
+ if (unregisterCanvasDraw) {
+ unregisterCanvasDraw();
+ unregisterCanvasDraw = null;
+ }
+ return;
+ }
+
+ void chartData();
+ void activeEmphasisSeriesIndex();
+ void hoveredState();
+ queueCanvasDraw();
+ });
+
+ createEffect(() => {
+ const canvasHost = refs.getCanvasHost();
+ if (!shouldUseCanvas() || !canvasHost) return;
+ const observer = new ResizeObserver(() => queueCanvasDraw());
+ observer.observe(canvasHost);
+ onCleanup(() => observer.disconnect());
+ });
+
+ onCleanup(() => {
+ pendingHoverPosition = null;
+ if (typeof window !== 'undefined' && hoverRafId !== null) {
+ window.cancelAnimationFrame(hoverRafId);
+ hoverRafId = null;
+ }
+ if (unregisterCanvasDraw) {
+ unregisterCanvasDraw();
+ unregisterCanvasDraw = null;
+ }
+ });
+
+ return {
+ activeEmphasisSeriesIndex,
+ axisTicks,
+ chartData,
+ formatValue,
+ gridLineX,
+ gridLineY,
+ handleClick,
+ handleMouseLeave,
+ handleMouseMove,
+ hoveredState,
+ shouldUseCanvas,
+ vbH,
+ vbW,
+ xAxisBandPx,
+ xAxisTicks,
+ };
+}
+
+export type InteractiveSparklineState = ReturnType<typeof useInteractiveSparklineState>;
diff --git a/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts b/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
index fbf4e92146..559a3f8fb3 100644
--- a/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
+++ b/frontend-modern/src/utils/__tests__/frontendResourceTypeBoundaries.test.ts
@@ -10,7 +10,10 @@ import alertTargetTypesSource from '@/utils/alertTargetTypes.ts?raw';
import resourceBadgesSource from '@/components/Infrastructure/resourceBadges.ts?raw';
import infrastructureSummaryTableSource from '@/components/shared/InfrastructureSummaryTable.tsx?raw';
import infrastructureSummaryTableRowSource from '@/components/shared/InfrastructureSummaryTableRow.tsx?raw';
+import interactiveSparklineSource from '@/components/shared/InteractiveSparkline.tsx?raw';
+import interactiveSparklineModelSource from '@/components/shared/interactiveSparklineModel.ts?raw';
import sharedInfrastructureSummaryTableModelSource from '@/components/shared/infrastructureSummaryTableModel.ts?raw';
+import interactiveSparklineStateSource from '@/components/shared/useInteractiveSparklineState.ts?raw';
import infrastructureSummaryTableStateSource from '@/components/shared/useInfrastructureSummaryTableState.ts?raw';
import resourceBadgePresentationSource from '@/utils/resourceBadgePresentation.ts?raw';
import workloadTypeBadgesSource from '@/components/shared/workloadTypeBadges.ts?raw';
@@ -2621,6 +2624,16 @@ describe('frontend resource type boundaries', () => {
expect(sharedInfrastructureSummaryTableModelSource).toContain(
'resolveInfrastructureSummaryLinkedAgent',
);
+ expect(interactiveSparklineSource).toContain('useInteractiveSparklineState');
+ expect(interactiveSparklineSource).not.toContain('scheduleSparkline');
+ expect(interactiveSparklineSource).not.toContain('downsampleLTTB');
+ expect(interactiveSparklineSource).not.toContain('createSignal');
+ expect(interactiveSparklineStateSource).toContain('scheduleSparkline');
+ expect(interactiveSparklineStateSource).toContain('createSignal');
+ expect(interactiveSparklineModelSource).toContain('buildInteractiveSparklineChartData');
+ expect(interactiveSparklineModelSource).toContain(
+ 'computeInteractiveSparklineHoverState',
+ );
expect(useUnifiedResourcesSource).not.toContain('normalizeResourcePolicyAISafeSummary(');
expect(useUnifiedResourcesSource).not.toContain('normalizeResourcePolicy(');
expect(useUnifiedResourcesSource).not.toContain('const resolvePolicySensitivity =');