diff --git a/.changeset/heatmap-inline-settings.md b/.changeset/heatmap-inline-settings.md new file mode 100644 index 0000000000..9f56875219 --- /dev/null +++ b/.changeset/heatmap-inline-settings.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +feat: Edit heatmap value and count inline in the chart editor diff --git a/packages/app/src/components/ChartDisplaySettingsDrawer.tsx b/packages/app/src/components/ChartDisplaySettingsDrawer.tsx index 20f3ee0894..b65654f4b6 100644 --- a/packages/app/src/components/ChartDisplaySettingsDrawer.tsx +++ b/packages/app/src/components/ChartDisplaySettingsDrawer.tsx @@ -27,6 +27,7 @@ import { shouldFillNullsWithZero } from '@/ChartUtils'; import { MAX_RENDERED_TIME_CHART_SERIES } from '@/defaults'; import { FormatTime } from '@/useFormatTime'; +import { HeatmapScaleControl } from './DBHeatmapChart/HeatmapScaleControl'; import { BackgroundChartInput } from './BackgroundChartInput'; import { attachLocalIds, @@ -35,6 +36,7 @@ import { stripLocalIds, } from './ColorRulesEditor'; import { ColorSwatchInput } from './ColorSwatchInput'; +import type { HeatmapScaleType } from './DBHeatmapChart'; import { CheckBoxControlled, TextInputControlled } from './InputControlled'; import { DEFAULT_NUMBER_FORMAT, NumberFormatForm } from './NumberFormat'; @@ -61,6 +63,8 @@ export type ChartConfigDisplaySettings = Pick< // PromQL-only: Handlebars template over each series' Prometheus label set // that renders the legend/tooltip name. legendTemplate?: string; + // Heatmaps only; stored on the heatmap's select[0]. + heatmapScaleType?: HeatmapScaleType; }; /** @@ -113,6 +117,7 @@ function applyDefaultSettings( ? attachLocalIds(settings.colorRules) : undefined, backgroundChart: settings.backgroundChart, + heatmapScaleType: settings.heatmapScaleType ?? 'log', }; } @@ -243,6 +248,8 @@ export default function ChartDisplaySettingsDrawer({ ? 'Available on PromQL range queries.' : 'Available on query-builder number tiles.'; + const showHeatmapScale = displayType === DisplayType.Heatmap; + return ( )} + {showHeatmapScale && ( + <> + ( + + )} + /> + + + )} + {showLegendTemplate && ( <> diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx index 7ee774b584..6b17169d4e 100644 --- a/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx @@ -83,7 +83,6 @@ type ChartEditorControlsProps = { chartConfigForExplanations?: ChartConfigWithOptTimestamp; onSubmit: (suppressErrorNotification?: boolean) => void; openDisplaySettings: () => void; - openHeatmapSettings: () => void; }; export function ChartEditorControls({ @@ -117,7 +116,6 @@ export function ChartEditorControls({ chartConfigForExplanations, onSubmit, openDisplaySettings, - openHeatmapSettings, }: ChartEditorControlsProps) { // Formulas (HDX-5080): derived series computed from the chart's series via // letter-ref arithmetic expressions. Metric and event (log/trace) sources, @@ -224,12 +222,15 @@ export function ChartEditorControls({ {displayType === DisplayType.Heatmap && Array.isArray(select) ? ( ) : displayType === DisplayType.EventPatterns ? ( diff --git a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx index 0cf87aae5e..a4dd663af9 100644 --- a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx @@ -71,14 +71,8 @@ import { isStringSelectDisplayType, validateChartForm, } from '@/components/ChartEditor/utils'; -import { - HEATMAP_DURATION_NUMBER_FORMAT, - type HeatmapScaleType, -} from '@/components/DBHeatmapChart'; +import { HEATMAP_DURATION_NUMBER_FORMAT } from '@/components/DBHeatmapChart'; import { ErrorBoundary } from '@/components/Error/ErrorBoundary'; -import HeatmapSettingsDrawer, { - HeatmapSettingsValues, -} from '@/components/HeatmapSettingsDrawer'; import { InputControlled } from '@/components/InputControlled'; import SaveToDashboardModal from '@/components/SaveToDashboardModal'; import { getStoredLanguage } from '@/components/SearchInput/SearchWhereInput'; @@ -349,6 +343,7 @@ export default function EditTimeChartForm({ colorRules, backgroundChart, legendTemplate, + heatmapScaleType, ] = useWatch({ control, name: [ @@ -364,6 +359,7 @@ export default function EditTimeChartForm({ 'colorRules', 'backgroundChart', 'legendTemplate', + 'series.0.heatmapScaleType', ], }); @@ -395,6 +391,7 @@ export default function EditTimeChartForm({ colorRules, backgroundChart, legendTemplate, + heatmapScaleType, }), [ alignDateRangeToGranularity, @@ -409,6 +406,7 @@ export default function EditTimeChartForm({ colorRules, backgroundChart, legendTemplate, + heatmapScaleType, ], ); @@ -417,11 +415,6 @@ export default function EditTimeChartForm({ { open: openDisplaySettings, close: closeDisplaySettings }, ] = useDisclosure(false); - const [ - heatmapSettingsOpened, - { open: openHeatmapSettings, close: closeHeatmapSettings }, - ] = useDisclosure(false); - // Only update this on submit, otherwise we'll have issues // with using the source value from the last submit // (ex. ignoring local custom source updates) @@ -890,6 +883,7 @@ export default function EditTimeChartForm({ colorRules, backgroundChart, legendTemplate, + heatmapScaleType, }: ChartConfigDisplaySettings, isDirty: boolean, ) => { @@ -916,6 +910,9 @@ export default function EditTimeChartForm({ if (configType === 'promql') { setValue('legendTemplate', legendTemplate ?? ''); } + if (displayType === DisplayType.Heatmap) { + setValue('series.0.heatmapScaleType', heatmapScaleType); + } // Display settings live in a separate drawer form, so RHF can't track // them. Latch dirty state only when the drawer reports actual changes. if (isDirty) { @@ -924,44 +921,7 @@ export default function EditTimeChartForm({ } onSubmit(); }, - [setValue, onDirtyChange, onSubmit, configType], - ); - - const handleUpdateHeatmapSettings = useCallback( - (data: HeatmapSettingsValues) => { - setValue('series.0.valueExpression', data.value); - setValue('series.0.countExpression', data.count || 'count()'); - setValue('series.0.heatmapScaleType', data.scaleType); - // Heatmap settings are applied outside RHF's change tracking. - subFormDirtyRef.current = true; - onDirtyChange?.(true); - onSubmit(); - closeHeatmapSettings(); - }, - [setValue, onDirtyChange, onSubmit, closeHeatmapSettings], - ); - - const heatmapValueExpression = useWatch({ - control, - name: 'series.0.valueExpression', - }); - const heatmapCountExpression = useWatch({ - control, - name: 'series.0.countExpression', - }); - const heatmapScaleType: HeatmapScaleType = - useWatch({ - control, - name: 'series.0.heatmapScaleType', - }) ?? 'log'; - - const heatmapSettingsDefaults = useMemo( - () => ({ - value: heatmapValueExpression || '', - count: heatmapCountExpression || 'count()', - scaleType: heatmapScaleType, - }), - [heatmapValueExpression, heatmapCountExpression, heatmapScaleType], + [setValue, onDirtyChange, onSubmit, configType, displayType], ); const tableConnection = useMemo( @@ -1152,7 +1112,6 @@ export default function EditTimeChartForm({ chartConfigForExplanations={chartConfigForExplanations} onSubmit={onSubmit} openDisplaySettings={openDisplaySettings} - openHeatmapSettings={openHeatmapSettings} /> )} - ); diff --git a/packages/app/src/components/DBEditTimeChartForm/HeatmapSeriesEditor.tsx b/packages/app/src/components/DBEditTimeChartForm/HeatmapSeriesEditor.tsx index 3fd98cfb7a..6b80142729 100644 --- a/packages/app/src/components/DBEditTimeChartForm/HeatmapSeriesEditor.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/HeatmapSeriesEditor.tsx @@ -1,51 +1,122 @@ import { useMemo } from 'react'; -import { Control, UseFormSetValue } from 'react-hook-form'; +import { Control, FieldErrors, UseFormSetValue } from 'react-hook-form'; import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; import { TSource } from '@hyperdx/common-utils/dist/types'; -import { Button, Divider, Flex } from '@mantine/core'; +import { Button, Divider, Flex, Stack, Text, Tooltip } from '@mantine/core'; +import { IconHelpCircle } from '@tabler/icons-react'; import { ChartEditorFormState } from '@/components/ChartEditor/types'; +import { EDITOR_INPUT_HEIGHTS } from '@/components/editorInputHeights'; import SearchWhereInput from '@/components/SearchInput/SearchWhereInput'; +import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor'; type HeatmapSeriesEditorProps = { control: Control; setValue: UseFormSetValue; + errors: FieldErrors; tableSource?: TSource; dateRange?: [Date, Date]; + parentRef: HTMLElement | null; onSubmit: () => void; onOpenDisplaySettings: () => void; }; +function InlineLabel({ label, tooltip }: { label: string; tooltip: string }) { + return ( + + + {label} + + + + + + ); +} + export function HeatmapSeriesEditor({ control, setValue, + errors, tableSource, dateRange, + parentRef, onSubmit, onOpenDisplaySettings, }: HeatmapSeriesEditorProps) { const connection = useMemo(() => tcFromSource(tableSource), [tableSource]); return ( - - - setValue('whereLanguage', lang) - } - enableVariables - /> - + + +
+ +
+ +
+ +
+ +
+
+ + setValue('whereLanguage', lang) + } + enableVariables + /> +
+ diff --git a/packages/app/src/components/DBHeatmapChart/HeatmapScaleControl.tsx b/packages/app/src/components/DBHeatmapChart/HeatmapScaleControl.tsx new file mode 100644 index 0000000000..f8583e4292 --- /dev/null +++ b/packages/app/src/components/DBHeatmapChart/HeatmapScaleControl.tsx @@ -0,0 +1,33 @@ +import { Box, SegmentedControl, Text } from '@mantine/core'; + +import type { HeatmapScaleType } from './heatmapGrid'; + +const SCALE_OPTIONS: { label: string; value: HeatmapScaleType }[] = [ + { label: 'Log', value: 'log' }, + { label: 'Linear', value: 'linear' }, +]; + +export function HeatmapScaleControl({ + value, + onChange, +}: { + value: HeatmapScaleType; + onChange: (value: HeatmapScaleType) => void; +}) { + return ( + + + Y axis scale + + { + if (v === 'log' || v === 'linear') onChange(v); + }} + data={SCALE_OPTIONS} + data-testid="heatmap-scale-control" + /> + + ); +} diff --git a/packages/app/src/components/DBHeatmapChart/heatmapQueries.ts b/packages/app/src/components/DBHeatmapChart/heatmapQueries.ts index 87ac2e3599..4a988f9cfd 100644 --- a/packages/app/src/components/DBHeatmapChart/heatmapQueries.ts +++ b/packages/app/src/components/DBHeatmapChart/heatmapQueries.ts @@ -153,7 +153,8 @@ export function buildHeatmapBucketConfig({ nBuckets: number; }): BuilderChartConfigWithDateRange { const valueExpression = config.select[0].valueExpression; - const countExpression = config.select[0].countExpression ?? 'count()'; + // The chart editor saves a cleared Count input as ''. + const countExpression = config.select[0].countExpression?.trim() || 'count()'; const isAggregateExpression = isAggregateFunction(valueExpression); const bucketExprAgg = diff --git a/packages/app/src/components/HeatmapSettingsDrawer.tsx b/packages/app/src/components/HeatmapSettingsDrawer.tsx index db2b5b56e5..15b47a02b9 100644 --- a/packages/app/src/components/HeatmapSettingsDrawer.tsx +++ b/packages/app/src/components/HeatmapSettingsDrawer.tsx @@ -3,18 +3,10 @@ import { useForm, useWatch } from 'react-hook-form'; import { z } from 'zod'; import { zodResolver } from '@hookform/resolvers/zod'; import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; -import { - Box, - Button, - Divider, - Drawer, - Group, - SegmentedControl, - Stack, - Text, -} from '@mantine/core'; +import { Button, Divider, Drawer, Group, Stack } from '@mantine/core'; import { IconPlayerPlay } from '@tabler/icons-react'; +import { HeatmapScaleControl } from '@/components/DBHeatmapChart/HeatmapScaleControl'; import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor'; const HeatmapSettingsSchema = z.object({ @@ -72,24 +64,10 @@ export default function HeatmapSettingsDrawer({ >
- - - Scale - - { - if (v === 'log' || v === 'linear') { - form.setValue('scaleType', v); - } - }} - data={[ - { label: 'Log', value: 'log' }, - { label: 'Linear', value: 'linear' }, - ]} - /> - + form.setValue('scaleType', v)} + /> diff --git a/packages/app/src/components/__tests__/ChartDisplaySettingsDrawer.test.tsx b/packages/app/src/components/__tests__/ChartDisplaySettingsDrawer.test.tsx index cc0a906752..a4a950074b 100644 --- a/packages/app/src/components/__tests__/ChartDisplaySettingsDrawer.test.tsx +++ b/packages/app/src/components/__tests__/ChartDisplaySettingsDrawer.test.tsx @@ -30,6 +30,51 @@ describe('ChartDisplaySettingsDrawer', () => { jest.clearAllMocks(); }); + describe('heatmap y axis scale', () => { + it('shows the scale only for heatmaps', () => { + const { unmount } = renderWithMantine( + , + ); + expect( + screen.queryByTestId('heatmap-scale-control'), + ).not.toBeInTheDocument(); + unmount(); + + renderWithMantine( + , + ); + expect(screen.getByTestId('heatmap-scale-control')).toBeInTheDocument(); + }); + + it('applies the chosen scale', async () => { + const onChange = jest.fn(); + const user = userEvent.setup(); + renderWithMantine( + , + ); + + await user.click(screen.getByText('Linear')); + await user.click(screen.getByRole('button', { name: /apply/i })); + + expect(onChange.mock.calls[0][0]).toMatchObject({ + heatmapScaleType: 'linear', + }); + }); + }); + describe('color picker section', () => { it('shows the color picker when displayType is Number', () => { renderWithMantine( diff --git a/packages/app/tests/e2e/components/ChartEditorComponent.ts b/packages/app/tests/e2e/components/ChartEditorComponent.ts index 213d02594a..30a1051d43 100644 --- a/packages/app/tests/e2e/components/ChartEditorComponent.ts +++ b/packages/app/tests/e2e/components/ChartEditorComponent.ts @@ -71,6 +71,13 @@ export class ChartEditorComponent { await this.chartTypeInput.getByRole('tab', { name: tabName }).click(); } + /** The heatmap's "Value" (y axis) SQL input. */ + get heatmapValueInput(): Locator { + return this.editorForm() + .getByTestId('heatmap-value-input') + .locator('.cm-content'); + } + /** * Set group by expression */ @@ -1143,6 +1150,27 @@ export class ChartEditorComponent { await this.applyDisplaySettings(); } + /** + * Set a heatmap's "Y axis scale" in the Display Settings drawer. Opens the + * drawer, picks the scale, then applies and closes. + */ + async setHeatmapScale(scale: 'Log' | 'Linear') { + await this.openDisplaySettings(); + await this.page + .getByTestId('heatmap-scale-control') + .locator('.mantine-SegmentedControl-label') + .filter({ hasText: new RegExp(`^${scale}$`) }) + .click(); + await this.applyDisplaySettings(); + } + + /** A "Y axis scale" option's (visually hidden) radio in the open drawer. */ + heatmapScaleOption(scale: 'Log' | 'Linear'): Locator { + return this.page + .getByTestId('heatmap-scale-control') + .getByRole('radio', { name: scale, exact: true }); + } + /** * Set the "Legend template" value in the Display Settings drawer (PromQL * charts only). Opens the drawer, fills the input, then applies and closes. diff --git a/packages/app/tests/e2e/features/heatmap-series.spec.ts b/packages/app/tests/e2e/features/heatmap-series.spec.ts new file mode 100644 index 0000000000..db1b780f7b --- /dev/null +++ b/packages/app/tests/e2e/features/heatmap-series.spec.ts @@ -0,0 +1,144 @@ +import { DisplayType } from '@hyperdx/common-utils/dist/types'; + +import { DashboardPage } from '../page-objects/DashboardPage'; +import { getApiUrl, getSources } from '../utils/api-helpers'; +import { expect, test } from '../utils/base-test'; +import { DEFAULT_TRACES_SOURCE_NAME } from '../utils/constants'; + +test.describe('Heatmap tiles', { tag: ['@full-stack', '@dashboard'] }, () => { + test('switching a new tile to heatmap fills in the trace duration', async ({ + page, + }) => { + const dashboardPage = new DashboardPage(page); + const editor = dashboardPage.chartEditor; + + await dashboardPage.goto(); + await dashboardPage.createNewDashboard(); + await dashboardPage.addTile(); + await expect(editor.nameInput).toBeVisible(); + await editor.waitForDataToLoad(); + await editor.setChartType(DisplayType.Heatmap); + + await expect(editor.source).toHaveValue(DEFAULT_TRACES_SOURCE_NAME); + // The placeholder also mentions Duration, so match the full expression. + await expect(editor.heatmapValueInput).toHaveText('(Duration)/1e6'); + }); + + test('distribution heatmap saves unchanged and keeps its event deltas link', async ({ + page, + }) => { + test.setTimeout(120000); + const dashboardPage = new DashboardPage(page); + const editor = dashboardPage.chartEditor; + const ts = Date.now(); + + let dashboardId = ''; + await test.step('Seed a distribution heatmap tile', async () => { + const traceSource = (await getSources(page, 'trace')).find( + s => s.name === DEFAULT_TRACES_SOURCE_NAME, + ); + expect(traceSource).toBeDefined(); + const response = await page.request.post(`${getApiUrl()}/dashboards`, { + data: { + name: `E2E Distribution Heatmap ${ts}`, + tags: [], + tiles: [ + { + id: `distribution-heatmap-${ts}`, + x: 0, + y: 0, + w: 12, + h: 10, + config: { + name: 'Distribution heatmap', + displayType: DisplayType.Heatmap, + source: traceSource._id, + select: [ + { + aggFn: 'count', + aggCondition: '', + aggConditionLanguage: 'lucene', + valueExpression: '(Duration)/1e6', + countExpression: 'count()', + heatmapScaleType: 'log', + }, + ], + where: '', + whereLanguage: 'lucene', + numberFormat: { output: 'duration', factor: 0.001 }, + }, + }, + ], + }, + }); + expect(response.ok()).toBe(true); + const body = await response.json(); + dashboardId = body.id; + }); + + const assertTileRenders = 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', async () => { + await dashboardPage.gotoDashboard(dashboardId); + await assertTileRenders(); + }); + + await test.step('Edit and save the tile unchanged', async () => { + await dashboardPage.editTile(0); + await expect(editor.nameInput).toBeVisible(); + await editor.waitForDataToLoad(); + // Known bug: the tile-menu click that opens the editor also bubbles to + // the tile and opens its event deltas popover, whose overlay covers the + // editor. Clicking the overlay dismisses it without reaching the editor. + // Drop these lines once the tile ignores clicks from its menu. + await expect( + page.getByTestId('heatmap-view-event-deltas-link'), + ).toBeVisible(); + await page.mouse.click(5, 5); + await editor.save(); + await dashboardPage.reload(); + await assertTileRenders(); + }); + + await test.step('Switch the y axis scale in display settings', async () => { + await dashboardPage.editTile(0); + await expect(editor.nameInput).toBeVisible(); + await editor.waitForDataToLoad(); + // The same known popover bug as above. + await expect( + page.getByTestId('heatmap-view-event-deltas-link'), + ).toBeVisible(); + await page.mouse.click(5, 5); + await editor.setHeatmapScale('Linear'); + await editor.save(); + await dashboardPage.reload(); + await assertTileRenders(); + + await dashboardPage.editTile(0); + await expect(editor.nameInput).toBeVisible(); + await editor.waitForDataToLoad(); + await expect( + page.getByTestId('heatmap-view-event-deltas-link'), + ).toBeVisible(); + await page.mouse.click(5, 5); + await editor.openDisplaySettings(); + await expect(editor.heatmapScaleOption('Linear')).toBeChecked(); + await editor.applyDisplaySettings(); + await editor.save(); + }); + + await test.step('Clicking the tile links to event deltas', async () => { + await dashboardPage.getTileHeatmap().container.click(); + const link = page.getByTestId('heatmap-view-event-deltas-link'); + await expect(link).toBeVisible(); + await link.click(); + await expect(page).toHaveURL(/\/search\?.*mode=delta/); + }); + }); +});