From 81e722d4bbdec3d115eeaf63215a75b38dd446b4 Mon Sep 17 00:00:00 2001 From: Karl Power Date: Mon, 5 Oct 2026 15:13:11 +0200 Subject: [PATCH 1/2] feat: Support minimum auto granularity on PromQL sources --- .changeset/promql-min-auto-granularity.md | 7 ++ packages/api/src/models/source.ts | 21 ++-- .../routers/api/__tests__/sources.int.test.ts | 98 +++++++++++++++++++ packages/app/src/ChartUtils.tsx | 1 + packages/app/src/__tests__/ChartUtils.test.ts | 46 +++++++++ .../DBEditTimeChartForm/ChartPreviewPanel.tsx | 6 +- .../__tests__/utils.test.ts | 53 ++++++++++ .../components/DBEditTimeChartForm/utils.ts | 9 +- packages/app/src/components/DBNumberChart.tsx | 21 ++-- packages/app/src/components/DBTableChart.tsx | 10 +- .../components/NumberTileBackgroundChart.tsx | 10 +- .../SourceForm/MetricTableModelForm.tsx | 26 +---- .../SourceForm/MinAutoGranularityFormRow.tsx | 35 +++++++ .../SourceForm/PromqlTableModelForm.tsx | 18 ++-- .../components/__tests__/DBBarChart.test.tsx | 1 + .../__tests__/DBNumberChart.test.tsx | 26 ++++- .../components/__tests__/DBPieChart.test.tsx | 16 ++- .../__tests__/DBTableChart.test.tsx | 27 +++++ .../NumberTileBackgroundChart.test.tsx | 28 +++++- .../components/charts/CategoricalChart.tsx | 13 ++- .../hooks/__tests__/useChartConfig.test.tsx | 26 ++++- packages/app/src/hooks/useChartConfig.tsx | 15 ++- packages/app/src/utils/promqlChartQuery.ts | 11 ++- .../common-utils/src/__tests__/macros.test.ts | 29 ++++++ .../common-utils/src/__tests__/types.test.ts | 36 +++++++ .../src/core/__tests__/promql.test.ts | 41 +++++++- packages/common-utils/src/core/promql.ts | 43 +++++--- packages/common-utils/src/macros.ts | 15 ++- packages/common-utils/src/types.ts | 41 ++++---- 29 files changed, 627 insertions(+), 102 deletions(-) create mode 100644 .changeset/promql-min-auto-granularity.md create mode 100644 packages/app/src/components/Sources/SourceForm/MinAutoGranularityFormRow.tsx diff --git a/.changeset/promql-min-auto-granularity.md b/.changeset/promql-min-auto-granularity.md new file mode 100644 index 0000000000..62f1b0e53c --- /dev/null +++ b/.changeset/promql-min-auto-granularity.md @@ -0,0 +1,7 @@ +--- +'@hyperdx/common-utils': minor +'@hyperdx/app': minor +'@hyperdx/api': patch +--- + +Support "Minimum auto granularity" on PromQL sources. It floors the step of charts on auto granularity, as it does for metric sources, and `$__rate_interval` now uses it as the source's scrape interval (still 15s when unset) instead of always assuming 15s. A granularity picked on a tile is never changed. diff --git a/packages/api/src/models/source.ts b/packages/api/src/models/source.ts index 998edea21c..664874df5b 100644 --- a/packages/api/src/models/source.ts +++ b/packages/api/src/models/source.ts @@ -271,6 +271,15 @@ const MetricTablesSchema = new Schema( { _id: false }, ); +// See MinAutoGranularitySchema in @hyperdx/common-utils, shared by the metric +// and PromQL sources. The setter mirrors that schema's '' -> undefined +// preprocess, since validateRequest doesn't write the parsed body back to +// req.body. +const minAutoGranularityField = { + type: String, + set: (v: string | null | undefined) => (v === '' ? undefined : v), +}; + type IMetricSource = Extract; export const MetricSource = Source.discriminator( SourceKind.Metric, @@ -284,13 +293,7 @@ export const MetricSource = Source.discriminator( logSourceId: String, // Unified metrics series table. Available only when `isMetricsSeriesTableEnabled` is set on the team document. seriesTable: String, - // See MetricSourceSchema.minAutoGranularity in @hyperdx/common-utils. - // The setter mirrors that schema's '' -> undefined preprocess, since - // validateRequest doesn't write the parsed body back to req.body. - minAutoGranularity: { - type: String, - set: (v: string | null | undefined) => (v === '' ? undefined : v), - }, + minAutoGranularity: minAutoGranularityField, }), ); @@ -300,5 +303,7 @@ export const MetricSource = Source.discriminator( type IPromqlSource = Extract; export const PromqlSource = Source.discriminator( SourceKind.Promql, - new Schema>({}), + new Schema>({ + minAutoGranularity: minAutoGranularityField, + }), ); diff --git a/packages/api/src/routers/api/__tests__/sources.int.test.ts b/packages/api/src/routers/api/__tests__/sources.int.test.ts index 2fa5fab643..531916af56 100644 --- a/packages/api/src/routers/api/__tests__/sources.int.test.ts +++ b/packages/api/src/routers/api/__tests__/sources.int.test.ts @@ -47,6 +47,17 @@ const MOCK_METRIC_SOURCE: Omit, 'id'> = { }, }; +const MOCK_PROMQL_SOURCE: Omit, 'id'> = { + kind: SourceKind.Promql, + name: 'Test PromQL Source', + connection: new Types.ObjectId().toString(), + from: { + databaseName: 'test_db', + tableName: 'metrics_ts', + }, + timestampValueExpression: 'timestamp', +}; + const createTestConnection = (team: Types.ObjectId, id: string) => Connection.create({ _id: id, @@ -63,6 +74,7 @@ const getLoggedInAgent = async (server: ReturnType) => { await Promise.all([ createTestConnection(result.team._id, MOCK_SOURCE.connection), createTestConnection(result.team._id, MOCK_METRIC_SOURCE.connection), + createTestConnection(result.team._id, MOCK_PROMQL_SOURCE.connection), ]); return result; @@ -803,6 +815,92 @@ describe('sources router', () => { }); }); + describe('minAutoGranularity on a PromQL source', () => { + it('POST / - creates a PromQL source with minAutoGranularity and it round-trips', async () => { + const { agent } = await getLoggedInAgent(server); + + const response = await agent + .post('/sources') + .send({ + ...MOCK_PROMQL_SOURCE, + minAutoGranularity: '1 minute', + }) + .expect(200); + + expect(response.body.minAutoGranularity).toBe('1 minute'); + + const sources = await Source.find({}).lean(); + expect(sources).toHaveLength(1); + const persisted = sources[0]; + if (persisted?.kind !== SourceKind.Promql) { + expect(persisted?.kind).toBe(SourceKind.Promql); + throw new Error('Source is not a PromQL source'); + } + expect(persisted.minAutoGranularity).toBe('1 minute'); + }); + + it("POST / - the form's \"No minimum\" value ('') persists as unset, not as an empty string", async () => { + const { agent } = await getLoggedInAgent(server); + + const response = await agent + .post('/sources') + .send({ + ...MOCK_PROMQL_SOURCE, + minAutoGranularity: '', + }) + .expect(200); + + expect(response.body).not.toHaveProperty('minAutoGranularity'); + + const sources = await Source.find({}).lean(); + expect(sources).toHaveLength(1); + expect(sources[0]).not.toHaveProperty('minAutoGranularity'); + }); + + it('PUT /:id - updates minAutoGranularity', async () => { + const { agent, team } = await getLoggedInAgent(server); + + const promqlSource = await Source.create({ + ...MOCK_PROMQL_SOURCE, + minAutoGranularity: '1 minute', + team: team._id, + }); + + await agent + .put(`/sources/${promqlSource._id}`) + .send({ + id: promqlSource._id.toString(), + ...MOCK_PROMQL_SOURCE, + minAutoGranularity: '5 minute', + }) + .expect(200); + + const updatedSource = await Source.findById(promqlSource._id).lean(); + expect(updatedSource).toHaveProperty('minAutoGranularity', '5 minute'); + }); + + it('PUT /:id - removes minAutoGranularity when omitted from the update payload', async () => { + const { agent, team } = await getLoggedInAgent(server); + + const promqlSource = await Source.create({ + ...MOCK_PROMQL_SOURCE, + minAutoGranularity: '1 minute', + team: team._id, + }); + + await agent + .put(`/sources/${promqlSource._id}`) + .send({ + id: promqlSource._id.toString(), + ...MOCK_PROMQL_SOURCE, + }) + .expect(200); + + const updatedSource = await Source.findById(promqlSource._id).lean(); + expect(updatedSource).not.toHaveProperty('minAutoGranularity'); + }); + }); + it('DELETE /:id - deletes a source', async () => { const { agent, team } = await getLoggedInAgent(server); diff --git a/packages/app/src/ChartUtils.tsx b/packages/app/src/ChartUtils.tsx index 79fe3b33d4..93df38bfb5 100644 --- a/packages/app/src/ChartUtils.tsx +++ b/packages/app/src/ChartUtils.tsx @@ -133,6 +133,7 @@ function getAlignedRangeAndGranularity( const granularity = getTimeChartGranularity( config.granularity, config.dateRange, + config.minGranularitySeconds, ); return { granularity, diff --git a/packages/app/src/__tests__/ChartUtils.test.ts b/packages/app/src/__tests__/ChartUtils.test.ts index 65b0c5ebe9..d8c2caedf5 100644 --- a/packages/app/src/__tests__/ChartUtils.test.ts +++ b/packages/app/src/__tests__/ChartUtils.test.ts @@ -1317,6 +1317,29 @@ describe('ChartUtils', () => { expect(result.dateRange).toEqual(promqlConfig.dateRange); }); + + it("floors an auto granularity at the source's minimum", () => { + const result = convertToPromqlTableChartConfig({ + ...promqlConfig, + minGranularitySeconds: 300, + }); + + expect(result.granularity).toBe('5 minute'); + expect(result.dateRange).toEqual([ + new Date('2025-11-26T00:00:00Z'), + new Date('2025-11-26T01:05:00Z'), + ]); + }); + + it('never floors a granularity the tile picked', () => { + const result = convertToPromqlTableChartConfig({ + ...promqlConfig, + granularity: '15 second', + minGranularitySeconds: 300, + }); + + expect(result.granularity).toBe('15 second'); + }); }); describe('convertToReducedPromqlChartConfig and convertToPromqlSparklineChartConfig', () => { @@ -1364,6 +1387,29 @@ describe('ChartUtils', () => { expect(result.granularity).toBe('30 minute'); }); + it("floors an auto granularity at the source's minimum", () => { + const result = convertToReducedPromqlChartConfig({ + ...promqlConfig, + minGranularitySeconds: 300, + }); + + expect(result.granularity).toBe('5 minute'); + expect(result.dateRange).toEqual([ + new Date('2025-11-26T00:00:00Z'), + new Date('2025-11-26T01:05:00Z'), + ]); + }); + + it('never floors a granularity the tile picked', () => { + const result = convertToReducedPromqlChartConfig({ + ...promqlConfig, + granularity: '15 second', + minGranularitySeconds: 300, + }); + + expect(result.granularity).toBe('15 second'); + }); + it('names the reducer, defaulting it, and queries one expression', () => { const result = convertToReducedPromqlChartConfig(promqlConfig); diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx index 8fe0e7b03b..d6b3a504dd 100644 --- a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx @@ -36,6 +36,7 @@ import { DBTimeChart } from '@/components/DBTimeChart'; import EmptyState from '@/components/EmptyState'; import PatternTable from '@/components/PatternTable'; import PromQLPreview from '@/components/PromQLEditor/PromQLPreview'; +import { getMinGranularitySeconds } from '@/hooks/useChartConfig'; import { getEventBody, getFirstTimestampValueExpression, @@ -169,9 +170,10 @@ export function ChartPreviewPanel({ }: ChartPreviewPanelProps) { const [isSampleEventsOpen, setIsSampleEventsOpen] = useState(false); + const minGranularitySeconds = getMinGranularitySeconds(tableSource); const renderedPromql = useMemo( - () => buildRenderedPromqlExpression(queriedConfig), - [queriedConfig], + () => buildRenderedPromqlExpression(queriedConfig, minGranularitySeconds), + [queriedConfig, minGranularitySeconds], ); const blockingFilterNames = missingRequiredFilterNames ?? []; diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts index 571fce470c..a71218d755 100644 --- a/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts +++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/utils.test.ts @@ -364,6 +364,59 @@ describe('buildRenderedPromqlExpression', () => { }, ); + describe("with the source's minimum auto granularity", () => { + const render = ( + displayType: DisplayType, + granularity: string, + minGranularitySeconds?: number, + ) => + buildRenderedPromqlExpression( + promqlConfig({ + displayType, + granularity, + promqlExpression: [ + { expression: 'rate(up[$__rate_interval]) / $__interval' }, + ], + }), + minGranularitySeconds, + )?.expressions?.[0].expression; + + // 70 minutes on auto resolves to 60s, below the 300s floor. + it('floors an auto step that is below it', () => { + const seventyMinutes: [Date, Date] = [ + new Date('2024-01-01T00:00:00Z'), + new Date('2024-01-01T01:10:00Z'), + ]; + const renderShort = (displayType: DisplayType) => + buildRenderedPromqlExpression( + { + ...promqlConfig({ + displayType, + granularity: 'auto', + promqlExpression: [{ expression: '$__interval' }], + }), + dateRange: seventyMinutes, + }, + 300, + )?.expressions?.[0].expression; + + expect(renderShort(DisplayType.Line)).toBe('300s'); + expect(renderShort(DisplayType.Table)).toBe('300s'); + expect(renderShort(DisplayType.Number)).toBe('300s'); + }); + + it('sizes $__rate_interval from it, however the granularity was chosen', () => { + // max(15 + 60, 4 * 60), with the hand-picked 15s step kept + expect(render(DisplayType.Line, '15 second', 60)).toBe( + 'rate(up[240s]) / 15s', + ); + }); + + it('assumes a 15s scrape interval without one', () => { + expect(render(DisplayType.Line, '15 second')).toBe('rate(up[60s]) / 15s'); + }); + }); + 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 8064954ae8..f42a957970 100644 --- a/packages/app/src/components/DBEditTimeChartForm/utils.ts +++ b/packages/app/src/components/DBEditTimeChartForm/utils.ts @@ -290,9 +290,14 @@ function toQueriedPromqlConfig( return config; } -/** The expressions a PromQL tile is queried with, with macros and variables substituted. */ +/** + * The expressions a PromQL tile is queried with, with macros and variables + * substituted. `minGranularitySeconds` is the PromQL source's floor, which the + * chart applies to `auto` granularity and `$__rate_interval`. + */ export function buildRenderedPromqlExpression( queriedConfig: ChartConfigWithDateRange | undefined, + minGranularitySeconds?: number, ): RenderedPromqlExpression | undefined { if (queriedConfig == null || !isPromqlChartConfig(queriedConfig)) { return undefined; @@ -300,7 +305,7 @@ export function buildRenderedPromqlExpression( try { const substituted = substitutePromqlChartConfigTemplates( - toQueriedPromqlConfig(queriedConfig), + toQueriedPromqlConfig({ ...queriedConfig, minGranularitySeconds }), ); return { expressions: getQueriedPromqlSeries(substituted).map((series, index) => ({ diff --git a/packages/app/src/components/DBNumberChart.tsx b/packages/app/src/components/DBNumberChart.tsx index bdbaafd174..6bbf580e3c 100644 --- a/packages/app/src/components/DBNumberChart.tsx +++ b/packages/app/src/components/DBNumberChart.tsx @@ -21,7 +21,10 @@ import { convertToNumberChartConfig, convertToReducedPromqlChartConfig, } from '@/ChartUtils'; -import { useQueriedChartConfig } from '@/hooks/useChartConfig'; +import { + getMinGranularitySeconds, + useQueriedChartConfig, +} from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; import { useSingleSeriesNumberFormat, useSource } from '@/source'; import { @@ -223,13 +226,21 @@ export default function DBNumberChart({ showMVOptimizationIndicator?: boolean; errorVariant?: ChartErrorStateVariant; }) { + const { data: source } = useSource({ + id: config.source, + }); + const minGranularitySeconds = getMinGranularitySeconds(source); + const queriedConfig = useMemo(() => { if (isBuilderChartConfig(config)) return convertToNumberChartConfig(config); if (isPromqlChartConfig(config)) { - return convertToReducedPromqlChartConfig(config); + return convertToReducedPromqlChartConfig({ + ...config, + minGranularitySeconds, + }); } return config; - }, [config]); + }, [config, minGranularitySeconds]); const builderQueriedConfig = isBuilderChartConfig(queriedConfig) ? queriedConfig @@ -273,10 +284,6 @@ export default function DBNumberChart({ : (Object.values(data?.data?.[0] ?? {})?.[0] ?? Number.NaN); const formattedValue = formatNumber(value as number, resolvedNumberFormat); - const { data: source } = useSource({ - id: config.source, - }); - // Resolve the display color in three layers: // 1. Conditional color rules evaluated against the raw value // (last-match-wins, Grafana threshold semantics). Falls through diff --git a/packages/app/src/components/DBTableChart.tsx b/packages/app/src/components/DBTableChart.tsx index 1b13510540..ce8f612e61 100644 --- a/packages/app/src/components/DBTableChart.tsx +++ b/packages/app/src/components/DBTableChart.tsx @@ -21,6 +21,7 @@ import { convertToTableChartConfig, } from '@/ChartUtils'; import { Table, TableVariant } from '@/HDXMultiSeriesTableChart'; +import { getMinGranularitySeconds } from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; import useOffsetPaginatedQuery from '@/hooks/useOffsetPaginatedQuery'; import { useOnClickLinkBuilder } from '@/hooks/useOnClickLinkBuilder'; @@ -88,10 +89,15 @@ export default function DBTableChart({ [onSortingChange], ); + const minGranularitySeconds = getMinGranularitySeconds(source); + const queriedConfig = useMemo(() => { if (isRawSqlChartConfig(config)) return config; if (isPromqlChartConfig(config)) { - return convertToPromqlTableChartConfig(config); + return convertToPromqlTableChartConfig({ + ...config, + minGranularitySeconds, + }); } const _config = convertToTableChartConfig(config); @@ -105,7 +111,7 @@ export default function DBTableChart({ }); } return _config; - }, [config, effectiveSort]); + }, [config, effectiveSort, minGranularitySeconds]); const { data: mvOptimizationData } = useMVOptimizationExplanation( isBuilderChartConfig(queriedConfig) ? queriedConfig : undefined, diff --git a/packages/app/src/components/NumberTileBackgroundChart.tsx b/packages/app/src/components/NumberTileBackgroundChart.tsx index db307f6830..6423eed777 100644 --- a/packages/app/src/components/NumberTileBackgroundChart.tsx +++ b/packages/app/src/components/NumberTileBackgroundChart.tsx @@ -105,17 +105,13 @@ function NumberTileBackgroundChartInner({ enabled?: boolean; }) { // useTimeChartSettings/convertToTimeChartConfig below resolve 'auto', so - // the minimum has to be applied before that happens. + // the minimum has to be applied before that happens. A PromQL tile applies + // it the same way DBNumberChart does, so the two share a cache entry. const { data: source } = useSource({ id: config.source }); const minGranularitySeconds = getMinGranularitySeconds(source); const queriedConfig = useMemo( - () => - buildSparklineQueryConfig( - isPromqlChartConfig(config) - ? config - : { ...config, minGranularitySeconds }, - ), + () => buildSparklineQueryConfig({ ...config, minGranularitySeconds }), [config, minGranularitySeconds], ); diff --git a/packages/app/src/components/Sources/SourceForm/MetricTableModelForm.tsx b/packages/app/src/components/Sources/SourceForm/MetricTableModelForm.tsx index 3950106646..4b56d35b51 100644 --- a/packages/app/src/components/Sources/SourceForm/MetricTableModelForm.tsx +++ b/packages/app/src/components/Sources/SourceForm/MetricTableModelForm.tsx @@ -7,7 +7,6 @@ import { notifications } from '@mantine/notifications'; import api from '@/api'; import { useTablesDirect } from '@/clickhouse'; import { DBTableSelectControlled } from '@/components/DBTableSelect'; -import SelectControlled from '@/components/SelectControlled'; import { SourceSelectControlled } from '@/components/SourceSelect'; import { useMetadataWithSettings } from '@/hooks/useMetadata'; import { useMetricsSeriesTableAvailability } from '@/hooks/useMetricsSeriesTableAvailability'; @@ -18,12 +17,9 @@ import { matchSeriesTable, } from '@/utils/metricTableAutofill'; -import { - DEFAULT_DATABASE, - MIN_AUTO_GRANULARITY_OPTIONS, - OTEL_CLICKHOUSE_EXPRESSIONS, -} from './constants'; +import { DEFAULT_DATABASE, OTEL_CLICKHOUSE_EXPRESSIONS } from './constants'; import { FormRow } from './FormRow'; +import { MinAutoGranularityFormRow } from './MinAutoGranularityFormRow'; import { TableModelProps } from './types'; export function MetricTableModelForm({ @@ -325,23 +321,7 @@ export function MetricTableModelForm({ > - - - + ); diff --git a/packages/app/src/components/Sources/SourceForm/MinAutoGranularityFormRow.tsx b/packages/app/src/components/Sources/SourceForm/MinAutoGranularityFormRow.tsx new file mode 100644 index 0000000000..d8a0d03bd4 --- /dev/null +++ b/packages/app/src/components/Sources/SourceForm/MinAutoGranularityFormRow.tsx @@ -0,0 +1,35 @@ +import { Control } from 'react-hook-form'; +import { TSource } from '@hyperdx/common-utils/dist/types'; + +import SelectControlled from '@/components/SelectControlled'; + +import { MIN_AUTO_GRANULARITY_OPTIONS } from './constants'; +import { FormRow } from './FormRow'; + +const MIN_AUTO_GRANULARITY_HELP_TEXT = + "Floor for 'auto granularity' on charts querying this source. Set this to your metrics' scrape/report interval to avoid sparse-looking charts on short time ranges. Doesn't affect an explicitly chosen (non-auto) granularity."; + +export function MinAutoGranularityFormRow({ + control, + helpText = MIN_AUTO_GRANULARITY_HELP_TEXT, +}: { + control: Control; + helpText?: string; +}) { + return ( + + + + ); +} diff --git a/packages/app/src/components/Sources/SourceForm/PromqlTableModelForm.tsx b/packages/app/src/components/Sources/SourceForm/PromqlTableModelForm.tsx index c4003db4bd..ff3df91ae5 100644 --- a/packages/app/src/components/Sources/SourceForm/PromqlTableModelForm.tsx +++ b/packages/app/src/components/Sources/SourceForm/PromqlTableModelForm.tsx @@ -1,16 +1,22 @@ import { useEffect } from 'react'; +import { MinAutoGranularityFormRow } from './MinAutoGranularityFormRow'; import { TableModelProps } from './types'; -export function PromqlTableModelForm({ - control: _control, - setValue, -}: TableModelProps) { +const PROMQL_MIN_AUTO_GRANULARITY_HELP_TEXT = + "Floor for 'auto granularity' on charts querying this source. Set this to your metrics' scrape interval to avoid sparse-looking charts on short time ranges. Doesn't affect an explicitly chosen (non-auto) granularity. $__rate_interval is also sized from it, assuming a 15 second scrape interval when no minimum is set."; + +export function PromqlTableModelForm({ control, setValue }: TableModelProps) { useEffect(() => { setValue('timestampValueExpression' as any, 'timestamp'); }, [setValue]); // PromQL sources use the standard database + table fields from BaseSourceSchema. - // No additional fields needed; the table should point to the TimeSeries engine table. - return null; + // The table should point to the TimeSeries engine table. + return ( + + ); } diff --git a/packages/app/src/components/__tests__/DBBarChart.test.tsx b/packages/app/src/components/__tests__/DBBarChart.test.tsx index 321f5069bc..6250a9a967 100644 --- a/packages/app/src/components/__tests__/DBBarChart.test.tsx +++ b/packages/app/src/components/__tests__/DBBarChart.test.tsx @@ -9,6 +9,7 @@ import { useSource } from '@/source'; jest.mock('@/hooks/useChartConfig', () => ({ useQueriedChartConfig: jest.fn(), + getMinGranularitySeconds: jest.fn().mockReturnValue(undefined), })); jest.mock('@/hooks/useMVOptimizationExplanation', () => ({ diff --git a/packages/app/src/components/__tests__/DBNumberChart.test.tsx b/packages/app/src/components/__tests__/DBNumberChart.test.tsx index 1ac4c19e10..a53c75d8ce 100644 --- a/packages/app/src/components/__tests__/DBNumberChart.test.tsx +++ b/packages/app/src/components/__tests__/DBNumberChart.test.tsx @@ -6,7 +6,10 @@ import DateRangeIndicator from '@/components/charts/DateRangeIndicator'; import DBNumberChart from '@/components/DBNumberChart'; import MVOptimizationIndicator from '@/components/MaterializedViews/MVOptimizationIndicator'; import NumberTileBackgroundChart from '@/components/NumberTileBackgroundChart'; -import { useQueriedChartConfig } from '@/hooks/useChartConfig'; +import { + getMinGranularitySeconds, + useQueriedChartConfig, +} from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; import { useSource } from '@/source'; import { NumberFormat } from '@/types'; @@ -15,6 +18,7 @@ import { formatNumber, getColorFromCSSToken } from '@/utils'; // Mock dependencies jest.mock('@/hooks/useChartConfig', () => ({ useQueriedChartConfig: jest.fn(), + getMinGranularitySeconds: jest.fn().mockReturnValue(undefined), })); jest.mock('@/hooks/useMVOptimizationExplanation', () => ({ @@ -793,6 +797,26 @@ describe('DBNumberChart', () => { }); }; + it("floors an auto granularity at the source's minimum", () => { + jest.mocked(getMinGranularitySeconds).mockReturnValueOnce(300); + + renderWithMantine( + , + ); + + const queriedConfig = mockUseQueriedChartConfig.mock.calls[0][0]; + expect(queriedConfig.granularity).toBe('5 minute'); + expect(queriedConfig.minGranularitySeconds).toBe(300); + }); + it('shows the value when the expression yields exactly one', () => { setInstantRows([{ series_name: 'up{service="accounting"}', value: 7 }]); mockFormatNumber.mockReturnValue('7'); diff --git a/packages/app/src/components/__tests__/DBPieChart.test.tsx b/packages/app/src/components/__tests__/DBPieChart.test.tsx index 18befe474c..e0277de2e7 100644 --- a/packages/app/src/components/__tests__/DBPieChart.test.tsx +++ b/packages/app/src/components/__tests__/DBPieChart.test.tsx @@ -4,12 +4,16 @@ import { screen, within } from '@testing-library/react'; import DateRangeIndicator from '@/components/charts/DateRangeIndicator'; import { DBPieChart } from '@/components/DBPieChart'; import MVOptimizationIndicator from '@/components/MaterializedViews/MVOptimizationIndicator'; -import { useQueriedChartConfig } from '@/hooks/useChartConfig'; +import { + getMinGranularitySeconds, + useQueriedChartConfig, +} from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; import { useSource } from '@/source'; jest.mock('@/hooks/useChartConfig', () => ({ useQueriedChartConfig: jest.fn(), + getMinGranularitySeconds: jest.fn().mockReturnValue(undefined), })); jest.mock('@/hooks/useMVOptimizationExplanation', () => ({ @@ -318,6 +322,16 @@ describe('DBPieChart', () => { expect(queriedConfig.granularity).toBe('1 minute'); }); + it("floors an auto granularity at the source's minimum", () => { + jest.mocked(getMinGranularitySeconds).mockReturnValueOnce(300); + + renderWithMantine(); + + const queriedConfig = mockUseQueriedChartConfig.mock.calls[0][0]; + expect(queriedConfig.granularity).toBe('5 minute'); + expect(queriedConfig.minGranularitySeconds).toBe(300); + }); + it('keeps the same query key when only the reducer changes', () => { renderWithMantine(); renderWithMantine( diff --git a/packages/app/src/components/__tests__/DBTableChart.test.tsx b/packages/app/src/components/__tests__/DBTableChart.test.tsx index 260b034db9..b755c3de10 100644 --- a/packages/app/src/components/__tests__/DBTableChart.test.tsx +++ b/packages/app/src/components/__tests__/DBTableChart.test.tsx @@ -4,6 +4,7 @@ import DateRangeIndicator from '@/components/charts/DateRangeIndicator'; import DBTableChart from '@/components/DBTableChart'; import MVOptimizationIndicator from '@/components/MaterializedViews/MVOptimizationIndicator'; import { Table } from '@/HDXMultiSeriesTableChart'; +import { getMinGranularitySeconds } from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; import useOffsetPaginatedQuery from '@/hooks/useOffsetPaginatedQuery'; import { useOnClickLinkBuilder } from '@/hooks/useOnClickLinkBuilder'; @@ -19,6 +20,11 @@ jest.mock('next/router', () => ({ useRouter: jest.fn(), })); +jest.mock('@/hooks/useChartConfig', () => ({ + ...jest.requireActual('@/hooks/useChartConfig'), + getMinGranularitySeconds: jest.fn().mockReturnValue(undefined), +})); + jest.mock('@/hooks/useMVOptimizationExplanation', () => ({ useMVOptimizationExplanation: jest.fn().mockReturnValue({ data: undefined, @@ -553,6 +559,27 @@ describe('DBTableChart', () => { expect(queried.granularity).toBe('1 minute'); }); + it("floors an auto granularity at the source's minimum", () => { + jest.mocked(getMinGranularitySeconds).mockReturnValueOnce(300); + + renderWithMantine( + , + ); + + const queried = jest + .mocked(useOffsetPaginatedQuery) + .mock.calls.at(-1)![0]; + expect(queried.granularity).toBe('5 minute'); + }); + it('sorts client-side, since the query cannot be re-ordered server-side', () => { renderWithMantine(); diff --git a/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx b/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx index 05d1e020af..ad68501ef8 100644 --- a/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx +++ b/packages/app/src/components/__tests__/NumberTileBackgroundChart.test.tsx @@ -8,7 +8,10 @@ import NumberTileBackgroundChart, { sparklinePointsFromGraphResults, } from '@/components/NumberTileBackgroundChart'; import { Sparkline } from '@/components/Sparkline'; -import { useQueriedChartConfig } from '@/hooks/useChartConfig'; +import { + getMinGranularitySeconds, + useQueriedChartConfig, +} from '@/hooks/useChartConfig'; import { useSource } from '@/source'; jest.mock('@/hooks/useChartConfig', () => ({ @@ -298,6 +301,29 @@ describe('NumberTileBackgroundChart', () => { expect(queriedConfig.granularity).toBe('1 minute'); }); + // The number tile applies the source's floor to its own config, and the two + // share one cached query, so the sparkline has to apply it identically. + it("floors an auto granularity at the source's minimum, like the value", () => { + jest.mocked(getMinGranularitySeconds).mockReturnValueOnce(300); + const autoConfig = { ...promqlConfig, granularity: 'auto' }; + + renderWithMantine( + , + ); + + const queriedConfig = mockUseQueriedChartConfig.mock.calls[0][0]; + expect(queriedConfig.granularity).toBe('5 minute'); + expect(queriedConfig).toEqual( + convertToPromqlSparklineChartConfig({ + ...autoConfig, + minGranularitySeconds: 300, + }), + ); + }); + 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({ diff --git a/packages/app/src/components/charts/CategoricalChart.tsx b/packages/app/src/components/charts/CategoricalChart.tsx index dcaa6d6322..6174430c12 100644 --- a/packages/app/src/components/charts/CategoricalChart.tsx +++ b/packages/app/src/components/charts/CategoricalChart.tsx @@ -13,7 +13,10 @@ import { formatResponseForCategoricalChart, } from '@/ChartUtils'; import MVOptimizationIndicator from '@/components/MaterializedViews/MVOptimizationIndicator'; -import { useQueriedChartConfig } from '@/hooks/useChartConfig'; +import { + getMinGranularitySeconds, + useQueriedChartConfig, +} from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; import { useSingleSeriesNumberFormat, useSource } from '@/source'; import { getColorProps } from '@/utils'; @@ -46,16 +49,20 @@ export function useCategoricalChart({ toolbarSuffix, }: CategoricalChartProps) { const { data: source } = useSource({ id: config.source }); + const minGranularitySeconds = getMinGranularitySeconds(source); const queriedConfig = useMemo(() => { if (isBuilderChartConfig(config)) { return convertToCategoricalChartConfig(config); } if (isPromqlChartConfig(config)) { - return convertToReducedPromqlChartConfig(config); + return convertToReducedPromqlChartConfig({ + ...config, + minGranularitySeconds, + }); } return config; - }, [config]); + }, [config, minGranularitySeconds]); const resolvedNumberFormat = useSingleSeriesNumberFormat(queriedConfig); diff --git a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx index be35e81cc7..c5b0be47ec 100644 --- a/packages/app/src/hooks/__tests__/useChartConfig.test.tsx +++ b/packages/app/src/hooks/__tests__/useChartConfig.test.tsx @@ -2412,7 +2412,31 @@ describe('useChartConfig', () => { expect(getMinGranularitySeconds(baseMetricSource)).toBeUndefined(); }); - it('returns undefined for a non-metric source', () => { + describe('for a PromQL source', () => { + const basePromqlSource = { + id: 'source-3', + kind: SourceKind.Promql, + name: 'Test Prometheus', + connection: 'conn-1', + from: { databaseName: 'default', tableName: 'metrics_ts' }, + timestampValueExpression: 'timestamp', + } satisfies Extract; + + it('converts the source minAutoGranularity setting to seconds', () => { + expect( + getMinGranularitySeconds({ + ...basePromqlSource, + minAutoGranularity: '1 minute', + }), + ).toBe(60); + }); + + it('returns undefined when the source has no minAutoGranularity set', () => { + expect(getMinGranularitySeconds(basePromqlSource)).toBeUndefined(); + }); + }); + + it('returns undefined for a source that has no such setting', () => { expect( getMinGranularitySeconds({ id: 'source-2', diff --git a/packages/app/src/hooks/useChartConfig.tsx b/packages/app/src/hooks/useChartConfig.tsx index b9e8e99f88..bb211f8c14 100644 --- a/packages/app/src/hooks/useChartConfig.tsx +++ b/packages/app/src/hooks/useChartConfig.tsx @@ -32,6 +32,7 @@ import { ChartConfigWithDateRange, ChartConfigWithOptDateRange, isMetricSource, + isPromqlSource, QuerySettings, TSource, } from '@hyperdx/common-utils/dist/types'; @@ -121,7 +122,11 @@ const shouldUseChunking = ( export function getMinGranularitySeconds( source: TSource | undefined, ): number | undefined { - if (!source || !isMetricSource(source) || !source.minAutoGranularity) { + if ( + !source || + !(isMetricSource(source) || isPromqlSource(source)) || + !source.minAutoGranularity + ) { return undefined; } return convertGranularityToSeconds(source.minAutoGranularity); @@ -408,7 +413,13 @@ export function useQueriedChartConfig( queryFn: async context => { // PromQL queries go through the Prometheus API route, not ClickHouse proxy if (isPromqlChartConfig(config) && config.dateRange) { - return queryPromqlChartConfig(config, config.dateRange, context.signal); + // Macros read the floor as the source's scrape interval, so it has to + // reach them even when the caller didn't put it on the config. + return queryPromqlChartConfig( + { ...config, minGranularitySeconds }, + config.dateRange, + context.signal, + ); } const optimizedConfig = { diff --git a/packages/app/src/utils/promqlChartQuery.ts b/packages/app/src/utils/promqlChartQuery.ts index 38949028ac..5af30933d5 100644 --- a/packages/app/src/utils/promqlChartQuery.ts +++ b/packages/app/src/utils/promqlChartQuery.ts @@ -8,6 +8,7 @@ import { import { renderPromqlSeriesNames } from '@hyperdx/common-utils/dist/core/seriesNameTemplate'; import { substitutePromqlChartConfigTemplates } from '@hyperdx/common-utils/dist/macros'; import { + DateRange, DisplayType, PrometheusMatrixResult, PromqlChartConfig, @@ -106,7 +107,7 @@ async function fetchInstantExpression( /** Evaluate one expression over the whole window, at the range endpoint. */ async function fetchRangeExpression( - config: PromqlChartConfig, + config: PromqlChartConfig & Pick, series: PromqlSeries, dateRange: [Date, Date], signal: AbortSignal, @@ -116,7 +117,11 @@ async function fetchRangeExpression( query: series.expression, start: startDate.getTime() / 1000, end: endDate.getTime() / 1000, - step: promqlStep(config.granularity, dateRange), + step: promqlStep( + config.granularity, + dateRange, + config.minGranularitySeconds, + ), connectionId: config.connection, database: config.from?.databaseName, table: config.from?.tableName, @@ -257,7 +262,7 @@ export function toTableRows( * response, so the chart formatters treat it like every other source. */ export async function queryPromqlChartConfig( - config: PromqlChartConfig, + config: PromqlChartConfig & Pick, dateRange: [Date, Date], signal: AbortSignal, ): Promise { diff --git a/packages/common-utils/src/__tests__/macros.test.ts b/packages/common-utils/src/__tests__/macros.test.ts index df0b1152f3..f3d7d29f00 100644 --- a/packages/common-utils/src/__tests__/macros.test.ts +++ b/packages/common-utils/src/__tests__/macros.test.ts @@ -686,10 +686,12 @@ describe('substitutePromqlChartConfigTemplates macros', () => { variables, granularity = '5 minute', dateRange = PROMQL_DATE_RANGE, + minGranularitySeconds, }: { variables?: ChartVariable[]; granularity?: string; dateRange?: [Date, Date]; + minGranularitySeconds?: number; } = {}, ) => substitutePromqlChartConfigTemplates({ @@ -699,6 +701,7 @@ describe('substitutePromqlChartConfigTemplates macros', () => { variables, granularity, dateRange, + minGranularitySeconds, }).promqlExpression; it('expands macros without a variable context', () => { @@ -717,6 +720,32 @@ describe('substitutePromqlChartConfigTemplates macros', () => { ).toBe('rate(up{service=~"(api|web)"}[315s])'); }); + it("sizes `$__rate_interval` from the source's minimum auto granularity", () => { + // max(15 + 60, 4 * 60): without the floor this would be 60s, a window that + // holds a single sample of data scraped every 60s. + expect( + substitute('rate(a[$__rate_interval])', { + granularity: '15 second', + minGranularitySeconds: 60, + }), + ).toBe('rate(a[240s])'); + }); + + it('floors `$__interval` only while the granularity is auto', () => { + expect( + substitute('avg_over_time(a[$__interval])', { + granularity: 'auto', + minGranularitySeconds: 300, + }), + ).toBe('avg_over_time(a[300s])'); + expect( + substitute('avg_over_time(a[$__interval])', { + granularity: '15 second', + minGranularitySeconds: 300, + }), + ).toBe('avg_over_time(a[15s])'); + }); + it('expands every expression of a multi-expression config', () => { expect( substitutePromqlChartConfigTemplates({ diff --git a/packages/common-utils/src/__tests__/types.test.ts b/packages/common-utils/src/__tests__/types.test.ts index 1efa62b32a..4f072291b7 100644 --- a/packages/common-utils/src/__tests__/types.test.ts +++ b/packages/common-utils/src/__tests__/types.test.ts @@ -14,6 +14,7 @@ import { MetricSourceSchema, PresetDashboard, PresetDashboardFilterSchema, + PromqlSourceSchema, SavedChartConfigSchema, } from '@/types'; @@ -885,6 +886,41 @@ describe('MetricSourceSchema.minAutoGranularity', () => { }); }); +describe('PromqlSourceSchema.minAutoGranularity', () => { + const base = { + id: 'source-1', + kind: 'promql' as const, + name: 'Prometheus', + connection: 'conn-1', + from: { databaseName: 'default', tableName: 'metrics_ts' }, + timestampValueExpression: 'timestamp', + }; + const parse = (minAutoGranularity: unknown) => + PromqlSourceSchema.safeParse({ ...base, minAutoGranularity }); + + it('accepts a valid interval or absent', () => { + const result = parse('1 minute'); + expect(result.success).toBe(true); + if (result.success) { + expect(result.data.minAutoGranularity).toBe('1 minute'); + } + expect(parse(undefined).success).toBe(true); + }); + + it("coerces the form's \"No minimum\" value ('') to undefined", () => { + const result = parse(''); + expect(result.success).toBe(true); + if (result.success) { + expect(result.data.minAutoGranularity).toBeUndefined(); + } + }); + + it('rejects a malformed interval', () => { + expect(parse('1 fortnight').success).toBe(false); + expect(parse('minute').success).toBe(false); + }); +}); + describe('MetricFormulaSchema', () => { it('parses an expression-only formula', () => { const result = MetricFormulaSchema.safeParse({ diff --git a/packages/common-utils/src/core/__tests__/promql.test.ts b/packages/common-utils/src/core/__tests__/promql.test.ts index 0271cc0523..9499ca9104 100644 --- a/packages/common-utils/src/core/__tests__/promql.test.ts +++ b/packages/common-utils/src/core/__tests__/promql.test.ts @@ -231,6 +231,19 @@ describe('promqlStep with a window', () => { expect(promqlStep(undefined, hour)).toBe('60s'); expect(promqlStep('auto', month)).not.toBe('60s'); }); + + it('floors an auto step at minGranularitySeconds', () => { + expect(promqlStep('auto', hour, 300)).toBe('300s'); + expect(promqlStep(undefined, hour, 300)).toBe('300s'); + }); + + it('leaves an auto step alone when it is already above the floor', () => { + expect(promqlStep('auto', month, 60)).toBe(promqlStep('auto', month)); + }); + + it('never floors a granularity the tile picked', () => { + expect(promqlStep('15 second', hour, 300)).toBe('15s'); + }); }); describe('PROMQL_MACROS', () => { @@ -242,9 +255,10 @@ describe('PROMQL_MACROS', () => { name: string, granularity: string, dateRange: [Date, Date], + minGranularitySeconds?: number, ) => PROMQL_MACROS.find(macro => macro.name === name)?.expand( - getPromqlMacroInputs(granularity, dateRange), + getPromqlMacroInputs(granularity, dateRange, minGranularitySeconds), ); it('uses the step for $__interval', () => { @@ -270,6 +284,31 @@ describe('PROMQL_MACROS', () => { expect(expandMacro('rate_interval', '1 minute', hour)).toBe('75s'); expect(expandMacro('rate_interval', '5 minute', hour)).toBe('315s'); }); + + it('floors an auto $__interval at minGranularitySeconds', () => { + expect(expandMacro('interval', 'auto', hour, 300)).toBe('300s'); + }); + + it('sizes $__rate_interval from minGranularitySeconds as the scrape interval', () => { + // max(60 + 60, 4 * 60) + expect(expandMacro('rate_interval', '1 minute', hour, 60)).toBe('240s'); + // max(300 + 300, 4 * 300) + expect(expandMacro('rate_interval', 'auto', hour, 300)).toBe('1200s'); + }); + + it('sizes $__rate_interval from the floor even when the tile picked its granularity', () => { + // The step stays 15s, but the data only arrives every 60s: + // max(15 + 60, 4 * 60) + expect(expandMacro('interval', '15 second', hour, 60)).toBe('15s'); + expect(expandMacro('rate_interval', '15 second', hour, 60)).toBe('240s'); + }); + + it('assumes a 15s scrape interval when there is no floor', () => { + expect(expandMacro('rate_interval', '15 second', hour, undefined)).toBe( + '60s', + ); + expect(expandMacro('rate_interval', '15 second', hour, 0)).toBe('60s'); + }); }); describe('isRangeQuery', () => { diff --git a/packages/common-utils/src/core/promql.ts b/packages/common-utils/src/core/promql.ts index 776d26ef20..6406cc6b7c 100644 --- a/packages/common-utils/src/core/promql.ts +++ b/packages/common-utils/src/core/promql.ts @@ -103,14 +103,22 @@ export function reducePromqlSamples( } } -/** A HyperDX granularity ("5 minute") as a Prometheus step, in seconds. */ +/** + * A HyperDX granularity ("5 minute") as a Prometheus step, in seconds. A + * `minGranularitySeconds` floor only applies when the granularity is `auto`. + */ const promqlStepSeconds = ( granularity: string | undefined, dateRange?: [Date, Date], + minGranularitySeconds?: number, ): number => { const resolved = (!granularity || granularity === 'auto') && dateRange - ? convertDateRangeToGranularityString(dateRange) + ? convertDateRangeToGranularityString( + dateRange, + undefined, + minGranularitySeconds, + ) : granularity; if (!resolved || resolved === 'auto') return 60; // convertGranularityToSeconds returns 0 for units it doesn't recognize. @@ -121,13 +129,13 @@ const promqlStepSeconds = ( export const promqlStep = ( granularity: string | undefined, dateRange?: [Date, Date], -): string => `${promqlStepSeconds(granularity, dateRange)}s`; + minGranularitySeconds?: number, +): string => + `${promqlStepSeconds(granularity, dateRange, minGranularitySeconds)}s`; /** - * HyperDX doesn't know a PromQL source's scrape interval, so `$__rate_interval` - * assumes Prometheus' default. - * - * TODO (HDX-5512): Use the PromQL source's minGranularitySeconds setting instead. + * Prometheus' default scrape interval, which `$__rate_interval` assumes when + * the PromQL source has no minimum auto granularity set. */ const PROMQL_DEFAULT_SCRAPE_INTERVAL_SECONDS = 15; @@ -135,6 +143,8 @@ const PROMQL_DEFAULT_SCRAPE_INTERVAL_SECONDS = 15; export type PromqlMacroInputs = { intervalSeconds: number; rangeSeconds: number; + /** How often the source's data is scraped, which `$__rate_interval` is sized from. */ + scrapeIntervalSeconds: number; }; export type PromqlMacro = MacroSuggestion & { @@ -162,11 +172,11 @@ export const PROMQL_MACROS = [ name: 'rate_interval', minArgs: 0, maxArgs: 0, - description: `A safe range for rate() and increase(): the larger of $__interval + ${PROMQL_DEFAULT_SCRAPE_INTERVAL_SECONDS}s and ${4 * PROMQL_DEFAULT_SCRAPE_INTERVAL_SECONDS}s, assuming a ${PROMQL_DEFAULT_SCRAPE_INTERVAL_SECONDS}s scrape interval. e.g. rate(metric[$__rate_interval]).`, - expand: ({ intervalSeconds }) => + description: `A safe range for rate() and increase(): the larger of $__interval + the scrape interval and 4 times the scrape interval. The scrape interval is the source's minimum auto granularity, or ${PROMQL_DEFAULT_SCRAPE_INTERVAL_SECONDS}s if it has none. e.g. rate(metric[$__rate_interval]).`, + expand: ({ intervalSeconds, scrapeIntervalSeconds }) => `${Math.max( - intervalSeconds + PROMQL_DEFAULT_SCRAPE_INTERVAL_SECONDS, - 4 * PROMQL_DEFAULT_SCRAPE_INTERVAL_SECONDS, + intervalSeconds + scrapeIntervalSeconds, + 4 * scrapeIntervalSeconds, )}s`, }, ] as const satisfies readonly PromqlMacro[]; @@ -181,14 +191,23 @@ export const isPromqlMacroName = (name: string): name is PromqlMacroName => export function getPromqlMacroInputs( granularity: string | undefined, dateRange: [Date, Date], + minGranularitySeconds?: number, ): PromqlMacroInputs { const [start, end] = dateRange; return { - intervalSeconds: promqlStepSeconds(granularity, dateRange), + intervalSeconds: promqlStepSeconds( + granularity, + dateRange, + minGranularitySeconds, + ), rangeSeconds: Math.max( 1, Math.round((end.getTime() - start.getTime()) / 1000), ), + // Unlike the step, this applies however the granularity was chosen: it + // describes the data, not the chart. + scrapeIntervalSeconds: + minGranularitySeconds || PROMQL_DEFAULT_SCRAPE_INTERVAL_SECONDS, }; } diff --git a/packages/common-utils/src/macros.ts b/packages/common-utils/src/macros.ts index d1f1429e80..ada123557d 100644 --- a/packages/common-utils/src/macros.ts +++ b/packages/common-utils/src/macros.ts @@ -526,14 +526,25 @@ export function substitutePromqlChartConfigTemplates< variables?: ChartVariable[]; granularity?: string; dateRange: [Date, Date]; + minGranularitySeconds?: number; }, >(config: T): T { - const { promqlExpression, variables = [], granularity, dateRange } = config; + const { + promqlExpression, + variables = [], + granularity, + dateRange, + minGranularitySeconds, + } = config; const variableContext: VariableContext = { variables, inputLanguage: 'promql', }; - const macroInputs = getPromqlMacroInputs(granularity, dateRange); + const macroInputs = getPromqlMacroInputs( + granularity, + dateRange, + minGranularitySeconds, + ); const macrosByName = new Map( PROMQL_MACROS.map(({ name, minArgs, maxArgs, description, expand }) => [ name, diff --git a/packages/common-utils/src/types.ts b/packages/common-utils/src/types.ts index 69c26e4699..47888e8286 100644 --- a/packages/common-utils/src/types.ts +++ b/packages/common-utils/src/types.ts @@ -1887,10 +1887,11 @@ export type DateRange = { // `__hdx_series_limit` CTE so every chunk ranks (and keeps) the same // top-N series. Never persisted. seriesLimitDateRange?: [Date, Date]; - // Runtime-only, populated from the queried MetricSource's + // Runtime-only, populated from the queried Metric or PromQL source's // `minAutoGranularity` (when set) by whichever caller resolves the source // before rendering. Floors "auto" granularity resolution; see - // `convertDateRangeToGranularityString`. Never persisted. + // `convertDateRangeToGranularityString`. PromQL also reads it as the + // source's scrape interval for `$__rate_interval`. Never persisted. minGranularitySeconds?: number; }; @@ -2548,6 +2549,24 @@ export const SessionSourceSchema = BaseSourceSchema.extend({ resourceAttributesExpression: z.string().optional(), }); +/** + * Floor for "Auto Granularity" on charts querying a metric or PromQL source. + * Without this, a short selected date range can auto-infer a bucket smaller + * than the metric's actual scrape/report interval, producing sparse/steppy + * series. Unset preserves the previous unfloored behavior. Only + * constrains auto-inference - an explicit (non-"auto") granularity + * picked on a tile is never overridden. PromQL sources also use it as the + * scrape interval that `$__rate_interval` is computed from. + * + * Preprocessed so the form's "No minimum" option (stored as `''`, since a + * Mantine Select needs a string value for every entry including the unset + * one) round-trips through the schema as `undefined`. + */ +export const MinAutoGranularitySchema = z.preprocess( + v => (v === '' ? undefined : v), + SQLIntervalSchema.optional(), +); + // Metric source form schema export const MetricSourceSchema = BaseSourceSchema.extend({ kind: z.literal(SourceKind.Metric), @@ -2568,27 +2587,13 @@ export const MetricSourceSchema = BaseSourceSchema.extend({ logSourceId: z.string().optional(), // Unified metrics series table. Available only when `isMetricsSeriesTableEnabled` is set on the team document. seriesTable: z.string().optional(), - /** - * Floor for "Auto Granularity" on charts querying this source. Without - * this, a short selected date range can auto-infer a bucket smaller than - * the metric's actual scrape/report interval, producing sparse/steppy - * series. Unset preserves the previous unfloored behavior. Only - * constrains auto-inference - an explicit (non-"auto") granularity - * picked on a tile is never overridden. - * - * Preprocessed so the form's "No minimum" option (stored as `''`, since a - * Mantine Select needs a string value for every entry including the unset - * one) round-trips through the schema as `undefined`. - */ - minAutoGranularity: z.preprocess( - v => (v === '' ? undefined : v), - SQLIntervalSchema.optional(), - ), + minAutoGranularity: MinAutoGranularitySchema, }); // PromQL source form schema export const PromqlSourceSchema = BaseSourceSchema.extend({ kind: z.literal(SourceKind.Promql), + minAutoGranularity: MinAutoGranularitySchema, }); // Union of all source form schemas for validation From 2f20b9a03654a01c5ea2b7cfe8449da1fa22548f Mon Sep 17 00:00:00 2001 From: Karl Power Date: Mon, 5 Oct 2026 15:58:04 +0200 Subject: [PATCH 2/2] use queried source in chart preview --- .../DBEditTimeChartForm/ChartPreviewPanel.tsx | 4 +- .../__tests__/ChartPreviewPanel.test.tsx | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx index d6b3a504dd..49a71a6182 100644 --- a/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx @@ -41,6 +41,7 @@ import { getEventBody, getFirstTimestampValueExpression, isSingleExpression, + useSource, } from '@/source'; import { orderByStringToSortingState, @@ -170,7 +171,8 @@ export function ChartPreviewPanel({ }: ChartPreviewPanelProps) { const [isSampleEventsOpen, setIsSampleEventsOpen] = useState(false); - const minGranularitySeconds = getMinGranularitySeconds(tableSource); + const { data: queriedSource } = useSource({ id: queriedConfig?.source }); + const minGranularitySeconds = getMinGranularitySeconds(queriedSource); const renderedPromql = useMemo( () => buildRenderedPromqlExpression(queriedConfig, minGranularitySeconds), [queriedConfig, minGranularitySeconds], diff --git a/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx b/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx index 0943d8d8a0..068897a2c6 100644 --- a/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx @@ -66,8 +66,10 @@ jest.mock('@/components/DBHeatmapChart', () => ({ HEATMAP_N_BUCKETS: 80, })); +const mockUseSource = jest.fn(); jest.mock('@/source', () => ({ getFirstTimestampValueExpression: jest.fn().mockReturnValue('Timestamp'), + useSource: (opts: { id?: string }) => mockUseSource(opts), })); const dateRange: [Date, Date] = [ @@ -118,6 +120,7 @@ const renderPanel = ( describe('ChartPreviewPanel', () => { beforeEach(() => { jest.clearAllMocks(); + mockUseSource.mockReturnValue({ data: undefined }); }); describe('when no query has been run', () => { @@ -278,6 +281,8 @@ describe('ChartPreviewPanel', () => { overrides: { promqlExpression?: PromqlExpressionList; displayType?: DisplayType; + source?: string; + granularity?: string; variables?: ChartVariable[]; } = {}, ): ChartConfigWithDateRange => ({ @@ -423,6 +428,44 @@ describe('ChartPreviewPanel', () => { ).not.toBeInTheDocument(); }); + // Selecting another source doesn't rerun the query, and the chart reads the + // floor from the source it ran with, so the preview has to as well. + it("expands macros with the queried source's floor, not the selected one's", async () => { + const promqlSource = ( + id: string, + minAutoGranularity?: string, + ): TSource => ({ + id, + kind: SourceKind.Promql, + name: id, + connection: 'default', + from: { databaseName: 'default', tableName: 'metrics_ts' }, + timestampValueExpression: 'timestamp', + minAutoGranularity, + }); + mockUseSource.mockImplementation(({ id }: { id?: string }) => ({ + data: + id === 'queried' ? promqlSource('queried', '1 minute') : undefined, + })); + + renderPanel({ + queriedConfig: promqlConfig({ + promqlExpression: 'rate(up[$__rate_interval])', + source: 'queried', + granularity: '15 second', + }), + tableSource: promqlSource('selected'), + showGeneratedPromql: true, + }); + await openGeneratedPromql(); + + // max(15 + 60, 4 * 60). The selected source has no floor, which would + // give max(15 + 15, 4 * 15) = 60s. + expect(screen.getByTestId('chart-promql-preview')).toHaveTextContent( + 'rate(up[240s])', + ); + }); + it('shows one preview per expression, labelled by alias', async () => { renderPanel({ queriedConfig: promqlConfig({