Repository navigation
refactor: Split up DBHeatmap into smaller, focused files; Add tests #3256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
packages/app/src/components/DBHeatmapChart/ColorLegend.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { Flex, Text } from '@mantine/core'; | ||
|
|
||
| type ColorLegendProps = { | ||
| colors: string[]; | ||
| }; | ||
|
|
||
| export function ColorLegend({ colors }: ColorLegendProps) { | ||
| return ( | ||
| <Flex | ||
| align="center" | ||
| gap={4} | ||
| role="img" | ||
| aria-label="Color scale: low to high count" | ||
| > | ||
| <Text size="10px" c="dimmed"> | ||
| Low | ||
| </Text> | ||
| <div | ||
| style={{ | ||
| display: 'flex', | ||
| width: 80, | ||
| height: 8, | ||
| borderRadius: 2, | ||
| overflow: 'hidden', | ||
| }} | ||
| > | ||
| {colors.map(color => ( | ||
| <div key={color} style={{ flex: 1, background: color }} /> | ||
| ))} | ||
| </div> | ||
| <Text size="10px" c="dimmed"> | ||
| High | ||
| </Text> | ||
| </Flex> | ||
| ); | ||
| } |
139 changes: 139 additions & 0 deletions
139
packages/app/src/components/DBHeatmapChart/DBHeatmapChart.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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( | ||
| <ColorLegend key="heatmap-legend" colors={palette} />, | ||
| ); | ||
| } | ||
|
|
||
| if (toolbarPrefix && toolbarPrefix.length > 0) { | ||
| allToolbarItems.push(...toolbarPrefix); | ||
| } | ||
|
|
||
| if (toolbarSuffix && toolbarSuffix.length > 0) { | ||
| allToolbarItems.push(...toolbarSuffix); | ||
| } | ||
|
|
||
| return allToolbarItems; | ||
| }, [showLegend, palette, toolbarPrefix, toolbarSuffix]); | ||
|
|
||
| return ( | ||
| <ChartContainer | ||
| title={title} | ||
| toolbarItems={toolbarItemsMemo} | ||
| disableReactiveContainer | ||
| > | ||
| {isLoading ? ( | ||
| <Text size="sm" ta="center" p="xl"> | ||
| Loading... | ||
| </Text> | ||
| ) : error ? ( | ||
| <ChartErrorState error={error} variant={errorVariant} /> | ||
| ) : view.grid.cells.length < 2 || view.generatedTsBuckets.length < 2 ? ( | ||
| <Text | ||
| size="sm" | ||
| ta="center" | ||
| p="xl" | ||
| className={cx({ 'effect-pulse': isRefreshing })} | ||
| > | ||
| Not enough data points to render heatmap. Try expanding your search | ||
| criteria. | ||
| </Text> | ||
| ) : ( | ||
| <HeatmapPlot | ||
| key={JSON.stringify(config)} | ||
| className={cx({ 'effect-pulse': isRefreshing })} | ||
| grid={view.grid} | ||
| numberFormat={config.numberFormat} | ||
| onFilter={ | ||
| onFilter | ||
| ? (xMin, xMax, yMin, yMax) => { | ||
| // 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} | ||
| /> | ||
| )} | ||
| </ChartContainer> | ||
| ); | ||
| } | ||
|
|
||
| export default dynamic(() => Promise.resolve(DBHeatmapChart), { | ||
| ssr: false, | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.