diff --git a/packages/app/src/components/DBHeatmapChart.tsx b/packages/app/src/components/DBHeatmapChart.tsx deleted file mode 100644 index 02a26f0a48..0000000000 --- a/packages/app/src/components/DBHeatmapChart.tsx +++ /dev/null @@ -1,1446 +0,0 @@ -import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; -import dynamic from 'next/dynamic'; -import cx from 'classnames'; -import type { Plugin } from 'uplot'; -import uPlot from 'uplot'; -import UplotReact from 'uplot-react'; -import { - ColumnMetaType, - inferTimestampColumn, -} from '@hyperdx/common-utils/dist/clickhouse'; -import { convertDateRangeToGranularityString } from '@hyperdx/common-utils/dist/core/utils'; -import { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types'; -import { DisplayType } from '@hyperdx/common-utils/dist/types'; -import { Divider, Flex, Text, useMantineColorScheme } from '@mantine/core'; -import { useElementSize } from '@mantine/hooks'; - -import { isAggregateFunction, timeBucketByGranularity } from '@/ChartUtils'; -import { useQueriedChartConfig } from '@/hooks/useChartConfig'; -import { NumberFormat } from '@/types'; -import { FormatTime } from '@/useFormatTime'; -import { formatDurationMsCompact, formatNumber } from '@/utils'; - -import ChartContainer from './charts/ChartContainer'; -import ChartErrorState, { - ChartErrorStateVariant, -} from './charts/ChartErrorState'; - -type Mode2DataArray = [number[], number[], number[]]; - -/** - * Drag-select bounds in data space: x in seconds (URL convention), y in - * the y-axis's natural unit (NOT log-space; callers pass the actual value - * users would expect to see, e.g. ms latency). yMin may be 0 when the - * selection touched the bottom bucket; the renderer clamps to the chart's - * visible y-axis floor. - */ -export type SelectionBounds = { - xMin: number; - xMax: number; - yMin: number; - yMax: number; -}; - -/** - * Reapply a persisted selection rectangle to a uPlot instance. Called on - * chart create and whenever the bounds prop changes; uPlot's `u.select` - * is owned by the chart, so it gets wiped on any chart recreation. We use - * the URL-backed bounds as the source of truth and mirror them onto the - * chart imperatively. fireHook=false avoids re-entering the setSelect hook. - */ -export function applySelectionToChart( - u: uPlot, - bounds: SelectionBounds | null | undefined, - scaleType: HeatmapScaleType, -) { - // The clear path runs BEFORE the scale-not-populated guard below so a - // null bounds always clears the rectangle, even on first paint when - // u.scales.y is not yet populated. Keep this ordering when refactoring; - // swapping it would suppress clears while scales are loading. - if (bounds == null) { - u.setSelect({ left: 0, top: 0, width: 0, height: 0 }, false); - return; - } - - const { xMin, xMax, yMin, yMax } = bounds; - - // x is in seconds in the URL; uPlot's x-axis is configured ms:1 so - // values are stored as ms. - const xMinPx = u.valToPos(xMin * 1000, 'x'); - const xMaxPx = u.valToPos(xMax * 1000, 'x'); - - const yScaleMin = u.scales.y?.min; - const yScaleMax = u.scales.y?.max; - if (yScaleMin == null || yScaleMax == null) { - return; - } - - // For log scale, y-axis values are stored in log-space. Convert and - // clamp to the visible axis: yMin may be 0 (bottom-bucket adjustment in - // HeatmapContainer's onFilter wrapper); yMax may exceed the axis. - let yLowPlot: number; - let yHighPlot: number; - if (scaleType === 'log') { - yHighPlot = yMax > 0 ? Math.min(Math.log(yMax), yScaleMax) : yScaleMax; - yLowPlot = yMin > 0 ? Math.max(Math.log(yMin), yScaleMin) : yScaleMin; - } else { - yHighPlot = Math.min(yMax, yScaleMax); - yLowPlot = Math.max(yMin, yScaleMin); - } - - // uPlot's y-axis: high data values map to small pixel y (top of chart). - const yHighPx = u.valToPos(yHighPlot, 'y'); - const yLowPx = u.valToPos(yLowPlot, 'y'); - - const left = Math.min(xMinPx, xMaxPx); - const right = Math.max(xMinPx, xMaxPx); - - u.setSelect( - { - left, - top: yHighPx, - width: right - left, - height: Math.max(0, yLowPx - yHighPx), - }, - false, - ); -} - -// From: https://github.com/leeoniya/uPlot/blob/a4edb297a9b80baf781f4d05a40fb52fae737bff/demos/latency-heatmap.html#L436 -function heatmapPaths(opts: { - disp: { fill: { lookup: string[]; values: any } }; -}) { - const { disp } = opts; - - return (u: uPlot, seriesIdx: number, _idx0: number, _idx1: number) => { - uPlot.orient( - u, - seriesIdx, - ( - _series, - _dataX, - _dataY, - scaleX, - scaleY, - valToPosX, - valToPosY, - xOff, - yOff, - xDim, - yDim, - _moveTo, - _lineTo, - rect, - _arc, - ) => { - // mode 2 data format is not supported in types properly - const d = u.data[seriesIdx] as unknown as Mode2DataArray; - const [xs, ys, counts] = d; - const dlen = xs.length; - - // fill colors are mapped from interpolating densities / counts along some gradient - // (should be quantized to 64 colors/levels max. e.g. 16) - const fills = disp.fill.values(u, seriesIdx); - - // let fillPaths = new Map(); // #rgba => Path2D - - const fillPalette = disp.fill.lookup ?? [...new Set(fills)]; - - const fillPaths = fillPalette.map(() => new Path2D()); - - // fillPalette.forEach(fill => { - // fillPaths.set(fill, new Path2D()); - // }); - - // detect x and y bin qtys by detecting layout repetition in x & y data - const yBinQty = dlen - ys.lastIndexOf(ys[0]); - const xBinQty = dlen / yBinQty; - const yBinIncr = ys[1] - ys[0]; - const xBinIncr = xs[yBinQty] - xs[0]; - - // uniform tile sizes based on zoom level - const xSize = - valToPosX(xBinIncr, scaleX, xDim, xOff) - - valToPosX(0, scaleX, xDim, xOff); - const ySize = - valToPosY(yBinIncr, scaleY, yDim, yOff) - - valToPosY(0, scaleY, yDim, yOff); - - // pre-compute x and y offsets - const cys = ys - .slice(0, yBinQty) - .map(y => Math.round(valToPosY(y, scaleY, yDim, yOff) - ySize / 2)); - const cxs = Array.from({ length: xBinQty }, (v, i) => - Math.round( - valToPosX(xs[i * yBinQty], scaleX, xDim, xOff) - xSize / 2, - ), - ); - - for (let i = 0; i < dlen; i++) { - // filter out 0 counts and out of view - if ( - counts[i] > 0 && - xs[i] >= (scaleX.min ?? -Infinity) && - xs[i] <= (scaleX.max ?? Infinity) && - ys[i] >= (scaleY.min ?? -Infinity) && - ys[i] <= (scaleY.max ?? Infinity) - ) { - const cx = cxs[~~(i / yBinQty)]; - const cy = cys[i % yBinQty]; - - const fillPath = fillPaths[fills[i]]; - - rect(fillPath, cx, cy, xSize, ySize); - } - } - - u.ctx.save(); - - u.ctx.rect(u.bbox.left, u.bbox.top, u.bbox.width, u.bbox.height); - u.ctx.clip(); - - fillPaths.forEach((p, i) => { - u.ctx.fillStyle = fillPalette[i]; - u.ctx.fill(p); - }); - u.ctx.restore(); - }, - ); - - return null; - }; -} - -// Theme-specific palettes. Red is deliberately avoided at the high end so -// it can be reserved for error overlays in the future. -// Dark theme: starts at a luminant indigo visible on dark bg, ends at bright amber. -// Light theme: starts at a saturated medium blue visible on white, ends at deep orange. -export const darkPalette = [ - '#7b6cf6', // indigo (low) - '#5a9cf6', // sky blue - '#38c9a0', // teal - '#6cd44a', // green - '#c4d629', // lime - '#f0c528', // gold - '#f5a623', // amber (high) -]; -export const lightPalette = [ - '#2a6fb5', // medium blue (low) - '#2a96a8', // teal - '#33a85e', // green - '#7db832', // lime - '#c4a820', // dark gold - '#e08a17', // orange - '#d46a12', // deep orange (high) -]; - -function makeCountsToFills(colors: string[]) { - return (u: uPlot, seriesIdx: number) => { - // mode 2 data format is not supported in types properly - const counts = u.data[seriesIdx][2] as unknown as number[]; - const dlen = counts.length; - - // Collect non-zero counts and sort to find a robust normalization ceiling. - // Using p95 instead of max prevents a single hot cell from washing out the - // rest of the chart, while still preserving cross-column comparability. - const nonZero: number[] = []; - for (let i = 0; i < dlen; i++) { - if (counts[i] > 0) nonZero.push(counts[i]); - } - nonZero.sort((a, b) => a - b); - - const paletteSize = colors.length; - const indexedFills = Array(dlen); - - if (nonZero.length === 0) { - indexedFills.fill(-1); - return indexedFills; - } - - const p95Idx = Math.floor((nonZero.length - 1) * 0.95); - const p95 = nonZero[p95Idx] ?? nonZero[nonZero.length - 1]; - const sqrtCeiling = Math.sqrt(p95); - - for (let i = 0; i < dlen; i++) { - indexedFills[i] = - counts[i] === 0 - ? -1 - : Math.max( - Math.min( - paletteSize - 1, - Math.floor( - (Math.sqrt(counts[i]) / (sqrtCeiling || 1)) * - (paletteSize - 1), - ), - ), - 0, - ); - } - - return indexedFills; - }; -} - -const axis: uPlot.Axis = { - stroke: 'rgba(102,102,102,1)', // color of the axis line - font: '12px IBM Plex Mono, monospace', - grid: { - show: true, // show grid lines - stroke: 'rgba(52,58,64)', // grid line color - dash: [3, 3], - width: 1, - }, - border: { - show: true, - // stroke: 'rgba(82,82,82)', // grid line color - stroke: 'rgba(102,102,102,1)', // color of the axis line - width: 1, - }, -}; - -const opt: uPlot.Options = { - width: 1500, - height: 600, - mode: 2, - ms: 1, - padding: [8, 8, 0, 4], - legend: { - show: false, - }, - scales: { - x: { - time: true, - }, - }, - axes: [ - { - ...axis, - gap: 10, - space: 60, - }, - { - ...axis, - }, - ], - series: [ - {}, - { - label: 'Latency', - // paths and fill colors are set dynamically per theme in the - // Heatmap component's useMemo — see buildSeriesForPalette(). - facets: [ - { - scale: 'x', - auto: true, - sorted: 1, - }, - { - scale: 'y', - auto: true, - }, - ], - }, - ], -}; - -/** Build the series[1] overrides for a given palette. */ -function buildSeriesForPalette(colors: string[]): Partial { - return { - paths: heatmapPaths({ - disp: { - fill: { - lookup: colors, - values: makeCountsToFills(colors), - }, - }, - }), - }; -} - -type HeatmapChartConfig = { - displayType: DisplayType.Heatmap; - select: [ - { - aggFn: 'heatmap'; - valueExpression: string; - countExpression?: string; - }, - ]; - from: BuilderChartConfigWithDateRange['from']; - where: BuilderChartConfigWithDateRange['where']; - dateRange: BuilderChartConfigWithDateRange['dateRange']; - granularity: BuilderChartConfigWithDateRange['granularity']; - timestampValueExpression: BuilderChartConfigWithDateRange['timestampValueExpression']; - numberFormat?: BuilderChartConfigWithDateRange['numberFormat']; - filters?: BuilderChartConfigWithDateRange['filters']; - connection: string; - with?: BuilderChartConfigWithDateRange['with']; -}; - -/** Build a HeatmapChartConfig from a builder chart config that has heatmap extras on select[0]. */ -export function toHeatmapChartConfig(config: BuilderChartConfigWithDateRange): { - heatmapConfig: HeatmapChartConfig; - scaleType: HeatmapScaleType; -} { - const firstSelect = Array.isArray(config.select) - ? config.select[0] - : undefined; - return { - heatmapConfig: { - ...config, - displayType: DisplayType.Heatmap, - select: [ - { - aggFn: 'heatmap' as const, - valueExpression: firstSelect?.valueExpression ?? '', - countExpression: firstSelect?.countExpression, - }, - ], - granularity: 'auto', - numberFormat: config.numberFormat, - }, - scaleType: firstSelect?.heatmapScaleType ?? 'log', - }; -} - -export const HEATMAP_N_BUCKETS = 80; - -/** - * Build the bounds (min/max) ChartConfig that runs first. Result feeds - * `effectiveMin`/`max` into `buildHeatmapBucketConfig`. - */ -export function buildHeatmapBoundsConfig({ - config, - scaleType, -}: { - config: HeatmapChartConfig; - scaleType: HeatmapScaleType; -}): BuilderChartConfigWithDateRange { - const valueExpression = config.select[0].valueExpression; - const isAggregateExpression = isAggregateFunction(valueExpression); - const qLo = scaleType === 'log' ? 0.01 : 0.001; - - return isAggregateExpression - ? { - ...config, - where: '', - orderBy: undefined, - granularity: undefined, - select: [ - { - aggFn: 'quantile' as const, - level: qLo, - aggCondition: `value_calc >= 0`, - aggConditionLanguage: 'sql', - valueExpression: 'value_calc', - alias: 'min', - }, - { - aggFn: 'max' as const, - valueExpression: 'value_calc', - alias: 'max', - }, - ], - with: [ - { - name: 'min_max_calc', - chartConfig: { - ...config, - select: [{ valueExpression, alias: 'value_calc' }], - orderBy: undefined, - }, - }, - ], - timestampValueExpression: '__hdx_time_bucket', - from: { databaseName: '', tableName: 'min_max_calc' }, - } - : { - ...config, - orderBy: undefined, - granularity: undefined, - select: [ - { - aggFn: 'quantile' as const, - level: qLo, - valueExpression, - aggCondition: `${valueExpression} >= 0`, - aggConditionLanguage: 'sql', - alias: 'min', - }, - { - aggFn: 'max' as const, - valueExpression, - alias: 'max', - }, - ], - }; -} - -/** - * Build the bucketed-counts ChartConfig that runs second. `effectiveMin`/`max` - * are usually numbers (resolved from the bounds query), but accept strings so - * callers — like the editor's SQL preview — can pass placeholder tokens - * (e.g. `'{min}'`) before the bounds are known. - */ -export function buildHeatmapBucketConfig({ - config, - scaleType, - effectiveMin, - max, - granularity, - nBuckets, -}: { - config: HeatmapChartConfig; - scaleType: HeatmapScaleType; - effectiveMin: string | number; - max: string | number; - granularity: string; - nBuckets: number; -}): BuilderChartConfigWithDateRange { - const valueExpression = config.select[0].valueExpression; - const countExpression = config.select[0].countExpression ?? 'count()'; - const isAggregateExpression = isAggregateFunction(valueExpression); - - const bucketExprAgg = - scaleType === 'log' - ? `widthBucket(log(greatest(toFloat64(value_calc), ${effectiveMin})), log(${effectiveMin}), log(${max}), ${nBuckets})` - : `widthBucket(value_calc, ${effectiveMin}, ${max}, ${nBuckets})`; - const bucketExprDirect = - scaleType === 'log' - ? `widthBucket(log(greatest(toFloat64(${valueExpression}), ${effectiveMin})), log(${effectiveMin}), log(${max}), ${nBuckets})` - : `widthBucket(${valueExpression}, ${effectiveMin}, ${max}, ${nBuckets})`; - - return isAggregateExpression - ? { - ...config, - where: '', - select: [ - { - valueExpression: 'sum(value_count)', - alias: 'count', - }, - ], - groupBy: [ - { - valueExpression: bucketExprAgg, - alias: 'x_bucket', - }, - ], - with: [ - { - name: 'bucket_calc', - chartConfig: { - ...config, - select: [ - { valueExpression, alias: 'value_calc' }, - { - valueExpression: countExpression, - alias: 'value_count', - }, - ], - granularity, - orderBy: undefined, - }, - }, - ], - timestampValueExpression: '__hdx_time_bucket', - from: { databaseName: '', tableName: 'bucket_calc' }, - orderBy: [{ valueExpression: 'x_bucket', ordering: 'ASC' }], - granularity, - } - : { - ...config, - select: [ - { - valueExpression: countExpression, - alias: 'count', - }, - ], - groupBy: [ - { - valueExpression: bucketExprDirect, - alias: 'x_bucket', - }, - ], - orderBy: [{ valueExpression: 'x_bucket', ordering: 'ASC' }], - granularity, - }; -} - -export function ColorLegend({ colors }: { colors: string[] }) { - return ( - - - Low - -
- {colors.map((color: string, i: number) => ( -
- ))} -
- - High - - - ); -} - -export type HeatmapScaleType = 'log' | 'linear'; - -export function formatDataForHeatmap({ - data, - timestampColumn, - generatedTsBuckets, - scaleType, - effectiveMin, - max, - nBuckets, -}: { - data: Record[]; - timestampColumn: ColumnMetaType; - generatedTsBuckets: Date[]; - scaleType: HeatmapScaleType; - effectiveMin: number; - max: number; - nBuckets: number; -}): Mode2DataArray { - const time: number[] = []; - const bucket: number[] = []; - const count: number[] = []; - - // Compute the y-axis value for a given bucket index. - // For log scale we store values in log space so that bins are uniformly - // spaced on the linear uPlot y-axis. The heatmapPaths renderer assumes - // uniform increments (yBinIncr = ys[1] - ys[0]) to compute tile height; - // with actual log-spaced values the first increment is tiny relative to - // the full range and tiles render at ~0px height (invisible). - const bucketToYValue = (j: number) => { - if (scaleType === 'log' && effectiveMin > 0 && max > effectiveMin) { - // Return the natural-log of the actual bucket boundary so that the - // y-values are uniformly spaced. Tick labels are exponentiated back - // via the tickFormatter below. - const actualValue = - effectiveMin * Math.pow(max / effectiveMin, j / nBuckets); - return Math.log(actualValue); - } - // Linear: min + j * step - return effectiveMin + j * ((max - effectiveMin) / nBuckets); - }; - - let dataIndex = 0; - for (let i = 0; i < generatedTsBuckets.length; i++) { - const generatedTs = generatedTsBuckets[i].getTime(); - - // CH widthBucket will return buckets from 0 to nBuckets + 1 - for (let j = 0; j <= nBuckets + 1; j++) { - const row = data[dataIndex]; - - if ( - row != null && - new Date(row[timestampColumn.name]).getTime() == generatedTs && - row['x_bucket'] == j - ) { - time.push(new Date(row[timestampColumn.name]).getTime()); - bucket.push(bucketToYValue(row['x_bucket'])); - count.push(Number.parseInt(row['count'], 10)); // UInt64 returns as string - - // Skip duplicate buckets (from unmerged distributed table results) - while ( - dataIndex < data.length && - new Date(data[dataIndex][timestampColumn.name]).getTime() == - generatedTs && - data[dataIndex]['x_bucket'] == j - ) { - dataIndex++; - } - } else { - time.push(generatedTs); - bucket.push(bucketToYValue(j)); - count.push(0); - } - } - } - - return [time, bucket, count]; -} - -/** - * Cumulative share (0-100) of events at or below each y-bucket, keyed by - * that bucket's y-value. - */ -export function computeBucketPercentiles( - data: Mode2DataArray, -): Map { - const [, ys, counts] = data; - - const bucketTotals = new Map(); - let total = 0; - for (let i = 0; i < ys.length; i++) { - bucketTotals.set(ys[i], (bucketTotals.get(ys[i]) ?? 0) + counts[i]); - total += counts[i]; - } - - const percentiles = new Map(); - if (total === 0) { - return percentiles; - } - - let cumulative = 0; - for (const [y, bucketTotal] of [...bucketTotals].sort(([a], [b]) => a - b)) { - cumulative += bucketTotal; - percentiles.set(y, (cumulative / total) * 100); - } - - return percentiles; -} - -function HeatmapContainer({ - config, - enabled = true, - onFilter, - onClearFilter, - selectionBounds, - title, - toolbarPrefix, - toolbarSuffix, - scaleType = 'log', - showLegend = false, - errorVariant, -}: { - config: HeatmapChartConfig; - enabled?: boolean; - onFilter?: (xMin: number, xMax: number, yMin: number, yMax: number) => void; - onClearFilter?: () => void; - /** - * The currently-applied drag-select bounds. When provided, the heatmap - * draws the dashed selection rectangle and reapplies it after any uPlot - * recreation (theme switch, prop change, resize) so the user always sees - * which slice they filtered. Caller owns the URL/query-state plumbing; - * this is purely a visual mirror of that state. - */ - selectionBounds?: SelectionBounds | null; - title?: React.ReactNode; - toolbarPrefix?: React.ReactNode[]; - toolbarSuffix?: React.ReactNode[]; - scaleType?: HeatmapScaleType; - showLegend?: boolean; - errorVariant?: ChartErrorStateVariant; -}) { - const dateRange = config.dateRange; - const granularity = convertDateRangeToGranularityString(dateRange, 245); - - const { colorScheme } = useMantineColorScheme(); - const palette = colorScheme === 'light' ? lightPalette : darkPalette; - - const nBuckets = HEATMAP_N_BUCKETS; - - // Use quantile-based lower bound to avoid near-zero outliers stretching - // the log axis. For the upper bound, use actual max() so that latency - // spikes (typically <1% of spans) remain visible — log scale already - // handles wide ranges naturally. Future: #1914 adds overflow-bucket - // indicators for smarter range clamping without hiding spikes. - const minMaxConfig = buildHeatmapBoundsConfig({ config, scaleType }); - - const { - data: minMaxData, - isLoading: isMinMaxLoading, - isPlaceholderData: isMinMaxPlaceholderData, - error: minMaxError, - } = useQueriedChartConfig(minMaxConfig, { - queryKey: ['heatmap', minMaxConfig], - enabled: enabled, - placeholderData: prev => prev, - }); - - // UInt64 are returned as strings; quantile returns floats - const min = Number.parseFloat(minMaxData?.data?.[0]?.['min'] ?? '0'); - const max = Number.parseFloat(minMaxData?.data?.[0]?.['max'] ?? '0'); - - // Ensure min > 0 for log scale (log(0) is undefined). - // Cap the range to ~4 orders of magnitude so the axis isn't dominated - // by a long empty tail of near-zero outliers. - const effectiveMin = - scaleType === 'log' ? Math.max(min, max * 1e-4 || 1e-4) : min; - - const bucketConfig = buildHeatmapBucketConfig({ - config, - scaleType, - effectiveMin, - max, - granularity, - nBuckets, - }); - - const canQueryBuckets = - !!minMaxData && bucketConfig != null && max > effectiveMin; - const { data, isLoading, isPlaceholderData, error } = useQueriedChartConfig( - bucketConfig, - { - queryKey: ['heatmap_bucket', bucketConfig], - // Wait for fresh bounds, so a refresh doesn't also query the new range - // bucketed with the previous range's min/max. - enabled: canQueryBuckets && !isMinMaxPlaceholderData, - // Only keep the previous buckets while this query can still run, so an - // empty refreshed range shows "Not enough data points" instead of - // pulsing on stale buckets. - placeholderData: canQueryBuckets ? prev => prev : undefined, - }, - ); - // A refresh keeps the previous heatmap on screen and pulses until both - // queries have fresh data. - const isRefreshing = isMinMaxPlaceholderData || isPlaceholderData; - - // Memoize so timeBucketByGranularity's fresh Date[] doesn't defeat - // the heatmapData memoization downstream. dateRange itself may be a - // fresh array each render, so depend on primitive ms + granularity. - const fromMs = dateRange[0]?.getTime() ?? 0; - const toMs = dateRange[1]?.getTime() ?? 0; - const generatedTsBuckets = useMemo( - () => timeBucketByGranularity(dateRange[0], dateRange[1], granularity), - // eslint-disable-next-line react-hooks/exhaustive-deps - [fromMs, toMs, granularity], - ); - - // Stable [time, bucket, count] arrays let uplot-react skip its setData - // path when only URL-state (xMin/xMax/yMin/yMax) changed. Pairs with the - // selectionBounds prop on Heatmap below: the prop reapplies u.select on - // any chart recreation, this memo prevents the recreation in the - // common case. - const heatmapData = useMemo(() => { - const timestampColumn = inferTimestampColumn(data?.meta ?? []); - if (data == null || data.data == null || timestampColumn == null) - return [[], [], []]; - - return formatDataForHeatmap({ - data: data.data, - timestampColumn, - generatedTsBuckets, - scaleType, - effectiveMin, - max, - nBuckets, - }); - }, [data, generatedTsBuckets, scaleType, effectiveMin, max, nBuckets]); - - // While refreshing, keep drawing the last settled heatmap. The previous - // bucket rows only line up with the time buckets, bounds and scale they were - // queried with, so re-plotting them on the new range would draw a blank or - // mis-scaled grid. - const currentView = useMemo( - () => ({ heatmapData, generatedTsBuckets, effectiveMin, scaleType }), - [heatmapData, generatedTsBuckets, effectiveMin, scaleType], - ); - const [settledView, setSettledView] = useState(currentView); - if (!isRefreshing && settledView !== currentView) { - setSettledView(currentView); - } - const view = isRefreshing ? settledView : currentView; - - const time = view.heatmapData[0]; - - const toolbarItemsMemo = useMemo(() => { - const allToolbarItems: React.ReactNode[] = []; - - if (showLegend) { - allToolbarItems.push( - , - ); - } - - if (toolbarPrefix && toolbarPrefix.length > 0) { - allToolbarItems.push(...toolbarPrefix); - } - - if (toolbarSuffix && toolbarSuffix.length > 0) { - allToolbarItems.push(...toolbarSuffix); - } - - return allToolbarItems; - }, [showLegend, palette, toolbarPrefix, toolbarSuffix]); - - const _error = error || minMaxError; - - return ( - - {isLoading || isMinMaxLoading ? ( - - Loading... - - ) : _error ? ( - - ) : time.length < 2 || view.generatedTsBuckets.length < 2 ? ( - - Not enough data points to render heatmap. Try expanding your search - criteria. - - ) : ( - { - // In log mode, the bottom bucket collects all values - // clamped by greatest(value, effectiveMin). If the - // selection touches that bucket, widen yMin to 0 so - // the downstream SQL filter captures all those spans. - // The 1.1x threshold adds 10% headroom to account for - // floating-point rounding in the bucket boundary. - const adjustedYMin = - view.scaleType === 'log' && yMin <= view.effectiveMin * 1.1 - ? 0 - : yMin; - onFilter(xMin, xMax, adjustedYMin, yMax); - } - : undefined - } - onClearFilter={onClearFilter} - scaleType={view.scaleType} - palette={palette} - selectionBounds={selectionBounds} - /> - )} - - ); -} - -export default dynamic(() => Promise.resolve(HeatmapContainer), { - ssr: false, -}); - -function highlightDataPlugin({ - proximity, - onPointHighlight, -}: { - proximity: number; - onPointHighlight: (point: { - // data point values - xVal: number; - yVal: number; - countVal: number; - // distance data - closestDistance: number; - closestIndex: number; - // bounding box for data point in css parent unit - xCoord: number; - yCoord: number; - xSize: number; - ySize: number; - }) => void; -}): Plugin { - // let dataPoint: HTMLDivElement | null = null; - let lastDataPointIndex = -1; // Used to prevent duplicate calls - return { - hooks: { - setCursor: (u: uPlot) => { - const { top, left } = u.cursor; - if (top == null || left == null) { - return; - } - - // const y = u.posToVal(top, 'y'); - // const x = u.posToVal(left, 'x'); - - const [xs, ys, counts] = u.data[1] as unknown as Mode2DataArray; - - let closestIndex = 0; - let closestDistance = Infinity; - - for (let i = 0; i < xs.length; i++) { - const xPx = u.valToPos(xs[i], 'x'); - const yPx = u.valToPos(ys[i], 'y'); - - // const distance = Math.abs(xs[i] - x) + Math.abs(ys[i] - y); - const distance = Math.sqrt((xPx - left) ** 2 + (yPx - top) ** 2); - - if (distance < closestDistance && counts[i] > 0) { - closestIndex = i; - closestDistance = distance; - } - } - - // Taken from heatmapPaths and modified for css pixels - const dlen = xs.length; - - // detect x and y bin qtys by detecting layout repetition in x & y data - const yBinQty = dlen - ys.lastIndexOf(ys[0]); - const yBinIncr = ys[1] - ys[0]; - const xBinIncr = xs[yBinQty] - xs[0]; - - const xSize = Math.abs(u.valToPos(xBinIncr, 'x') - u.valToPos(0, 'x')); - const ySize = Math.abs(u.valToPos(yBinIncr, 'y') - u.valToPos(0, 'y')); - - const xCoord = u.valToPos(xs[closestIndex], 'x'); - const yCoord = u.valToPos(ys[closestIndex], 'y'); - - const countVal = counts[closestIndex]; - const xVal = xs[closestIndex]; - const yVal = ys[closestIndex]; - - const { offsetLeft, offsetTop } = u.over; - - if ( - closestDistance < proximity && - closestIndex !== lastDataPointIndex - ) { - lastDataPointIndex = closestIndex; - onPointHighlight({ - xVal, - yVal, - countVal, - closestDistance, - closestIndex, - xCoord: xCoord + offsetLeft, - yCoord: yCoord + offsetTop, - xSize, - ySize, - }); - } - }, - }, - }; -} - -function Heatmap({ - className, - data, - numberFormat, - onFilter, - onClearFilter, - scaleType = 'linear', - palette, - selectionBounds, -}: { - className?: string; - data: Mode2DataArray; - numberFormat?: NumberFormat; - onFilter?: (xMin: number, xMax: number, yMin: number, yMax: number) => void; - onClearFilter?: () => void; - scaleType?: HeatmapScaleType; - palette: string[]; - selectionBounds?: SelectionBounds | null; -}) { - const [highlightedPoint, setHighlightedPoint] = useState< - | { - xVal: number; - yVal: number; - countVal: number; - closestDistance: number; - closestIndex: number; - xCoord: number; - yCoord: number; - xSize: number; - ySize: number; - } - | undefined - >(undefined); - - // Gate tooltip display on actual mouse interaction. uPlot fires setCursor - // on init (before user hovers), which would show the tooltip on page load. - const mouseInsideRef = useRef(false); - - // Depend on the boolean, not the onFilter function reference, so the - // options useMemo doesn't recompute (and re-initialize uPlot — wiping its - // internal u.select drag rectangle) on every parent render. - const hasFilter = !!onFilter; - - // Hold onFilter in a ref so the setSelect hook (captured inside the - // options useMemo) can always call the latest callback without needing - // onFilter in the memo's dep array. - const onFilterRef = useRef(onFilter); - useEffect(() => { - onFilterRef.current = onFilter; - }, [onFilter]); - - // Hold the uPlot instance so outside-click can explicitly clear the - // persisted u.select rectangle (which is owned by uPlot, not React). - const uplotRef = useRef(null); - - // Hold selectionBounds and scaleType in refs so the uPlot `ready` hook - // (captured inside the options useMemo via closure) always sees the - // latest values without needing them in the memo's dep array. Mutating - // a ref doesn't trigger re-renders, so the options identity stays - // stable across selection/scale changes. - const selectionBoundsRef = useRef(selectionBounds); - const scaleTypeRef = useRef(scaleType); - // Runs every commit; mirrors latest props into refs (no deps array on - // purpose). - useEffect(() => { - selectionBoundsRef.current = selectionBounds; - scaleTypeRef.current = scaleType; - }); - - const { ref, width, height } = useElementSize(); - - // Reapply the URL-backed selection whenever the bounds prop changes - // (e.g. a fresh drag-select arrives via the round-trip through the - // parent's URL state, or the parent clears the filter), or when the - // container resizes (uPlot calls setSize, not a full recreate, so the - // pixel-space u.select would otherwise be stale). This complements the - // uPlot `ready` hook below: that handles initial chart creation (and - // any recreation), this handles bounds and size changes against an - // existing chart. - useEffect(() => { - if (uplotRef.current) { - applySelectionToChart(uplotRef.current, selectionBounds, scaleType); - } - }, [selectionBounds, scaleType, width, height]); - - // Timestamp of the most recent drag-end. Guards the container's onClick - // handler from clearing the selection when the synthetic click event - // that fires on mouseup-after-drag arrives. - const justDraggedAtRef = useRef(0); - - // Stabilize on numberFormat content, not reference. Callers (e.g. - // DBSearchHeatmapChart) build a fresh `numberFormat` object on every - // render; depending on its identity would rebuild tickFormatter, then - // the options memo, then uplot-react would see new top-level keys via - // optionsUpdateState and treat the change as 'create', destroying the - // chart and wiping u.select. Hashing the contents lets the memo skip - // when the actual format is unchanged. (HDX-4147) - // - // Relies on NumberFormat being JSON-serializable: today it is plain - // config (string + number fields), so JSON.stringify is a faithful - // fingerprint. If the type ever grows a function-valued field - // (e.g. a custom `formatter` callback), switch to a shallow-equal - // helper keyed on the known fields, because functions stringify to - // undefined and would silently skip rebuilds. - const numberFormatKey = useMemo( - () => (numberFormat ? JSON.stringify(numberFormat) : ''), - [numberFormat], - ); - const tickFormatter = useCallback( - (value: number) => { - // y-values are stored in log space for log scale; exponentiate back - // to the actual value before formatting. - const actualValue = scaleType === 'log' ? Math.exp(value) : value; - - if (numberFormat?.unit === 'ms' || numberFormat?.output === 'duration') { - const msValue = - numberFormat?.output === 'duration' - ? actualValue * (numberFormat?.factor ?? 1) * 1000 - : actualValue; - return formatDurationMsCompact(msValue); - } - - return numberFormat - ? formatNumber(actualValue, { - ...numberFormat, - average: true, - mantissa: Math.abs(actualValue) >= 1 ? 0 : 2, - }) - : new Intl.NumberFormat('en-US', { - notation: 'compact', - compactDisplay: 'short', - }).format(actualValue); - }, - // eslint-disable-next-line react-hooks/exhaustive-deps - [numberFormatKey, scaleType], - ); - - const bucketPercentiles = useMemo( - () => computeBucketPercentiles(data), - [data], - ); - - const options: uPlot.Options = useMemo(() => { - const themedSeries = buildSeriesForPalette(palette); - return { - ...opt, - series: [opt.series[0], { ...opt.series[1], ...themedSeries }], - ...(opt != null && opt.axes != null - ? { - axes: [ - opt.axes[0], - { - ...opt.axes[1], - values: (_u: uPlot, vals: number[]) => { - return vals.map(tickFormatter); - }, - // Override the static size fn so it measures the actual - // formatted labels (from tickFormatter) rather than - // whatever raw values uPlot passes in a prior cycle. - size(self: uPlot, values: string[]) { - if (!values || values.length === 0) return 50; - const font = - self.axes[1]?.font ?? '12px IBM Plex Mono, monospace'; - const ctx = self.ctx; - ctx.save(); - ctx.font = font; - let maxW = 0; - for (const v of values) { - const w = ctx.measureText(v).width; - if (w > maxW) maxW = w; - } - ctx.restore(); - return Math.ceil(maxW) + 16; - }, - // For log scale, place ticks at powers of 10 (0.01, 0.1, 1, - // 10, 100…) so labels are clean round numbers instead of - // arbitrary positions in log-space. - ...(scaleType === 'log' - ? { - splits: (u: uPlot) => { - const [yMin, yMax] = - u.scales.y!.min != null - ? [u.scales.y!.min, u.scales.y!.max!] - : [0, 1]; - // yMin/yMax are in log-space (natural log) - const realMin = Math.exp(yMin); - const realMax = Math.exp(yMax); - const splits: number[] = []; - // Generate powers of 10 within range - const startExp = Math.floor(Math.log10(realMin)); - const endExp = Math.ceil(Math.log10(realMax)); - for (let e = startExp; e <= endExp; e++) { - const v = Math.pow(10, e); - const logV = Math.log(v); - if (logV >= yMin && logV <= yMax) { - splits.push(logV); - } - } - // If too few splits, add intermediate values (×3) - if (splits.length < 3) { - for (let e = startExp; e <= endExp; e++) { - for (const mult of [1, 3]) { - const v = mult * Math.pow(10, e); - const logV = Math.log(v); - if (logV >= yMin && logV <= yMax) { - splits.push(logV); - } - } - } - // Deduplicate and sort - return [...new Set(splits)].sort((a, b) => a - b); - } - return splits; - }, - } - : {}), - }, - ], - } - : {}), - width, - height, - cursor: { - drag: { - setScale: false, - x: hasFilter, - y: hasFilter, - dist: 5, - }, - show: true, - focus: { - prox: 100, - }, - }, - plugins: [ - // legendAsTooltipPlugin() - - highlightDataPlugin({ - proximity: 20, - onPointHighlight: ({ - xVal, - yVal, - countVal, - closestDistance, - closestIndex, - xCoord, - yCoord, - xSize, - ySize, - }) => { - // Only show tooltip after the user has actually hovered the chart. - // uPlot fires setCursor on init which would trigger this on page load. - if (!mouseInsideRef.current) return; - setHighlightedPoint({ - xVal, - yVal, - countVal, - closestDistance, - closestIndex, - xCoord, - yCoord, - xSize, - ySize, - }); - }, - }), - { - hooks: { - // Fires once after uPlot finishes initial layout/draw. At - // onCreate time scales aren't reliably populated for mode-2 - // facet data, so reapplying the URL-backed selection from - // here ensures valToPos has the bounds it needs. (HDX-4147) - ready: u => { - applySelectionToChart( - u, - selectionBoundsRef.current, - scaleTypeRef.current, - ); - }, - setSelect: u => { - // Ignore zero-size selections (e.g. single-click) - if (u.select.width <= 0 || u.select.height <= 0) { - return; - } - - const xMin = u.posToVal(u.select.left, 'x'); - const xMax = u.posToVal(u.select.left + u.select.width, 'x'); - const rawYMax = u.posToVal(u.select.top, 'y'); - const rawYMin = u.posToVal(u.select.top + u.select.height, 'y'); - - // y-values are stored in log space for log scale; convert back - const yMin = scaleType === 'log' ? Math.exp(rawYMin) : rawYMin; - const yMax = scaleType === 'log' ? Math.exp(rawYMax) : rawYMax; - - // Apply the filter immediately on drag end. Record the - // timestamp so the synthetic click event that follows the - // drag (mouseup fires a click on the container) doesn't - // immediately clear the selection we just made. - justDraggedAtRef.current = performance.now(); - onFilterRef.current?.(xMin / 1000, xMax / 1000, yMin, yMax); - }, - }, - }, - ], - }; - }, [width, height, tickFormatter, scaleType, palette, hasFilter]); - - const highlightedPercentile = - highlightedPoint && bucketPercentiles.get(highlightedPoint.yVal); - - return ( -
{ - // Chromium fires a click event on mouseup even after a drag. - // Ignore it; the drag itself was handled by setSelect. - if (performance.now() - justDraggedAtRef.current < 300) { - return; - } - if (!hasFilter) return; - // Random click on the chart clears the persisted selection and - // exits comparison mode. - uplotRef.current?.setSelect( - { left: 0, top: 0, width: 0, height: 0 }, - false, - ); - onClearFilter?.(); - }} - onMouseEnter={() => { - mouseInsideRef.current = true; - }} - onMouseLeave={() => { - mouseInsideRef.current = false; - setHighlightedPoint(undefined); - }} - > - { - uplotRef.current = chart; - }} - onDelete={() => { - uplotRef.current = null; - }} - /> - {highlightedPoint != null && ( - <> -
-
width / 2 - ? { - right: width - highlightedPoint.xCoord + 10, - } - : { - left: highlightedPoint.xCoord + 10, - }), - maxWidth: '50%', - overflow: 'hidden', - textOverflow: 'ellipsis', - whiteSpace: 'nowrap' as const, - backdropFilter: 'blur(8px)', - backgroundColor: 'var(--mantine-color-body)', - border: '1px solid var(--mantine-color-default-border)', - borderRadius: 4, - pointerEvents: 'none', - }} - > - {onFilter && ( - <> - - Drag to Compare · Click to Clear - - - - )} -
- -
-
- Y Value: {tickFormatter(highlightedPoint.yVal)} - {highlightedPercentile != null && - ` (p${new Intl.NumberFormat('en-US', { - maximumFractionDigits: 1, - }).format(highlightedPercentile)})`} -
-
- Count Value:{' '} - {new Intl.NumberFormat('en-US', { - notation: 'standard', - compactDisplay: 'short', - }).format(highlightedPoint.countVal)} -
-
- - )} -
- ); -} diff --git a/packages/app/src/components/DBHeatmapChart/ColorLegend.tsx b/packages/app/src/components/DBHeatmapChart/ColorLegend.tsx new file mode 100644 index 0000000000..c55139e76d --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/ColorLegend.tsx @@ -0,0 +1,36 @@ +import { Flex, Text } from '@mantine/core'; + +type ColorLegendProps = { + colors: string[]; +}; + +export function ColorLegend({ colors }: ColorLegendProps) { + return ( + + + Low + +
+ {colors.map(color => ( +
+ ))} +
+ + High + + + ); +} diff --git a/packages/app/src/components/DBHeatmapChart/DBHeatmapChart.tsx b/packages/app/src/components/DBHeatmapChart/DBHeatmapChart.tsx new file mode 100644 index 0000000000..6ab4b224e4 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/DBHeatmapChart.tsx @@ -0,0 +1,139 @@ +import { useMemo } from 'react'; +import dynamic from 'next/dynamic'; +import cx from 'classnames'; +import { Text, useMantineColorScheme } from '@mantine/core'; + +import ChartContainer from '@/components/charts/ChartContainer'; +import ChartErrorState, { + ChartErrorStateVariant, +} from '@/components/charts/ChartErrorState'; + +import { ColorLegend } from './ColorLegend'; +import type { HeatmapScaleType } from './heatmapGrid'; +import { HeatmapPlot } from './HeatmapPlot'; +import type { HeatmapChartConfig } from './heatmapQueries'; +import { darkPalette, lightPalette } from './palette'; +import type { SelectionBounds } from './selection'; +import { useHeatmapData } from './useHeatmapData'; + +type DBHeatmapChartProps = { + config: HeatmapChartConfig; + enabled?: boolean; + onFilter?: (xMin: number, xMax: number, yMin: number, yMax: number) => void; + onClearFilter?: () => void; + /** + * The currently-applied drag-select bounds. When provided, the heatmap + * draws the dashed selection rectangle and reapplies it after any uPlot + * recreation (theme switch, prop change, resize) so the user always sees + * which slice they filtered. Caller owns the URL/query-state plumbing; + * this is purely a visual mirror of that state. + */ + selectionBounds?: SelectionBounds | null; + title?: React.ReactNode; + toolbarPrefix?: React.ReactNode[]; + toolbarSuffix?: React.ReactNode[]; + scaleType?: HeatmapScaleType; + showLegend?: boolean; + errorVariant?: ChartErrorStateVariant; +}; + +function DBHeatmapChart({ + config, + enabled = true, + onFilter, + onClearFilter, + selectionBounds, + title, + toolbarPrefix, + toolbarSuffix, + scaleType = 'log', + showLegend = false, + errorVariant, +}: DBHeatmapChartProps) { + const { colorScheme } = useMantineColorScheme(); + const palette = colorScheme === 'light' ? lightPalette : darkPalette; + + const { view, isLoading, isRefreshing, error } = useHeatmapData({ + config, + scaleType, + enabled, + }); + + const toolbarItemsMemo = useMemo(() => { + const allToolbarItems: React.ReactNode[] = []; + + if (showLegend) { + allToolbarItems.push( + , + ); + } + + if (toolbarPrefix && toolbarPrefix.length > 0) { + allToolbarItems.push(...toolbarPrefix); + } + + if (toolbarSuffix && toolbarSuffix.length > 0) { + allToolbarItems.push(...toolbarSuffix); + } + + return allToolbarItems; + }, [showLegend, palette, toolbarPrefix, toolbarSuffix]); + + return ( + + {isLoading ? ( + + Loading... + + ) : error ? ( + + ) : view.grid.cells.length < 2 || view.generatedTsBuckets.length < 2 ? ( + + Not enough data points to render heatmap. Try expanding your search + criteria. + + ) : ( + { + // In log mode, the bottom bucket collects all values + // clamped by greatest(value, effectiveMin). If the + // selection touches that bucket, widen yMin to 0 so + // the downstream SQL filter captures all those spans. + // The 1.1x threshold adds 10% headroom to account for + // floating-point rounding in the bucket boundary. + const adjustedYMin = + view.scaleType === 'log' && yMin <= view.effectiveMin * 1.1 + ? 0 + : yMin; + onFilter(xMin, xMax, adjustedYMin, yMax); + } + : undefined + } + onClearFilter={onClearFilter} + scaleType={view.scaleType} + palette={palette} + selectionBounds={selectionBounds} + /> + )} + + ); +} + +export default dynamic(() => Promise.resolve(DBHeatmapChart), { + ssr: false, +}); diff --git a/packages/app/src/components/DBHeatmapChart/HeatmapPlot.tsx b/packages/app/src/components/DBHeatmapChart/HeatmapPlot.tsx new file mode 100644 index 0000000000..a1fadfb309 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/HeatmapPlot.tsx @@ -0,0 +1,304 @@ +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import cx from 'classnames'; +import uPlot from 'uplot'; +import UplotReact from 'uplot-react'; +import { useElementSize } from '@mantine/hooks'; + +import { NumberFormat } from '@/types'; + +import { formatHeatmapTick, logScaleSplits } from './heatmapAxis'; +import { + computeBucketPercentiles, + gridToPlotData, + HeatmapGrid, + heatmapRowCount, + HeatmapScaleType, +} from './heatmapGrid'; +import { baseHeatmapOptions, buildSeriesForPalette } from './heatmapPaths'; +import { HeatmapTooltip } from './HeatmapTooltip'; +import { highlightDataPlugin, HighlightedPoint } from './highlightDataPlugin'; +import { applySelectionToChart, SelectionBounds } from './selection'; + +type HeatmapPlotProps = { + className?: string; + grid: HeatmapGrid; + numberFormat?: NumberFormat; + onFilter?: (xMin: number, xMax: number, yMin: number, yMax: number) => void; + onClearFilter?: () => void; + scaleType?: HeatmapScaleType; + palette: string[]; + selectionBounds?: SelectionBounds | null; +}; + +export function HeatmapPlot({ + className, + grid, + numberFormat, + onFilter, + onClearFilter, + scaleType = 'linear', + palette, + selectionBounds, +}: HeatmapPlotProps) { + const [highlightedPoint, setHighlightedPoint] = useState< + HighlightedPoint | undefined + >(undefined); + + // Gate tooltip display on actual mouse interaction. uPlot fires setCursor + // on init (before user hovers), which would show the tooltip on page load. + const mouseInsideRef = useRef(false); + + // Depend on the boolean, not the onFilter function reference, so the + // options useMemo doesn't recompute (and re-initialize uPlot — wiping its + // internal u.select drag rectangle) on every parent render. + const hasFilter = !!onFilter; + + // Hold onFilter in a ref so the setSelect hook (captured inside the + // options useMemo) can always call the latest callback without needing + // onFilter in the memo's dep array. + const onFilterRef = useRef(onFilter); + useEffect(() => { + onFilterRef.current = onFilter; + }, [onFilter]); + + // Hold the uPlot instance so outside-click can explicitly clear the + // persisted u.select rectangle (which is owned by uPlot, not React). + const uplotRef = useRef(null); + + // Hold selectionBounds and scaleType in refs so the uPlot `ready` hook + // (captured inside the options useMemo via closure) always sees the + // latest values without needing them in the memo's dep array. Mutating + // a ref doesn't trigger re-renders, so the options identity stays + // stable across selection/scale changes. + const selectionBoundsRef = useRef(selectionBounds); + const scaleTypeRef = useRef(scaleType); + // Runs every commit; mirrors latest props into refs (no deps array on + // purpose). + useEffect(() => { + selectionBoundsRef.current = selectionBounds; + scaleTypeRef.current = scaleType; + }); + + const { ref, width, height } = useElementSize(); + + // Reapply the URL-backed selection whenever the bounds prop changes + // (e.g. a fresh drag-select arrives via the round-trip through the + // parent's URL state, or the parent clears the filter), or when the + // container resizes (uPlot calls setSize, not a full recreate, so the + // pixel-space u.select would otherwise be stale). This complements the + // uPlot `ready` hook below: that handles initial chart creation (and + // any recreation), this handles bounds and size changes against an + // existing chart. + useEffect(() => { + if (uplotRef.current) { + applySelectionToChart(uplotRef.current, selectionBounds, scaleType); + } + }, [selectionBounds, scaleType, width, height]); + + // Timestamp of the most recent drag-end. Guards the container's onClick + // handler from clearing the selection when the synthetic click event + // that fires on mouseup-after-drag arrives. + const justDraggedAtRef = useRef(0); + + // Stabilize on numberFormat content, not reference. Callers (e.g. + // DBSearchHeatmapChart) build a fresh `numberFormat` object on every + // render; depending on its identity would rebuild tickFormatter, then + // the options memo, then uplot-react would see new top-level keys via + // optionsUpdateState and treat the change as 'create', destroying the + // chart and wiping u.select. Hashing the contents lets the memo skip + // when the actual format is unchanged. (HDX-4147) + // + // Relies on NumberFormat being JSON-serializable: today it is plain + // config (string + number fields), so JSON.stringify is a faithful + // fingerprint. If the type ever grows a function-valued field + // (e.g. a custom `formatter` callback), switch to a shallow-equal + // helper keyed on the known fields, because functions stringify to + // undefined and would silently skip rebuilds. + const numberFormatKey = useMemo( + () => (numberFormat ? JSON.stringify(numberFormat) : ''), + [numberFormat], + ); + const tickFormatter = useCallback( + (value: number) => formatHeatmapTick(value, scaleType, numberFormat), + // eslint-disable-next-line react-hooks/exhaustive-deps + [numberFormatKey, scaleType], + ); + + const plotData = useMemo(() => gridToPlotData(grid), [grid]); + const rowCount = heatmapRowCount(grid.yAxis); + const bucketPercentiles = useMemo( + () => computeBucketPercentiles(grid), + [grid], + ); + + const options: uPlot.Options = useMemo(() => { + const opt = baseHeatmapOptions; + const themedSeries = buildSeriesForPalette(palette); + return { + ...opt, + series: [opt.series[0], { ...opt.series[1], ...themedSeries }], + ...(opt != null && opt.axes != null + ? { + axes: [ + opt.axes[0], + { + ...opt.axes[1], + values: (_u: uPlot, vals: number[]) => { + return vals.map(tickFormatter); + }, + // Override the static size fn so it measures the actual + // formatted labels (from tickFormatter) rather than + // whatever raw values uPlot passes in a prior cycle. + size(self: uPlot, values: string[]) { + if (!values || values.length === 0) return 50; + const font = + self.axes[1]?.font ?? '12px IBM Plex Mono, monospace'; + const ctx = self.ctx; + ctx.save(); + ctx.font = font; + let maxW = 0; + for (const v of values) { + const w = ctx.measureText(v).width; + if (w > maxW) maxW = w; + } + ctx.restore(); + return Math.ceil(maxW) + 16; + }, + ...(scaleType === 'log' + ? { + splits: (u: uPlot) => { + const [yMin, yMax] = + u.scales.y!.min != null + ? [u.scales.y!.min, u.scales.y!.max!] + : [0, 1]; + return logScaleSplits(yMin, yMax); + }, + } + : {}), + }, + ], + } + : {}), + width, + height, + cursor: { + drag: { + setScale: false, + x: hasFilter, + y: hasFilter, + dist: 5, + }, + show: true, + focus: { + prox: 100, + }, + }, + plugins: [ + highlightDataPlugin({ + proximity: 20, + onPointHighlight: point => { + // Only show tooltip after the user has actually hovered the chart. + // uPlot fires setCursor on init which would trigger this on page load. + if (!mouseInsideRef.current) return; + setHighlightedPoint(point); + }, + }), + { + hooks: { + // Fires once after uPlot finishes initial layout/draw. At + // onCreate time scales aren't reliably populated for mode-2 + // facet data, so reapplying the URL-backed selection from + // here ensures valToPos has the bounds it needs. (HDX-4147) + ready: u => { + applySelectionToChart( + u, + selectionBoundsRef.current, + scaleTypeRef.current, + ); + }, + setSelect: u => { + // Ignore zero-size selections (e.g. single-click) + if (u.select.width <= 0 || u.select.height <= 0) { + return; + } + + const xMin = u.posToVal(u.select.left, 'x'); + const xMax = u.posToVal(u.select.left + u.select.width, 'x'); + const rawYMax = u.posToVal(u.select.top, 'y'); + const rawYMin = u.posToVal(u.select.top + u.select.height, 'y'); + + // y-values are stored in log space for log scale; convert back + const yMin = scaleType === 'log' ? Math.exp(rawYMin) : rawYMin; + const yMax = scaleType === 'log' ? Math.exp(rawYMax) : rawYMax; + + // Apply the filter immediately on drag end. Record the + // timestamp so the synthetic click event that follows the + // drag (mouseup fires a click on the container) doesn't + // immediately clear the selection we just made. + justDraggedAtRef.current = performance.now(); + onFilterRef.current?.(xMin / 1000, xMax / 1000, yMin, yMax); + }, + }, + }, + ], + }; + }, [width, height, tickFormatter, scaleType, palette, hasFilter]); + + const highlightedPercentile = + highlightedPoint && rowCount > 0 + ? bucketPercentiles.get(highlightedPoint.closestIndex % rowCount) + : undefined; + + return ( +
{ + // Chromium fires a click event on mouseup even after a drag. + // Ignore it; the drag itself was handled by setSelect. + if (performance.now() - justDraggedAtRef.current < 300) { + return; + } + if (!hasFilter) return; + // Random click on the chart clears the persisted selection and + // exits comparison mode. + uplotRef.current?.setSelect( + { left: 0, top: 0, width: 0, height: 0 }, + false, + ); + onClearFilter?.(); + }} + onMouseEnter={() => { + mouseInsideRef.current = true; + }} + onMouseLeave={() => { + mouseInsideRef.current = false; + setHighlightedPoint(undefined); + }} + > + { + uplotRef.current = chart; + }} + onDelete={() => { + uplotRef.current = null; + }} + /> + {highlightedPoint != null && ( + + )} +
+ ); +} diff --git a/packages/app/src/components/DBHeatmapChart/HeatmapTooltip.tsx b/packages/app/src/components/DBHeatmapChart/HeatmapTooltip.tsx new file mode 100644 index 0000000000..22db48df5d --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/HeatmapTooltip.tsx @@ -0,0 +1,91 @@ +import { Divider, Text } from '@mantine/core'; + +import { FormatTime } from '@/useFormatTime'; + +import type { HighlightedPoint } from './highlightDataPlugin'; + +type HeatmapTooltipProps = { + point: HighlightedPoint; + /** Chart container size, used to keep the tooltip inside it. */ + width: number; + height: number; + showDragHint: boolean; + formatY: (value: number) => string; + percentile: number | undefined; +}; + +export function HeatmapTooltip({ + point, + width, + height, + showDragHint, + formatY, + percentile, +}: HeatmapTooltipProps) { + return ( + <> +
+
width / 2 + ? { + right: width - point.xCoord + 10, + } + : { + left: point.xCoord + 10, + }), + maxWidth: '50%', + overflow: 'hidden', + textOverflow: 'ellipsis', + whiteSpace: 'nowrap' as const, + backdropFilter: 'blur(8px)', + backgroundColor: 'var(--mantine-color-body)', + border: '1px solid var(--mantine-color-default-border)', + borderRadius: 4, + pointerEvents: 'none', + }} + > + {showDragHint && ( + <> + + Drag to Compare · Click to Clear + + + + )} +
+ +
+
+ Y Value: {formatY(point.yVal)} + {percentile != null && + ` (p${new Intl.NumberFormat('en-US', { + maximumFractionDigits: 1, + }).format(percentile)})`} +
+
+ Count Value:{' '} + {new Intl.NumberFormat('en-US', { + notation: 'standard', + compactDisplay: 'short', + }).format(point.countVal)} +
+
+ + ); +} diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/ColorLegend.test.tsx b/packages/app/src/components/DBHeatmapChart/__tests__/ColorLegend.test.tsx new file mode 100644 index 0000000000..a07e760b8a --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/__tests__/ColorLegend.test.tsx @@ -0,0 +1,15 @@ +import { screen } from '@testing-library/react'; + +import { ColorLegend } from '@/components/DBHeatmapChart/ColorLegend'; + +describe('ColorLegend', () => { + it('renders one swatch per palette color between the low and high labels', () => { + renderWithMantine(); + + const legend = screen.getByRole('img', { + name: 'Color scale: low to high count', + }); + expect(legend).toHaveTextContent(/^Low.*High$/); + expect(legend.querySelectorAll('div[style*="background"]')).toHaveLength(3); + }); +}); diff --git a/packages/app/src/components/__tests__/DBHeatmapChart.render.test.tsx b/packages/app/src/components/DBHeatmapChart/__tests__/DBHeatmapChart.render.test.tsx similarity index 98% rename from packages/app/src/components/__tests__/DBHeatmapChart.render.test.tsx rename to packages/app/src/components/DBHeatmapChart/__tests__/DBHeatmapChart.render.test.tsx index 203d394154..5b5d197650 100644 --- a/packages/app/src/components/__tests__/DBHeatmapChart.render.test.tsx +++ b/packages/app/src/components/DBHeatmapChart/__tests__/DBHeatmapChart.render.test.tsx @@ -30,8 +30,8 @@ jest.mock('next/dynamic', () => ({ }, })); -// Heatmap passes uPlot `[[], [time, bucket, count]]` plus its options; record -// what it plots and with which axes. +// HeatmapPlot passes uPlot `[[], [time, bucket, count, ...cellExtents]]` +// plus its options; record what it plots and with which axes. const mockPlot = jest.fn(); jest.mock('uplot-react', () => ({ __esModule: true, diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapPlot.test.tsx b/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapPlot.test.tsx new file mode 100644 index 0000000000..c298c632ce --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapPlot.test.tsx @@ -0,0 +1,92 @@ +import { fireEvent, screen } from '@testing-library/react'; + +import { + gridToPlotData, + HeatmapGrid, +} from '@/components/DBHeatmapChart/heatmapGrid'; +import { HeatmapPlot } from '@/components/DBHeatmapChart/HeatmapPlot'; + +type PlotOptions = { + cursor?: { drag?: { x?: boolean; y?: boolean } }; + axes?: { splits?: unknown }[]; +}; + +const mockPlot = jest.fn(); +jest.mock('uplot-react', () => ({ + __esModule: true, + default: ({ + data, + options, + }: { + data: [unknown, unknown]; + options: PlotOptions; + }) => { + mockPlot(data[1], options); + return
; + }, +})); + +const grid: HeatmapGrid = { + times: [1000, 2000], + stepMs: 1000, + yAxis: { type: 'numeric', scale: 'linear', edges: [0, 1, 2] }, + cells: [1, 2, 3, 4], + cellKind: 'count', +}; + +const lastData = () => mockPlot.mock.calls.at(-1)?.[0]; +const lastOptions = (): PlotOptions => mockPlot.mock.calls.at(-1)?.[1]; + +const renderPlot = ( + props: Partial> = {}, +) => + renderWithMantine(); + +describe('HeatmapPlot', () => { + beforeEach(() => { + mockPlot.mockReset(); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('plots the grid as uPlot mode-2 data with cell extents', () => { + renderPlot(); + + expect(lastData()).toEqual(gridToPlotData(grid)); + }); + + it('enables drag-select only when the caller can filter', () => { + const { unmount } = renderPlot(); + expect(lastOptions().cursor?.drag).toMatchObject({ x: false, y: false }); + unmount(); + + renderPlot({ onFilter: jest.fn() }); + expect(lastOptions().cursor?.drag).toMatchObject({ x: true, y: true }); + }); + + it('places log-scale ticks at powers of 10', () => { + const { unmount } = renderPlot({ scaleType: 'linear' }); + expect(lastOptions().axes?.[1]?.splits).toBeUndefined(); + unmount(); + + renderPlot({ scaleType: 'log' }); + expect(lastOptions().axes?.[1]?.splits).toEqual(expect.any(Function)); + }); + + it('clears the selection on click only when the caller can filter', () => { + // Clicks within 300ms of the last drag are ignored + jest.spyOn(performance, 'now').mockReturnValue(10_000); + const onClearFilter = jest.fn(); + + const { unmount } = renderPlot({ onClearFilter }); + fireEvent.click(screen.getByTestId('heatmap-plot')); + expect(onClearFilter).not.toHaveBeenCalled(); + unmount(); + + renderPlot({ onFilter: jest.fn(), onClearFilter }); + fireEvent.click(screen.getByTestId('heatmap-plot')); + expect(onClearFilter).toHaveBeenCalledTimes(1); + }); +}); diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapTooltip.test.tsx b/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapTooltip.test.tsx new file mode 100644 index 0000000000..f86a300716 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/__tests__/HeatmapTooltip.test.tsx @@ -0,0 +1,59 @@ +import { screen } from '@testing-library/react'; + +import { HeatmapTooltip } from '@/components/DBHeatmapChart/HeatmapTooltip'; + +jest.mock('@/useFormatTime', () => ({ + FormatTime: ({ value }: { value: number }) => time:{value}, +})); + +const point = { + xVal: 1000, + yVal: 2, + countVal: 1234, + closestDistance: 0, + closestIndex: 0, + xCoord: 10, + yCoord: 10, + xSize: 5, + ySize: 5, +}; + +const renderTooltip = ( + props: Partial> = {}, +) => + renderWithMantine( + `y=${v}`} + percentile={undefined} + {...props} + />, + ); + +describe('HeatmapTooltip', () => { + it('shows the time, formatted y value and count', () => { + renderTooltip(); + + expect(screen.getByText('time:1000')).toBeInTheDocument(); + expect(screen.getByText('y=2')).toBeInTheDocument(); + expect(screen.getByText('1,234')).toBeInTheDocument(); + }); + + it('appends the percentile when known', () => { + renderTooltip({ percentile: 95.25 }); + + expect(screen.getByText(/y=2 \(p95\.3\)/)).toBeInTheDocument(); + }); + + it('shows the drag hint only when filtering is enabled', () => { + const { unmount } = renderTooltip(); + expect(screen.queryByText(/Drag to Compare/)).not.toBeInTheDocument(); + unmount(); + + renderTooltip({ showDragHint: true }); + expect(screen.getByText(/Drag to Compare/)).toBeInTheDocument(); + }); +}); diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/heatmapAxis.test.ts b/packages/app/src/components/DBHeatmapChart/__tests__/heatmapAxis.test.ts new file mode 100644 index 0000000000..3b4deae2dc --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/__tests__/heatmapAxis.test.ts @@ -0,0 +1,35 @@ +import { + formatHeatmapTick, + logScaleSplits, +} from '@/components/DBHeatmapChart/heatmapAxis'; + +describe('formatHeatmapTick', () => { + it('formats linear values compactly without a number format', () => { + expect(formatHeatmapTick(1500, 'linear', undefined)).toBe('1.5K'); + }); + + it('exponentiates log-space values before formatting', () => { + expect(formatHeatmapTick(Math.log(1000), 'log', undefined)).toBe('1K'); + }); + + it('formats duration outputs from seconds via the factor', () => { + // factor 0.001: values are ms, so 250 -> 250ms + expect( + formatHeatmapTick(250, 'linear', { output: 'duration', factor: 0.001 }), + ).toBe('250ms'); + }); +}); + +describe('logScaleSplits', () => { + it('places splits at powers of 10 within the range', () => { + const splits = logScaleSplits(Math.log(1), Math.log(1000)); + expect(splits.map(Math.exp).map(v => Math.round(v))).toEqual([ + 1, 10, 100, 1000, + ]); + }); + + it('adds ×3 intermediates when the range holds fewer than 3 powers of 10', () => { + const splits = logScaleSplits(Math.log(2), Math.log(40)); + expect(splits.map(Math.exp).map(v => Math.round(v))).toEqual([3, 10, 30]); + }); +}); diff --git a/packages/app/src/components/__tests__/heatmapBuckets.test.ts b/packages/app/src/components/DBHeatmapChart/__tests__/heatmapBounds.test.ts similarity index 85% rename from packages/app/src/components/__tests__/heatmapBuckets.test.ts rename to packages/app/src/components/DBHeatmapChart/__tests__/heatmapBounds.test.ts index a54f003abe..2278cbf4f1 100644 --- a/packages/app/src/components/__tests__/heatmapBuckets.test.ts +++ b/packages/app/src/components/DBHeatmapChart/__tests__/heatmapBounds.test.ts @@ -1,9 +1,8 @@ /** * Tests for heatmap bucket boundary computation algorithm. * - * Validates the logic extracted from DBHeatmapChart's HeatmapContainer — - * specifically the range calculation, effectiveMin capping, and - * bucketToYValue mapping. + * Validates the bounds policy (quantile lower bound, effectiveMin capping) + * and the row placement of the server-side bucket grid. * * Background: The initial PR #1913 used p99 as the max boundary for log scale. * When latency spikes are rare (<1% of spans), they fell above p99 and were @@ -12,19 +11,15 @@ * wide ranges naturally. Future: #1914 adds overflow-bucket indicators. */ -// --------------------------------------------------------------------------- -// Extracted algorithm (mirrors DBHeatmapChart HeatmapContainer) -// --------------------------------------------------------------------------- - -type ScaleType = 'log' | 'linear'; - -/** - * Compute quantile level for the lower bound. - * Upper bound uses actual max() — no quantile needed. - */ -function getQuantileLo(scaleType: ScaleType) { - return scaleType === 'log' ? 0.01 : 0.001; -} +import { + computeEffectiveMin, + heatmapLowQuantile, +} from '@/components/DBHeatmapChart/heatmapBounds'; +import { + gridFromBucketRows, + gridToPlotData, + HeatmapScaleType, +} from '@/components/DBHeatmapChart/heatmapGrid'; /** * Simulate what ClickHouse `quantile(level)(values)` returns. @@ -37,35 +32,6 @@ function quantile(values: number[], level: number): number { return sorted[Math.max(0, idx)]; } -/** - * Compute effectiveMin given the min/max and scale type. - */ -function computeEffectiveMin( - min: number, - max: number, - scaleType: ScaleType, -): number { - return scaleType === 'log' ? Math.max(min, max * 1e-4 || 1e-4) : min; -} - -/** - * Compute the y-value for a given bucket index. - */ -function bucketToYValue( - j: number, - nBuckets: number, - effectiveMin: number, - max: number, - scaleType: ScaleType, -): number { - if (scaleType === 'log' && effectiveMin > 0 && max > effectiveMin) { - const actualValue = - effectiveMin * Math.pow(max / effectiveMin, j / nBuckets); - return Math.log(actualValue); - } - return effectiveMin + j * ((max - effectiveMin) / nBuckets); -} - /** * Simulate ClickHouse widthBucket(value, lo, hi, nBuckets). * Returns 0 for values < lo, nBuckets+1 for values >= hi, @@ -106,10 +72,10 @@ function widthBucketLog( */ function computeHeatmapBuckets( values: number[], - scaleType: ScaleType, + scaleType: HeatmapScaleType, nBuckets = 40, ) { - const qLo = getQuantileLo(scaleType); + const qLo = heatmapLowQuantile(scaleType); const nonNeg = values.filter(v => v >= 0); const min = quantile(nonNeg, qLo); const max = Math.max(...values); // actual max, not quantile @@ -136,7 +102,7 @@ function computeHeatmapBuckets( */ function computeHeatmapBuckets_BUGGY( values: number[], - scaleType: ScaleType, + scaleType: HeatmapScaleType, nBuckets = 40, ) { const qLo = scaleType === 'log' ? 0.01 : 0.001; @@ -206,11 +172,11 @@ function generateLatencyData(opts: { describe('Heatmap bucket boundary algorithm', () => { describe('quantile lower bound selection', () => { it('log scale uses p1 for lower bound', () => { - expect(getQuantileLo('log')).toBe(0.01); + expect(heatmapLowQuantile('log')).toBe(0.01); }); it('linear scale uses p0.1 for lower bound', () => { - expect(getQuantileLo('linear')).toBe(0.001); + expect(heatmapLowQuantile('linear')).toBe(0.001); }); }); @@ -228,22 +194,36 @@ describe('Heatmap bucket boundary algorithm', () => { }); }); - describe('bucketToYValue', () => { + describe('row centers', () => { + // Row j of the server-side grid is centered on the upper bound of + // widthBucket's bucket j. + const rowCenters = ( + effectiveMin: number, + max: number, + scaleType: HeatmapScaleType, + nBuckets: number, + ) => + gridToPlotData( + gridFromBucketRows({ + data: [], + timestampColumn: { name: 'ts', type: 'DateTime' }, + generatedTsBuckets: [new Date(0)], + scaleType, + effectiveMin, + max, + nBuckets, + }), + )[1]; + it('linear: produces uniformly spaced values', () => { - const nBuckets = 10; - const values = Array.from({ length: nBuckets + 1 }, (_, j) => - bucketToYValue(j, nBuckets, 0, 100, 'linear'), - ); - for (let i = 0; i <= nBuckets; i++) { + const values = rowCenters(0, 100, 'linear', 10); + for (let i = 0; i <= 10; i++) { expect(values[i]).toBeCloseTo(i * 10); } }); it('log: produces uniformly spaced values in log space', () => { - const nBuckets = 10; - const values = Array.from({ length: nBuckets + 1 }, (_, j) => - bucketToYValue(j, nBuckets, 1, 1000, 'log'), - ); + const values = rowCenters(1, 1000, 'log', 10); const diffs = values.slice(1).map((v, i) => v - values[i]); for (let i = 1; i < diffs.length; i++) { expect(diffs[i]).toBeCloseTo(diffs[0], 5); diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/heatmapGrid.test.ts b/packages/app/src/components/DBHeatmapChart/__tests__/heatmapGrid.test.ts new file mode 100644 index 0000000000..19412df5ac --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/__tests__/heatmapGrid.test.ts @@ -0,0 +1,221 @@ +import { + computeBucketPercentiles, + gridFromBucketRows, + gridToPlotData, + HeatmapGrid, + rowPlotBounds, +} from '@/components/DBHeatmapChart/heatmapGrid'; + +// CH widthBucket returns buckets 0..nBuckets+1, so each time bucket +// produces nBuckets+2 grid cells. +const N_BUCKETS = 4; +const CELLS_PER_TS = N_BUCKETS + 2; + +const T0 = '2026-07-06T00:00:00Z'; +const T1 = '2026-07-06T01:00:00Z'; + +// Linear scale with effectiveMin=0 and max=nBuckets centers row j on y=j, +// so y-values can be asserted directly. +const baseArgs = { + timestampColumn: { name: '__hdx_time_bucket', type: 'DateTime' }, + generatedTsBuckets: [new Date(T0), new Date(T1)], + scaleType: 'linear' as const, + effectiveMin: 0, + max: N_BUCKETS, + nBuckets: N_BUCKETS, +}; + +// UInt64 counts are returned as strings by ClickHouse +const row = (ts: string, xBucket: number, count: string) => ({ + __hdx_time_bucket: ts, + x_bucket: xBucket, + count, +}); + +describe('gridFromBucketRows', () => { + it('generates a dense zero-filled grid when there is no data', () => { + const grid = gridFromBucketRows({ ...baseArgs, data: [] }); + + expect(grid.times).toEqual([ + new Date(T0).getTime(), + new Date(T1).getTime(), + ]); + expect(grid.stepMs).toBe(60 * 60 * 1000); + expect(grid.yAxis).toEqual({ + type: 'numeric', + scale: 'linear', + edges: [-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5], + }); + expect(grid.cells).toEqual(Array(2 * CELLS_PER_TS).fill(0)); + expect(grid.cellKind).toBe('count'); + }); + + it('places each row count into its (time, x_bucket) cell', () => { + const { cells } = gridFromBucketRows({ + ...baseArgs, + data: [row(T0, 1, '5'), row(T0, 3, '2'), row(T1, 0, '7')], + }); + + // prettier-ignore + expect(cells).toEqual([ + 0, 5, 0, 2, 0, 0, // T0 + 7, 0, 0, 0, 0, 0, // T1 + ]); + }); + + it('tolerates time buckets with no rows at all', () => { + const { cells } = gridFromBucketRows({ + ...baseArgs, + data: [row(T1, 2, '9')], + }); + + // prettier-ignore + expect(cells).toEqual([ + 0, 0, 0, 0, 0, 0, // T0 (empty) + 0, 0, 9, 0, 0, 0, // T1 + ]); + }); + + // A Distributed table can return the same (time, x_bucket) group more than once. + // We drop the duplicate row(s). Summing the duplicate groups may not always be correct, + // since the user sets the aggregation function. This is an unexpected case, so we just + // want to make the behavior sane. + it('drops duplicate (time, x_bucket) groups from unmerged distributed results', () => { + const { cells } = gridFromBucketRows({ + ...baseArgs, + data: [ + row(T0, 1, '5'), + row(T0, 1, '3'), + row(T0, 2, '4'), + row(T1, 1, '6'), + ], + }); + + // The grid stays dense — duplicates collapse into their cell + // prettier-ignore + expect(cells).toEqual([ + 0, 5, 4, 0, 0, 0, // T0: bucket 1 = 5, with the duplicate value + 0, 6, 0, 0, 0, 0, // T1: cells after the duplicate must still render + ]); + }); + + it('centers log-scale rows on evenly spaced log values', () => { + const grid = gridFromBucketRows({ + ...baseArgs, + scaleType: 'log', + effectiveMin: 1, + max: 10_000, + data: [], + }); + + expect(grid.yAxis).toMatchObject({ type: 'numeric', scale: 'log' }); + const [, ys] = gridToPlotData(grid); + // Row j sits on log(10^j): the upper bound of widthBucket's bucket j + for (let j = 0; j < CELLS_PER_TS; j++) { + expect(ys[j]).toBeCloseTo(Math.log(10 ** j)); + } + }); + + it('falls back to linear rows when the log range is unusable', () => { + const grid = gridFromBucketRows({ + ...baseArgs, + scaleType: 'log', + effectiveMin: 0, + data: [], + }); + + expect(grid.yAxis).toMatchObject({ scale: 'linear' }); + }); +}); + +const grid = (overrides: Partial): HeatmapGrid => ({ + times: [1000, 2000], + stepMs: 1000, + yAxis: { type: 'numeric', scale: 'linear', edges: [0, 1, 3] }, + cells: [1, 2, 3, 4], + cellKind: 'count', + ...overrides, +}); + +describe('rowPlotBounds', () => { + it('keeps uneven linear rows as-is', () => { + expect(rowPlotBounds(grid({}).yAxis)).toEqual({ lo: [0, 1], hi: [1, 3] }); + }); + + it('plots log rows in natural-log space', () => { + const { lo, hi } = rowPlotBounds({ + type: 'numeric', + scale: 'log', + edges: [1, 10, 1000], + }); + expect(lo).toEqual([0, Math.log(10)]); + expect(hi).toEqual([Math.log(10), Math.log(1000)]); + }); + + it('draws an open top row as tall as the row below it', () => { + const { lo, hi } = rowPlotBounds({ + type: 'numeric', + scale: 'linear', + edges: [0, 1, 3, Infinity], + openTop: true, + }); + expect(lo).toEqual([0, 1, 3]); + expect(hi).toEqual([1, 3, 5]); + }); + + it('gives each series row a unit-height band', () => { + expect(rowPlotBounds({ type: 'series', labels: ['a', 'b'] })).toEqual({ + lo: [0, 1], + hi: [1, 2], + }); + }); +}); + +describe('gridToPlotData', () => { + it('emits one cell per (time, row) with its center and extent', () => { + const [xs, ys, cells, x0s, x1s, y0s, y1s] = gridToPlotData(grid({})); + + expect(xs).toEqual([1000, 1000, 2000, 2000]); + expect(ys).toEqual([0.5, 2, 0.5, 2]); + expect(cells).toEqual([1, 2, 3, 4]); + expect(x0s).toEqual([500, 500, 1500, 1500]); + expect(x1s).toEqual([1500, 1500, 2500, 2500]); + expect(y0s).toEqual([0, 1, 0, 1]); + expect(y1s).toEqual([1, 3, 1, 3]); + }); +}); + +describe('computeBucketPercentiles', () => { + it('reports the share of events at or below each row, pooled across time buckets', () => { + const percentiles = computeBucketPercentiles( + gridFromBucketRows({ + ...baseArgs, + data: [ + row(T0, 1, '3'), + row(T0, 4, '1'), + row(T1, 1, '5'), + row(T1, 2, '1'), + ], + }), + ); + + expect(percentiles).toEqual( + new Map([ + [0, 0], + [1, 80], // 3 + 5 of the 10 events, both time buckets counted + [2, 90], + [3, 90], // empty bucket carries the share of the one below it + [4, 100], + [5, 100], + ]), + ); + }); + + it('returns no percentiles when the grid holds no events', () => { + // Every cell is 0, so there is no distribution to place a value in + // the tooltip omits the percentile rather than dividing by zero. + expect( + computeBucketPercentiles(gridFromBucketRows({ ...baseArgs, data: [] })), + ).toEqual(new Map()); + }); +}); diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/heatmapPaths.test.ts b/packages/app/src/components/DBHeatmapChart/__tests__/heatmapPaths.test.ts new file mode 100644 index 0000000000..1c95b36748 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/__tests__/heatmapPaths.test.ts @@ -0,0 +1,124 @@ +import type uPlot from 'uplot'; + +import { + gridToPlotData, + HeatmapGrid, +} from '@/components/DBHeatmapChart/heatmapGrid'; +import { heatmapPaths } from '@/components/DBHeatmapChart/heatmapPaths'; + +const mockRect = jest.fn(); +const scaleX = { min: 0, max: 10_000 }; +const scaleY = { min: -100, max: 100 }; + +// Hand the painter identity x positions and an inverted y axis +// (pixel = 100 - 10 * value), the way uPlot orients a canvas. +jest.mock('uplot', () => ({ + __esModule: true, + default: { + orient: ( + _u: unknown, + _seriesIdx: number, + draw: (...args: unknown[]) => void, + ) => + draw( + null, + null, + null, + scaleX, + scaleY, + (v: number) => v, + (v: number) => 100 - 10 * v, + 0, + 0, + 0, + 0, + null, + null, + mockRect, + null, + ), + }, +})); + +class StubPath2D {} + +function paint(grid: HeatmapGrid, fills: number[]) { + const u = { + data: [[], gridToPlotData(grid)], + bbox: { left: 0, top: 0, width: 100, height: 100 }, + ctx: { + save: jest.fn(), + rect: jest.fn(), + clip: jest.fn(), + fill: jest.fn(), + restore: jest.fn(), + }, + }; + const paths = heatmapPaths({ + disp: { fill: { lookup: ['#000', '#fff'], values: () => fills } }, + }); + paths(u as unknown as uPlot, 1, 0, 0); + return u; +} + +describe('heatmapPaths', () => { + beforeAll(() => { + (globalThis as { Path2D?: unknown }).Path2D = StubPath2D; + }); + + beforeEach(() => { + mockRect.mockReset(); + }); + + it('draws each cell from its own extent, so uneven rows keep their heights', () => { + paint( + { + times: [1000], + stepMs: 1000, + yAxis: { type: 'numeric', scale: 'linear', edges: [0, 1, 3] }, + cells: [1, 5], + cellKind: 'count', + }, + [0, 1], + ); + + expect(mockRect.mock.calls.map(([, ...rect]) => rect)).toEqual([ + [500, 100, 1000, -10], // row 0: y 0..1 + [500, 90, 1000, -20], // row 1: y 1..3, twice as tall + ]); + }); + + it('skips empty cells and cells outside the visible scale', () => { + paint( + { + times: [1000, 20_000], + stepMs: 1000, + yAxis: { type: 'numeric', scale: 'linear', edges: [0, 1, 2] }, + cells: [0, 3, 4, 4], + cellKind: 'count', + }, + [-1, 0, 1, 1], + ); + + // Only (t=1000, row 1): row 0 is empty and t=20000 is past scaleX.max + expect(mockRect).toHaveBeenCalledTimes(1); + expect(mockRect.mock.calls[0].slice(1)).toEqual([500, 90, 1000, -10]); + }); + + it('fills each cell into the path for its palette color', () => { + const u = paint( + { + times: [1000], + stepMs: 1000, + yAxis: { type: 'numeric', scale: 'linear', edges: [0, 1, 2] }, + cells: [1, 1], + cellKind: 'count', + }, + [1, 1], + ); + + const [firstPath] = mockRect.mock.calls[0]; + expect(mockRect.mock.calls[1][0]).toBe(firstPath); + expect(u.ctx.fill).toHaveBeenCalledTimes(2); // one per palette color + }); +}); diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/highlightDataPlugin.test.ts b/packages/app/src/components/DBHeatmapChart/__tests__/highlightDataPlugin.test.ts new file mode 100644 index 0000000000..5ef88782a9 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/__tests__/highlightDataPlugin.test.ts @@ -0,0 +1,56 @@ +import type uPlot from 'uplot'; + +import { gridToPlotData } from '@/components/DBHeatmapChart/heatmapGrid'; +import { highlightDataPlugin } from '@/components/DBHeatmapChart/highlightDataPlugin'; + +// Identity x positions; y pixels grow downward (pixel = 100 - 10 * value). +const valToPos = (v: number, axis: 'x' | 'y') => + axis === 'x' ? v : 100 - 10 * v; + +function hover(cursor: { left: number; top: number }) { + const onPointHighlight = jest.fn(); + const plugin = highlightDataPlugin({ proximity: 20, onPointHighlight }); + const u = { + cursor, + over: { offsetLeft: 5, offsetTop: 7 }, + valToPos, + data: [ + [], + gridToPlotData({ + times: [100], + stepMs: 10, + yAxis: { type: 'numeric', scale: 'linear', edges: [0, 1, 3] }, + cells: [2, 6], + cellKind: 'count', + }), + ], + }; + const setCursor = plugin.hooks.setCursor; + if (typeof setCursor !== 'function') throw new Error('no setCursor hook'); + setCursor(u as unknown as uPlot); + return onPointHighlight; +} + +describe('highlightDataPlugin', () => { + it('reports the nearest non-empty cell with its own size', () => { + // Row 1 spans y 1..3, centered at y=2 -> pixel 80 + const onPointHighlight = hover({ left: 100, top: 80 }); + + expect(onPointHighlight).toHaveBeenCalledWith( + expect.objectContaining({ + xVal: 100, + yVal: 2, + countVal: 6, + closestIndex: 1, + xCoord: 105, + yCoord: 87, + xSize: 10, + ySize: 20, + }), + ); + }); + + it('ignores the cursor when no cell is within the proximity', () => { + expect(hover({ left: 500, top: 80 })).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/app/src/components/DBHeatmapChart/__tests__/palette.test.ts b/packages/app/src/components/DBHeatmapChart/__tests__/palette.test.ts new file mode 100644 index 0000000000..e8c5a4f4da --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/__tests__/palette.test.ts @@ -0,0 +1,19 @@ +import { countsToPaletteIndexes } from '@/components/DBHeatmapChart/palette'; + +describe('countsToPaletteIndexes', () => { + it('marks empty cells with -1', () => { + expect(countsToPaletteIndexes([0, 0], 7)).toEqual([-1, -1]); + }); + + it('maps counts onto the palette by square root', () => { + // p95 of [1, 4, 16] is 4, so sqrt(count) / 2 scales the palette + expect(countsToPaletteIndexes([0, 1, 4, 16], 5)).toEqual([-1, 2, 4, 4]); + }); + + it('clamps cells above the p95 ceiling to the top color', () => { + const counts = [...Array(99).fill(1), 1000]; + const indexes = countsToPaletteIndexes(counts, 7); + expect(indexes[99]).toBe(6); + expect(indexes[0]).toBe(6); + }); +}); diff --git a/packages/app/src/components/DBHeatmapChart.test.tsx b/packages/app/src/components/DBHeatmapChart/__tests__/selection.test.ts similarity index 98% rename from packages/app/src/components/DBHeatmapChart.test.tsx rename to packages/app/src/components/DBHeatmapChart/__tests__/selection.test.ts index 4bf406c9cd..5d329fb158 100644 --- a/packages/app/src/components/DBHeatmapChart.test.tsx +++ b/packages/app/src/components/DBHeatmapChart/__tests__/selection.test.ts @@ -1,6 +1,9 @@ import type uPlot from 'uplot'; -import { applySelectionToChart, SelectionBounds } from './DBHeatmapChart'; +import { + applySelectionToChart, + SelectionBounds, +} from '@/components/DBHeatmapChart/selection'; type StubU = { scales: { diff --git a/packages/app/src/components/DBHeatmapChart/heatmapAxis.ts b/packages/app/src/components/DBHeatmapChart/heatmapAxis.ts new file mode 100644 index 0000000000..3aaefde64a --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/heatmapAxis.ts @@ -0,0 +1,70 @@ +import { NumberFormat } from '@/types'; +import { formatDurationMsCompact, formatNumber } from '@/utils'; + +import type { HeatmapScaleType } from './heatmapGrid'; + +/** Format a plot-space y value as a y-axis / tooltip label. */ +export function formatHeatmapTick( + value: number, + scaleType: HeatmapScaleType, + numberFormat: NumberFormat | undefined, +) { + // y-values are stored in log space for log scale; exponentiate back + // to the actual value before formatting. + const actualValue = scaleType === 'log' ? Math.exp(value) : value; + + if (numberFormat?.unit === 'ms' || numberFormat?.output === 'duration') { + const msValue = + numberFormat?.output === 'duration' + ? actualValue * (numberFormat?.factor ?? 1) * 1000 + : actualValue; + return formatDurationMsCompact(msValue); + } + + return numberFormat + ? formatNumber(actualValue, { + ...numberFormat, + average: true, + mantissa: Math.abs(actualValue) >= 1 ? 0 : 2, + }) + : new Intl.NumberFormat('en-US', { + notation: 'compact', + compactDisplay: 'short', + }).format(actualValue); +} + +/** + * Log-scale y-axis splits at powers of 10 (0.01, 0.1, 1, 10, 100…) so labels + * are clean round numbers instead of arbitrary positions in log space. + * `yMin`/`yMax` and the returned splits are natural-log values. + */ +export function logScaleSplits(yMin: number, yMax: number): number[] { + const realMin = Math.exp(yMin); + const realMax = Math.exp(yMax); + const splits: number[] = []; + // Generate powers of 10 within range + const startExp = Math.floor(Math.log10(realMin)); + const endExp = Math.ceil(Math.log10(realMax)); + for (let e = startExp; e <= endExp; e++) { + const v = Math.pow(10, e); + const logV = Math.log(v); + if (logV >= yMin && logV <= yMax) { + splits.push(logV); + } + } + // If too few splits, add intermediate values (×3) + if (splits.length < 3) { + for (let e = startExp; e <= endExp; e++) { + for (const mult of [1, 3]) { + const v = mult * Math.pow(10, e); + const logV = Math.log(v); + if (logV >= yMin && logV <= yMax) { + splits.push(logV); + } + } + } + // Deduplicate and sort + return [...new Set(splits)].sort((a, b) => a - b); + } + return splits; +} diff --git a/packages/app/src/components/DBHeatmapChart/heatmapBounds.ts b/packages/app/src/components/DBHeatmapChart/heatmapBounds.ts new file mode 100644 index 0000000000..5d1db49594 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/heatmapBounds.ts @@ -0,0 +1,27 @@ +import type { HeatmapScaleType } from './heatmapGrid'; + +export const HEATMAP_N_BUCKETS = 80; + +/** + * Quantile used for the y-axis lower bound. A quantile (rather than min) + * keeps near-zero outliers from stretching the log axis. The upper bound + * uses the actual max so that rare latency spikes stay visible; log scale + * already compresses wide ranges. + */ +export function heatmapLowQuantile(scaleType: HeatmapScaleType) { + return scaleType === 'log' ? 0.01 : 0.001; +} + +/** + * Lower bound actually used for bucketing. Log scale needs a positive + * minimum (log(0) is undefined), and the range is capped to ~4 orders of + * magnitude so the axis isn't dominated by a long empty tail of near-zero + * outliers. + */ +export function computeEffectiveMin( + min: number, + max: number, + scaleType: HeatmapScaleType, +) { + return scaleType === 'log' ? Math.max(min, max * 1e-4 || 1e-4) : min; +} diff --git a/packages/app/src/components/DBHeatmapChart/heatmapGrid.ts b/packages/app/src/components/DBHeatmapChart/heatmapGrid.ts new file mode 100644 index 0000000000..5dbea3e348 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/heatmapGrid.ts @@ -0,0 +1,214 @@ +import { ColumnMetaType } from '@hyperdx/common-utils/dist/clickhouse'; + +export type HeatmapScaleType = 'log' | 'linear'; + +export type HeatmapYAxis = + | { + type: 'numeric'; + scale: HeatmapScaleType; + /** `rows + 1` ascending row boundaries in data space. Rows may be uneven. */ + edges: number[]; + /** + * The top row has no upper bound (e.g. a `+Inf` histogram bucket), so + * its last edge is ignored and it is drawn as tall as the row below. + */ + openTop?: boolean; + } + | { type: 'series'; labels: string[] }; + +/** Source-agnostic heatmap data: one column per time bucket, one row per y bucket or series. */ +export type HeatmapGrid = { + /** Column centers, ms ascending. */ + times: number[]; + /** Column width, ms. */ + stepMs: number; + yAxis: HeatmapYAxis; + /** Row-major: `cells[timeIdx * rowCount + rowIdx]`. */ + cells: number[]; + cellKind: 'count' | 'value'; +}; + +export const EMPTY_HEATMAP_GRID: HeatmapGrid = { + times: [], + stepMs: 0, + yAxis: { type: 'numeric', scale: 'linear', edges: [] }, + cells: [], + cellKind: 'count', +}; + +export function heatmapRowCount(yAxis: HeatmapYAxis) { + return yAxis.type === 'series' + ? yAxis.labels.length + : Math.max(0, yAxis.edges.length - 1); +} + +/** + * Lower and upper bound of each row in plot space. The uPlot y scale is + * linear, so log-scale rows are plotted in natural-log space and tick labels + * are exponentiated back. + */ +export function rowPlotBounds(yAxis: HeatmapYAxis): { + lo: number[]; + hi: number[]; +} { + const rows = heatmapRowCount(yAxis); + if (yAxis.type === 'series') { + return { + lo: Array.from({ length: rows }, (_, r) => r), + hi: Array.from({ length: rows }, (_, r) => r + 1), + }; + } + + const toPlot = yAxis.scale === 'log' ? Math.log : (v: number) => v; + const lo = yAxis.edges.slice(0, rows).map(toPlot); + const hi = yAxis.edges.slice(1, rows + 1).map(toPlot); + if (yAxis.openTop && rows > 0) { + const last = rows - 1; + const height = rows > 1 ? hi[last - 1] - lo[last - 1] : 1; + hi[last] = lo[last] + height; + } + return { lo, hi }; +} + +/** + * uPlot mode-2 data: `[xs, ys, cells]` are the facet values (cell centers) + * and counts that uPlot reads; the remaining arrays carry each cell's extent + * for the painter and tooltip. All y values are in plot space. + */ +export type HeatmapPlotData = [ + xs: number[], + ys: number[], + cells: number[], + x0s: number[], + x1s: number[], + y0s: number[], + y1s: number[], +]; + +export function gridToPlotData(grid: HeatmapGrid): HeatmapPlotData { + const rows = heatmapRowCount(grid.yAxis); + const { lo, hi } = rowPlotBounds(grid.yAxis); + const halfStep = grid.stepMs / 2; + + const xs: number[] = []; + const ys: number[] = []; + const x0s: number[] = []; + const x1s: number[] = []; + const y0s: number[] = []; + const y1s: number[] = []; + for (const t of grid.times) { + for (let r = 0; r < rows; r++) { + xs.push(t); + ys.push((lo[r] + hi[r]) / 2); + x0s.push(t - halfStep); + x1s.push(t + halfStep); + y0s.push(lo[r]); + y1s.push(hi[r]); + } + } + return [xs, ys, grid.cells, x0s, x1s, y0s, y1s]; +} + +/** + * Grid for the rows of the server-side `widthBucket` query. widthBucket + * returns buckets 0 (underflow) through nBuckets + 1 (overflow), so each time + * bucket has nBuckets + 2 rows. + */ +export function gridFromBucketRows({ + data, + timestampColumn, + generatedTsBuckets, + scaleType, + effectiveMin, + max, + nBuckets, +}: { + data: Record[]; + timestampColumn: ColumnMetaType; + generatedTsBuckets: Date[]; + scaleType: HeatmapScaleType; + effectiveMin: number; + max: number; + nBuckets: number; +}): HeatmapGrid { + const isLog = scaleType === 'log' && effectiveMin > 0 && max > effectiveMin; + const bucketValue = (j: number) => + isLog + ? effectiveMin * Math.pow(max / effectiveMin, j / nBuckets) + : effectiveMin + j * ((max - effectiveMin) / nBuckets); + + // Row j is centered on bucketValue(j), which is the upper bound of + // widthBucket's bucket j, so the row boundaries sit half a bucket either + // side of it. + const rows = nBuckets + 2; + const edges = Array.from({ length: rows + 1 }, (_, k) => + bucketValue(k - 0.5), + ); + + const times = generatedTsBuckets.map(d => d.getTime()); + const cells: number[] = []; + + let dataIndex = 0; + for (const generatedTs of times) { + for (let j = 0; j < rows; j++) { + const row = data[dataIndex]; + + if ( + row != null && + new Date(row[timestampColumn.name]).getTime() == generatedTs && + row['x_bucket'] == j + ) { + cells.push(Number.parseInt(row['count'], 10)); // UInt64 returns as string + + // Skip duplicate buckets (from unmerged distributed table results) + while ( + dataIndex < data.length && + new Date(data[dataIndex][timestampColumn.name]).getTime() == + generatedTs && + data[dataIndex]['x_bucket'] == j + ) { + dataIndex++; + } + } else { + cells.push(0); + } + } + } + + return { + times, + stepMs: times.length > 1 ? times[1] - times[0] : 0, + yAxis: { type: 'numeric', scale: isLog ? 'log' : 'linear', edges }, + cells, + cellKind: 'count', + }; +} + +/** + * Cumulative share (0-100) of events at or below each row, keyed by row + * index and pooled across all time buckets. + */ +export function computeBucketPercentiles( + grid: HeatmapGrid, +): Map { + const rows = heatmapRowCount(grid.yAxis); + const rowTotals = new Array(rows).fill(0); + let total = 0; + for (let i = 0; i < grid.cells.length; i++) { + rowTotals[i % rows] += grid.cells[i]; + total += grid.cells[i]; + } + + const percentiles = new Map(); + if (total === 0) { + return percentiles; + } + + let cumulative = 0; + rowTotals.forEach((rowTotal, r) => { + cumulative += rowTotal; + percentiles.set(r, (cumulative / total) * 100); + }); + + return percentiles; +} diff --git a/packages/app/src/components/DBHeatmapChart/heatmapPaths.ts b/packages/app/src/components/DBHeatmapChart/heatmapPaths.ts new file mode 100644 index 0000000000..2a431531cc --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/heatmapPaths.ts @@ -0,0 +1,160 @@ +import uPlot from 'uplot'; + +import type { HeatmapPlotData } from './heatmapGrid'; +import { makeCountsToFills } from './palette'; + +// Adapted from: https://github.com/leeoniya/uPlot/blob/a4edb297a9b80baf781f4d05a40fb52fae737bff/demos/latency-heatmap.html#L436 +// Each cell is drawn from its own extent (x0..x1, y0..y1) rather than from a +// bin size inferred from the data layout, so rows may have uneven heights. +export function heatmapPaths(opts: { + disp: { fill: { lookup: string[]; values: any } }; +}) { + const { disp } = opts; + + return (u: uPlot, seriesIdx: number, _idx0: number, _idx1: number) => { + uPlot.orient( + u, + seriesIdx, + ( + _series, + _dataX, + _dataY, + scaleX, + scaleY, + valToPosX, + valToPosY, + xOff, + yOff, + xDim, + yDim, + _moveTo, + _lineTo, + rect, + _arc, + ) => { + // mode 2 data format is not supported in types properly + const d = u.data[seriesIdx] as unknown as HeatmapPlotData; + const [xs, ys, counts, x0s, x1s, y0s, y1s] = d; + + // fill colors are mapped from interpolating densities / counts along some gradient + // (should be quantized to 64 colors/levels max. e.g. 16) + const fills = disp.fill.values(u, seriesIdx); + const fillPalette = disp.fill.lookup ?? [...new Set(fills)]; + const fillPaths = fillPalette.map(() => new Path2D()); + + for (let i = 0; i < xs.length; i++) { + // filter out 0 counts and out of view + if ( + counts[i] > 0 && + xs[i] >= (scaleX.min ?? -Infinity) && + xs[i] <= (scaleX.max ?? Infinity) && + ys[i] >= (scaleY.min ?? -Infinity) && + ys[i] <= (scaleY.max ?? Infinity) + ) { + const left = valToPosX(x0s[i], scaleX, xDim, xOff); + const right = valToPosX(x1s[i], scaleX, xDim, xOff); + const bottom = valToPosY(y0s[i], scaleY, yDim, yOff); + const top = valToPosY(y1s[i], scaleY, yDim, yOff); + + rect( + fillPaths[fills[i]], + Math.round(left), + Math.round(bottom), + right - left, + top - bottom, + ); + } + } + + u.ctx.save(); + + u.ctx.rect(u.bbox.left, u.bbox.top, u.bbox.width, u.bbox.height); + u.ctx.clip(); + + fillPaths.forEach((p, i) => { + u.ctx.fillStyle = fillPalette[i]; + u.ctx.fill(p); + }); + u.ctx.restore(); + }, + ); + + return null; + }; +} + +const axis: uPlot.Axis = { + stroke: 'rgba(102,102,102,1)', // color of the axis line + font: '12px IBM Plex Mono, monospace', + grid: { + show: true, // show grid lines + stroke: 'rgba(52,58,64)', // grid line color + dash: [3, 3], + width: 1, + }, + border: { + show: true, + // stroke: 'rgba(82,82,82)', // grid line color + stroke: 'rgba(102,102,102,1)', // color of the axis line + width: 1, + }, +}; + +export const baseHeatmapOptions: uPlot.Options = { + width: 1500, + height: 600, + mode: 2, + ms: 1, + padding: [8, 8, 0, 4], + legend: { + show: false, + }, + scales: { + x: { + time: true, + }, + }, + axes: [ + { + ...axis, + gap: 10, + space: 60, + }, + { + ...axis, + }, + ], + series: [ + {}, + { + label: 'Latency', + // paths and fill colors are set dynamically per theme in the + // HeatmapPlot component's useMemo — see buildSeriesForPalette(). + facets: [ + { + scale: 'x', + auto: true, + sorted: 1, + }, + { + scale: 'y', + auto: true, + }, + ], + }, + ], +}; + +/** Build the series[1] overrides for a given palette. */ +export function buildSeriesForPalette(colors: string[]): Partial { + return { + paths: heatmapPaths({ + disp: { + fill: { + lookup: colors, + values: makeCountsToFills(colors), + }, + }, + }), + }; +} diff --git a/packages/app/src/components/DBHeatmapChart/heatmapQueries.ts b/packages/app/src/components/DBHeatmapChart/heatmapQueries.ts new file mode 100644 index 0000000000..2cc4ecf1eb --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/heatmapQueries.ts @@ -0,0 +1,217 @@ +import { + BuilderChartConfigWithDateRange, + DisplayType, +} from '@hyperdx/common-utils/dist/types'; + +import { isAggregateFunction } from '@/ChartUtils'; + +import { heatmapLowQuantile } from './heatmapBounds'; +import type { HeatmapScaleType } from './heatmapGrid'; + +export type HeatmapChartConfig = { + displayType: DisplayType.Heatmap; + select: [ + { + aggFn: 'heatmap'; + valueExpression: string; + countExpression?: string; + }, + ]; + from: BuilderChartConfigWithDateRange['from']; + where: BuilderChartConfigWithDateRange['where']; + dateRange: BuilderChartConfigWithDateRange['dateRange']; + granularity: BuilderChartConfigWithDateRange['granularity']; + timestampValueExpression: BuilderChartConfigWithDateRange['timestampValueExpression']; + numberFormat?: BuilderChartConfigWithDateRange['numberFormat']; + filters?: BuilderChartConfigWithDateRange['filters']; + connection: string; + with?: BuilderChartConfigWithDateRange['with']; +}; + +/** Build a HeatmapChartConfig from a builder chart config that has heatmap extras on select[0]. */ +export function toHeatmapChartConfig(config: BuilderChartConfigWithDateRange): { + heatmapConfig: HeatmapChartConfig; + scaleType: HeatmapScaleType; +} { + const firstSelect = Array.isArray(config.select) + ? config.select[0] + : undefined; + return { + heatmapConfig: { + ...config, + displayType: DisplayType.Heatmap, + select: [ + { + aggFn: 'heatmap' as const, + valueExpression: firstSelect?.valueExpression ?? '', + countExpression: firstSelect?.countExpression, + }, + ], + granularity: 'auto', + numberFormat: config.numberFormat, + }, + scaleType: firstSelect?.heatmapScaleType ?? 'log', + }; +} + +/** + * Build the bounds (min/max) ChartConfig that runs first. Result feeds + * `effectiveMin`/`max` into `buildHeatmapBucketConfig`. + */ +export function buildHeatmapBoundsConfig({ + config, + scaleType, +}: { + config: HeatmapChartConfig; + scaleType: HeatmapScaleType; +}): BuilderChartConfigWithDateRange { + const valueExpression = config.select[0].valueExpression; + const isAggregateExpression = isAggregateFunction(valueExpression); + const qLo = heatmapLowQuantile(scaleType); + + return isAggregateExpression + ? { + ...config, + where: '', + orderBy: undefined, + granularity: undefined, + select: [ + { + aggFn: 'quantile' as const, + level: qLo, + aggCondition: `value_calc >= 0`, + aggConditionLanguage: 'sql', + valueExpression: 'value_calc', + alias: 'min', + }, + { + aggFn: 'max' as const, + valueExpression: 'value_calc', + alias: 'max', + }, + ], + with: [ + { + name: 'min_max_calc', + chartConfig: { + ...config, + select: [{ valueExpression, alias: 'value_calc' }], + orderBy: undefined, + }, + }, + ], + timestampValueExpression: '__hdx_time_bucket', + from: { databaseName: '', tableName: 'min_max_calc' }, + } + : { + ...config, + orderBy: undefined, + granularity: undefined, + select: [ + { + aggFn: 'quantile' as const, + level: qLo, + valueExpression, + aggCondition: `${valueExpression} >= 0`, + aggConditionLanguage: 'sql', + alias: 'min', + }, + { + aggFn: 'max' as const, + valueExpression, + alias: 'max', + }, + ], + }; +} + +/** + * Build the bucketed-counts ChartConfig that runs second. `effectiveMin`/`max` + * are usually numbers (resolved from the bounds query), but accept strings so + * callers — like the editor's SQL preview — can pass placeholder tokens + * (e.g. `'{min}'`) before the bounds are known. + */ +export function buildHeatmapBucketConfig({ + config, + scaleType, + effectiveMin, + max, + granularity, + nBuckets, +}: { + config: HeatmapChartConfig; + scaleType: HeatmapScaleType; + effectiveMin: string | number; + max: string | number; + granularity: string; + nBuckets: number; +}): BuilderChartConfigWithDateRange { + const valueExpression = config.select[0].valueExpression; + const countExpression = config.select[0].countExpression ?? 'count()'; + const isAggregateExpression = isAggregateFunction(valueExpression); + + const bucketExprAgg = + scaleType === 'log' + ? `widthBucket(log(greatest(toFloat64(value_calc), ${effectiveMin})), log(${effectiveMin}), log(${max}), ${nBuckets})` + : `widthBucket(value_calc, ${effectiveMin}, ${max}, ${nBuckets})`; + const bucketExprDirect = + scaleType === 'log' + ? `widthBucket(log(greatest(toFloat64(${valueExpression}), ${effectiveMin})), log(${effectiveMin}), log(${max}), ${nBuckets})` + : `widthBucket(${valueExpression}, ${effectiveMin}, ${max}, ${nBuckets})`; + + return isAggregateExpression + ? { + ...config, + where: '', + select: [ + { + valueExpression: 'sum(value_count)', + alias: 'count', + }, + ], + groupBy: [ + { + valueExpression: bucketExprAgg, + alias: 'x_bucket', + }, + ], + with: [ + { + name: 'bucket_calc', + chartConfig: { + ...config, + select: [ + { valueExpression, alias: 'value_calc' }, + { + valueExpression: countExpression, + alias: 'value_count', + }, + ], + granularity, + orderBy: undefined, + }, + }, + ], + timestampValueExpression: '__hdx_time_bucket', + from: { databaseName: '', tableName: 'bucket_calc' }, + orderBy: [{ valueExpression: 'x_bucket', ordering: 'ASC' }], + granularity, + } + : { + ...config, + select: [ + { + valueExpression: countExpression, + alias: 'count', + }, + ], + groupBy: [ + { + valueExpression: bucketExprDirect, + alias: 'x_bucket', + }, + ], + orderBy: [{ valueExpression: 'x_bucket', ordering: 'ASC' }], + granularity, + }; +} diff --git a/packages/app/src/components/DBHeatmapChart/highlightDataPlugin.ts b/packages/app/src/components/DBHeatmapChart/highlightDataPlugin.ts new file mode 100644 index 0000000000..a26e2572c2 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/highlightDataPlugin.ts @@ -0,0 +1,89 @@ +import type { Plugin } from 'uplot'; +import type uPlot from 'uplot'; + +import type { HeatmapPlotData } from './heatmapGrid'; + +export type HighlightedPoint = { + // data point values + xVal: number; + yVal: number; + countVal: number; + // distance data + closestDistance: number; + closestIndex: number; + // bounding box for data point in css parent unit + xCoord: number; + yCoord: number; + xSize: number; + ySize: number; +}; + +export function highlightDataPlugin({ + proximity, + onPointHighlight, +}: { + proximity: number; + onPointHighlight: (point: HighlightedPoint) => void; +}): Plugin { + let lastDataPointIndex = -1; // Used to prevent duplicate calls + return { + hooks: { + setCursor: (u: uPlot) => { + const { top, left } = u.cursor; + if (top == null || left == null) { + return; + } + + const [xs, ys, counts, x0s, x1s, y0s, y1s] = u + .data[1] as unknown as HeatmapPlotData; + + let closestIndex = 0; + let closestDistance = Infinity; + + for (let i = 0; i < xs.length; i++) { + const xPx = u.valToPos(xs[i], 'x'); + const yPx = u.valToPos(ys[i], 'y'); + + const distance = Math.sqrt((xPx - left) ** 2 + (yPx - top) ** 2); + + if (distance < closestDistance && counts[i] > 0) { + closestIndex = i; + closestDistance = distance; + } + } + + const xSize = Math.abs( + u.valToPos(x1s[closestIndex], 'x') - + u.valToPos(x0s[closestIndex], 'x'), + ); + const ySize = Math.abs( + u.valToPos(y1s[closestIndex], 'y') - + u.valToPos(y0s[closestIndex], 'y'), + ); + + const xCoord = u.valToPos(xs[closestIndex], 'x'); + const yCoord = u.valToPos(ys[closestIndex], 'y'); + + const { offsetLeft, offsetTop } = u.over; + + if ( + closestDistance < proximity && + closestIndex !== lastDataPointIndex + ) { + lastDataPointIndex = closestIndex; + onPointHighlight({ + xVal: xs[closestIndex], + yVal: ys[closestIndex], + countVal: counts[closestIndex], + closestDistance, + closestIndex, + xCoord: xCoord + offsetLeft, + yCoord: yCoord + offsetTop, + xSize, + ySize, + }); + } + }, + }, + }; +} diff --git a/packages/app/src/components/DBHeatmapChart/index.ts b/packages/app/src/components/DBHeatmapChart/index.ts new file mode 100644 index 0000000000..42f313fdbd --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/index.ts @@ -0,0 +1,11 @@ +export { ColorLegend } from './ColorLegend'; +export { default } from './DBHeatmapChart'; +export { HEATMAP_N_BUCKETS } from './heatmapBounds'; +export type { HeatmapScaleType } from './heatmapGrid'; +export { + buildHeatmapBoundsConfig, + buildHeatmapBucketConfig, + toHeatmapChartConfig, +} from './heatmapQueries'; +export { darkPalette, lightPalette } from './palette'; +export type { SelectionBounds } from './selection'; diff --git a/packages/app/src/components/DBHeatmapChart/palette.ts b/packages/app/src/components/DBHeatmapChart/palette.ts new file mode 100644 index 0000000000..e935c93170 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/palette.ts @@ -0,0 +1,78 @@ +import type uPlot from 'uplot'; + +// Theme-specific palettes. Red is deliberately avoided at the high end so +// it can be reserved for error overlays in the future. +// Dark theme: starts at a luminant indigo visible on dark bg, ends at bright amber. +// Light theme: starts at a saturated medium blue visible on white, ends at deep orange. +export const darkPalette = [ + '#7b6cf6', // indigo (low) + '#5a9cf6', // sky blue + '#38c9a0', // teal + '#6cd44a', // green + '#c4d629', // lime + '#f0c528', // gold + '#f5a623', // amber (high) +]; +export const lightPalette = [ + '#2a6fb5', // medium blue (low) + '#2a96a8', // teal + '#33a85e', // green + '#7db832', // lime + '#c4a820', // dark gold + '#e08a17', // orange + '#d46a12', // deep orange (high) +]; + +/** Map each cell's count to a palette index, or -1 for empty cells. */ +export function countsToPaletteIndexes( + counts: number[], + paletteSize: number, +): number[] { + const dlen = counts.length; + + // Collect non-zero counts and sort to find a robust normalization ceiling. + // Using p95 instead of max prevents a single hot cell from washing out the + // rest of the chart, while still preserving cross-column comparability. + const nonZero: number[] = []; + for (let i = 0; i < dlen; i++) { + if (counts[i] > 0) nonZero.push(counts[i]); + } + nonZero.sort((a, b) => a - b); + + const indexedFills = Array(dlen); + + if (nonZero.length === 0) { + indexedFills.fill(-1); + return indexedFills; + } + + const p95Idx = Math.floor((nonZero.length - 1) * 0.95); + const p95 = nonZero[p95Idx] ?? nonZero[nonZero.length - 1]; + const sqrtCeiling = Math.sqrt(p95); + + for (let i = 0; i < dlen; i++) { + indexedFills[i] = + counts[i] === 0 + ? -1 + : Math.max( + Math.min( + paletteSize - 1, + Math.floor( + (Math.sqrt(counts[i]) / (sqrtCeiling || 1)) * (paletteSize - 1), + ), + ), + 0, + ); + } + + return indexedFills; +} + +export function makeCountsToFills(colors: string[]) { + return (u: uPlot, seriesIdx: number) => + // mode 2 data format is not supported in types properly + countsToPaletteIndexes( + u.data[seriesIdx][2] as unknown as number[], + colors.length, + ); +} diff --git a/packages/app/src/components/DBHeatmapChart/selection.ts b/packages/app/src/components/DBHeatmapChart/selection.ts new file mode 100644 index 0000000000..1ca1e84693 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/selection.ts @@ -0,0 +1,82 @@ +import type uPlot from 'uplot'; + +import type { HeatmapScaleType } from './heatmapGrid'; + +/** + * Drag-select bounds in data space: x in seconds (URL convention), y in + * the y-axis's natural unit (NOT log-space; callers pass the actual value + * users would expect to see, e.g. ms latency). yMin may be 0 when the + * selection touched the bottom bucket; the renderer clamps to the chart's + * visible y-axis floor. + */ +export type SelectionBounds = { + xMin: number; + xMax: number; + yMin: number; + yMax: number; +}; + +/** + * Reapply a persisted selection rectangle to a uPlot instance. Called on + * chart create and whenever the bounds prop changes; uPlot's `u.select` + * is owned by the chart, so it gets wiped on any chart recreation. We use + * the URL-backed bounds as the source of truth and mirror them onto the + * chart imperatively. fireHook=false avoids re-entering the setSelect hook. + */ +export function applySelectionToChart( + u: uPlot, + bounds: SelectionBounds | null | undefined, + scaleType: HeatmapScaleType, +) { + // The clear path runs BEFORE the scale-not-populated guard below so a + // null bounds always clears the rectangle, even on first paint when + // u.scales.y is not yet populated. Keep this ordering when refactoring; + // swapping it would suppress clears while scales are loading. + if (bounds == null) { + u.setSelect({ left: 0, top: 0, width: 0, height: 0 }, false); + return; + } + + const { xMin, xMax, yMin, yMax } = bounds; + + // x is in seconds in the URL; uPlot's x-axis is configured ms:1 so + // values are stored as ms. + const xMinPx = u.valToPos(xMin * 1000, 'x'); + const xMaxPx = u.valToPos(xMax * 1000, 'x'); + + const yScaleMin = u.scales.y?.min; + const yScaleMax = u.scales.y?.max; + if (yScaleMin == null || yScaleMax == null) { + return; + } + + // For log scale, y-axis values are stored in log-space. Convert and + // clamp to the visible axis: yMin may be 0 (bottom-bucket adjustment in + // DBHeatmapChart's onFilter wrapper); yMax may exceed the axis. + let yLowPlot: number; + let yHighPlot: number; + if (scaleType === 'log') { + yHighPlot = yMax > 0 ? Math.min(Math.log(yMax), yScaleMax) : yScaleMax; + yLowPlot = yMin > 0 ? Math.max(Math.log(yMin), yScaleMin) : yScaleMin; + } else { + yHighPlot = Math.min(yMax, yScaleMax); + yLowPlot = Math.max(yMin, yScaleMin); + } + + // uPlot's y-axis: high data values map to small pixel y (top of chart). + const yHighPx = u.valToPos(yHighPlot, 'y'); + const yLowPx = u.valToPos(yLowPlot, 'y'); + + const left = Math.min(xMinPx, xMaxPx); + const right = Math.max(xMinPx, xMaxPx); + + u.setSelect( + { + left, + top: yHighPx, + width: right - left, + height: Math.max(0, yLowPx - yHighPx), + }, + false, + ); +} diff --git a/packages/app/src/components/DBHeatmapChart/useHeatmapData.ts b/packages/app/src/components/DBHeatmapChart/useHeatmapData.ts new file mode 100644 index 0000000000..78ebbf6800 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/useHeatmapData.ts @@ -0,0 +1,146 @@ +import { useMemo, useState } from 'react'; +import { inferTimestampColumn } from '@hyperdx/common-utils/dist/clickhouse'; +import { convertDateRangeToGranularityString } from '@hyperdx/common-utils/dist/core/utils'; + +import { timeBucketByGranularity } from '@/ChartUtils'; +import { useQueriedChartConfig } from '@/hooks/useChartConfig'; + +import { computeEffectiveMin, HEATMAP_N_BUCKETS } from './heatmapBounds'; +import { + EMPTY_HEATMAP_GRID, + gridFromBucketRows, + HeatmapGrid, + HeatmapScaleType, +} from './heatmapGrid'; +import { + buildHeatmapBoundsConfig, + buildHeatmapBucketConfig, + HeatmapChartConfig, +} from './heatmapQueries'; + +export type HeatmapView = { + grid: HeatmapGrid; + generatedTsBuckets: Date[]; + effectiveMin: number; + scaleType: HeatmapScaleType; +}; + +/** + * Query a heatmap and shape it into a `HeatmapGrid`. While a refresh is in + * flight, `view` stays on the last settled result. + */ +export function useHeatmapData({ + config, + scaleType, + enabled, +}: { + config: HeatmapChartConfig; + scaleType: HeatmapScaleType; + enabled: boolean; +}) { + const dateRange = config.dateRange; + const granularity = convertDateRangeToGranularityString(dateRange, 245); + + const nBuckets = HEATMAP_N_BUCKETS; + + // Future: #1914 adds overflow-bucket indicators for smarter range + // clamping without hiding spikes. + const minMaxConfig = buildHeatmapBoundsConfig({ config, scaleType }); + + const { + data: minMaxData, + isLoading: isMinMaxLoading, + isPlaceholderData: isMinMaxPlaceholderData, + error: minMaxError, + } = useQueriedChartConfig(minMaxConfig, { + queryKey: ['heatmap', minMaxConfig], + enabled: enabled, + placeholderData: prev => prev, + }); + + // UInt64 are returned as strings; quantile returns floats + const min = Number.parseFloat(minMaxData?.data?.[0]?.['min'] ?? '0'); + const max = Number.parseFloat(minMaxData?.data?.[0]?.['max'] ?? '0'); + + const effectiveMin = computeEffectiveMin(min, max, scaleType); + + const bucketConfig = buildHeatmapBucketConfig({ + config, + scaleType, + effectiveMin, + max, + granularity, + nBuckets, + }); + + const canQueryBuckets = + !!minMaxData && bucketConfig != null && max > effectiveMin; + const { data, isLoading, isPlaceholderData, error } = useQueriedChartConfig( + bucketConfig, + { + queryKey: ['heatmap_bucket', bucketConfig], + // Wait for fresh bounds, so a refresh doesn't also query the new range + // bucketed with the previous range's min/max. + enabled: canQueryBuckets && !isMinMaxPlaceholderData, + // Only keep the previous buckets while this query can still run, so an + // empty refreshed range shows "Not enough data points" instead of + // pulsing on stale buckets. + placeholderData: canQueryBuckets ? prev => prev : undefined, + }, + ); + // A refresh keeps the previous heatmap on screen and pulses until both + // queries have fresh data. + const isRefreshing = isMinMaxPlaceholderData || isPlaceholderData; + + // Memoize so timeBucketByGranularity's fresh Date[] doesn't defeat + // the grid memoization downstream. dateRange itself may be a + // fresh array each render, so depend on primitive ms + granularity. + const fromMs = dateRange[0]?.getTime() ?? 0; + const toMs = dateRange[1]?.getTime() ?? 0; + const generatedTsBuckets = useMemo( + () => timeBucketByGranularity(dateRange[0], dateRange[1], granularity), + // eslint-disable-next-line react-hooks/exhaustive-deps + [fromMs, toMs, granularity], + ); + + // A stable grid lets uplot-react skip its setData path when only + // URL-state (xMin/xMax/yMin/yMax) changed. Pairs with the selectionBounds + // prop on HeatmapPlot: the prop reapplies u.select on any chart + // recreation, this memo prevents the recreation in the common case. + const grid = useMemo(() => { + const timestampColumn = inferTimestampColumn(data?.meta ?? []); + if (data == null || data.data == null || timestampColumn == null) + return EMPTY_HEATMAP_GRID; + + return gridFromBucketRows({ + data: data.data, + timestampColumn, + generatedTsBuckets, + scaleType, + effectiveMin, + max, + nBuckets, + }); + }, [data, generatedTsBuckets, scaleType, effectiveMin, max, nBuckets]); + + // While refreshing, keep drawing the last settled heatmap. The previous + // bucket rows only line up with the time buckets, bounds and scale they were + // queried with, so re-plotting them on the new range would draw a blank or + // mis-scaled grid. + const currentView = useMemo( + () => ({ grid, generatedTsBuckets, effectiveMin, scaleType }), + [grid, generatedTsBuckets, effectiveMin, scaleType], + ); + const [settledView, setSettledView] = useState(currentView); + if (!isRefreshing && settledView !== currentView) { + setSettledView(currentView); + } + const view = isRefreshing ? settledView : currentView; + + return { + view, + isLoading: isLoading || isMinMaxLoading, + isRefreshing, + error: error || minMaxError, + }; +} diff --git a/packages/app/src/components/Search/DBSearchHeatmapChart.tsx b/packages/app/src/components/Search/DBSearchHeatmapChart.tsx index 815712964a..4fd1a0424a 100644 --- a/packages/app/src/components/Search/DBSearchHeatmapChart.tsx +++ b/packages/app/src/components/Search/DBSearchHeatmapChart.tsx @@ -174,6 +174,7 @@ export function DBSearchHeatmapChart({ variant="subtle" size="sm" onClick={settingsHandlers.open} + data-testid="heatmap-settings-button" style={{ position: 'absolute', top: 4, diff --git a/packages/app/src/components/__tests__/DBHeatmapChart.test.ts b/packages/app/src/components/__tests__/DBHeatmapChart.test.ts deleted file mode 100644 index b2382f76b1..0000000000 --- a/packages/app/src/components/__tests__/DBHeatmapChart.test.ts +++ /dev/null @@ -1,149 +0,0 @@ -import { - computeBucketPercentiles, - formatDataForHeatmap, -} from '@/components/DBHeatmapChart'; - -// CH widthBucket returns buckets 0..nBuckets+1, so each time bucket -// produces nBuckets+2 grid cells. -const N_BUCKETS = 4; -const CELLS_PER_TS = N_BUCKETS + 2; - -const T0 = '2026-07-06T00:00:00Z'; -const T1 = '2026-07-06T01:00:00Z'; - -// Linear scale with effectiveMin=0 and max=nBuckets makes -// bucketToYValue(j) === j, so y-values can be asserted directly. -const baseArgs = { - timestampColumn: { name: '__hdx_time_bucket', type: 'DateTime' }, - generatedTsBuckets: [new Date(T0), new Date(T1)], - scaleType: 'linear' as const, - effectiveMin: 0, - max: N_BUCKETS, - nBuckets: N_BUCKETS, -}; - -// UInt64 counts are returned as strings by ClickHouse -const row = (ts: string, xBucket: number, count: string) => ({ - __hdx_time_bucket: ts, - x_bucket: xBucket, - count, -}); - -describe('formatDataForHeatmap', () => { - it('generates a dense zero-filled grid when there is no data', () => { - const [time, bucket, count] = formatDataForHeatmap({ - ...baseArgs, - data: [], - }); - - expect(time).toEqual([ - ...Array(CELLS_PER_TS).fill(new Date(T0).getTime()), - ...Array(CELLS_PER_TS).fill(new Date(T1).getTime()), - ]); - expect(bucket).toEqual([0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]); - expect(count).toEqual(Array(2 * CELLS_PER_TS).fill(0)); - }); - - it('places each row count into its (time, x_bucket) cell', () => { - const [time, bucket, count] = formatDataForHeatmap({ - ...baseArgs, - data: [row(T0, 1, '5'), row(T0, 3, '2'), row(T1, 0, '7')], - }); - - expect(time).toHaveLength(2 * CELLS_PER_TS); - expect(bucket).toHaveLength(2 * CELLS_PER_TS); - // prettier-ignore - expect(count).toEqual([ - 0, 5, 0, 2, 0, 0, // T0 - 7, 0, 0, 0, 0, 0, // T1 - ]); - }); - - it('tolerates time buckets with no rows at all', () => { - const [, , count] = formatDataForHeatmap({ - ...baseArgs, - data: [row(T1, 2, '9')], - }); - - // prettier-ignore - expect(count).toEqual([ - 0, 0, 0, 0, 0, 0, // T0 (empty) - 0, 0, 9, 0, 0, 0, // T1 - ]); - }); - - // A Distributed table can return the same (time, x_bucket) group more than once. - // We drop the duplicate row(s). Summing the duplicate groups may not always be correct, - // since the user sets the aggregation function. This is an unexpected case, so we just - // want to make the behavior sane. - it('drops duplicate (time, x_bucket) groups from unmerged distributed results', () => { - const [time, bucket, count] = formatDataForHeatmap({ - ...baseArgs, - data: [ - row(T0, 1, '5'), - row(T0, 1, '3'), - row(T0, 2, '4'), - row(T1, 1, '6'), - ], - }); - - // The grid stays dense — duplicates collapse into their cell - expect(time).toHaveLength(2 * CELLS_PER_TS); - expect(bucket).toHaveLength(2 * CELLS_PER_TS); - // prettier-ignore - expect(count).toEqual([ - 0, 5, 4, 0, 0, 0, // T0: bucket 1 = 5, with the duplicate value - 0, 6, 0, 0, 0, 0, // T1: cells after the duplicate must still render - ]); - }); -}); - -describe('computeBucketPercentiles', () => { - it('reports the share of events at or below each bucket, pooled across time buckets', () => { - const percentiles = computeBucketPercentiles( - formatDataForHeatmap({ - ...baseArgs, - data: [ - row(T0, 1, '3'), - row(T0, 4, '1'), - row(T1, 1, '5'), - row(T1, 2, '1'), - ], - }), - ); - - expect(percentiles).toEqual( - new Map([ - [0, 0], - [1, 80], // 3 + 5 of the 10 events, both time buckets counted - [2, 90], - [3, 90], // empty bucket carries the share of the one below it - [4, 100], - [5, 100], - ]), - ); - }); - - it('returns no percentiles when the grid holds no events', () => { - // Every cell is 0, so there is no distribution to place a value in - // the tooltip omits the percentile rather than dividing by zero. - expect( - computeBucketPercentiles(formatDataForHeatmap({ ...baseArgs, data: [] })), - ).toEqual(new Map()); - }); - - it('orders buckets by y-value rather than by the order they arrive in', () => { - const percentiles = computeBucketPercentiles([ - [0, 0], // times, unused - [5, 1], // y-values, highest first - [1, 3], // counts - ]); - - expect(percentiles).toEqual( - new Map([ - [1, 75], - [5, 100], - ]), - ); - }); -}); diff --git a/packages/app/tests/e2e/components/HeatmapComponent.ts b/packages/app/tests/e2e/components/HeatmapComponent.ts new file mode 100644 index 0000000000..2bf8183a79 --- /dev/null +++ b/packages/app/tests/e2e/components/HeatmapComponent.ts @@ -0,0 +1,94 @@ +/** + * HeatmapComponent - a heatmap rendered by DBHeatmapChart, on the search + * page's event deltas view or in a dashboard tile. + */ +import { expect, Locator, Page } from '@playwright/test'; + +export class HeatmapComponent { + readonly page: Page; + private readonly root: Page | Locator; + + /** + * @param root - Scope that contains the chart. The "not enough data" message + * replaces the plot container, so this must be wider than the plot itself. + */ + constructor(page: Page, root: Page | Locator = page) { + this.page = page; + this.root = root; + } + + get container() { + return this.root.locator('.heatmap-selection-container'); + } + + get canvas() { + return this.container.locator('canvas'); + } + + get notEnoughDataText() { + return this.root.getByText('Not enough data points to render heatmap'); + } + + /** The hover tooltip, which only appears for a cell with a non-zero count. */ + get tooltip() { + return this.container.getByText('Count Value:'); + } + + /** + * Sweep the cursor over the plot until the tooltip appears, proving at least + * one populated cell was drawn. A zero-filled grid still mounts a canvas, so + * checking the canvas alone does not. + */ + async hoverPopulatedCell() { + // A refresh keeps the previous grid on screen while it pulses; wait for + // the fresh data so the hover tests what the current query drew. + await expect(this.container).not.toHaveClass(/effect-pulse/, { + timeout: 20000, + }); + const plot = this.container.locator('.u-over'); + await expect(plot).toBeVisible(); + const box = await plot.boundingBox(); + if (!box) { + throw new Error('Heatmap plot area not found'); + } + // The tooltip fires within 20px of a cell center; a 25px grid keeps every + // point of the plot within ~18px of a sample. + const step = 25; + for (let y = step / 2; y < box.height; y += step) { + for (let x = step / 2; x < box.width; x += step) { + await this.page.mouse.move(box.x + x, box.y + y); + if (await this.tooltip.isVisible()) { + return; + } + } + } + await expect(this.tooltip).toBeVisible(); + } + + /** + * Drag a rectangle over the inner area of the heatmap plot. Targets uPlot's + * `.u-over` overlay, which receives the drag events and excludes the axes. + */ + async dragSelect( + start: { x: number; y: number } = { x: 0.3, y: 0.2 }, + end: { x: number; y: number } = { x: 0.7, y: 0.8 }, + ) { + const plot = this.container.locator('.u-over'); + await expect(plot).toBeVisible(); + const box = await plot.boundingBox(); + if (!box) { + throw new Error('Heatmap plot area not found'); + } + await this.page.mouse.move( + box.x + box.width * start.x, + box.y + box.height * start.y, + ); + await this.page.mouse.down(); + await this.page.mouse.move( + box.x + box.width * end.x, + box.y + box.height * end.y, + { steps: 10 }, + ); + await this.page.mouse.up(); + } +} diff --git a/packages/app/tests/e2e/features/heatmap.spec.ts b/packages/app/tests/e2e/features/heatmap.spec.ts new file mode 100644 index 0000000000..0a511291a3 --- /dev/null +++ b/packages/app/tests/e2e/features/heatmap.spec.ts @@ -0,0 +1,107 @@ +import { DisplayType } from '@hyperdx/common-utils/dist/types'; + +import { DashboardPage } from '../page-objects/DashboardPage'; +import { SearchPage } from '../page-objects/SearchPage'; +import { expect, test } from '../utils/base-test'; +import { DEFAULT_TRACES_SOURCE_NAME } from '../utils/constants'; + +test.describe('Heatmap', () => { + test( + 'event deltas heatmap renders, drag-selects, and switches scale', + { tag: ['@full-stack', '@search'] }, + async ({ page }) => { + test.setTimeout(90000); + const searchPage = new SearchPage(page); + + await test.step('Open event deltas on the trace source', async () => { + await searchPage.goto(); + await searchPage.selectSource(DEFAULT_TRACES_SOURCE_NAME); + await searchPage.timePicker.selectRelativeTime('Last 1 hour'); + await searchPage.table.waitForRowsToPopulate(); + await searchPage.switchToEventDeltas(); + }); + + await test.step('Heatmap renders', async () => { + await expect(searchPage.heatmap.canvas.first()).toBeVisible({ + timeout: 20000, + }); + await expect(searchPage.heatmap.notEnoughDataText).toHaveCount(0); + await expect(searchPage.chartErrorState).toHaveCount(0); + await searchPage.heatmap.hoverPopulatedCell(); + await expect(searchPage.deltaNoSelectionHint).toBeVisible({ + timeout: 20000, + }); + }); + + await test.step('Drag-select enters comparison mode', async () => { + await searchPage.heatmap.dragSelect(); + await expect(page).toHaveURL(/[?&]xMin=/); + await expect(page).toHaveURL(/[?&]xMax=/); + await expect(page).toHaveURL(/[?&]yMin=/); + await expect(page).toHaveURL(/[?&]yMax=/); + await expect(searchPage.deltaSelectionLegend).toBeVisible(); + await expect(searchPage.deltaBackgroundLegend).toBeVisible(); + await expect(searchPage.deltaNoSelectionHint).toHaveCount(0); + }); + + await test.step('Switch scale to linear', async () => { + await searchPage.openHeatmapSettings(); + await searchPage.setHeatmapScale('Linear'); + await searchPage.applyHeatmapSettings(); + await expect(page).toHaveURL(/[?&]scaleType=linear/); + await expect(searchPage.heatmap.canvas.first()).toBeVisible({ + timeout: 20000, + }); + await expect(searchPage.heatmap.notEnoughDataText).toHaveCount(0); + await expect(searchPage.chartErrorState).toHaveCount(0); + await searchPage.heatmap.hoverPopulatedCell(); + }); + }, + ); + + test( + 'dashboard heatmap tile renders and persists across reload', + { tag: ['@full-stack', '@dashboard'] }, + async ({ page }) => { + test.setTimeout(90000); + const dashboardPage = new DashboardPage(page); + const editor = dashboardPage.chartEditor; + const ts = Date.now(); + const dashboardName = `E2E Heatmap Dashboard ${ts}`; + const tileName = `E2E Heatmap Tile ${ts}`; + + await test.step('Create a dashboard', async () => { + await dashboardPage.goto(); + await dashboardPage.createNewDashboard(); + await dashboardPage.editDashboardName(dashboardName); + }); + + await test.step('Add a heatmap tile on the trace source', async () => { + await dashboardPage.addTile(); + await expect(editor.nameInput).toBeVisible(); + await editor.waitForDataToLoad(); + await editor.setChartType(DisplayType.Heatmap); + await editor.selectSource(DEFAULT_TRACES_SOURCE_NAME); + await expect(editor.source).toHaveValue(DEFAULT_TRACES_SOURCE_NAME); + await editor.setChartName(tileName); + await editor.runQuery(false); + await editor.save(); + }); + + const assertTileRendersHeatmap = async () => { + const heatmap = dashboardPage.getTileHeatmap(); + await expect(heatmap.canvas.first()).toBeVisible({ timeout: 20000 }); + await expect(dashboardPage.getTileError()).toHaveCount(0); + await expect(heatmap.notEnoughDataText).toHaveCount(0); + await heatmap.hoverPopulatedCell(); + }; + + await test.step('Tile renders the heatmap', assertTileRendersHeatmap); + + await test.step('Tile still renders after reload', async () => { + await dashboardPage.reload(); + await assertTileRendersHeatmap(); + }); + }, + ); +}); diff --git a/packages/app/tests/e2e/page-objects/DashboardPage.ts b/packages/app/tests/e2e/page-objects/DashboardPage.ts index 93ebe88383..963f4b15af 100644 --- a/packages/app/tests/e2e/page-objects/DashboardPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts @@ -6,6 +6,7 @@ import { DisplayType } from '@hyperdx/common-utils/dist/types'; import { expect, Locator, Page } from '@playwright/test'; import { ChartEditorComponent } from '../components/ChartEditorComponent'; +import { HeatmapComponent } from '../components/HeatmapComponent'; import { TimePickerComponent } from '../components/TimePickerComponent'; import { dismissSqlAutocomplete, @@ -31,7 +32,7 @@ export type FilterRequirementOptions = { * Used with verifyTileFormFromConfig */ export type TileConfig = { - displayType: Exclude; + displayType: DisplayType; sourceId?: string; select?: | { @@ -55,7 +56,8 @@ type SeriesType = | 'markdown' | 'pie' | 'event_patterns' - | 'bar'; + | 'bar' + | 'heatmap'; /** * Series data structure for chart verification @@ -649,6 +651,10 @@ export class DashboardPage { return this.getTiles().nth(index); } + getTileHeatmap(tileIndex = 0) { + return new HeatmapComponent(this.page, this.getTile(tileIndex)); + } + /** * Hover over a tile to reveal action buttons */ diff --git a/packages/app/tests/e2e/page-objects/SearchPage.ts b/packages/app/tests/e2e/page-objects/SearchPage.ts index bf56ede396..45d0ae9f72 100644 --- a/packages/app/tests/e2e/page-objects/SearchPage.ts +++ b/packages/app/tests/e2e/page-objects/SearchPage.ts @@ -5,6 +5,7 @@ import { expect, Locator, Page } from '@playwright/test'; import { FilterComponent } from '../components/FilterComponent'; +import { HeatmapComponent } from '../components/HeatmapComponent'; import { InfrastructurePanelComponent } from '../components/InfrastructurePanelComponent'; import { PatternSidePanelComponent } from '../components/PatternSidePanelComponent'; import { SavedSearchModalComponent } from '../components/SavedSearchModalComponent'; @@ -28,6 +29,7 @@ export class SearchPage { readonly patternSidePanel: PatternSidePanelComponent; readonly infrastructure: InfrastructurePanelComponent; readonly filters: FilterComponent; + readonly heatmap: HeatmapComponent; readonly whereInput: WhereInputComponent; readonly savedSearchModal: SavedSearchModalComponent; readonly savedSearchNameTitle: Locator; @@ -56,6 +58,7 @@ export class SearchPage { this.patternSidePanel = new PatternSidePanelComponent(page); this.infrastructure = new InfrastructurePanelComponent(page); this.filters = new FilterComponent(page); + this.heatmap = new HeatmapComponent(page); this.savedSearchModal = new SavedSearchModalComponent(page); this.alertModal = new SearchPageAlertModalComponent(page); this.alertsButtonLocator = page.getByTestId('alerts-button'); @@ -114,6 +117,56 @@ export class SearchPage { return this.page.getByRole('tab', { name: 'Event Patterns' }); } + /** The "Event Deltas" analysis-mode tab, shown only for trace sources. */ + get eventDeltasTab() { + return this.page.getByRole('tab', { name: 'Event Deltas' }); + } + + async switchToEventDeltas() { + await this.eventDeltasTab.click(); + await expect(this.page).toHaveURL(/[?&]mode=delta/); + } + + get chartErrorState() { + return this.page.getByTestId('chart-error-state'); + } + + /** DBDeltaChart legend entries shown only while a selection is compared. */ + get deltaSelectionLegend() { + return this.page.getByText('Selection', { exact: true }); + } + + get deltaBackgroundLegend() { + return this.page.getByText('Background', { exact: true }); + } + + /** DBDeltaChart hint shown while there is no selection to compare. */ + get deltaNoSelectionHint() { + return this.page.getByText( + 'Select an area on the chart above to enable comparisons', + ); + } + + get heatmapSettingsDrawer() { + return this.page.getByRole('dialog', { name: 'Display Settings' }); + } + + async openHeatmapSettings() { + await this.page.getByTestId('heatmap-settings-button').click(); + await expect(this.heatmapSettingsDrawer).toBeVisible(); + } + + async setHeatmapScale(scale: 'Log' | 'Linear') { + await this.heatmapSettingsDrawer.getByText(scale, { exact: true }).click(); + } + + async applyHeatmapSettings() { + await this.heatmapSettingsDrawer + .getByRole('button', { name: 'Apply' }) + .click(); + await expect(this.heatmapSettingsDrawer).toBeHidden(); + } + /** * The pattern list table (the "Event Patterns" grid). It renders before the * flyout in the DOM (the flyout is a portaled drawer), so `.first()` resolves