Skip to content

Commit 8d2b55c

Browse files
authored
Merge branch 'main' into fix/implicit-search-fixedstring-hastoken
2 parents 6dcd370 + ff5befa commit 8d2b55c

31 files changed

Lines changed: 674 additions & 102 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@hyperdx/common-utils': minor
3+
'@hyperdx/app': minor
4+
'@hyperdx/api': patch
5+
---
6+
7+
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.

‎.github/VOUCHED.td‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
adityapimpalkar
2929
alex-fedotyev
3030
alok87
31+
archat-hash
3132
arj22
3233
aryainguz
34+
bilalatique
3335
brandon-pereira
3436
colehpage
3537
copilot

‎packages/api/src/models/source.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ const MetricTablesSchema = new Schema(
271271
{ _id: false },
272272
);
273273

274+
// See MinAutoGranularitySchema in @hyperdx/common-utils, shared by the metric
275+
// and PromQL sources. The setter mirrors that schema's '' -> undefined
276+
// preprocess, since validateRequest doesn't write the parsed body back to
277+
// req.body.
278+
const minAutoGranularityField = {
279+
type: String,
280+
set: (v: string | null | undefined) => (v === '' ? undefined : v),
281+
};
282+
274283
type IMetricSource = Extract<ISource, { kind: SourceKind.Metric }>;
275284
export const MetricSource = Source.discriminator<IMetricSource>(
276285
SourceKind.Metric,
@@ -284,13 +293,7 @@ export const MetricSource = Source.discriminator<IMetricSource>(
284293
logSourceId: String,
285294
// Unified metrics series table. Available only when `isMetricsSeriesTableEnabled` is set on the team document.
286295
seriesTable: String,
287-
// See MetricSourceSchema.minAutoGranularity in @hyperdx/common-utils.
288-
// The setter mirrors that schema's '' -> undefined preprocess, since
289-
// validateRequest doesn't write the parsed body back to req.body.
290-
minAutoGranularity: {
291-
type: String,
292-
set: (v: string | null | undefined) => (v === '' ? undefined : v),
293-
},
296+
minAutoGranularity: minAutoGranularityField,
294297
}),
295298
);
296299

@@ -300,5 +303,7 @@ export const MetricSource = Source.discriminator<IMetricSource>(
300303
type IPromqlSource = Extract<ISource, { kind: SourceKind.Promql }>;
301304
export const PromqlSource = Source.discriminator<IPromqlSource>(
302305
SourceKind.Promql,
303-
new Schema<Extract<ISource, { kind: SourceKind.Promql }>>({}),
306+
new Schema<Extract<ISource, { kind: SourceKind.Promql }>>({
307+
minAutoGranularity: minAutoGranularityField,
308+
}),
304309
);

‎packages/api/src/routers/api/__tests__/sources.int.test.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ const MOCK_METRIC_SOURCE: Omit<Extract<TSource, { kind: 'metric' }>, 'id'> = {
4747
},
4848
};
4949

