diff --git a/.changeset/promql-number-sparkline.md b/.changeset/promql-number-sparkline.md new file mode 100644 index 0000000000..606ede7336 --- /dev/null +++ b/.changeset/promql-number-sparkline.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/app': patch +'@hyperdx/common-utils': patch +--- + +feat: Support background sparklines on PromQL range number tiles diff --git a/packages/app/src/ChartUtils.tsx b/packages/app/src/ChartUtils.tsx index 35a99e86b5..50786b04ff 100644 --- a/packages/app/src/ChartUtils.tsx +++ b/packages/app/src/ChartUtils.tsx @@ -9,6 +9,11 @@ import { JSDataType, ResponseJSON, } from '@hyperdx/common-utils/dist/clickhouse'; +import { + DEFAULT_PROMQL_REDUCER, + getQueriedPromqlSeries, + isRangeQuery, +} from '@hyperdx/common-utils/dist/core/promql'; import { isMetricChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig'; import { SERIES_KEY_JOINER } from '@hyperdx/common-utils/dist/core/seriesNameTemplate'; import { @@ -28,10 +33,12 @@ import { BuilderSavedChartConfig, ChartConfigWithDateRange, ChartConfigWithOptDateRange, + DateRange, DisplayType, Filter, isSearchableSource, MetricsDataType as MetricsDataTypeV2, + PromqlChartConfig, SourceKind, SQLInterval, TMetricSource, @@ -115,6 +122,41 @@ function getTimeChartDateRange( export const MAX_TIME_CHART_SERIES = DEFAULT_SERIES_LIMIT; +/** + * A PromQL number tile's queried config, shared by the value and the sparkline + * drawn behind it. + */ +export function convertToPromqlNumberChartConfig( + config: PromqlChartConfig & DateRange, + { withReducer }: { withReducer: boolean }, +): PromqlChartConfig & DateRange { + const granularity = getTimeChartGranularity( + config.granularity, + config.dateRange, + ); + return { + ...config, + granularity, + // Align the date range with the same buckets used by the sparkline and + // timeseries charts, when using a (bucketed) range query. Alignment is + // desirable for range queries to ensure consistent bucket boundaries + // and sizes. + dateRange: isRangeQuery(config) + ? getTimeChartDateRange( + config.dateRange, + config.alignDateRangeToGranularity, + granularity, + ) + : config.dateRange, + promqlExpression: getQueriedPromqlSeries(config).map(series => ({ + ...series, + reducer: withReducer + ? (series.reducer ?? DEFAULT_PROMQL_REDUCER) + : undefined, + })), + }; +} + export function convertToTimeChartConfig( config: ChartConfigWithDateRange, ): ChartConfigWithDateRange { diff --git a/packages/app/src/__tests__/ChartUtils.test.ts b/packages/app/src/__tests__/ChartUtils.test.ts index 5d3b70b926..928b5727cb 100644 --- a/packages/app/src/__tests__/ChartUtils.test.ts +++ b/packages/app/src/__tests__/ChartUtils.test.ts @@ -1,5 +1,8 @@ +import { DEFAULT_PROMQL_REDUCER } from '@hyperdx/common-utils/dist/core/promql'; import { BuilderChartConfigWithDateRange, + DisplayType, + PromqlReducer, SourceKind, TSource, } from '@hyperdx/common-utils/dist/types'; @@ -7,6 +10,7 @@ import { import { ChartKeyJoiner, convertToNumberChartConfig, + convertToPromqlNumberChartConfig, convertToTableChartConfig, convertToTimeChartConfig, findNearestSeriesKey, @@ -20,6 +24,7 @@ import { resolveRenderedSeriesCap, } from '@/defaults'; import { COLORS } from '@/utils'; +import { stripClientSideConfigFields } from '@/utils/chartConfig'; // Anchor info/error to concrete hexes rather than `getChartColorInfo()` / // `getChartColorError()` so a regression that breaks the helpers can't @@ -1279,6 +1284,124 @@ describe('ChartUtils', () => { }); }); + describe('convertToPromqlNumberChartConfig', () => { + const promqlConfig = { + configType: 'promql' as const, + displayType: DisplayType.Number, + connection: 'conn', + promqlExpression: [ + { expression: 'up', queryType: 'range' as const }, + { expression: 'down', queryType: 'range' as const }, + ], + dateRange: [ + new Date('2025-11-26T00:00:14.076Z'), + new Date('2025-11-26T01:00:14.076Z'), + ] as [Date, Date], + }; + + // Prometheus answers at `start + k * step`, so an unaligned start or an + // 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, + }); + + expect(result.dateRange).toEqual([ + new Date('2025-11-26T00:00:00Z'), + new Date('2025-11-26T01:01:00Z'), + ]); + expect(result.granularity).toBe('1 minute'); + }); + + 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 }, + ); + + expect(result.dateRange).toEqual([ + new Date('2025-11-26T00:00:14.076Z'), + new Date('2025-11-27T00:00:14.076Z'), + ]); + expect(result.granularity).toBe('30 minute'); + }); + + it('names the reducer, defaulting it, and queries one expression', () => { + const result = convertToPromqlNumberChartConfig(promqlConfig, { + withReducer: true, + }); + + expect(result).toEqual( + expect.objectContaining({ + promqlExpression: [ + { + expression: 'up', + queryType: 'range', + reducer: DEFAULT_PROMQL_REDUCER, + }, + ], + }), + ); + }); + + it('keeps a chosen reducer', () => { + const result = convertToPromqlNumberChartConfig( + { + ...promqlConfig, + promqlExpression: [ + { + expression: 'up', + queryType: 'range' as const, + reducer: PromqlReducer.Max, + }, + ], + }, + { withReducer: true }, + ); + + expect(result).toEqual( + expect.objectContaining({ + promqlExpression: [ + { + expression: 'up', + queryType: 'range', + reducer: PromqlReducer.Max, + }, + ], + }), + ); + }); + + // 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, + }); + + expect(sparkline).toEqual({ + ...value, + promqlExpression: [ + { expression: 'up', queryType: 'range', reducer: undefined }, + ], + }); + expect(stripClientSideConfigFields(sparkline)).toEqual( + stripClientSideConfigFields(value), + ); + }); + }); + describe('convertToTableChartConfig', () => { it('should remove granularity from the config', () => { const config = { diff --git a/packages/app/src/components/BackgroundChartInput.tsx b/packages/app/src/components/BackgroundChartInput.tsx index 54d3e5011b..128091ab48 100644 --- a/packages/app/src/components/BackgroundChartInput.tsx +++ b/packages/app/src/components/BackgroundChartInput.tsx @@ -15,18 +15,20 @@ const TYPE_OPTIONS = [ * "Area" set the shape. The color swatch is an optional palette-token * override; when unset the sparkline inherits the tile's static color. * - * `disabled` is set for raw SQL number tiles, which have no time dimension to - * bucket: the control stays visible but inert with a hint, so the option is - * discoverable rather than missing. + * `disabled` is set where the tile's query has no buckets to plot -- raw SQL, + * or a PromQL instant query. The control stays visible but inert with + * `disabledHint` explaining which, so the option stays discoverable. */ export function BackgroundChartInput({ value, onChange, disabled = false, + disabledHint = 'Available on query-builder number tiles.', }: { value?: BackgroundChart; onChange: (value: BackgroundChart | undefined) => void; disabled?: boolean; + disabledHint?: string; }) { return ( @@ -51,7 +53,7 @@ export function BackgroundChartInput({ /> {disabled ? ( - Available on query-builder number tiles. + {disabledHint} ) : ( value && ( diff --git a/packages/app/src/components/ChartDisplaySettingsDrawer.tsx b/packages/app/src/components/ChartDisplaySettingsDrawer.tsx index 48b7fbd88c..a03b94a2e1 100644 --- a/packages/app/src/components/ChartDisplaySettingsDrawer.tsx +++ b/packages/app/src/components/ChartDisplaySettingsDrawer.tsx @@ -80,6 +80,8 @@ interface ChartDisplaySettingsDrawerProps { displayType: DisplayType; /** 'sql' for raw SQL chart configs; anything else is treated as a builder config. */ configType?: 'sql' | 'builder' | 'promql'; + /** Whether a PromQL tile's queried expression runs over a range. */ + promqlUsesRange?: boolean; previousDateRange?: [Date, Date]; onChange: (settings: ChartConfigDisplaySettings, isDirty: boolean) => void; onClose: () => void; @@ -119,6 +121,7 @@ export default function ChartDisplaySettingsDrawer({ opened, displayType, configType, + promqlUsesRange = false, defaultNumberFormat, onChange, onClose, @@ -222,13 +225,19 @@ export default function ChartDisplaySettingsDrawer({ // `select[i].color`. const showTileColor = displayType === DisplayType.Number; - // The background sparkline is derived from a time-bucketed version of the - // tile's query, so it only applies to builder number tiles: raw SQL number - // tiles return a single value with no time dimension to bucket. On a SQL - // number tile the control is shown disabled with a hint rather than hidden, - // so the option stays discoverable. + // The sparkline needs buckets. A builder tile derives them from a + // time-bucketed version of its query; a PromQL tile reuses the buckets its + // range query fetches, so an instant one has none. Raw SQL returns a + // single value with no time dimension at all. Where it cannot apply the + // control is shown disabled with a hint rather than hidden, so the option + // stays discoverable. const showBackgroundChart = displayType === DisplayType.Number; - const isBackgroundChartDisabled = configType === 'sql'; + const isBackgroundChartDisabled = + configType === 'sql' || (configType === 'promql' && !promqlUsesRange); + const backgroundChartDisabledHint = + configType === 'promql' + ? 'Available on PromQL range queries.' + : 'Available on query-builder number tiles.'; return ( )} /> diff --git a/packages/app/src/components/ChartEditor/__tests__/utils.test.ts b/packages/app/src/components/ChartEditor/__tests__/utils.test.ts index f208304472..d31bd56168 100644 --- a/packages/app/src/components/ChartEditor/__tests__/utils.test.ts +++ b/packages/app/src/components/ChartEditor/__tests__/utils.test.ts @@ -129,6 +129,19 @@ describe('convertFormStateToSavedChartConfig', () => { expect(result).toMatchObject({ alternateRowBackground: true }); }); + it('persists backgroundChart for a promql+number config', () => { + const form: ChartEditorFormState = { + configType: 'promql', + displayType: DisplayType.Number, + promqlExpression: 'up', + connection: 'conn-1', + backgroundChart: { type: 'area' }, + series: [], + }; + const result = convertFormStateToSavedChartConfig(form, undefined); + expect(result).toMatchObject({ backgroundChart: { type: 'area' } }); + }); + it('persists alternateRowBackground for a promql+table config', () => { const form: ChartEditorFormState = { configType: 'promql', @@ -409,6 +422,21 @@ describe('convertFormStateToChartConfig', () => { expect(result).toMatchObject({ alternateRowBackground: true }); }); + // The sparkline only renders if the rendered config carries this, and the + // promql branch builds its config from an explicit pick list. + it('threads backgroundChart into the rendered promql+number config', () => { + const form: ChartEditorFormState = { + configType: 'promql', + displayType: DisplayType.Number, + promqlExpression: 'up', + connection: 'conn-1', + backgroundChart: { type: 'area' }, + series: [], + }; + const result = convertFormStateToChartConfig(form, dateRange, undefined); + expect(result).toMatchObject({ backgroundChart: { type: 'area' } }); + }); + it('threads alternateRowBackground into the rendered promql+table config', () => { const form: ChartEditorFormState = { configType: 'promql', diff --git a/packages/app/src/components/ChartEditor/utils.ts b/packages/app/src/components/ChartEditor/utils.ts index 9bbb675de4..b61cdcbbdb 100644 --- a/packages/app/src/components/ChartEditor/utils.ts +++ b/packages/app/src/components/ChartEditor/utils.ts @@ -201,6 +201,7 @@ export function convertFormStateToSavedChartConfig( 'numberFormat', 'color', 'colorRules', + 'backgroundChart', 'granularity', 'compareToPreviousPeriod', 'fillNulls', @@ -299,6 +300,7 @@ export function convertFormStateToChartConfig( 'numberFormat', 'color', 'colorRules', + 'backgroundChart', 'granularity', 'compareToPreviousPeriod', 'fillNulls', diff --git a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx index af43ca8c15..51109dab4c 100644 --- a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx @@ -8,6 +8,7 @@ import { } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; +import { isReducibleRangeQuery } from '@hyperdx/common-utils/dist/core/promql'; import { displayTypeSupportsBuilderAlerts, displayTypeSupportsRawSqlAlerts, @@ -267,6 +268,7 @@ export default function EditTimeChartForm({ const markdown = useWatch({ control, name: 'markdown' }); const granularity = useWatch({ control, name: 'granularity' }); const configType = useWatch({ control, name: 'configType' }); + const promqlExpressions = useWatch({ control, name: 'promqlExpressions' }); const chartConfigAlert = chartConfig.alert; const isRawSqlInput = @@ -1113,6 +1115,10 @@ export default function EditTimeChartForm({ previousDateRange={!dashboardId ? previousDateRange : undefined} displayType={displayType} configType={configType} + promqlUsesRange={isReducibleRangeQuery({ + promqlExpression: promqlExpressions, + displayType, + })} onChange={handleUpdateDisplaySettings} onClose={closeDisplaySettings} isPerSeriesNumberFormatAllowed={configType !== 'sql'} diff --git a/packages/app/src/components/DBNumberChart.tsx b/packages/app/src/components/DBNumberChart.tsx index 308def077c..26b47e2f31 100644 --- a/packages/app/src/components/DBNumberChart.tsx +++ b/packages/app/src/components/DBNumberChart.tsx @@ -19,6 +19,7 @@ import { Flex, Text } from '@mantine/core'; import { buildMVDateRangeIndicator, convertToNumberChartConfig, + convertToPromqlNumberChartConfig, } from '@/ChartUtils'; import { useQueriedChartConfig } from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; @@ -222,13 +223,13 @@ export default function DBNumberChart({ showMVOptimizationIndicator?: boolean; errorVariant?: ChartErrorStateVariant; }) { - const queriedConfig = useMemo( - () => - isBuilderChartConfig(config) - ? convertToNumberChartConfig(config) - : config, - [config], - ); + const queriedConfig = useMemo(() => { + if (isBuilderChartConfig(config)) return convertToNumberChartConfig(config); + if (isPromqlChartConfig(config)) { + return convertToPromqlNumberChartConfig(config, { withReducer: true }); + } + return config; + }, [config]); const builderQueriedConfig = isBuilderChartConfig(queriedConfig) ? queriedConfig @@ -262,6 +263,11 @@ export default function DBNumberChart({ promqlRows.map((row: Record) => row.series_name), ).size; + // 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 resolvedNumberFormat = useSingleSeriesNumberFormat(queriedConfig); const value = valueColumn @@ -395,10 +401,12 @@ export default function DBNumberChart({ className={isLoading || isPlaceholderData ? 'effect-pulse' : ''} style={{ position: 'relative', width: '100%', height: '100%' }} > - {config.backgroundChart && ( + {showSparkline && config.backgroundChart && ( )}
diff --git a/packages/app/src/components/NumberTileBackgroundChart.tsx b/packages/app/src/components/NumberTileBackgroundChart.tsx index 209063efb5..2511d34930 100644 --- a/packages/app/src/components/NumberTileBackgroundChart.tsx +++ b/packages/app/src/components/NumberTileBackgroundChart.tsx @@ -1,6 +1,10 @@ import { useMemo } from 'react'; import { ErrorBoundary } from 'react-error-boundary'; -import { isBuilderChartConfig } from '@hyperdx/common-utils/dist/guards'; +import { isReducibleRangeQuery } from '@hyperdx/common-utils/dist/core/promql'; +import { + isBuilderChartConfig, + isPromqlChartConfig, +} from '@hyperdx/common-utils/dist/guards'; import { BackgroundChart, ChartConfigWithDateRange, @@ -10,6 +14,7 @@ import { } from '@hyperdx/common-utils/dist/types'; import { + convertToPromqlNumberChartConfig, convertToTimeChartConfig, formatResponseForTimeChart, shouldFillNullsWithZero, @@ -56,24 +61,19 @@ export function sparklinePointsFromGraphResults( } /** - * Derive the sparkline's time-series query config from a number tile's config. - * - * The big number strips both `granularity` and `groupBy` - * (`convertToNumberChartConfig`), collapsing the query to a single aggregate. - * The sparkline must plot that same single series, so it strips `groupBy` too; - * otherwise a tile carrying a residual `groupBy` (left over from a prior Line - * display type) would query multiple series, and the renderer plots only the - * first, which would not match the value. `granularity` is kept (auto when - * unset) to recover the temporal trend behind the value. - * - * Number-tile display-only fields are dropped as well: they flow into the - * query key (via `convertToTimeChartConfig`), so leaving them in would refetch - * identical time-series data on every purely visual edit (sparkline type, tile - * color, color rules, number format). Exported for unit testing. + * Derive the sparkline's query config from the given number tile's config. */ -export function buildSparklineTimeConfig( +export function buildSparklineQueryConfig( config: ChartConfigWithDateRange, ): ChartConfigWithDateRange { + // 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 }); + } + + // Display-only fields are dropped to avoid refetching identical time-series + // data on every purely visual edit. const { backgroundChart: _backgroundChart, color: _color, @@ -90,36 +90,53 @@ export function buildSparklineTimeConfig( if (isBuilderChartConfig(timeConfig)) { delete timeConfig.groupBy; } - return timeConfig; + return convertToTimeChartConfig(timeConfig); } function NumberTileBackgroundChartInner({ config, backgroundChart, + queryKeyPrefix, + enabled, }: { config: ChartConfigWithDateRange; backgroundChart: BackgroundChart; + queryKeyPrefix?: string; + enabled?: boolean; }) { // useTimeChartSettings/convertToTimeChartConfig below resolve 'auto', so // the minimum has to be applied before that happens. const { data: source } = useSource({ id: config.source }); const minGranularitySeconds = getMinGranularitySeconds(source); - const timeConfig = useMemo( - () => buildSparklineTimeConfig({ ...config, minGranularitySeconds }), + const queriedConfig = useMemo( + () => + buildSparklineQueryConfig( + isPromqlChartConfig(config) + ? config + : { ...config, minGranularitySeconds }, + ), [config, minGranularitySeconds], ); const { dateRange, granularity, fillNulls } = - useTimeChartSettings(timeConfig); - const queriedConfig = useMemo( - () => convertToTimeChartConfig(timeConfig), - [timeConfig], - ); + useTimeChartSettings(queriedConfig); const { data } = useQueriedChartConfig(queriedConfig, { placeholderData: prev => prev, - queryKey: ['number-tile-background', queriedConfig], + enabled, + // A PromQL tile shares the cache entry its value is served from, so it has + // to leave the key to the hook. A builder tile derives a query of its + // own, and namespaces its own key. + ...(isPromqlChartConfig(config) + ? { queryKeyPrefix } + : { + queryKey: [ + ...(queryKeyPrefix ? [queryKeyPrefix] : []), + 'number-tile-background', + queriedConfig, + ], + }), }); const points = useMemo(() => { @@ -172,23 +189,31 @@ function NumberTileBackgroundChartInner({ /** * Faint line / area sparkline drawn behind a number tile's value. Returns - * null for non-builder configs (raw SQL number tiles have no structured time - * dimension to bucket) and wraps the renderer in an error boundary so a - * sparkline failure never blanks the tile's value. + * null for configs that don't support bucketed queries. Wrapped in an + * error boundary so a sparkline failure never blanks the tile's value. */ export default function NumberTileBackgroundChart({ config, backgroundChart, + queryKeyPrefix, + enabled = true, }: { config: ChartConfigWithDateRange; backgroundChart: BackgroundChart; + queryKeyPrefix?: string; + enabled?: boolean; }) { - if (!isBuilderChartConfig(config)) return null; + const canBucketQuery = + isBuilderChartConfig(config) || + (isPromqlChartConfig(config) && isReducibleRangeQuery(config)); + if (!canBucketQuery) return null; return ( }> ); diff --git a/packages/app/src/components/__tests__/DBNumberChart.test.tsx b/packages/app/src/components/__tests__/DBNumberChart.test.tsx index 672581a9ab..4853ad134c 100644 --- a/packages/app/src/components/__tests__/DBNumberChart.test.tsx +++ b/packages/app/src/components/__tests__/DBNumberChart.test.tsx @@ -866,6 +866,62 @@ describe('DBNumberChart', () => { ).not.toBeInTheDocument(); }); + describe('background chart', () => { + const rangeConfig = { + ...promqlConfig, + promqlExpression: [ + { expression: 'e2e_service_up', queryType: 'range' as const }, + ], + backgroundChart: { type: 'area' as const }, + }; + + // Whether the tile's query can be bucketed at all is the sparkline's own + // call, so the tile hands over its config and queries nothing extra. + it('hands the sparkline the tile config, and no buckets of its own', () => { + setInstantRows([{ series_name: 'up', value: 5 }]); + + renderWithMantine( + , + ); + + expect(jest.mocked(NumberTileBackgroundChart)).toHaveBeenCalledWith( + expect.objectContaining({ + config: rangeConfig, + backgroundChart: { type: 'area' }, + queryKeyPrefix: 'tile-1', + }), + undefined, + ); + expect(mockUseQueriedChartConfig).toHaveBeenCalledTimes(1); + }); + + it("passes the tile's disabled gate on to the sparkline", () => { + setInstantRows([{ series_name: 'up', value: 5 }]); + + renderWithMantine( + , + ); + + expect(jest.mocked(NumberTileBackgroundChart)).toHaveBeenCalledWith( + expect.objectContaining({ enabled: false }), + undefined, + ); + }); + + it('draws nothing when the expression yields several series', () => { + setInstantRows([ + { series_name: 'up{service="accounting"}', value: 1 }, + { series_name: 'up{service="api-server"}', value: 2 }, + ]); + + renderWithMantine(); + + expect( + screen.queryByTestId('number-tile-background-chart'), + ).not.toBeInTheDocument(); + }); + }); + // Color rules already applied to PromQL tiles; what changes is that the // value they match is now the current one instead of the oldest bucket. it('resolves a color rule against the displayed value', () => { diff --git a/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx b/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx index a8f7f4c7e2..906dd0442c 100644 --- a/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx +++ b/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx @@ -1,10 +1,13 @@ import React from 'react'; -import { DisplayType } from '@hyperdx/common-utils/dist/types'; +import { DisplayType, PromqlReducer } from '@hyperdx/common-utils/dist/types'; +import { screen } from '@testing-library/react'; +import { convertToPromqlNumberChartConfig } from '@/ChartUtils'; import NumberTileBackgroundChart, { - buildSparklineTimeConfig, + buildSparklineQueryConfig, sparklinePointsFromGraphResults, } from '@/components/NumberTileBackgroundChart'; +import { Sparkline } from '@/components/Sparkline'; import { useQueriedChartConfig } from '@/hooks/useChartConfig'; import { useSource } from '@/source'; @@ -17,6 +20,10 @@ jest.mock('@/source', () => ({ useSource: jest.fn(), })); +jest.mock('../Sparkline', () => ({ + Sparkline: jest.fn(() =>
), +})); + describe('sparklinePointsFromGraphResults', () => { const ts = '__hdx_time_bucket'; const value = 'Count'; @@ -73,7 +80,7 @@ describe('sparklinePointsFromGraphResults', () => { }); }); -describe('buildSparklineTimeConfig', () => { +describe('buildSparklineQueryConfig', () => { const baseConfig = { dateRange: [ new Date('2024-01-01T00:00:00Z'), @@ -87,7 +94,7 @@ describe('buildSparklineTimeConfig', () => { }; it('drops groupBy and the display-only fields, keeps granularity, and forces a Line display type', () => { - const result = buildSparklineTimeConfig({ + const result = buildSparklineQueryConfig({ ...baseConfig, granularity: '5 minute', groupBy: 'ServiceName', @@ -108,9 +115,32 @@ describe('buildSparklineTimeConfig', () => { expect(result.displayType).toBe(DisplayType.Line); }); - it("defaults granularity to 'auto' when the tile has none", () => { - const result = buildSparklineTimeConfig(baseConfig); - expect(result.granularity).toBe('auto'); + // Naming no reducer is what leaves the shared response unreduced, so this + // observer plots the buckets the number was reduced from. + it("shares a promql tile's query, naming no reducer", () => { + const promqlConfig = { + configType: 'promql' as const, + displayType: DisplayType.Number, + connection: 'test-connection', + dateRange: baseConfig.dateRange, + promqlExpression: [ + { + expression: 'up', + queryType: 'range' as const, + reducer: PromqlReducer.Max, + }, + ], + }; + + expect(buildSparklineQueryConfig(promqlConfig)).toEqual( + convertToPromqlNumberChartConfig(promqlConfig, { withReducer: false }), + ); + }); + + it('resolves an absent granularity, so the samples land on the grid', () => { + const result = buildSparklineQueryConfig(baseConfig); + expect(result.granularity).not.toBe('auto'); + expect(result.granularity).toBeDefined(); expect(result.displayType).toBe(DisplayType.Line); }); }); @@ -147,7 +177,198 @@ describe('NumberTileBackgroundChart', () => { ); expect(mockUseQueriedChartConfig).toHaveBeenCalled(); - const queriedConfig = mockUseQueriedChartConfig.mock.calls[0][0]; + const [queriedConfig, options] = mockUseQueriedChartConfig.mock.calls[0]; expect(queriedConfig).not.toHaveProperty('groupBy'); + // A builder tile has no query to share with its value, so it keeps its own + // namespaced key rather than the one the hook builds. + expect(options.queryKey).toEqual(['number-tile-background', queriedConfig]); + }); + + it('issues no query while the tile is disabled', () => { + renderWithMantine( + , + ); + + const [, options] = mockUseQueriedChartConfig.mock.calls[0]; + expect(options.enabled).toBe(false); + }); + + it("scopes a builder tile's own key with the prefix, when given one", () => { + renderWithMantine( + , + ); + + const [queriedConfig, options] = mockUseQueriedChartConfig.mock.calls[0]; + expect(options.queryKey).toEqual([ + 'tile-1', + 'number-tile-background', + queriedConfig, + ]); + }); + + describe('promql configs', () => { + const promqlConfig = { + configType: 'promql' as const, + displayType: DisplayType.Number, + promqlExpression: [ + { expression: 'e2e_service_up', queryType: 'range' as const }, + ], + connection: 'test-connection', + granularity: '1 minute' as const, + dateRange: [ + new Date('2024-01-01T00:00:00Z'), + new Date('2024-01-01T00:10:00Z'), + ] as [Date, Date], + backgroundChart: { type: 'area' as const }, + }; + + const sampleRows = ( + values: number[], + startDate: Date, + stepSeconds: number, + ) => ({ + data: values.map((value, index) => ({ + __hdx_time_bucket: new Date( + startDate.getTime() + index * stepSeconds * 1000, + ).toISOString(), + value, + series_name: 'up', + })), + meta: [ + { name: '__hdx_time_bucket', type: 'DateTime64(3)' }, + { name: 'value', type: 'Float64' }, + { name: 'series_name', type: 'String' }, + ], + rows: values.length, + isComplete: true, + }); + + // Prometheus answers a range query at `start + k * step`. Aligning the + // range and resolving the granularity is what puts those samples on the + // same grid the empty-bucket filler generates; querying the tile's own + // unaligned range instead would interleave a zero between every sample and + // draw a sawtooth. + it('queries a granularity-aligned range', () => { + renderWithMantine( + , + ); + + const queriedConfig = mockUseQueriedChartConfig.mock.calls[0][0]; + expect(queriedConfig.dateRange).toEqual([ + new Date('2024-01-01T00:00:00Z'), + new Date('2024-01-01T00:11:00Z'), + ]); + expect(queriedConfig.granularity).toBe('1 minute'); + }); + + it('plots one point per sample, with no zeros between them', () => { + const values = [1, 1.2, 1.1, 1.3, 1.25, 1.4, 1.35, 1.5, 1.45, 1.6, 1.55]; + mockUseQueriedChartConfig.mockReturnValue({ + data: sampleRows(values, promqlConfig.dateRange[0], 60), + }); + + renderWithMantine( + , + ); + + const { points } = jest.mocked(Sparkline).mock.calls[0][0]; + expect(points.map(({ y }) => y)).toEqual(values); + }); + + // The value's query differs only in the reducer, which the hook keeps out + // of the key it builds -- so replacing that key would split the two apart. + it('leaves the key to the hook, which the value shares', () => { + renderWithMantine( + , + ); + + const [, options] = mockUseQueriedChartConfig.mock.calls[0]; + expect(options).not.toHaveProperty('queryKey'); + }); + + // A number evaluates only its first expression, but a leftover row would + // otherwise be queried and plotted alongside it. + it('queries only the expression the number came from', () => { + renderWithMantine( + , + ); + + const queriedConfig = mockUseQueriedChartConfig.mock.calls[0][0]; + expect(queriedConfig.promqlExpression).toEqual([ + { expression: 'e2e_service_up', queryType: 'range' }, + ]); + }); + + // An instant query answers with a single point, so there is no trend. + it('renders nothing, and queries nothing, for an instant tile', () => { + renderWithMantine( + , + ); + + expect(mockUseQueriedChartConfig).not.toHaveBeenCalled(); + expect( + screen.queryByTestId('number-tile-background-chart'), + ).not.toBeInTheDocument(); + }); }); }); diff --git a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx index 3aaf0f208e..0793af11e0 100644 --- a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx +++ b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx @@ -1,6 +1,7 @@ import React from 'react'; import { ResponseJSON } from '@hyperdx/common-utils/dist/clickhouse'; import { ClickhouseClient } from '@hyperdx/common-utils/dist/clickhouse/browser'; +import { DEFAULT_PROMQL_REDUCER } from '@hyperdx/common-utils/dist/core/promql'; import { isBuilderChartConfig } from '@hyperdx/common-utils/dist/guards'; import { ChartConfigWithDateRange, @@ -966,7 +967,10 @@ describe('useChartConfig', () => { it('collapses the buckets to one row per series', async () => { const { result } = renderHook( - () => useQueriedChartConfig(rangeNumberConfig()), + () => + useQueriedChartConfig( + rangeNumberConfig({ reducer: DEFAULT_PROMQL_REDUCER }), + ), { wrapper }, ); @@ -1044,6 +1048,46 @@ describe('useChartConfig', () => { expect(prometheusApi.queryRange).toHaveBeenCalledTimes(1); }); + // The whole point of keeping the reducer out of the key: a number tile + // names one and reads the value, the sparkline behind it names none and + // reads the buckets, and Prometheus is asked once. + it('serves the value and the buckets from one request', async () => { + const valueConfig = rangeNumberConfig({ + reducer: PromqlReducer.Max, + }); + const bucketConfig = rangeNumberConfig(); + const { result } = renderHook( + () => ({ + value: useQueriedChartConfig(valueConfig), + buckets: useQueriedChartConfig(bucketConfig), + }), + { wrapper }, + ); + + await waitFor(() => + expect(result.current.buckets.isSuccess).toBe(true), + ); + expect( + result.current.value.data?.data.map((r: any) => r.value), + ).toEqual([8, 3]); + expect( + result.current.buckets.data?.data.map((r: any) => r.value), + ).toEqual([2, 8, 5, 1, 3]); + expect(prometheusApi.queryRange).toHaveBeenCalledTimes(1); + }); + + it('leaves the buckets alone when no reducer is named', async () => { + const { result } = renderHook( + () => useQueriedChartConfig(rangeNumberConfig()), + { wrapper }, + ); + + await waitFor(() => expect(result.current.isSuccess).toBe(true)); + expect(result.current.data?.data.map((r: any) => r.value)).toEqual([ + 2, 8, 5, 1, 3, + ]); + }); + it('re-queries when a field other than the reducer changes', async () => { const { result, rerender } = renderHook( ({ expression }: { expression: string }) => diff --git a/packages/app/src/hooks/useChartConfig.tsx b/packages/app/src/hooks/useChartConfig.tsx index f94c339de7..ea96708564 100644 --- a/packages/app/src/hooks/useChartConfig.tsx +++ b/packages/app/src/hooks/useChartConfig.tsx @@ -8,9 +8,8 @@ import { import { ClickhouseClient } from '@hyperdx/common-utils/dist/clickhouse/browser'; import { Metadata } from '@hyperdx/common-utils/dist/core/metadata'; import { - displayTypeSupportsReducer, + appliesPromqlReducer, getQueriedPromqlSeries, - isRangeQuery, } from '@hyperdx/common-utils/dist/core/promql'; import { isMetricChartConfig, @@ -346,12 +345,11 @@ export function useQueriedChartConfig( }); const minGranularitySeconds = getMinGranularitySeconds(source); - // A PromQL range query keeps every bucket in the cache and is reduced to a - // single value per series on read. + // 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 = - isPromqlChartConfig(config) && - displayTypeSupportsReducer(config) && - isRangeQuery(config); + isPromqlChartConfig(config) && appliesPromqlReducer(config); const rangeReducer = reducesRangeBuckets ? getQueriedPromqlSeries(config)[0]?.reducer : undefined; diff --git a/packages/app/tests/e2e/components/ChartEditorComponent.ts b/packages/app/tests/e2e/components/ChartEditorComponent.ts index 13fa539dac..90f8086625 100644 --- a/packages/app/tests/e2e/components/ChartEditorComponent.ts +++ b/packages/app/tests/e2e/components/ChartEditorComponent.ts @@ -1173,6 +1173,21 @@ export class ChartEditorComponent { } } + /** + * Set the "Background chart" type in the Display Settings drawer (number + * tiles only). Opens the drawer, picks the type, then applies and closes. + */ + async setBackgroundChart(type: 'None' | 'Line' | 'Area') { + await this.openDisplaySettings(); + const drawer = this.page.getByRole('dialog', { name: 'Display Settings' }); + // Mantine repeats the aria-label on the options listbox, so match the role. + await drawer + .getByRole('combobox', { name: 'Number tile background chart type' }) + .click(); + await drawer.getByRole('option', { name: type, exact: true }).click(); + await this.applyDisplaySettings(); + } + /** * Open the Display Settings drawer and wait for it to become visible. */ diff --git a/packages/app/tests/e2e/features/promql-number.spec.ts b/packages/app/tests/e2e/features/promql-number.spec.ts index 50640eb88f..c7238dcdf8 100644 --- a/packages/app/tests/e2e/features/promql-number.spec.ts +++ b/packages/app/tests/e2e/features/promql-number.spec.ts @@ -153,6 +153,26 @@ test.describe( await expect(editorGranularity).toBeHidden(); }); + await test.step('A background chart plots the range behind the value', async () => { + await editor.setPromqlQueryType('Range', { reducer: 'Max' }); + await editor.setBackgroundChart('Area'); + await editor.runQuery(false); + + await expect( + page.getByTestId('number-tile-background-chart'), + ).toBeVisible({ timeout: 30000 }); + }); + + await test.step('Switching to instant drops the background chart', async () => { + await editor.setPromqlQueryType('Instant'); + await editor.runQuery(false); + + // An instant query has a single point, so there is no trend to plot. + await expect( + page.getByTestId('number-tile-background-chart'), + ).toBeHidden(); + }); + await test.step('The range choice round-trips through a save', async () => { await editor.setPromqlQueryType('Range', { reducer: 'Count' }); await editor.save(); diff --git a/packages/common-utils/src/core/promql.ts b/packages/common-utils/src/core/promql.ts index f5afac9ff1..e60a4c4830 100644 --- a/packages/common-utils/src/core/promql.ts +++ b/packages/common-utils/src/core/promql.ts @@ -115,3 +115,20 @@ export const isRangeQuery = (config: { getQueriedPromqlSeries(config).some( series => promqlSeriesQueryType(series) === 'range', ); + +/** + * Whether this config's range buckets can be collapsed to one value per series by + * a client-side reducer, rather than plotted. + */ +export const isReducibleRangeQuery = (config: { + promqlExpression?: PromqlExpressionList; + displayType?: DisplayType; +}): boolean => displayTypeSupportsReducer(config) && isRangeQuery(config); + +/** Whether the config specifies a reducer. */ +export const appliesPromqlReducer = (config: { + promqlExpression?: PromqlExpressionList; + displayType?: DisplayType; +}): boolean => + isReducibleRangeQuery(config) && + getQueriedPromqlSeries(config)[0]?.reducer != null;