diff --git a/.changeset/heatmap-series-mode.md b/.changeset/heatmap-series-mode.md new file mode 100644 index 0000000000..d0f1d967a2 --- /dev/null +++ b/.changeset/heatmap-series-mode.md @@ -0,0 +1,7 @@ +--- +'@hyperdx/app': minor +'@hyperdx/common-utils': minor +'@hyperdx/api': patch +--- + +feat: Add a series mode to builder heatmaps diff --git a/packages/api/src/mcp/tools/dashboards/schemas.ts b/packages/api/src/mcp/tools/dashboards/schemas.ts index a944ff5fff..28a09cb92b 100644 --- a/packages/api/src/mcp/tools/dashboards/schemas.ts +++ b/packages/api/src/mcp/tools/dashboards/schemas.ts @@ -928,7 +928,7 @@ const mcpHeatmapSelectItemSchema = z.object({ }); // Heatmap tiles are builder-only and currently restricted to Trace sources -// (see HEATMAP_ALLOWED_SOURCE_KINDS in `packages/common-utils/src/guards.ts`). +// (see HEATMAP_DISTRIBUTION_SOURCE_KINDS in `packages/common-utils/src/guards.ts`). // The save path runs `getHeatmapTilesWithIncompatibleSources` after schema // validation to enforce that, mirroring the REST handler. const mcpHeatmapTileSchema = mcpTileLayoutSchema.extend({ diff --git a/packages/api/src/routers/external-api/__tests__/dashboards.int.test.ts b/packages/api/src/routers/external-api/__tests__/dashboards.int.test.ts index af0eb9327b..04eb023ef8 100644 --- a/packages/api/src/routers/external-api/__tests__/dashboards.int.test.ts +++ b/packages/api/src/routers/external-api/__tests__/dashboards.int.test.ts @@ -1,5 +1,6 @@ import { isBuilderSavedChartConfig } from '@hyperdx/common-utils/dist/guards'; import { + DisplayType, MetricsDataType, OnboardingTaskId, SourceKind, @@ -2879,6 +2880,39 @@ describe('External API v2 Dashboards - new format', () => { ); }); + it('skips series-mode heatmap tiles on GET', async () => { + // Series-mode heatmaps have no external representation yet; emitting + // them as distribution tiles would corrupt them on GET -> PUT. + const dashboard = await createTestDashboard({ + tiles: [ + { + id: new ObjectId().toString(), + x: 0, + y: 0, + w: 6, + h: 3, + config: { + name: 'Series heatmap', + displayType: DisplayType.Heatmap, + source: traceSource._id.toString(), + select: [ + { aggFn: 'count', aggCondition: '', valueExpression: '' }, + ], + groupBy: 'ServiceName', + where: '', + heatmap: { mode: 'series' }, + }, + }, + ], + }); + + const response = await authRequest( + 'get', + `${BASE_URL}/${dashboard._id}`, + ).expect(200); + expect(response.body.data.tiles).toEqual([]); + }); + it('round-trips a heatmap tile with only required fields', async () => { // Covers the minimal payload path: countExpression, heatmapScaleType, // where, whereLanguage, and numberFormat are all omitted on the diff --git a/packages/api/src/routers/external-api/v2/utils/alertChartConfig.ts b/packages/api/src/routers/external-api/v2/utils/alertChartConfig.ts index f7356078f9..a2f660a675 100644 --- a/packages/api/src/routers/external-api/v2/utils/alertChartConfig.ts +++ b/packages/api/src/routers/external-api/v2/utils/alertChartConfig.ts @@ -107,6 +107,8 @@ export const EVALUATION_INERT_CONFIG_KEYS: ReadonlySet = new Set([ 'fitYAxisToData', 'onClick', 'alternateRowBackground', + // Heatmap tiles carry no alerts; alert display types ignore it. + 'heatmap', // Client-side null-bucket rendering; the alert task ignores it. 'fillNulls', // Evaluation-relevant only with a groupBy, which (on variants whose shape diff --git a/packages/api/src/routers/external-api/v2/utils/dashboards.ts b/packages/api/src/routers/external-api/v2/utils/dashboards.ts index fa3a4eb311..ec6890d8fe 100644 --- a/packages/api/src/routers/external-api/v2/utils/dashboards.ts +++ b/packages/api/src/routers/external-api/v2/utils/dashboards.ts @@ -1,3 +1,4 @@ +import { getHeatmapMode } from '@hyperdx/common-utils/dist/core/heatmap'; import { displayTypeSupportsBuilderAlerts, displayTypeSupportsRawSqlAlerts, @@ -538,6 +539,20 @@ function convertTileToExternalChart( return undefined; } + // Series-mode heatmaps have no external schema yet. Emitting them as + // distribution tiles would corrupt them on a GET -> PUT round-trip. + if ( + isBuilderSavedChartConfig(tile.config) && + tile.config.displayType === DisplayType.Heatmap && + getHeatmapMode(tile.config) === 'series' + ) { + logger.warn( + { dashboardId, tileId: tile.id }, + 'Skipping series-mode heatmap tile in external API response (not yet supported)', + ); + return undefined; + } + // Returned in case of a failure converting the saved chart config const defaultTileConfig: ExternalDashboardTileConfig = isRawSqlSavedChartConfig(tile.config) @@ -1051,7 +1066,7 @@ function getMissingSources( /** * Returns source IDs referenced by heatmap tiles that exist but are not * compatible with heatmap rendering. The heatmap UI gates the source picker - * via the same `HEATMAP_ALLOWED_SOURCE_KINDS` set used here (see + * via the same `HEATMAP_DISTRIBUTION_SOURCE_KINDS` set used here (see * `packages/common-utils/src/guards.ts` and `ChartEditorControls.tsx`), so * UI and API gates move together. */ diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index 5888314024..82c19c3857 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -249,6 +249,7 @@ function HeatmapTile({ enabled?: boolean; }) { const heatmapQuery = toHeatmapQuery(queriedConfig); + const { mode } = heatmapQuery; const [clickPos, setClickPos] = useState<{ x: number; y: number } | null>( null, @@ -256,7 +257,9 @@ function HeatmapTile({ const containerRef = useRef(null); const eventDeltasUrl = useMemo(() => { - if (!source) return null; + // Search page event deltas only supports trace sources and distribution mode + if (!source || !isTraceSource(source) || mode !== 'distribution') + return null; const url = buildEventsSearchUrl({ source, config: queriedConfig, @@ -265,7 +268,7 @@ function HeatmapTile({ if (!url) return null; const separator = url.includes('?') ? '&' : '?'; return `${url}${separator}mode=delta`; - }, [source, queriedConfig, dateRange]); + }, [source, mode, queriedConfig, dateRange]); const handleClick = useCallback( (e: React.MouseEvent) => { diff --git a/packages/app/src/components/ChartDisplaySettingsDrawer.tsx b/packages/app/src/components/ChartDisplaySettingsDrawer.tsx index b65654f4b6..559f859348 100644 --- a/packages/app/src/components/ChartDisplaySettingsDrawer.tsx +++ b/packages/app/src/components/ChartDisplaySettingsDrawer.tsx @@ -7,6 +7,7 @@ import { import { ChartConfigWithDateRange, DisplayType, + HeatmapMode, MAX_LEGEND_TEMPLATE_LENGTH, NumberFormat, } from '@hyperdx/common-utils/dist/types'; @@ -63,7 +64,7 @@ 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]. + // Distribution heatmaps only; stored on the heatmap's select[0]. heatmapScaleType?: HeatmapScaleType; }; @@ -86,6 +87,8 @@ interface ChartDisplaySettingsDrawerProps { configType?: 'sql' | 'builder' | 'promql'; /** Whether a PromQL tile's queried expression runs over a range. */ promqlUsesRange?: boolean; + /** A builder heatmap's mode; only distribution heatmaps have a numeric y axis to scale. */ + heatmapMode?: HeatmapMode; previousDateRange?: [Date, Date]; onChange: (settings: ChartConfigDisplaySettings, isDirty: boolean) => void; onClose: () => void; @@ -127,6 +130,7 @@ export default function ChartDisplaySettingsDrawer({ displayType, configType, promqlUsesRange = false, + heatmapMode, defaultNumberFormat, onChange, onClose, @@ -248,7 +252,9 @@ export default function ChartDisplaySettingsDrawer({ ? 'Available on PromQL range queries.' : 'Available on query-builder number tiles.'; - const showHeatmapScale = displayType === DisplayType.Heatmap; + // Log scale only means something on a numeric (distribution mode) y axis + const showHeatmapScale = + displayType === DisplayType.Heatmap && heatmapMode === 'distribution'; return ( ({ + getStoredLanguage: jest.fn().mockReturnValue('lucene'), +})); + +const traceSource: TSource = { + id: 'source-trace', + name: 'Trace Source', + kind: SourceKind.Trace, + connection: 'conn-1', + from: { databaseName: 'db', tableName: 'spans' }, + timestampValueExpression: 'Timestamp', + durationExpression: 'Duration', + spanIdExpression: 'SpanId', + traceIdExpression: 'TraceId', + parentSpanIdExpression: 'ParentSpanId', + defaultTableSelectExpression: 'SpanName', + implicitColumnExpression: 'SpanName', + statusCodeExpression: 'StatusCode', + spanNameExpression: 'SpanName', + spanKindExpression: 'SpanKind', + durationPrecision: 9, +}; + +const metricSource: TMetricSource = { + id: 'source-metric', + name: 'Metric Source', + kind: SourceKind.Metric, + connection: 'conn-1', + from: { databaseName: 'db', tableName: '' }, + timestampValueExpression: 'TimeUnix', + metricTables: { + gauge: 'gauge_table', + histogram: '', + sum: '', + summary: '', + 'exponential histogram': '', + }, + resourceAttributesExpression: 'ResourceAttributes', +}; + +const legacySelectItem = { + aggFn: 'count' as const, + aggCondition: '', + aggConditionLanguage: 'lucene' as const, + valueExpression: '(Duration)/1e6', + countExpression: 'count()', + heatmapScaleType: 'linear' as const, +}; + +// A distribution heatmap as the editor saved it before `heatmap` existed. +const legacyHeatmapTile: BuilderSavedChartConfig = { + name: 'Latency', + displayType: DisplayType.Heatmap, + source: 'source-trace', + select: [legacySelectItem], + where: "ServiceName = 'api'", + whereLanguage: 'sql', + numberFormat: { output: 'duration', factor: 0.001 }, +}; + +function roundTrip( + config: BuilderSavedChartConfig, + source: TSource, +): BuilderSavedChartConfig { + const saved = convertFormStateToSavedChartConfig( + convertSavedChartConfigToFormState(config), + source, + ); + if (saved == null || !isBuilderSavedChartConfig(saved)) { + throw new Error('Expected a builder config'); + } + return saved; +} + +describe('heatmap form state conversion', () => { + it('round-trips a tile saved before heatmap modes existed unchanged', () => { + const saved = roundTrip(legacyHeatmapTile, traceSource); + + expect(saved).toMatchObject(legacyHeatmapTile); + expect(saved.heatmap).toBeUndefined(); + }); + + it('round-trips a distribution tile and drops its group by', () => { + const saved = roundTrip( + { + ...legacyHeatmapTile, + groupBy: 'ServiceName', + heatmap: { mode: 'distribution' }, + }, + traceSource, + ); + expect(saved.heatmap).toEqual({ mode: 'distribution' }); + expect(saved.groupBy).toBeUndefined(); + }); + + it('round-trips a series tile', () => { + const seriesTile: BuilderSavedChartConfig = { + name: 'Requests', + displayType: DisplayType.Heatmap, + source: 'source-trace', + select: [ + { + aggFn: 'avg', + aggCondition: '', + aggConditionLanguage: 'lucene', + valueExpression: 'Duration', + }, + ], + groupBy: 'ServiceName', + where: '', + heatmap: { mode: 'series' }, + }; + + const saved = roundTrip(seriesTile, traceSource); + expect(saved.heatmap).toEqual({ mode: 'series' }); + expect(saved.groupBy).toBe('ServiceName'); + expect(saved.select).toEqual(seriesTile.select); + }); + + it('drops the where from a series tile, which has no input for it', () => { + const form = convertSavedChartConfigToFormState({ + ...legacyHeatmapTile, + heatmap: { mode: 'series' }, + }); + + expect(convertFormStateToSavedChartConfig(form, traceSource)).toMatchObject( + { where: '' }, + ); + expect( + convertFormStateToChartConfig( + form, + [new Date('2024-01-01'), new Date('2024-01-02')], + traceSource, + ), + ).toMatchObject({ where: '' }); + }); + + it('keeps the heatmap mode on the queried config', () => { + const config = convertFormStateToChartConfig( + convertSavedChartConfigToFormState({ + ...legacyHeatmapTile, + heatmap: { mode: 'series' }, + }), + [new Date('2024-01-01'), new Date('2024-01-02')], + traceSource, + ); + expect(config).toMatchObject({ heatmap: { mode: 'series' } }); + }); + + it('drops the heatmap mode from other display types', () => { + const saved = convertFormStateToSavedChartConfig( + { + ...convertSavedChartConfigToFormState({ + ...legacyHeatmapTile, + heatmap: { mode: 'series' }, + }), + displayType: DisplayType.Line, + }, + traceSource, + ); + expect(saved).toMatchObject({ heatmap: undefined }); + }); +}); + +describe('heatmap validation', () => { + const validate = (form: Partial, source: TSource) => + validateChartForm( + { series: [], ...form } as ChartEditorFormState, + source, + jest.fn(), + ).map(e => e.message); + + it('requires a value expression in distribution mode', () => { + expect( + validate( + { + displayType: DisplayType.Heatmap, + source: 'source-trace', + series: [{ aggFn: 'count', aggCondition: '', valueExpression: '' }], + }, + traceSource, + ), + ).toContain('Value expression is required for heatmap charts'); + }); + + it('accepts a count series without an expression in series mode', () => { + expect( + validate( + { + displayType: DisplayType.Heatmap, + source: 'source-trace', + heatmap: { mode: 'series' }, + series: [{ aggFn: 'count', aggCondition: '', valueExpression: '' }], + }, + traceSource, + ), + ).toEqual([]); + }); + + it('requires an expression for non-count series in series mode', () => { + expect( + validate( + { + displayType: DisplayType.Heatmap, + source: 'source-trace', + heatmap: { mode: 'series' }, + series: [{ aggFn: 'avg', aggCondition: '', valueExpression: '' }], + }, + traceSource, + ), + ).toEqual(['Expression is required for series 1']); + }); + + it('requires a metric name on metric sources in series mode', () => { + expect( + validate( + { + displayType: DisplayType.Heatmap, + source: 'source-metric', + heatmap: { mode: 'series' }, + series: [ + { + aggFn: 'avg', + aggCondition: '', + valueExpression: 'Value', + metricType: MetricsDataType.Gauge, + }, + ], + }, + metricSource, + ), + ).toEqual(['Metric is required']); + }); +}); diff --git a/packages/app/src/components/ChartEditor/utils.ts b/packages/app/src/components/ChartEditor/utils.ts index af2dfe3a45..9320453535 100644 --- a/packages/app/src/components/ChartEditor/utils.ts +++ b/packages/app/src/components/ChartEditor/utils.ts @@ -1,6 +1,7 @@ import { omit, pick } from 'lodash'; import { Path, UseFormSetError } from 'react-hook-form'; import { validateFormula } from '@hyperdx/common-utils/dist/core/formula'; +import { getHeatmapMode } from '@hyperdx/common-utils/dist/core/heatmap'; import { displayTypeSupportsInstantQuery, displayTypeSupportsReducer, @@ -12,7 +13,7 @@ import { validateRawSqlForAlert, } from '@hyperdx/common-utils/dist/core/utils'; import { - HEATMAP_ALLOWED_SOURCE_KINDS, + getHeatmapSourceKinds, isBuilderSavedChartConfig, isPromqlSavedChartConfig, isRawSqlSavedChartConfig, @@ -25,6 +26,7 @@ import { ChartVariable, DisplayType, getSampleWeightExpression, + HeatmapMode, isLogSource, isMetricSource, isRangeThresholdType, @@ -91,6 +93,9 @@ function normalizeChartConfig< | 'onClick' | 'formulas' | 'showOperandSeries' + | 'groupBy' + | 'heatmap' + | 'where' >, >(config: C, source: TSource): C { const isMetricSource = source.kind === SourceKind.Metric; @@ -131,6 +136,25 @@ function normalizeChartConfig< config.onClick && config.displayType === DisplayType.Table ? config.onClick : undefined, + ...normalizeHeatmapFields(config), + }; +} + +/** Remove or default heatmap-related fields based on displayType and heatmap mode */ +function normalizeHeatmapFields< + C extends Pick< + BuilderSavedChartConfig, + 'displayType' | 'groupBy' | 'heatmap' | 'where' + >, +>(config: C): Pick { + if (config.displayType !== DisplayType.Heatmap) { + return { groupBy: config.groupBy, heatmap: undefined, where: config.where }; + } + const isDistribution = getHeatmapMode(config) === 'distribution'; + return { + groupBy: isDistribution ? undefined : config.groupBy, + heatmap: config.heatmap, + where: isDistribution ? config.where : '', }; } @@ -194,15 +218,17 @@ const ROW_LISTING_SOURCE_KINDS = NON_PROMQL_SOURCE_KINDS.filter( export function getAllowedSourceKinds({ configType, displayType, + heatmapMode, }: { configType: ChartEditorFormState['configType']; displayType: DisplayType | undefined; + heatmapMode?: HeatmapMode; }): SourceKind[] { if (configType === 'promql' && isPromqlDisplayType(displayType)) { return [SourceKind.Promql]; } if (displayType === DisplayType.Heatmap) { - return [...HEATMAP_ALLOWED_SOURCE_KINDS]; + return [...getHeatmapSourceKinds(heatmapMode)]; } if (isStringSelectDisplayType(displayType)) { return ROW_LISTING_SOURCE_KINDS; @@ -711,10 +737,12 @@ export const validateChartForm = ( }); } - // Validate heatmap requires a value expression + // Distribution heatmaps require a value expression. Series heatmaps are + // validated like any other builder series above. if ( !isRawSqlChart && form.displayType === DisplayType.Heatmap && + getHeatmapMode(form) === 'distribution' && Array.isArray(form.series) && form.series.length > 0 && !form.series[0]?.valueExpression diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx index 6b17169d4e..963f3c2df1 100644 --- a/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx @@ -13,6 +13,7 @@ import { isBuilderChartConfig } from '@hyperdx/common-utils/dist/guards'; import { ChartConfigWithOptTimestamp, DisplayType, + HeatmapMode, SourceKind, TSource, } from '@hyperdx/common-utils/dist/types'; @@ -83,6 +84,8 @@ type ChartEditorControlsProps = { chartConfigForExplanations?: ChartConfigWithOptTimestamp; onSubmit: (suppressErrorNotification?: boolean) => void; openDisplaySettings: () => void; + heatmapMode: HeatmapMode; + onHeatmapModeChange: (mode: HeatmapMode) => void; }; export function ChartEditorControls({ @@ -116,6 +119,8 @@ export function ChartEditorControls({ chartConfigForExplanations, onSubmit, openDisplaySettings, + heatmapMode, + onHeatmapModeChange, }: ChartEditorControlsProps) { // Formulas (HDX-5080): derived series computed from the chart's series via // letter-ref arithmetic expressions. Metric and event (log/trace) sources, @@ -225,12 +230,15 @@ export function ChartEditorControls({ key={fields[0]?.id} control={control} setValue={setValue} + clearErrors={clearErrors} errors={errors} tableSource={tableSource} dateRange={dateRange} parentRef={parentRef} onSubmit={onSubmit} onOpenDisplaySettings={openDisplaySettings} + mode={heatmapMode} + onModeChange={onHeatmapModeChange} /> ) : displayType === DisplayType.EventPatterns ? ( diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx index 49a71a6182..c15f0687cc 100644 --- a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx @@ -25,6 +25,7 @@ import { DBBarChart } from '@/components/DBBarChart'; import DBHeatmapChart, { buildHeatmapBoundsConfig, buildHeatmapBucketConfig, + buildHeatmapSeriesConfig, HEATMAP_N_BUCKETS, toHeatmapQuery, } from '@/components/DBHeatmapChart'; @@ -79,7 +80,8 @@ function HeatmapPreview({ * Heatmap renders via two sequential ClickHouse queries — bounds first, then * the bucketed-counts query that uses the resolved min/max. Show both, * labeled, with placeholder tokens for the bucket-array literals (which only - * exist at runtime once the bounds query returns). + * exist at runtime once the bounds query returns). A series heatmap runs a + * single time-chart query instead. */ function HeatmapSQLPreview({ config, @@ -96,10 +98,22 @@ function HeatmapSQLPreview({ ...config, timestampValueExpression, }; - const { config: heatmapConfig, scaleType } = - toHeatmapQuery(configWithTimestamp); + const query = toHeatmapQuery(configWithTimestamp); const granularity = convertDateRangeToGranularityString(dateRange, 245); + if (query.mode === 'series') { + return ( + + ); + } + const { config: heatmapConfig, scaleType } = query; + const boundsConfig = buildHeatmapBoundsConfig({ config: heatmapConfig, scaleType, diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartSeriesEditor.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartSeriesEditor.tsx index feecf7049f..49debf3cab 100644 --- a/packages/app/src/components/DBEditTimeChartForm/ChartSeriesEditor.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/ChartSeriesEditor.tsx @@ -70,6 +70,7 @@ type ChartSeriesEditorProps = { showHaving: boolean; showDuplicate: boolean; showColor: boolean; + showSeriesNumberFormat?: boolean; tableName: string; length: number; tableSource?: TSource; @@ -93,6 +94,7 @@ export function ChartSeriesEditor({ showHaving, showDuplicate, showColor, + showSeriesNumberFormat = true, tableName: _tableName, parentRef, length, @@ -300,7 +302,9 @@ export function ChartSeriesEditor({ onSwap={onSwapSeries} onRemove={length > 1 ? onRemoveSeries : undefined} onDuplicate={showDuplicate ? onDuplicateSeries : undefined} - onOpenNumberFormat={openSeriesNumberFormat} + onOpenNumberFormat={ + showSeriesNumberFormat ? openSeriesNumberFormat : undefined + } leadingSection={ isFormulaSourceKind(tableSource?.kind) ? ( diff --git a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx index a4dd663af9..857d4986f7 100644 --- a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx @@ -1,12 +1,15 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { isEqual } from 'lodash'; import { Controller, useFieldArray, useForm, + type UseFormGetValues, type UseFormSetValue, useWatch, } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; +import { getHeatmapMode } from '@hyperdx/common-utils/dist/core/heatmap'; import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; import { isReducibleRangeQuery } from '@hyperdx/common-utils/dist/core/promql'; import { @@ -15,6 +18,7 @@ import { } from '@hyperdx/common-utils/dist/core/utils'; import { displayTypeRequiresSource, + getHeatmapSourceKinds, isRawSqlSavedChartConfig, } from '@hyperdx/common-utils/dist/guards'; import { @@ -23,6 +27,7 @@ import { DashboardFilter, DisplayType, Filter, + HeatmapMode, SavedChartConfig, SourceKind, TSource, @@ -152,23 +157,52 @@ type EditTimeChartFormProps = { const ALERT_IGNORES_DASHBOARD_FILTERS = 'Dashboard-level filter and variable selections cannot be applied when an alert is configured.'; -/** Populate form state with the standard heatmap series + duration numberFormat. */ +/** + * Populate form state with the standard series for a heatmap mode. + * Distribution heatmaps bucket the trace duration (or `fallbackValueExpression` + * on a trace source without one) with a duration number format; series + * heatmaps start from a plain count. + */ function applyHeatmapDefaults( setValue: UseFormSetValue, - valueExpression: string, + getValues: UseFormGetValues, + { + mode, + tableSource, + fallbackValueExpression = '', + }: { + mode: HeatmapMode; + tableSource: TSource | undefined; + fallbackValueExpression?: string; + }, ) { + const isTraceDuration = + tableSource?.kind === SourceKind.Trace && !!tableSource.durationExpression; const heatmapSeries: SavedChartConfigWithSelectArray['select'] = [ { aggFn: 'count', aggCondition: '', aggConditionLanguage: getStoredLanguage() ?? 'lucene', - valueExpression, + valueExpression: + mode === 'distribution' + ? isTraceDuration + ? getDurationMsExpression(tableSource) + : fallbackValueExpression + : '', }, ]; setValue('select', heatmapSeries); setValue('series', heatmapSeries); - setValue('series.0.countExpression', 'count()'); - setValue('numberFormat', { ...HEATMAP_DURATION_NUMBER_FORMAT }); + + if (mode === 'distribution') { + setValue('series.0.countExpression', 'count()'); + setValue('numberFormat', { ...HEATMAP_DURATION_NUMBER_FORMAT }); + } else if ( + isEqual(getValues('numberFormat'), HEATMAP_DURATION_NUMBER_FORMAT) + ) { + // Unselect the default-applied duration format for series mode + setValue('numberFormat', undefined); + } } export default function EditTimeChartForm({ @@ -276,6 +310,9 @@ export default function EditTimeChartForm({ const configType = useWatch({ control, name: 'configType' }); const connection = useWatch({ control, name: 'connection' }); const promqlExpressions = useWatch({ control, name: 'promqlExpressions' }); + const heatmapMode = getHeatmapMode({ + heatmap: useWatch({ control, name: 'heatmap' }), + }); const chartConfigAlert = chartConfig.alert; const isRawSqlInput = @@ -286,8 +323,8 @@ export default function EditTimeChartForm({ const { data: sources } = useSources(); const allowedSourceKinds = useMemo( - () => getAllowedSourceKinds({ configType, displayType }), - [configType, displayType], + () => getAllowedSourceKinds({ configType, displayType, heatmapMode }), + [configType, displayType, heatmapMode], ); // A source selection that current mode can't query counts as no source, @@ -706,11 +743,15 @@ export default function EditTimeChartForm({ // Switching editor mode or display type can invalidate the selected source: // Swap in the first source the picker still offers rather than // leaving a selection it no longer lists. - const prevSourceModeRef = useRef({ configType, displayType }); + const prevSourceModeRef = useRef({ configType, displayType, heatmapMode }); useEffect(() => { - // Run only on configType and displayType changes + // Run only on configType, displayType and heatmap mode changes const prev = prevSourceModeRef.current; - if (prev.configType === configType && prev.displayType === displayType) { + if ( + prev.configType === configType && + prev.displayType === displayType && + prev.heatmapMode === heatmapMode + ) { return; } @@ -718,7 +759,7 @@ export default function EditTimeChartForm({ if (sources == null) return; if (!displayTypeRequiresSource(displayType)) return; - prevSourceModeRef.current = { configType, displayType }; + prevSourceModeRef.current = { configType, displayType, heatmapMode }; // Builder and PromQL require a source, so an empty selection // should be filled in. Raw SQL source is optional, so leave it. @@ -737,15 +778,16 @@ export default function EditTimeChartForm({ // Select the first valid source setValue('source', firstSourceItemValue(allowedSourceItems) ?? ''); - // Record that a display-type change triggered a source change, so - // that auto-submit runs in the correct effect below. + // Record that a display-type or heatmap mode change triggered a source + // change, so that auto-submit runs in the correct effect below. isDisplayTypeSourceSwapPendingRef.current = - prev.displayType !== displayType; + prev.displayType !== displayType || prev.heatmapMode !== heatmapMode; }, [ allowedSourceItems, allowedSourceKinds, configType, displayType, + heatmapMode, setValue, sourceId, sources, @@ -769,18 +811,16 @@ export default function EditTimeChartForm({ // Two entry paths into Heatmap: // - From Search/RawSQL: select is a string; clear `where` too // - From another builder tab: select is already an array - const fallbackValue = Array.isArray(select) - ? (select[0]?.valueExpression ?? '') - : ''; - const defaultValue = - tableSource?.kind === SourceKind.Trace && - tableSource.durationExpression - ? getDurationMsExpression(tableSource) - : fallbackValue; if (typeof select === 'string') { setValue('where', ''); } - applyHeatmapDefaults(setValue, defaultValue); + applyHeatmapDefaults(setValue, getValues, { + mode: heatmapMode, + tableSource, + fallbackValueExpression: Array.isArray(select) + ? (select[0]?.valueExpression ?? '') + : '', + }); } else if (!Array.isArray(select)) { const defaultSeries: SavedChartConfigWithSelectArray['select'] = [ { @@ -802,7 +842,31 @@ export default function EditTimeChartForm({ onSubmit(true); } } - }, [displayType, select, setValue, onSubmit, configType, tableSource]); + }, [ + displayType, + select, + setValue, + getValues, + onSubmit, + configType, + tableSource, + heatmapMode, + ]); + + // Switching heatmap mode may trigger a source swap, and should apply new defaults. + const onHeatmapModeChange = useCallback( + (mode: HeatmapMode) => { + setValue('heatmap.mode', mode); + applyHeatmapDefaults(setValue, getValues, { mode, tableSource }); + if ( + tableSource != null && + getHeatmapSourceKinds(mode).includes(tableSource.kind) + ) { + onSubmit(true); + } + }, + [setValue, getValues, tableSource, onSubmit], + ); // Handle auto-submitting and form state updates when the source changes. useEffect(() => { @@ -816,15 +880,27 @@ export default function EditTimeChartForm({ if ( displayType === DisplayType.Heatmap && + heatmapMode === 'distribution' && tableSource?.kind === SourceKind.Trace && tableSource.durationExpression ) { - applyHeatmapDefaults(setValue, getDurationMsExpression(tableSource)); + applyHeatmapDefaults(setValue, getValues, { + mode: 'distribution', + tableSource, + }); onSubmit(true); } else if (swappedSourceDueToDisplayTypeChange) { onSubmit(true); } - }, [sourceId, displayType, tableSource, setValue, onSubmit]); + }, [ + sourceId, + displayType, + heatmapMode, + tableSource, + setValue, + getValues, + onSubmit, + ]); // Emulate the date range picker auto-searching similar to dashboards useEffect(() => { @@ -910,7 +986,10 @@ export default function EditTimeChartForm({ if (configType === 'promql') { setValue('legendTemplate', legendTemplate ?? ''); } - if (displayType === DisplayType.Heatmap) { + if ( + displayType === DisplayType.Heatmap && + heatmapMode === 'distribution' + ) { setValue('series.0.heatmapScaleType', heatmapScaleType); } // Display settings live in a separate drawer form, so RHF can't track @@ -921,7 +1000,7 @@ export default function EditTimeChartForm({ } onSubmit(); }, - [setValue, onDirtyChange, onSubmit, configType, displayType], + [setValue, onDirtyChange, onSubmit, configType, displayType, heatmapMode], ); const tableConnection = useMemo( @@ -1112,6 +1191,8 @@ export default function EditTimeChartForm({ chartConfigForExplanations={chartConfigForExplanations} onSubmit={onSubmit} openDisplaySettings={openDisplaySettings} + heatmapMode={heatmapMode} + onHeatmapModeChange={onHeatmapModeChange} /> )} ; setValue: UseFormSetValue; + clearErrors: UseFormClearErrors; errors: FieldErrors; tableSource?: TSource; dateRange?: [Date, Date]; parentRef: HTMLElement | null; onSubmit: () => void; onOpenDisplaySettings: () => void; + mode: HeatmapMode; + onModeChange: (mode: HeatmapMode) => void; }; function InlineLabel({ label, tooltip }: { label: string; tooltip: string }) { @@ -37,79 +56,124 @@ function InlineLabel({ label, tooltip }: { label: string; tooltip: string }) { export function HeatmapSeriesEditor({ control, setValue, + clearErrors, errors, tableSource, dateRange, parentRef, onSubmit, onOpenDisplaySettings, + mode, + onModeChange, }: HeatmapSeriesEditorProps) { const connection = useMemo(() => tcFromSource(tableSource), [tableSource]); return ( - -
- -
- -
- -
- -
-
- { + if (value === 'distribution' || value === 'series') { + onModeChange(value); + } + }} + data={[ + { label: 'Distribution', value: 'distribution' }, + { label: 'Series', value: 'series' }, + ]} + data-testid="heatmap-mode-control" + /> + {mode === 'series' ? ( + - setValue('whereLanguage', lang) - } - enableVariables + setValue={setValue} + connectionId={tableSource?.connection} + showGroupBy + showHaving={false} + showDuplicate={false} + showColor={false} + showSeriesNumberFormat={false} + tableName={tableSource?.from.tableName ?? ''} + tableSource={tableSource} + errors={Array.isArray(errors.series) ? errors.series[0] : undefined} + clearErrors={clearErrors} /> -
+ ) : ( + +
+ +
+ +
+ +
+ +
+
+ + setValue('whereLanguage', lang) + } + enableVariables + /> +
+ )}