Skip to content

Commit 647abb2

Browse files
Merge branch 'main' into brandon/brandon-trace-waterfall-tool-improvements
2 parents bd06371 + 51d35da commit 647abb2

32 files changed

Lines changed: 2594 additions & 1660 deletions

‎packages/app/src/components/DBHeatmapChart.tsx‎

Lines changed: 0 additions & 1446 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Flex, Text } from '@mantine/core';
2+
3+
type ColorLegendProps = {
4+
colors: string[];
5+
};
6+
7+
export function ColorLegend({ colors }: ColorLegendProps) {
8+
return (
9+
<Flex
10+
align="center"
11+
gap={4}
12+
role="img"
13+
aria-label="Color scale: low to high count"
14+
>
15+
<Text size="10px" c="dimmed">
16+
Low
17+
</Text>
18+
<div
19+
style={{
20+
display: 'flex',
21+
width: 80,
22+
height: 8,
23+
borderRadius: 2,
24+
overflow: 'hidden',
25+
}}
26+
>
27+
{colors.map(color => (
28+
<div key={color} style={{ flex: 1, background: color }} />
29+
))}
30+
</div>
31+
<Text size="10px" c="dimmed">
32+
High
33+
</Text>
34+
</Flex>
35+
);
36+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import { useMemo } from 'react';
2+
import dynamic from 'next/dynamic';
3+
import cx from 'classnames';
4+
import { Text, useMantineColorScheme } from '@mantine/core';
5+
6+
import ChartContainer from '@/components/charts/ChartContainer';
7+
import ChartErrorState, {
8+
ChartErrorStateVariant,
9+
} from '@/components/charts/ChartErrorState';
10+
11+
import { ColorLegend } from './ColorLegend';
12+
import type { HeatmapScaleType } from './heatmapGrid';
13+
import { HeatmapPlot } from './HeatmapPlot';
14+
import type { HeatmapChartConfig } from './heatmapQueries';
15+
import { darkPalette, lightPalette } from './palette';
16+
import type { SelectionBounds } from './selection';
17+
import { useHeatmapData } from './useHeatmapData';
18+
19+
type DBHeatmapChartProps = {
20+
config: HeatmapChartConfig;
21+
enabled?: boolean;
22+
onFilter?: (xMin: number, xMax: number, yMin: number, yMax: number) => void;
23+
onClearFilter?: () => void;
24+
/**
25+
* The currently-applied drag-select bounds. When provided, the heatmap
26+
* draws the dashed selection rectangle and reapplies it after any uPlot
27+
* recreation (theme switch, prop change, resize) so the user always sees
28+
* which slice they filtered. Caller owns the URL/query-state plumbing;
29+
* this is purely a visual mirror of that state.
30+
*/
31+
selectionBounds?: SelectionBounds | null;
32+
title?: React.ReactNode;
33+
toolbarPrefix?: React.ReactNode[];
34+
toolbarSuffix?: React.ReactNode[];
35+
scaleType?: HeatmapScaleType;
36+
showLegend?: boolean;
37+
errorVariant?: ChartErrorStateVariant;
38+
};
39+
40+
function DBHeatmapChart({
41+
config,
42+
enabled = true,
43+
onFilter,
44+
onClearFilter,
45+
selectionBounds,
46+
title,
47+
toolbarPrefix,
48+
toolbarSuffix,
49+
scaleType = 'log',
50+
showLegend = false,
51+
errorVariant,
52+
}: DBHeatmapChartProps) {
53+
const { colorScheme } = useMantineColorScheme();
54+
const palette = colorScheme === 'light' ? lightPalette : darkPalette;
55+
56+
const { view, isLoading, isRefreshing, error } = useHeatmapData({
57+
config,
58+
scaleType,
59+
enabled,
60+
});
61+
62+
const toolbarItemsMemo = useMemo(() => {
63+
const allToolbarItems: React.ReactNode[] = [];
64+
65+
if (showLegend) {
66+
allToolbarItems.push(
67+
<ColorLegend key="heatmap-legend" colors={palette} />,
68+
);
69+
}
70+
71+
if (toolbarPrefix && toolbarPrefix.length > 0) {
72+
allToolbarItems.push(...toolbarPrefix);
73+
}
74+
75+
if (toolbarSuffix && toolbarSuffix.length > 0) {
76+
allToolbarItems.push(...toolbarSuffix);
77+
}
78+
79+
return allToolbarItems;
80+
}, [showLegend, palette, toolbarPrefix, toolbarSuffix]);
81+
82+
return (
83+
<ChartContainer
84+
title={title}
85+
toolbarItems={toolbarItemsMemo}
86+
disableReactiveContainer
87+
>
88+
{isLoading ? (
89+
<Text size="sm" ta="center" p="xl">
90+
Loading...
91+
</Text>
92+
) : error ? (
93+
<ChartErrorState error={error} variant={errorVariant} />
94+
) : view.grid.cells.length < 2 || view.generatedTsBuckets.length < 2 ? (
95+
<Text
96+
size="sm"
97+
ta="center"
98+
p="xl"
99+
className={cx({ 'effect-pulse': isRefreshing })}
100+
>
101+
Not enough data points to render heatmap. Try expanding your search
102+
criteria.
103+
</Text>
104+
) : (
105+
<HeatmapPlot
106+
key={JSON.stringify(config)}
107+
className={cx({ 'effect-pulse': isRefreshing })}
108+
grid={view.grid}
109+
numberFormat={config.numberFormat}
110+
onFilter={
111+
onFilter
112+
? (xMin, xMax, yMin, yMax) => {
113+
// In log mode, the bottom bucket collects all values
114+
// clamped by greatest(value, effectiveMin). If the
115+
// selection touches that bucket, widen yMin to 0 so
116+
// the downstream SQL filter captures all those spans.
117+
// The 1.1x threshold adds 10% headroom to account for
118+
// floating-point rounding in the bucket boundary.
119+
const adjustedYMin =
120+
view.scaleType === 'log' && yMin <= view.effectiveMin * 1.1
121+
? 0
122+
: yMin;
123+
onFilter(xMin, xMax, adjustedYMin, yMax);
124+
}
125+
: undefined
126+
}
127+
onClearFilter={onClearFilter}
128+
scaleType={view.scaleType}
129+
palette={palette}
130+
selectionBounds={selectionBounds}
131+
/>
132+
)}
133+
</ChartContainer>
134+
);
135+
}
136+
137+
export default dynamic(() => Promise.resolve(DBHeatmapChart), {
138+
ssr: false,
139+
});

0 commit comments

Comments
 (0)