Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions packages/app/src/ClickhousePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -668,23 +668,27 @@ function ClickhousePage() {
<DBHeatmapChart
title="Query Latency"
toolbarSuffix={heatmapToolbarItems}
config={{
displayType: DisplayType.Heatmap,
select: [
{
aggFn: 'heatmap',
valueExpression: 'query_duration_ms',
},
],
from,
dateRange: searchedTimeRange,
granularity: 'auto',
timestampValueExpression: 'event_time',
connection,
where: `query_kind='Select' AND (
query={{
mode: 'distribution',
scaleType: 'log',
config: {
displayType: DisplayType.Heatmap,
select: [
{
aggFn: 'heatmap',
valueExpression: 'query_duration_ms',
},
],
from,
dateRange: searchedTimeRange,
granularity: 'auto',
timestampValueExpression: 'event_time',
connection,
where: `query_kind='Select' AND (
type='ExceptionWhileProcessing' OR type='QueryFinish'
)`,
filters,
filters,
},
}}
onFilter={(tsStart, tsEnd, latencyMin, latencyMax) => {
onTimeRangeSelect(
Expand Down
9 changes: 3 additions & 6 deletions packages/app/src/DBDashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ import ChartContainer, {
} from './components/charts/ChartContainer';
import DashboardFiltersModal from './components/DashboardFiltersModal';
import { DBBarChart } from './components/DBBarChart';
import DBHeatmapChart, {
toHeatmapChartConfig,
} from './components/DBHeatmapChart';
import DBHeatmapChart, { toHeatmapQuery } from './components/DBHeatmapChart';
import { DBPieChart } from './components/DBPieChart';
import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar';
import OnboardingModal from './components/OnboardingModal';
Expand Down Expand Up @@ -249,7 +247,7 @@ function HeatmapTile({
dateRange: [Date, Date];
enabled?: boolean;
}) {
const { heatmapConfig, scaleType } = toHeatmapChartConfig(queriedConfig);
const heatmapQuery = toHeatmapQuery(queriedConfig);

const [clickPos, setClickPos] = useState<{ x: number; y: number } | null>(
null,
Expand Down Expand Up @@ -291,8 +289,7 @@ function HeatmapTile({
title={title}
toolbarPrefix={toolbarPrefix}
toolbarSuffix={toolbarSuffix}
config={heatmapConfig}
scaleType={scaleType}
query={heatmapQuery}
enabled={enabled}
showLegend
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import DBHeatmapChart, {
buildHeatmapBoundsConfig,
buildHeatmapBucketConfig,
HEATMAP_N_BUCKETS,
toHeatmapChartConfig,
toHeatmapQuery,
} from '@/components/DBHeatmapChart';
import DBNumberChart from '@/components/DBNumberChart';
import { DBPieChart } from '@/components/DBPieChart';
Expand Down Expand Up @@ -62,12 +62,10 @@ function HeatmapPreview({
}: {
config: BuilderChartConfigWithDateRange;
}) {
const { heatmapConfig, scaleType } = toHeatmapChartConfig(config);
return (
<div className="flex-grow-1 d-flex flex-column" style={{ height: 400 }}>
<DBHeatmapChart
config={heatmapConfig}
scaleType={scaleType}
query={toHeatmapQuery(config)}
showLegend
errorVariant="inline"
/>
Expand Down Expand Up @@ -96,8 +94,8 @@ function HeatmapSQLPreview({
...config,
timestampValueExpression,
};
const { heatmapConfig, scaleType } =
toHeatmapChartConfig(configWithTimestamp);
const { config: heatmapConfig, scaleType } =
toHeatmapQuery(configWithTimestamp);
const granularity = convertDateRangeToGranularityString(dateRange, 245);

const boundsConfig = buildHeatmapBoundsConfig({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ import {
isStringSelectDisplayType,
validateChartForm,
} from '@/components/ChartEditor/utils';
import type { HeatmapScaleType } from '@/components/DBHeatmapChart';
import {
HEATMAP_DURATION_NUMBER_FORMAT,
type HeatmapScaleType,
} from '@/components/DBHeatmapChart';
import { ErrorBoundary } from '@/components/Error/ErrorBoundary';
import HeatmapSettingsDrawer, {
HeatmapSettingsValues,
Expand Down Expand Up @@ -171,7 +174,7 @@ function applyHeatmapDefaults(
setValue('select', heatmapSeries);
setValue('series', heatmapSeries);
setValue('series.0.countExpression', 'count()');
setValue('numberFormat', { output: 'duration', factor: 0.001 });
setValue('numberFormat', { ...HEATMAP_DURATION_NUMBER_FORMAT });
}

export default function EditTimeChartForm({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ jest.mock('@/components/DBSqlRowTableWithSidebar', () => ({
jest.mock('@/components/DBHeatmapChart', () => ({
__esModule: true,
default: () => <div data-testid="db-heatmap-chart">Heatmap Chart</div>,
toHeatmapChartConfig: (config: unknown) => ({
heatmapConfig: config,
scaleType: 'log' as const,
toHeatmapQuery: (config: unknown) => ({
mode: 'distribution',
config,
scaleType: 'log',
}),
buildHeatmapBoundsConfig: ({ config }: { config: unknown }) => config,
buildHeatmapBucketConfig: ({ config }: { config: unknown }) => config,
Expand Down
140 changes: 33 additions & 107 deletions packages/app/src/components/DBHeatmapChart/DBHeatmapChart.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
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 { HeatmapChartView, HeatmapChartViewProps } from './HeatmapChartView';
import type { HeatmapQuery } from './heatmapQueries';
import type { SelectionBounds } from './selection';
import { useHeatmapData } from './useHeatmapData';

type DBHeatmapChartProps = {
config: HeatmapChartConfig;
type DBHeatmapChartProps = HeatmapChartViewProps & {
query: HeatmapQuery;
enabled?: boolean;
onFilter?: (xMin: number, xMax: number, yMin: number, yMax: number) => void;
onClearFilter?: () => void;
Expand All @@ -29,108 +18,45 @@ type DBHeatmapChartProps = {
* 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,
query,
enabled = true,
onFilter,
onClearFilter,
selectionBounds,
title,
toolbarPrefix,
toolbarSuffix,
scaleType = 'log',
showLegend = false,
errorVariant,
...viewProps
}: DBHeatmapChartProps) {
const { colorScheme } = useMantineColorScheme();
const palette = colorScheme === 'light' ? lightPalette : darkPalette;

const { view, isLoading, isRefreshing, error } = useHeatmapData({
config,
scaleType,
const data = useHeatmapData({
config: query.config,
scaleType: query.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]);
const { view } = data;

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>
<HeatmapChartView
{...viewProps}
data={data}
plotKey={JSON.stringify(query.config)}
numberFormat={query.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
}
/>
);
}

Expand Down
Loading
Loading