From 012e81527cff32c595c3c49f9538bf43cce86858 Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Wed, 30 Sep 2026 16:57:33 -0400 Subject: [PATCH] feat: Support PromQL pie/bar charts --- .changeset/promql-pie-bar-tiles.md | 6 + packages/app/src/ChartUtils.tsx | 32 +++-- packages/app/src/__tests__/ChartUtils.test.ts | 63 ++++------ .../ChartEditor/PromqlQueryTypeControls.tsx | 11 +- .../__tests__/utils.test.ts | 20 +++ .../components/DBEditTimeChartForm/utils.ts | 11 +- packages/app/src/components/DBNumberChart.tsx | 23 ++-- .../components/NumberTileBackgroundChart.tsx | 4 +- .../__tests__/DBNumberChart.test.tsx | 17 --- .../components/__tests__/DBPieChart.test.tsx | 70 ++++++++++- .../NumberTileBackgroundChart.test.tsx | 4 +- .../components/charts/CategoricalChart.tsx | 19 ++- .../charts/MultipleValuesIndicator.tsx | 28 +---- .../hooks/__tests__/useChartConfig.test.tsx | 61 ++++++++- packages/app/src/hooks/useChartConfig.tsx | 18 +-- .../e2e/components/ChartEditorComponent.ts | 6 +- .../tests/e2e/features/promql-number.spec.ts | 23 +++- .../tests/e2e/features/promql-pie-bar.spec.ts | 117 ++++++++++++++++++ .../src/core/__tests__/promql.test.ts | 62 ++++++++-- packages/common-utils/src/core/promql.ts | 20 ++- 20 files changed, 463 insertions(+), 152 deletions(-) create mode 100644 .changeset/promql-pie-bar-tiles.md create mode 100644 packages/app/tests/e2e/features/promql-pie-bar.spec.ts diff --git a/.changeset/promql-pie-bar-tiles.md b/.changeset/promql-pie-bar-tiles.md new file mode 100644 index 0000000000..d1bbb971f7 --- /dev/null +++ b/.changeset/promql-pie-bar-tiles.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/common-utils': patch +'@hyperdx/app': patch +--- + +feat: Support pie and bar tiles on PromQL sources diff --git a/packages/app/src/ChartUtils.tsx b/packages/app/src/ChartUtils.tsx index 8ff78246c0..79fe3b33d4 100644 --- a/packages/app/src/ChartUtils.tsx +++ b/packages/app/src/ChartUtils.tsx @@ -147,21 +147,39 @@ function getAlignedRangeAndGranularity( } /** - * A PromQL number tile's queried config, shared by the value and the sparkline - * drawn behind it. + * Converts the given config into one that is suitable for a tile that shows + * 1 value per series (number, pie, and bar tiles). The reducer defaults to + * the last value. */ -export function convertToPromqlNumberChartConfig( +export function convertToReducedPromqlChartConfig( config: PromqlChartConfig & DateRange, - { withReducer }: { withReducer: boolean }, ): PromqlChartConfig & DateRange { return { ...config, ...getAlignedRangeAndGranularity(config), promqlExpression: getQueriedPromqlSeries(config).map(series => ({ ...series, - reducer: withReducer - ? (series.reducer ?? DEFAULT_PROMQL_REDUCER) - : undefined, + reducer: series.reducer ?? DEFAULT_PROMQL_REDUCER, + })), + }; +} + +/** + * The config for the sparkline behind a PromQL number tile: the tile's query + * with no reducer, so the buckets are plotted rather than collapsed. + * + * Intentionally matches convertToReducedPromqlChartConfig except for the reducer, + * so that react-query keys remain consistent between the reduced and sparkline versions. + */ +export function convertToPromqlSparklineChartConfig( + config: PromqlChartConfig & DateRange, +): PromqlChartConfig & DateRange { + const reduced = convertToReducedPromqlChartConfig(config); + return { + ...reduced, + promqlExpression: getQueriedPromqlSeries(reduced).map(series => ({ + ...series, + reducer: undefined, })), }; } diff --git a/packages/app/src/__tests__/ChartUtils.test.ts b/packages/app/src/__tests__/ChartUtils.test.ts index 165e8fe50b..65b0c5ebe9 100644 --- a/packages/app/src/__tests__/ChartUtils.test.ts +++ b/packages/app/src/__tests__/ChartUtils.test.ts @@ -10,8 +10,9 @@ import { import { ChartKeyJoiner, convertToNumberChartConfig, - convertToPromqlNumberChartConfig, + convertToPromqlSparklineChartConfig, convertToPromqlTableChartConfig, + convertToReducedPromqlChartConfig, convertToTableChartConfig, convertToTimeChartConfig, findNearestSeriesKey, @@ -1318,7 +1319,7 @@ describe('ChartUtils', () => { }); }); - describe('convertToPromqlNumberChartConfig', () => { + describe('convertToReducedPromqlChartConfig and convertToPromqlSparklineChartConfig', () => { const promqlConfig = { configType: 'promql' as const, displayType: DisplayType.Number, @@ -1337,9 +1338,7 @@ describe('ChartUtils', () => { // unresolved step puts the samples between the buckets the sparkline plots // on -- which the empty-bucket filler then fills with zeros. it('aligns the date range and resolves the granularity', () => { - const result = convertToPromqlNumberChartConfig(promqlConfig, { - withReducer: true, - }); + const result = convertToReducedPromqlChartConfig(promqlConfig); expect(result.dateRange).toEqual([ new Date('2025-11-26T00:00:00Z'), @@ -1349,17 +1348,14 @@ describe('ChartUtils', () => { }); it('leaves the date range unaligned for an instant query', () => { - const result = convertToPromqlNumberChartConfig( - { - ...promqlConfig, - promqlExpression: [{ expression: 'up', queryType: 'instant' }], - dateRange: [ - new Date('2025-11-26T00:00:14.076Z'), - new Date('2025-11-27T00:00:14.076Z'), - ], - }, - { withReducer: true }, - ); + const result = convertToReducedPromqlChartConfig({ + ...promqlConfig, + promqlExpression: [{ expression: 'up', queryType: 'instant' }], + dateRange: [ + new Date('2025-11-26T00:00:14.076Z'), + new Date('2025-11-27T00:00:14.076Z'), + ], + }); expect(result.dateRange).toEqual([ new Date('2025-11-26T00:00:14.076Z'), @@ -1369,9 +1365,7 @@ describe('ChartUtils', () => { }); it('names the reducer, defaulting it, and queries one expression', () => { - const result = convertToPromqlNumberChartConfig(promqlConfig, { - withReducer: true, - }); + const result = convertToReducedPromqlChartConfig(promqlConfig); expect(result).toEqual( expect.objectContaining({ @@ -1387,19 +1381,16 @@ describe('ChartUtils', () => { }); it('keeps a chosen reducer', () => { - const result = convertToPromqlNumberChartConfig( - { - ...promqlConfig, - promqlExpression: [ - { - expression: 'up', - queryType: 'range' as const, - reducer: PromqlReducer.Max, - }, - ], - }, - { withReducer: true }, - ); + const result = convertToReducedPromqlChartConfig({ + ...promqlConfig, + promqlExpression: [ + { + expression: 'up', + queryType: 'range' as const, + reducer: PromqlReducer.Max, + }, + ], + }); expect(result).toEqual( expect.objectContaining({ @@ -1417,12 +1408,8 @@ describe('ChartUtils', () => { // Without a reducer named, the hook leaves the buckets alone -- which is // how the sparkline reads the same response as the value. it('names no reducer for the sparkline, otherwise matching the value', () => { - const value = convertToPromqlNumberChartConfig(promqlConfig, { - withReducer: true, - }); - const sparkline = convertToPromqlNumberChartConfig(promqlConfig, { - withReducer: false, - }); + const value = convertToReducedPromqlChartConfig(promqlConfig); + const sparkline = convertToPromqlSparklineChartConfig(promqlConfig); expect(sparkline).toEqual({ ...value, diff --git a/packages/app/src/components/ChartEditor/PromqlQueryTypeControls.tsx b/packages/app/src/components/ChartEditor/PromqlQueryTypeControls.tsx index 17fb81bb73..bb1e6dbbbf 100644 --- a/packages/app/src/components/ChartEditor/PromqlQueryTypeControls.tsx +++ b/packages/app/src/components/ChartEditor/PromqlQueryTypeControls.tsx @@ -48,15 +48,12 @@ export default function PromqlQueryTypeControls({ name: `promqlExpressions.${index}.reducer`, }); const queryTypeValue = queryType.value ?? DEFAULT_PROMQL_QUERY_TYPE; - const isRange = queryTypeValue === 'range'; - const showReducer = isRange && isReducerSupported; const reducerValue = reducer.value ?? DEFAULT_PROMQL_REDUCER; const reducerLabel = REDUCER_OPTIONS.find( o => o.value === reducerValue, )?.label; - const summary = isRange - ? `Settings: Range${showReducer ? ` / ${reducerLabel}` : ''}` - : 'Settings: Instant'; + const queryTypeLabel = queryTypeValue === 'range' ? 'Range' : 'Instant'; + const summary = `Settings: ${queryTypeLabel}${isReducerSupported ? ` / ${reducerLabel}` : ''}`; return ( <> @@ -75,7 +72,7 @@ export default function PromqlQueryTypeControls({ }} data-testid={`promql-query-type-input-${index}`} /> - {showReducer && ( + {isReducerSupported && ( <> Reduced to @@ -87,7 +84,7 @@ export default function PromqlQueryTypeControls({ value={reducerValue} allowDeselect={false} comboboxProps={{ withinPortal: false }} - aria-label="PromQL range reducer" + aria-label="PromQL reducer" onChange={next => { if (next == null) return; reducer.onChange(next); diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts index 2ce5342b07..571fce470c 100644 --- a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts +++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts @@ -344,6 +344,26 @@ describe('buildRenderedPromqlExpression', () => { ).toBe('rate(up[60s])'); }); + it.each([DisplayType.Pie, DisplayType.Bar])( + 'expands macros with the aligned range a %s tile queries with', + displayType => { + const unaligned: [Date, Date] = [ + new Date('2024-01-01T00:00:14Z'), + new Date('2024-01-01T01:10:14Z'), + ]; + expect( + buildRenderedPromqlExpression({ + ...promqlConfig({ + displayType, + granularity: 'auto', + promqlExpression: [{ expression: 'up[$__interval] / $__range' }], + }), + dateRange: unaligned, + })?.expressions?.[0].expression, + ).toBe('up[60s] / 4260s'); + }, + ); + it('reports a substitution failure instead of an expression', () => { const result = buildRenderedPromqlExpression( promqlConfig({ diff --git a/packages/app/src/components/DBEditTimeChartForm/utils.ts b/packages/app/src/components/DBEditTimeChartForm/utils.ts index 629c59fc6c..8064954ae8 100644 --- a/packages/app/src/components/DBEditTimeChartForm/utils.ts +++ b/packages/app/src/components/DBEditTimeChartForm/utils.ts @@ -3,7 +3,10 @@ import { TableConnection, TableConnectionChoice, } from '@hyperdx/common-utils/dist/core/metadata'; -import { getQueriedPromqlSeries } from '@hyperdx/common-utils/dist/core/promql'; +import { + displayTypeSupportsReducer, + getQueriedPromqlSeries, +} from '@hyperdx/common-utils/dist/core/promql'; import { isTimeSeriesDisplayType } from '@hyperdx/common-utils/dist/core/utils'; import { configConsumesBroadcastFilters, @@ -38,8 +41,8 @@ import { filterReferencedVariables } from '@hyperdx/common-utils/dist/variables' import { convertToCategoricalChartConfig, convertToNumberChartConfig, - convertToPromqlNumberChartConfig, convertToPromqlTableChartConfig, + convertToReducedPromqlChartConfig, convertToTableChartConfig, convertToTimeChartConfig, tryExpandConfigVariables, @@ -274,8 +277,8 @@ export type RenderedPromqlExpression = function toQueriedPromqlConfig( config: PromqlChartConfig & DateRange, ): PromqlChartConfig & DateRange { - if (config.displayType === DisplayType.Number) { - return convertToPromqlNumberChartConfig(config, { withReducer: false }); + if (displayTypeSupportsReducer(config)) { + return convertToReducedPromqlChartConfig(config); } if (config.displayType === DisplayType.Table) { return convertToPromqlTableChartConfig(config); diff --git a/packages/app/src/components/DBNumberChart.tsx b/packages/app/src/components/DBNumberChart.tsx index 26b47e2f31..bdbaafd174 100644 --- a/packages/app/src/components/DBNumberChart.tsx +++ b/packages/app/src/components/DBNumberChart.tsx @@ -19,7 +19,7 @@ import { Flex, Text } from '@mantine/core'; import { buildMVDateRangeIndicator, convertToNumberChartConfig, - convertToPromqlNumberChartConfig, + convertToReducedPromqlChartConfig, } from '@/ChartUtils'; import { useQueriedChartConfig } from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; @@ -226,7 +226,7 @@ export default function DBNumberChart({ const queriedConfig = useMemo(() => { if (isBuilderChartConfig(config)) return convertToNumberChartConfig(config); if (isPromqlChartConfig(config)) { - return convertToPromqlNumberChartConfig(config, { withReducer: true }); + return convertToReducedPromqlChartConfig(config); } return config; }, [config]); @@ -255,18 +255,16 @@ export default function DBNumberChart({ ) : error; - const promqlRows = isPromqlChartConfig(queriedConfig) - ? (data?.data ?? []) - : []; - const promqlValueCount = promqlRows.length; - const promqlSeriesCount = new Set( - promqlRows.map((row: Record) => row.series_name), - ).size; + // The reducer leaves one row per series. + const promqlSeriesCount = isPromqlChartConfig(queriedConfig) + ? (data?.data?.length ?? 0) + : 0; // Several series leave the value ambiguous (see the warning) and the tile // shows whichever came first, so a trend behind it would mislead. Whether // the config can be bucketed at all is the sparkline's own call. - const showSparkline = config.backgroundChart != null && promqlValueCount <= 1; + const showSparkline = + config.backgroundChart != null && promqlSeriesCount <= 1; const resolvedNumberFormat = useSingleSeriesNumberFormat(queriedConfig); @@ -328,14 +326,12 @@ export default function DBNumberChart({ allToolbarItems.push(...toolbarPrefix); } - if (promqlValueCount > 1 || promqlSeriesCount > 1) { + if (promqlSeriesCount > 1) { allToolbarItems.push( , ); } @@ -373,7 +369,6 @@ export default function DBNumberChart({ mvOptimizationData, queriedConfig, builderQueriedConfig, - promqlValueCount, promqlSeriesCount, ]); diff --git a/packages/app/src/components/NumberTileBackgroundChart.tsx b/packages/app/src/components/NumberTileBackgroundChart.tsx index 2511d34930..db307f6830 100644 --- a/packages/app/src/components/NumberTileBackgroundChart.tsx +++ b/packages/app/src/components/NumberTileBackgroundChart.tsx @@ -14,7 +14,7 @@ import { } from '@hyperdx/common-utils/dist/types'; import { - convertToPromqlNumberChartConfig, + convertToPromqlSparklineChartConfig, convertToTimeChartConfig, formatResponseForTimeChart, shouldFillNullsWithZero, @@ -69,7 +69,7 @@ export function buildSparklineQueryConfig( // A PromQL tile shares the same (range) query as the number chart, without a reducer. // This ensures that the sparkline can re-use the same react-query cache entry. if (isPromqlChartConfig(config)) { - return convertToPromqlNumberChartConfig(config, { withReducer: false }); + return convertToPromqlSparklineChartConfig(config); } // Display-only fields are dropped to avoid refetching identical time-series diff --git a/packages/app/src/components/__tests__/DBNumberChart.test.tsx b/packages/app/src/components/__tests__/DBNumberChart.test.tsx index 4853ad134c..1ac4c19e10 100644 --- a/packages/app/src/components/__tests__/DBNumberChart.test.tsx +++ b/packages/app/src/components/__tests__/DBNumberChart.test.tsx @@ -830,23 +830,6 @@ describe('DBNumberChart', () => { expect(screen.getByTestId('number-chart-value')).toHaveTextContent('1'); }); - // Several values under one name is the other failure: the expression is - // returning a range rather than a single sample per series. - it('warns differently when one series carries several values', () => { - setInstantRows([ - { series_name: 'up', value: 1 }, - { series_name: 'up', value: 2 }, - ]); - mockFormatNumber.mockReturnValue('1'); - - renderWithMantine(); - - expect(screen.getByTestId('multiple-values-indicator')).toHaveAttribute( - 'aria-label', - 'Query returned 2 values for one series', - ); - }); - it('does not warn for a non-PromQL config with several rows', () => { mockUseQueriedChartConfig.mockReturnValue({ data: { diff --git a/packages/app/src/components/__tests__/DBPieChart.test.tsx b/packages/app/src/components/__tests__/DBPieChart.test.tsx index 335e7f25e1..18befe474c 100644 --- a/packages/app/src/components/__tests__/DBPieChart.test.tsx +++ b/packages/app/src/components/__tests__/DBPieChart.test.tsx @@ -1,4 +1,5 @@ -import { screen } from '@testing-library/react'; +import { DisplayType, PromqlReducer } from '@hyperdx/common-utils/dist/types'; +import { screen, within } from '@testing-library/react'; import DateRangeIndicator from '@/components/charts/DateRangeIndicator'; import { DBPieChart } from '@/components/DBPieChart'; @@ -294,4 +295,71 @@ describe('DBPieChart', () => { // Verify DateRangeIndicator was not called expect(jest.mocked(DateRangeIndicator)).not.toHaveBeenCalled(); }); + + describe('PromQL', () => { + const promqlConfig = { + configType: 'promql' as const, + displayType: DisplayType.Pie, + connection: 'test-connection', + promqlExpression: [{ expression: 'up' }, { expression: 'down' }], + dateRange: [ + new Date('2025-11-26T00:00:14.076Z'), + new Date('2025-11-26T01:00:14.076Z'), + ] as [Date, Date], + }; + + it('queries the first expression, reduced to one value per series', () => { + renderWithMantine(); + + const queriedConfig = mockUseQueriedChartConfig.mock.calls[0][0]; + expect(queriedConfig.promqlExpression).toEqual([ + { expression: 'up', reducer: PromqlReducer.LastNotNull }, + ]); + expect(queriedConfig.granularity).toBe('1 minute'); + }); + + it('keeps the same query key when only the reducer changes', () => { + renderWithMantine(); + renderWithMantine( + , + ); + + const [first, second] = mockUseQueriedChartConfig.mock.calls.map( + ([, options]) => options.queryKey, + ); + expect(second).toEqual(first); + }); + + it('draws a slice per series, largest first', () => { + mockUseQueriedChartConfig.mockReturnValue({ + data: { + data: [ + { series_name: 'cart', value: 2 }, + { series_name: 'checkout', value: 7 }, + { series_name: 'ad', value: 4 }, + ], + meta: [ + { name: 'series_name', type: 'String' }, + { name: 'value', type: 'Float64' }, + ], + }, + isLoading: false, + isError: false, + }); + + renderWithMantine(); + + const labels = within(screen.getByTestId('pie-chart-legend')) + .getAllByTitle(/.+/) + .map(el => el.getAttribute('title')); + expect(labels).toEqual(['checkout', 'ad', 'cart']); + }); + }); }); diff --git a/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx b/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx index 906dd0442c..05d1e020af 100644 --- a/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx +++ b/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx @@ -2,7 +2,7 @@ import React from 'react'; import { DisplayType, PromqlReducer } from '@hyperdx/common-utils/dist/types'; import { screen } from '@testing-library/react'; -import { convertToPromqlNumberChartConfig } from '@/ChartUtils'; +import { convertToPromqlSparklineChartConfig } from '@/ChartUtils'; import NumberTileBackgroundChart, { buildSparklineQueryConfig, sparklinePointsFromGraphResults, @@ -133,7 +133,7 @@ describe('buildSparklineQueryConfig', () => { }; expect(buildSparklineQueryConfig(promqlConfig)).toEqual( - convertToPromqlNumberChartConfig(promqlConfig, { withReducer: false }), + convertToPromqlSparklineChartConfig(promqlConfig), ); }); diff --git a/packages/app/src/components/charts/CategoricalChart.tsx b/packages/app/src/components/charts/CategoricalChart.tsx index bc4bd6f4ae..dcaa6d6322 100644 --- a/packages/app/src/components/charts/CategoricalChart.tsx +++ b/packages/app/src/components/charts/CategoricalChart.tsx @@ -1,11 +1,15 @@ import { useMemo } from 'react'; import { hasNonEmptyOrderBy } from '@hyperdx/common-utils/dist/core/utils'; -import { isBuilderChartConfig } from '@hyperdx/common-utils/dist/guards'; +import { + isBuilderChartConfig, + isPromqlChartConfig, +} from '@hyperdx/common-utils/dist/guards'; import { ChartConfigWithOptTimestamp } from '@hyperdx/common-utils/dist/types'; import { buildMVDateRangeIndicator, convertToCategoricalChartConfig, + convertToReducedPromqlChartConfig, formatResponseForCategoricalChart, } from '@/ChartUtils'; import MVOptimizationIndicator from '@/components/MaterializedViews/MVOptimizationIndicator'; @@ -13,6 +17,7 @@ import { useQueriedChartConfig } from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; import { useSingleSeriesNumberFormat, useSource } from '@/source'; import { getColorProps } from '@/utils'; +import { stripClientSideConfigFields } from '@/utils/chartConfig'; import { ChartErrorStateVariant } from './ChartErrorState'; @@ -43,9 +48,13 @@ export function useCategoricalChart({ const { data: source } = useSource({ id: config.source }); const queriedConfig = useMemo(() => { - return isBuilderChartConfig(config) - ? convertToCategoricalChartConfig(config) - : config; + if (isBuilderChartConfig(config)) { + return convertToCategoricalChartConfig(config); + } + if (isPromqlChartConfig(config)) { + return convertToReducedPromqlChartConfig(config); + } + return config; }, [config]); const resolvedNumberFormat = useSingleSeriesNumberFormat(queriedConfig); @@ -59,7 +68,7 @@ export function useCategoricalChart({ const { data, isLoading, isError, error, isPlaceholderData } = useQueriedChartConfig(queriedConfig, { placeholderData: (prev: any) => prev, - queryKey: [queryKeyPrefix, queriedConfig], + queryKey: [queryKeyPrefix, stripClientSideConfigFields(queriedConfig)], enabled, }); diff --git a/packages/app/src/components/charts/MultipleValuesIndicator.tsx b/packages/app/src/components/charts/MultipleValuesIndicator.tsx index 4987065e96..afbec5bc29 100644 --- a/packages/app/src/components/charts/MultipleValuesIndicator.tsx +++ b/packages/app/src/components/charts/MultipleValuesIndicator.tsx @@ -2,48 +2,30 @@ import { Tooltip } from '@mantine/core'; import { IconAlertTriangle } from '@tabler/icons-react'; interface MultipleValuesIndicatorProps { - valueCount: number; seriesCount: number; /** How to collapse several series into one. */ multipleSeriesHint?: string; - /** How to make one series yield a single value. */ - multipleValuesHint?: string; } -/** - * Warns that a single-value chart got more than one value back, naming which - * of the two causes it is: several series, each with its own value, or one - * series carrying several values over time. - */ +/** Warns that a single-value chart got several series back, each with its own value. */ export default function MultipleValuesIndicator({ - valueCount, seriesCount, multipleSeriesHint, - multipleValuesHint, }: MultipleValuesIndicatorProps) { - if (valueCount <= 1) { + if (seriesCount <= 1) { return null; } - const isMultipleSeries = seriesCount > 1; - const summary = isMultipleSeries - ? `This query returned ${seriesCount.toLocaleString()} series, each with its own value.` - : `This query returned ${valueCount.toLocaleString()} values for a single series.`; - const hint = isMultipleSeries ? multipleSeriesHint : multipleValuesHint; const label = - `${summary} A number chart shows one, so the first is displayed.` + - (hint ? ` ${hint}` : ''); + `This query returned ${seriesCount.toLocaleString()} series, each with its own value. A number chart shows one, so the first is displayed.` + + (multipleSeriesHint ? ` ${multipleSeriesHint}` : ''); return ( diff --git a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx index 0793af11e0..ec92278b23 100644 --- a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx +++ b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx @@ -919,7 +919,7 @@ describe('useChartConfig', () => { }); }); - describe('range reducers', () => { + describe('reducers', () => { const rangeSamples = { status: 'success' as const, data: { @@ -1103,6 +1103,65 @@ describe('useChartConfig', () => { expect(prometheusApi.queryRange).toHaveBeenCalledTimes(2), ); }); + + it('reduces the matrix an instant range selector returns', async () => { + jest.mocked(prometheusApi.query).mockResolvedValue(rangeSamples); + + const config = createPromqlConfig({ + displayType: DisplayType.Pie, + promqlExpression: [ + { + expression: 'e2e_service_up[5m]', + queryType: 'instant', + reducer: PromqlReducer.Max, + }, + ], + }); + const { result } = renderHook(() => useQueriedChartConfig(config), { + wrapper, + }); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + expect(prometheusApi.queryRange).not.toHaveBeenCalled(); + expect(result.current.data?.data).toEqual([ + { series_name: 'e2e_service_up{service="accounting"}', value: 8 }, + { series_name: 'e2e_service_up{service="api-server"}', value: 3 }, + ]); + }); + + it('passes an instant vector through any reducer unchanged', async () => { + jest.mocked(prometheusApi.query).mockResolvedValue({ + status: 'success', + data: { + resultType: 'vector', + result: [ + { + metric: { __name__: 'e2e_service_up', service: 'accounting' }, + value: [1673312400, '7'], + }, + ], + }, + }); + + const config = createPromqlConfig({ + displayType: DisplayType.Number, + promqlExpression: [ + { + expression: 'e2e_service_up', + queryType: 'instant', + reducer: PromqlReducer.Sum, + }, + ], + }); + const { result } = renderHook(() => useQueriedChartConfig(config), { + wrapper, + }); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + expect(result.current.data?.data).toEqual([ + { series_name: 'e2e_service_up', value: 7 }, + ]); + }); }); }); diff --git a/packages/app/src/hooks/useChartConfig.tsx b/packages/app/src/hooks/useChartConfig.tsx index ea96708564..954fe46e16 100644 --- a/packages/app/src/hooks/useChartConfig.tsx +++ b/packages/app/src/hooks/useChartConfig.tsx @@ -345,18 +345,18 @@ export function useQueriedChartConfig( }); const minGranularitySeconds = getMinGranularitySeconds(source); - // A PromQL range query keeps every bucket in the cache. An observer whose - // config names a reducer collapses them to a single value per series on - // read; the sparkline behind a number tile names none, so it plots them. - const reducesRangeBuckets = + // A PromQL query keeps every sample in the cache. An observer whose config + // names a reducer collapses them to a single value per series on read; the + // sparkline behind a number tile names none, so it plots them. + const appliesReducer = isPromqlChartConfig(config) && appliesPromqlReducer(config); - const rangeReducer = reducesRangeBuckets + const reducer = appliesReducer ? getQueriedPromqlSeries(config)[0]?.reducer : undefined; - const selectRangeReduced = useCallback( - (result: TQueryFnData) => reduceBucketRows(result, rangeReducer), - [rangeReducer], + const selectReduced = useCallback( + (result: TQueryFnData) => reduceBucketRows(result, reducer), + [reducer], ); const query = useQuery({ @@ -433,7 +433,7 @@ export function useQueriedChartConfig( return queryClient.getQueryData(context.queryKey)!; }, // PromQL reducer is applied as a client-side react-query select function - select: reducesRangeBuckets ? selectRangeReduced : undefined, + select: appliesReducer ? selectReduced : undefined, retry: 1, refetchOnWindowFocus: false, ...options, diff --git a/packages/app/tests/e2e/components/ChartEditorComponent.ts b/packages/app/tests/e2e/components/ChartEditorComponent.ts index 12f5c8e21a..213d02594a 100644 --- a/packages/app/tests/e2e/components/ChartEditorComponent.ts +++ b/packages/app/tests/e2e/components/ChartEditorComponent.ts @@ -1157,7 +1157,7 @@ export class ChartEditorComponent { /** * Choose how a PromQL expression is evaluated, from the toggle under the * expression editor. `reducer` is the visible label (e.g. "Max"), and only - * applies to a range query. + * applies to display types that reduce each series to one value. */ async setPromqlQueryType( queryType: 'Instant' | 'Range', @@ -1170,9 +1170,7 @@ export class ChartEditorComponent { await group.getByText(queryType, { exact: true }).click(); if (reducer) { - await this.page - .getByRole('combobox', { name: 'PromQL range reducer' }) - .click(); + await this.page.getByRole('combobox', { name: 'PromQL reducer' }).click(); await this.page .getByRole('option', { name: reducer, exact: true }) .click(); diff --git a/packages/app/tests/e2e/features/promql-number.spec.ts b/packages/app/tests/e2e/features/promql-number.spec.ts index 5e1d9cc892..295346c245 100644 --- a/packages/app/tests/e2e/features/promql-number.spec.ts +++ b/packages/app/tests/e2e/features/promql-number.spec.ts @@ -111,7 +111,7 @@ test.describe( // rather than claiming the instant query it is not running. await expect( page.getByTestId('promql-query-type-control-0'), - ).toContainText('Settings: Range'); + ).toContainText('Settings: Range / Last'); }); await test.step('Count over the range reports more than one sample', async () => { @@ -126,6 +126,27 @@ test.describe( await editor.setPromqlQueryType('Instant'); await editor.runQuery(false); + // Count still applies, but an instant vector has one sample per series. + await expect(value).toHaveText('1', { timeout: 30000 }); + await expect( + page.getByRole('combobox', { name: 'PromQL reducer' }), + ).toHaveValue('Count'); + }); + + await test.step('An instant range selector is reduced too', async () => { + // A range selector evaluates to every sample in the window, even as an + // instant query. The expression is set after the query type: it does + // not end in `}`, so the autocomplete popup can stay open. + await editor.replacePromqlExpression(`${ONE_SERIES}[5m]`); + await editor.runQuery(false); + + await expect(value).not.toHaveText('1', { timeout: 30000 }); + await expect( + page.getByTestId('multiple-values-indicator'), + ).toBeHidden(); + + await editor.replacePromqlExpression(ONE_SERIES); + await editor.runQuery(false); await expect(value).toHaveText('1', { timeout: 30000 }); }); diff --git a/packages/app/tests/e2e/features/promql-pie-bar.spec.ts b/packages/app/tests/e2e/features/promql-pie-bar.spec.ts new file mode 100644 index 0000000000..091e460928 --- /dev/null +++ b/packages/app/tests/e2e/features/promql-pie-bar.spec.ts @@ -0,0 +1,117 @@ +/** + * PromQL pie and bar tiles draw one slice or bar per series: a range query is + * reduced to one value per series, and an instant query already is one. The + * series come from the expression, not from a group-by, so there is no ORDER BY + * or series limit to set. + * + * The seed gives `e2e_service_up` one series per `SERVICES` entry, labelled + * `service`, one sample of 1 a minute. + */ +import { DisplayType } from '@hyperdx/common-utils/dist/types'; + +import { DashboardPage } from '../page-objects/DashboardPage'; +import { SERVICES } from '../seed-clickhouse'; +import { expect, test } from '../utils/base-test'; +import { E2E_PROMQL_METRIC_NAME, PROMQL_SOURCE_NAME } from '../utils/constants'; + +/** + * Every seeded series. The trailing brace closes the editor's autocomplete + * popup, which would otherwise cover the query type control. + */ +const ALL_SERIES = `${E2E_PROMQL_METRIC_NAME}{service!=""}`; + +test.describe( + 'PromQL pie and bar tiles', + { tag: ['@dashboard', '@full-stack'] }, + () => { + test('draws one slice per series for range and instant queries', async ({ + page, + }) => { + test.setTimeout(120000); + + const dashboardPage = new DashboardPage(page); + const editor = dashboardPage.chartEditor; + const legend = page.getByTestId('pie-chart-legend'); + const drawer = page.getByRole('dialog', { name: 'Display Settings' }); + // Scoped to the editor: the dashboard header carries its own picker. + const editorGranularity = page + .getByTestId('tile-editor-form') + .getByTestId('granularity-picker'); + + await test.step('Create a PromQL pie tile', async () => { + await dashboardPage.goto(); + await dashboardPage.createNewDashboard(); + await dashboardPage.addTile(); + await expect(editor.nameInput).toBeVisible(); + await editor.waitForDataToLoad(); + await editor.switchToPromqlMode(); + await editor.selectSource(PROMQL_SOURCE_NAME); + await editor.setChartName('PromQL pie tile'); + await editor.setChartType(DisplayType.Pie); + await editor.replacePromqlExpression(ALL_SERIES); + await editor.runQuery(false); + }); + + await test.step('A range query draws one slice per series', async () => { + await expect(legend).toBeVisible({ timeout: 30000 }); + await expect(legend.getByTitle(/service="/)).toHaveCount( + SERVICES.length, + ); + await expect(editorGranularity).toBeVisible(); + await expect(page.getByTestId('order-by-input')).toBeHidden(); + }); + + await test.step('Display settings offer a legend template and number format, not a series limit', async () => { + await editor.setLegendTemplate('{{service}}'); + await editor.openDisplaySettings(); + await expect(drawer.getByLabel('Series Limit')).toBeHidden(); + await editor.setNumberFormatOutput('Currency'); + await editor.applyDisplaySettings(); + await editor.runQuery(false); + + for (const service of SERVICES) { + await expect(legend.getByTitle(service, { exact: true })).toBeVisible( + { timeout: 30000 }, + ); + } + await expect(legend.getByText('$1.00').first()).toBeVisible(); + }); + + await test.step('An instant query draws the same slices, with no granularity', async () => { + await editor.setPromqlQueryType('Instant'); + await editor.runQuery(false); + + await expect(editorGranularity).toBeHidden(); + await expect(legend.getByTitle(/.+/)).toHaveCount(SERVICES.length, { + timeout: 30000, + }); + }); + + await test.step('The same query draws a bar per series', async () => { + await editor.setChartType(DisplayType.Bar); + await editor.runQuery(false); + + const bars = page.getByTestId('bar-chart-container'); + await expect(bars).toBeVisible({ timeout: 30000 }); + await expect( + bars.getByText('accounting', { exact: true }), + ).toBeVisible(); + await expect(page.getByTestId('order-by-input')).toBeHidden(); + }); + + await test.step('The saved tile renders as a bar chart', async () => { + await editor.save(); + await expect(dashboardPage.getTiles()).toHaveCount(1, { + timeout: 10000, + }); + await expect( + dashboardPage + .getTiles() + .first() + .getByTestId('bar-chart-container') + .getByText('accounting', { exact: true }), + ).toBeVisible({ timeout: 30000 }); + }); + }); + }, +); diff --git a/packages/common-utils/src/core/__tests__/promql.test.ts b/packages/common-utils/src/core/__tests__/promql.test.ts index 1c631f2ba0..0271cc0523 100644 --- a/packages/common-utils/src/core/__tests__/promql.test.ts +++ b/packages/common-utils/src/core/__tests__/promql.test.ts @@ -1,4 +1,5 @@ import { + appliesPromqlReducer, displayTypeSupportsInstantQuery, displayTypeSupportsReducer, getPromqlMacroInputs, @@ -108,14 +109,16 @@ describe('getQueriedPromqlSeries', () => { }); describe('displayTypeSupportsInstantQuery', () => { - it.each([DisplayType.Number, DisplayType.Table])( - 'offers an instant query on %s tiles', - displayType => { - expect(displayTypeSupportsInstantQuery({ displayType })).toBe(true); - }, - ); + it.each([ + DisplayType.Number, + DisplayType.Table, + DisplayType.Pie, + DisplayType.Bar, + ])('offers an instant query on %s tiles', displayType => { + expect(displayTypeSupportsInstantQuery({ displayType })).toBe(true); + }); - it.each([DisplayType.Line, DisplayType.StackedBar, DisplayType.Pie])( + it.each([DisplayType.Line, DisplayType.StackedBar])( 'always range-queries %s tiles', displayType => { expect(displayTypeSupportsInstantQuery({ displayType })).toBe(false); @@ -124,11 +127,12 @@ describe('displayTypeSupportsInstantQuery', () => { }); describe('displayTypeSupportsReducer', () => { - it('reduces range buckets on number tiles', () => { - expect( - displayTypeSupportsReducer({ displayType: DisplayType.Number }), - ).toBe(true); - }); + it.each([DisplayType.Number, DisplayType.Pie, DisplayType.Bar])( + 'reduces range buckets on %s tiles', + displayType => { + expect(displayTypeSupportsReducer({ displayType })).toBe(true); + }, + ); it('keeps every sample on table tiles', () => { expect(displayTypeSupportsReducer({ displayType: DisplayType.Table })).toBe( @@ -328,3 +332,37 @@ describe('isRangeQuery', () => { expect(isRangeQuery({ displayType: DisplayType.Line })).toBe(false); }); }); + +describe('appliesPromqlReducer', () => { + it.each(['instant', 'range'] as const)( + 'applies to a %s query on a reducing display type', + queryType => { + expect( + appliesPromqlReducer({ + promqlExpression: [ + { expression: 'up', queryType, reducer: PromqlReducer.Max }, + ], + displayType: DisplayType.Pie, + }), + ).toBe(true); + }, + ); + + it('does not apply when no reducer is named', () => { + expect( + appliesPromqlReducer({ + promqlExpression: [{ expression: 'up', queryType: 'instant' }], + displayType: DisplayType.Number, + }), + ).toBe(false); + }); + + it('does not apply to a display type that plots every sample', () => { + expect( + appliesPromqlReducer({ + promqlExpression: [{ expression: 'up', reducer: PromqlReducer.Max }], + displayType: DisplayType.Line, + }), + ).toBe(false); + }); +}); diff --git a/packages/common-utils/src/core/promql.ts b/packages/common-utils/src/core/promql.ts index 9f35dcbcad..776d26ef20 100644 --- a/packages/common-utils/src/core/promql.ts +++ b/packages/common-utils/src/core/promql.ts @@ -48,12 +48,22 @@ export const displayTypeSupportsInstantQuery = (config: { displayType?: DisplayType; }): boolean => config.displayType === DisplayType.Number || - config.displayType === DisplayType.Table; + config.displayType === DisplayType.Table || + config.displayType === DisplayType.Pie || + config.displayType === DisplayType.Bar; -/** Whether a display type collapses a range query to one value per series using a client-side reducer. */ +/** + * Whether a display type collapses each series to one value using a client-side + * reducer. Applies to instant and range queries alike: an instant vector has + * one sample per series, so any reducer returns it, while an instant query + * with a range selector (`up[5m]`) returns several. + */ export const displayTypeSupportsReducer = (config: { displayType?: DisplayType; -}): boolean => config.displayType === DisplayType.Number; +}): boolean => + config.displayType === DisplayType.Number || + config.displayType === DisplayType.Pie || + config.displayType === DisplayType.Bar; /** The reducer applied when none is chosen. */ export const DEFAULT_PROMQL_REDUCER = PromqlReducer.LastNotNull; @@ -203,10 +213,10 @@ export const isReducibleRangeQuery = (config: { displayType?: DisplayType; }): boolean => displayTypeSupportsReducer(config) && isRangeQuery(config); -/** Whether the config specifies a reducer. */ +/** Whether the config specifies a reducer its display type applies. */ export const appliesPromqlReducer = (config: { promqlExpression?: PromqlExpressionList; displayType?: DisplayType; }): boolean => - isReducibleRangeQuery(config) && + displayTypeSupportsReducer(config) && getQueriedPromqlSeries(config)[0]?.reducer != null;