Skip to content

Commit 17a217b

Browse files
committed
feat: Support PromQL table charts
1 parent cd3c9af commit 17a217b

20 files changed

Lines changed: 934 additions & 20 deletions

‎.changeset/promql-table-tiles.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@hyperdx/common-utils': patch
3+
'@hyperdx/app': patch
4+
---
5+
6+
feat: Support table tiles on PromQL sources

‎packages/app/src/ChartUtils.tsx‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,31 +123,40 @@ function getTimeChartDateRange(
123123
export const MAX_TIME_CHART_SERIES = DEFAULT_SERIES_LIMIT;
124124

125125
/**
126-
* A PromQL number tile's queried config, shared by the value and the sparkline
127-
* drawn behind it.
126+
* A PromQL config's resolved granularity, and its date range aligned to that
127+
* granularity's buckets when it runs a range query, so its samples land on the
128+
* same boundaries as the timeseries charts' and stay put across refreshes.
128129
*/
129-
export function convertToPromqlNumberChartConfig(
130+
function getAlignedRangeAndGranularity(
130131
config: PromqlChartConfig & DateRange,
131-
{ withReducer }: { withReducer: boolean },
132-
): PromqlChartConfig & DateRange {
132+
): Pick<PromqlChartConfig & DateRange, 'granularity' | 'dateRange'> {
133133
const granularity = getTimeChartGranularity(
134134
config.granularity,
135135
config.dateRange,
136136
);
137137
return {
138-
...config,
139138
granularity,
140-
// Align the date range with the same buckets used by the sparkline and
141-
// timeseries charts, when using a (bucketed) range query. Alignment is
142-
// desirable for range queries to ensure consistent bucket boundaries
143-
// and sizes.
144139
dateRange: isRangeQuery(config)
145140
? getTimeChartDateRange(
146141
config.dateRange,
147142
config.alignDateRangeToGranularity,
148143
granularity,
149144
)
150145
: config.dateRange,
146+
};
147+
}
148+
149+
/**
150+
* A PromQL number tile's queried config, shared by the value and the sparkline
151+
* drawn behind it.
152+
*/
153+
export function convertToPromqlNumberChartConfig(
154+
config: PromqlChartConfig & DateRange,
155+
{ withReducer }: { withReducer: boolean },
156+
): PromqlChartConfig & DateRange {
157+
return {
158+
...config,
159+
...getAlignedRangeAndGranularity(config),
151160
promqlExpression: getQueriedPromqlSeries(config).map(series => ({
152161
...series,
153162
reducer: withReducer
@@ -157,6 +166,13 @@ export function convertToPromqlNumberChartConfig(
157166
};
158167
}
159168

169+
/** A PromQL table tile's queried config. */
170+
export function convertToPromqlTableChartConfig(
171+
config: PromqlChartConfig & DateRange,
172+
): PromqlChartConfig & DateRange {
173+
return { ...config, ...getAlignedRangeAndGranularity(config) };
174+
}
175+
160176
export function convertToTimeChartConfig(
161177
config: ChartConfigWithDateRange,
162178
): ChartConfigWithDateRange {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ChartKeyJoiner,
1212
convertToNumberChartConfig,
1313
convertToPromqlNumberChartConfig,
14+
convertToPromqlTableChartConfig,
1415
convertToTableChartConfig,
1516
convertToTimeChartConfig,
1617
findNearestSeriesKey,
@@ -1284,6 +1285,39 @@ describe('ChartUtils', () => {
12841285
});
12851286
});
12861287

1288+
describe('convertToPromqlTableChartConfig', () => {
1289+
const promqlConfig = {
1290+
configType: 'promql' as const,
1291+
displayType: DisplayType.Table,
1292+
connection: 'conn',
1293+
promqlExpression: [{ expression: 'up' }, { expression: 'down' }],
1294+
dateRange: [
1295+
new Date('2025-11-26T00:00:14.076Z'),
1296+
new Date('2025-11-26T01:00:14.076Z'),
1297+
] as [Date, Date],
1298+
};
1299+
1300+
it('aligns a range query to its granularity, keeping every expression', () => {
1301+
const result = convertToPromqlTableChartConfig(promqlConfig);
1302+
1303+
expect(result.dateRange).toEqual([
1304+
new Date('2025-11-26T00:00:00Z'),
1305+
new Date('2025-11-26T01:01:00Z'),
1306+
]);
1307+
expect(result.granularity).toBe('1 minute');
1308+
expect(result.promqlExpression).toEqual(promqlConfig.promqlExpression);
1309+
});
1310+
1311+
it('leaves the date range unaligned for an instant query', () => {
1312+
const result = convertToPromqlTableChartConfig({
1313+
...promqlConfig,
1314+
promqlExpression: [{ expression: 'up', queryType: 'instant' }],
1315+
});
1316+
1317+
expect(result.dateRange).toEqual(promqlConfig.dateRange);
1318+
});
1319+
});
1320+
12871321
describe('convertToPromqlNumberChartConfig', () => {
12881322
const promqlConfig = {
12891323
configType: 'promql' as const,

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ describe('prometheusApi.query', () => {
136136
expect(params.has('step')).toBe(false);
137137
});
138138

139+
it('forwards a series limit only when one is given', async () => {
140+
await prometheusApi.query({
141+
query: 'up',
142+
time: 1,
143+
connectionId: 'conn',
144+
limit: 25,
145+
});
146+
expect(postedParams().get('limit')).toBe('25');
147+
148+
post.mockClear();
149+
await prometheusApi.query({ query: 'up', time: 1, connectionId: 'conn' });
150+
expect(postedParams().has('limit')).toBe(false);
151+
});
152+
139153
it('omits the table params a Prometheus-backed connection has no use for', async () => {
140154
await prometheusApi.query({
141155
query: 'up',

‎packages/app/src/api.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,8 @@ export const prometheusApi = {
902902
connectionId: string;
903903
database?: string;
904904
table?: string;
905+
/** Maximum number of series to return. */
906+
limit?: number;
905907
signal?: AbortSignal;
906908
}): Promise<PrometheusInstantQueryResponse> =>
907909
prometheusFetch(
@@ -910,6 +912,7 @@ export const prometheusApi = {
910912
query: params.query,
911913
time: String(params.time),
912914
connectionId: params.connectionId,
915+
...(params.limit ? { limit: String(params.limit) } : {}),
913916
...(params.database ? { database: params.database } : {}),
914917
...(params.table ? { table: params.table } : {}),
915918
},

‎packages/app/src/components/ChartDisplaySettingsDrawer.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,12 @@ export default function ChartDisplaySettingsDrawer({
202202
const showSeriesLimit = isTimeChart && configType !== 'promql';
203203
const isRawSqlTimeChart = showSeriesLimit && configType === 'sql';
204204

205-
// Every PromQL display except Number surfaces the series name
205+
// Every PromQL display that surfaces a series name. A number tile shows one
206+
// value and a table gives each label its own column, so neither has a legend.
206207
const showLegendTemplate =
207-
configType === 'promql' && displayType !== DisplayType.Number;
208+
configType === 'promql' &&
209+
displayType !== DisplayType.Number &&
210+
displayType !== DisplayType.Table;
208211

209212
// On pie/bar builder charts, seriesLimit becomes a plain SQL LIMIT on the
210213
// number of slices/bars; raw SQL configs author their own LIMIT directly.
@@ -218,7 +221,8 @@ export default function ChartDisplaySettingsDrawer({
218221
// column ordering needs the builder `select` structure to know which columns
219222
// are group-by keys, so it stays builder-only.
220223
const showTableOptions = displayType === DisplayType.Table;
221-
const showGroupByColumnsOnLeft = showTableOptions && configType !== 'sql';
224+
const showGroupByColumnsOnLeft =
225+
showTableOptions && configType !== 'sql' && configType !== 'promql';
222226

223227
// Tile-level color is only meaningful for number tiles today.
224228
// Per-series colors on line / bar / pie ship in a follow-up PR via

‎packages/app/src/components/ChartEditor/__tests__/utils.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,28 @@ describe('PromQL expressions', () => {
701701
});
702702
});
703703

704+
it('keeps the query type but drops the reducer on a table chart', () => {
705+
expect(
706+
convertFormStateToSavedChartConfig(
707+
promqlForm(
708+
[
709+
{
710+
expression: 'up',
711+
queryType: 'instant',
712+
reducer: PromqlReducer.Max,
713+
},
714+
],
715+
DisplayType.Table,
716+
),
717+
undefined,
718+
),
719+
).toMatchObject({
720+
promqlExpression: [
721+
{ expression: 'up', queryType: 'instant', reducer: undefined },
722+
],
723+
});
724+
});
725+
704726
// A stale `instant` on a time series tile would hide its granularity picker,
705727
// and a reducer it never applies is dead weight in the saved config.
706728
it('drops the query type and reducer a time series chart cannot offer', () => {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ export function ChartActionBar({
186186
{(activeTab === 'table' ||
187187
activeTab === 'pie' ||
188188
activeTab === 'bar') &&
189-
!isRawSqlInput && (
189+
!isRawSqlInput &&
190+
configType !== 'promql' && (
190191
<div style={{ width: 400 }} data-testid="order-by-input">
191192
<SQLInlineEditorControlled
192193
parentRef={parentRef}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,11 @@ export function ChartPreviewPanel({
257257
})
258258
: undefined
259259
}
260-
onSortingChange={onTableSortingChange}
260+
onSortingChange={
261+
isBuilderChartConfig(queriedConfig)
262+
? onTableSortingChange
263+
: undefined
264+
}
261265
sort={tableSortState}
262266
showMVOptimizationIndicator={false}
263267
errorVariant="inline"

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,19 @@ describe('ChartActionBar', () => {
301301
expect(screen.queryByTestId('sql-editor-order-by')).not.toBeInTheDocument();
302302
});
303303

304+
it('should not render ORDER BY editor for a PromQL table', () => {
305+
renderActionBar(
306+
{ activeTab: 'table' },
307+
{
308+
configType: 'promql',
309+
displayType: DisplayType.Table,
310+
promqlExpressions: [{ expression: 'up' }],
311+
},
312+
);
313+
314+
expect(screen.queryByTestId('sql-editor-order-by')).not.toBeInTheDocument();
315+
});
316+
304317
it('should not render ORDER BY editor for time tab', () => {
305318
renderActionBar({ activeTab: 'time' });
306319

0 commit comments

Comments
 (0)