50+
const MOCK_PROMQL_SOURCE: Omit<Extract<TSource, { kind: 'promql' }>, 'id'> = {
51+
kind: SourceKind.Promql,
52+
name: 'Test PromQL Source',
53+
connection: new Types.ObjectId().toString(),
54+
from: {
55+
databaseName: 'test_db',
56+
tableName: 'metrics_ts',
57+
},
58+
timestampValueExpression: 'timestamp',
59+
};
60+
5061
const createTestConnection = (team: Types.ObjectId, id: string) =>
5162
Connection.create({
5263
_id: id,
@@ -63,6 +74,7 @@ const getLoggedInAgent = async (server: ReturnType<typeof getServer>) => {
6374
await Promise.all([
6475
createTestConnection(result.team._id, MOCK_SOURCE.connection),
6576
createTestConnection(result.team._id, MOCK_METRIC_SOURCE.connection),
77+
createTestConnection(result.team._id, MOCK_PROMQL_SOURCE.connection),
6678
]);
6779

6880
return result;
@@ -803,6 +815,92 @@ describe('sources router', () => {
803815
});
804816
});
805817

818+
describe('minAutoGranularity on a PromQL source', () => {
819+
it('POST / - creates a PromQL source with minAutoGranularity and it round-trips', async () => {
820+
const { agent } = await getLoggedInAgent(server);
821+
822+
const response = await agent
823+
.post('/sources')
824+
.send({
825+
...MOCK_PROMQL_SOURCE,
826+
minAutoGranularity: '1 minute',
827+
})
828+
.expect(200);
829+
830+
expect(response.body.minAutoGranularity).toBe('1 minute');
831+
832+
const sources = await Source.find({}).lean();
833+
expect(sources).toHaveLength(1);
834+
const persisted = sources[0];
835+
if (persisted?.kind !== SourceKind.Promql) {
836+
expect(persisted?.kind).toBe(SourceKind.Promql);
837+
throw new Error('Source is not a PromQL source');
838+
}
839+
expect(persisted.minAutoGranularity).toBe('1 minute');
840+
});
841+
842+
it("POST / - the form's \"No minimum\" value ('') persists as unset, not as an empty string", async () => {
843+
const { agent } = await getLoggedInAgent(server);
844+
845+
const response = await agent
846+
.post('/sources')
847+
.send({
848+
...MOCK_PROMQL_SOURCE,
849+
minAutoGranularity: '',
850+
})
851+
.expect(200);
852+
853+
expect(response.body).not.toHaveProperty('minAutoGranularity');
854+
855+
const sources = await Source.find({}).lean();
856+
expect(sources).toHaveLength(1);
857+
expect(sources[0]).not.toHaveProperty('minAutoGranularity');
858+
});
859+
860+
it('PUT /:id - updates minAutoGranularity', async () => {
861+
const { agent, team } = await getLoggedInAgent(server);
862+
863+
const promqlSource = await Source.create({
864+
...MOCK_PROMQL_SOURCE,
865+
minAutoGranularity: '1 minute',
866+
team: team._id,
867+
});
868+
869+
await agent
870+
.put(`/sources/${promqlSource._id}`)
871+
.send({
872+
id: promqlSource._id.toString(),
873+
...MOCK_PROMQL_SOURCE,
874+
minAutoGranularity: '5 minute',
875+
})
876+
.expect(200);
877+
878+
const updatedSource = await Source.findById(promqlSource._id).lean();
879+
expect(updatedSource).toHaveProperty('minAutoGranularity', '5 minute');
880+
});
881+
882+
it('PUT /:id - removes minAutoGranularity when omitted from the update payload', async () => {
883+
const { agent, team } = await getLoggedInAgent(server);
884+
885+
const promqlSource = await Source.create({
886+
...MOCK_PROMQL_SOURCE,
887+
minAutoGranularity: '1 minute',
888+
team: team._id,
889+
});
890+
891+
await agent
892+
.put(`/sources/${promqlSource._id}`)
893+
.send({
894+
id: promqlSource._id.toString(),
895+
...MOCK_PROMQL_SOURCE,
896+
})
897+
.expect(200);
898+
899+
const updatedSource = await Source.findById(promqlSource._id).lean();
900+
expect(updatedSource).not.toHaveProperty('minAutoGranularity');
901+
});
902+
});
903+
806904
it('DELETE /:id - deletes a source', async () => {
807905
const { agent, team } = await getLoggedInAgent(server);
808906

‎packages/app/src/ChartUtils.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ function getAlignedRangeAndGranularity(
133133
const granularity = getTimeChartGranularity(
134134
config.granularity,
135135
config.dateRange,
136+
config.minGranularitySeconds,
136137
);
137138
return {
138139
granularity,

‎packages/app/src/__tests__/ChartUtils.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,29 @@ describe('ChartUtils', () => {
13171317

13181318
expect(result.dateRange).toEqual(promqlConfig.dateRange);
13191319
});
1320+
1321+
it("floors an auto granularity at the source's minimum", () => {
1322+
const result = convertToPromqlTableChartConfig({
1323+
...promqlConfig,
1324+
minGranularitySeconds: 300,
1325+
});
1326+
1327+
expect(result.granularity).toBe('5 minute');
1328+
expect(result.dateRange).toEqual([
1329+
new Date('2025-11-26T00:00:00Z'),
1330+
new Date('2025-11-26T01:05:00Z'),
1331+
]);
1332+
});
1333+
1334+
it('never floors a granularity the tile picked', () => {
1335+
const result = convertToPromqlTableChartConfig({
1336+
...promqlConfig,
1337+
granularity: '15 second',
1338+
minGranularitySeconds: 300,
1339+
});
1340+
1341+
expect(result.granularity).toBe('15 second');
1342+
});
13201343
});
13211344

13221345
describe('convertToReducedPromqlChartConfig and convertToPromqlSparklineChartConfig', () => {
@@ -1364,6 +1387,29 @@ describe('ChartUtils', () => {
13641387
expect(result.granularity).toBe('30 minute');
13651388
});
13661389

1390+
it("floors an auto granularity at the source's minimum", () => {
1391+
const result = convertToReducedPromqlChartConfig({
1392+
...promqlConfig,
1393+
minGranularitySeconds: 300,
1394+
});
1395+
1396+
expect(result.granularity).toBe('5 minute');
1397+
expect(result.dateRange).toEqual([
1398+
new Date('2025-11-26T00:00:00Z'),
1399+
new Date('2025-11-26T01:05:00Z'),
1400+
]);
1401+
});
1402+
1403+
it('never floors a granularity the tile picked', () => {
1404+
const result = convertToReducedPromqlChartConfig({
1405+
...promqlConfig,
1406+
granularity: '15 second',
1407+
minGranularitySeconds: 300,
1408+
});
1409+
1410+
expect(result.granularity).toBe('15 second');
1411+
});
1412+
13671413
it('names the reducer, defaulting it, and queries one expression', () => {
13681414
const result = convertToReducedPromqlChartConfig(promqlConfig);
13691415

‎packages/app/src/components/DBEditTimeChartForm/ChartPreviewPanel.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ import { DBTimeChart } from '@/components/DBTimeChart';
3636
import EmptyState from '@/components/EmptyState';
3737
import PatternTable from '@/components/PatternTable';
3838
import PromQLPreview from '@/components/PromQLEditor/PromQLPreview';
39+
import { getMinGranularitySeconds } from '@/hooks/useChartConfig';
3940
import {
4041
getEventBody,
4142
getFirstTimestampValueExpression,
4243
isSingleExpression,
44+
useSource,
4345
} from '@/source';
4446
import {
4547
orderByStringToSortingState,
@@ -169,9 +171,11 @@ export function ChartPreviewPanel({
169171
}: ChartPreviewPanelProps) {
170172
const [isSampleEventsOpen, setIsSampleEventsOpen] = useState(false);
171173

174+
const { data: queriedSource } = useSource({ id: queriedConfig?.source });
175+
const minGranularitySeconds = getMinGranularitySeconds(queriedSource);
172176
const renderedPromql = useMemo(
173-
() => buildRenderedPromqlExpression(queriedConfig),
174-
[queriedConfig],
177+
() => buildRenderedPromqlExpression(queriedConfig, minGranularitySeconds),
178+
[queriedConfig, minGranularitySeconds],
175179
);
176180

177181
const blockingFilterNames = missingRequiredFilterNames ?? [];

‎packages/app/src/components/DBEditTimeChartForm/__tests__/ChartPreviewPanel.test.tsx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ jest.mock('@/components/DBHeatmapChart', () => ({
6666
HEATMAP_N_BUCKETS: 80,
6767
}));
6868

69+
const mockUseSource = jest.fn();
6970
jest.mock('@/source', () => ({
7071
getFirstTimestampValueExpression: jest.fn().mockReturnValue('Timestamp'),
72+
useSource: (opts: { id?: string }) => mockUseSource(opts),
7173
}));
7274

7375
const dateRange: [Date, Date] = [
@@ -118,6 +120,7 @@ const renderPanel = (
118120
describe('ChartPreviewPanel', () => {
119121
beforeEach(() => {
120122
jest.clearAllMocks();
123+
mockUseSource.mockReturnValue({ data: undefined });
121124
});
122125

123126
describe('when no query has been run', () => {
@@ -278,6 +281,8 @@ describe('ChartPreviewPanel', () => {
278281
overrides: {
279282
promqlExpression?: PromqlExpressionList;
280283
displayType?: DisplayType;
284+
source?: string;
285+
granularity?: string;
281286
variables?: ChartVariable[];
282287
} = {},
283288
): ChartConfigWithDateRange => ({
@@ -423,6 +428,44 @@ describe('ChartPreviewPanel', () => {
423428
).not.toBeInTheDocument();
424429
});
425430

431+
// Selecting another source doesn't rerun the query, and the chart reads the
432+
// floor from the source it ran with, so the preview has to as well.
433+
it("expands macros with the queried source's floor, not the selected one's", async () => {
434+
const promqlSource = (
435+
id: string,
436+
minAutoGranularity?: string,
437+
): TSource => ({
438+
id,
439+
kind: SourceKind.Promql,
440+
name: id,
441+
connection: 'default',
442+
from: { databaseName: 'default', tableName: 'metrics_ts' },
443+
timestampValueExpression: 'timestamp',
444+
minAutoGranularity,
445+
});
446+
mockUseSource.mockImplementation(({ id }: { id?: string }) => ({
447+
data:
448+
id === 'queried' ? promqlSource('queried', '1 minute') : undefined,
449+
}));
450+
451+
renderPanel({
452+
queriedConfig: promqlConfig({
453+
promqlExpression: 'rate(up[$__rate_interval])',
454+
source: 'queried',
455+
granularity: '15 second',
456+
}),
457+
tableSource: promqlSource('selected'),
458+
showGeneratedPromql: true,
459+
});
460+
await openGeneratedPromql();
461+
462+
// max(15 + 60, 4 * 60). The selected source has no floor, which would
463+
// give max(15 + 15, 4 * 15) = 60s.
464+
expect(screen.getByTestId('chart-promql-preview')).toHaveTextContent(
465+
'rate(up[240s])',
466+
);
467+
});
468+
426469
it('shows one preview per expression, labelled by alias', async () => {
427470
renderPanel({
428471
queriedConfig: promqlConfig({

0 commit comments

Comments
 (0)