Skip to content

Commit 0dece9f

Browse files
Merge branch 'main' into drew/heatmap-series-5
2 parents bb5395b + 1ce8b88 commit 0dece9f

19 files changed

Lines changed: 604 additions & 24 deletions

‎.changeset/promql-series-limit.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@hyperdx/app': patch
3+
---
4+
5+
feat: support a series limit on PromQL line, stacked bar, pie and bar charts
6+
7+
The Display Settings drawer now offers Series Limit for PromQL charts. On line and stacked bar charts it keeps the top N series by peak value (leave empty for the default of 250, or set 0 for unlimited). On pie and bar charts it keeps the N largest slices or bars (leave empty to show all).

‎.changeset/text-index-key-order.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@hyperdx/common-utils': patch
3+
'@hyperdx/app': patch
4+
'@hyperdx/api': patch
5+
---
6+
7+
fix: rank Map keys by frequency when a text index serves key discovery
8+
9+
The two `mergeTreeTextIndex` paths in `getMapKeys` grouped keys and applied
10+
`LIMIT maxKeys` with no `ORDER BY`, so on a column with more keys than the
11+
limit the filter sidebar showed an arbitrary subset. They now order by
12+
`sum(cardinality) DESC, key`, which keeps the most frequent keys, as the key
13+
rollup path already does with `sum(count) DESC`.

‎packages/app/src/ChartUtils.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ export function formatResponseForCategoricalChart(
550550
data: ResponseJSON<Record<string, unknown>>,
551551
getColor: (index: number, label: string) => string,
552552
applyDefaultOrder: boolean = true,
553+
maxGroups: number = DEFAULT_MAX_CATEGORICAL_GROUPS,
553554
): Array<{ label: string; value: number; color: string }> {
554555
if (data.meta == null) {
555556
throw new Error('No meta data found in response');
@@ -587,7 +588,7 @@ export function formatResponseForCategoricalChart(
587588
}
588589

589590
return labelsAndValues
590-
.slice(0, DEFAULT_MAX_CATEGORICAL_GROUPS)
591+
.slice(0, Math.min(maxGroups, DEFAULT_MAX_CATEGORICAL_GROUPS))
591592
.map((entry, index) => ({
592593
...entry,
593594
color: getColor(index, entry.label),

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,47 @@ describe('ChartUtils', () => {
17251725
{ label: 'svc', value: 5, color: 'color-0-svc' },
17261726
]);
17271727
});
1728+
1729+
describe('maxGroups', () => {
1730+
const manyGroups = (count: number) => ({
1731+
data: Array.from({ length: count }, (_, i) => ({
1732+
value: i,
1733+
series_name: `s${i}`,
1734+
})),
1735+
meta: [
1736+
{ name: 'value', type: 'Float64' },
1737+
{ name: 'series_name', type: 'String' },
1738+
],
1739+
});
1740+
1741+
it('keeps the largest maxGroups groups', () => {
1742+
const result = formatResponseForCategoricalChart(
1743+
manyGroups(5),
1744+
getColor,
1745+
true,
1746+
2,
1747+
);
1748+
expect(result.map(e => e.label)).toEqual(['s4', 's3']);
1749+
});
1750+
1751+
it('caps at 500 groups by default', () => {
1752+
const result = formatResponseForCategoricalChart(
1753+
manyGroups(600),
1754+
getColor,
1755+
);
1756+
expect(result).toHaveLength(500);
1757+
});
1758+
1759+
it('cannot raise the 500-group cap', () => {
1760+
const result = formatResponseForCategoricalChart(
1761+
manyGroups(600),
1762+
getColor,
1763+
true,
1764+
1000,
1765+
);
1766+
expect(result).toHaveLength(500);
1767+
});
1768+
});
17281769
});
17291770

17301771
describe('findNearestSeriesKey', () => {

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,12 @@ export default function ChartDisplaySettingsDrawer({
205205

206206
// Series Limit applies to every time chart. On builder group-by charts a
207207
// positive value drives the __hdx_series_limit SQL CTE (trimming what's
208-
// fetched); on raw SQL it drives the client-side render cap in
209-
// `formatResponseForTimeChart` (raw SQL can't inject the CTE). PromQL is
210-
// excluded — its series come from Prometheus, not this pipeline.
211-
const showSeriesLimit = isTimeChart && configType !== 'promql';
212-
const isRawSqlTimeChart = showSeriesLimit && configType === 'sql';
208+
// fetched); on raw SQL and PromQL it drives the client-side render cap in
209+
// `formatResponseForTimeChart` (neither can take the CTE, and a Prometheus
210+
// `limit` would keep an arbitrary label-sorted subset, not the top N).
211+
const showSeriesLimit = isTimeChart;
212+
const isClientSideSeriesLimit =
213+
showSeriesLimit && (configType === 'sql' || configType === 'promql');
213214

214215
// Every PromQL display that surfaces a series name. A number tile shows one
215216
// value and a table gives each label its own column, so neither has a legend.
@@ -219,11 +220,11 @@ export default function ChartDisplaySettingsDrawer({
219220
displayType !== DisplayType.Table;
220221

221222
// On pie/bar builder charts, seriesLimit becomes a plain SQL LIMIT on the
222-
// number of slices/bars; raw SQL configs author their own LIMIT directly.
223+
// number of slices/bars; on PromQL it trims the reduced series client-side
224+
// in `useCategoricalChart`. Raw SQL configs author their own LIMIT directly.
223225
const isCategoricalChart =
224226
displayType === DisplayType.Pie || displayType === DisplayType.Bar;
225-
const showCategoricalLimit =
226-
isCategoricalChart && configType !== 'sql' && configType !== 'promql';
227+
const showCategoricalLimit = isCategoricalChart && configType !== 'sql';
227228

228229
// Table display options. Alternate Row Background is purely presentational
229230
// (it stripes rendered rows), so it applies to any table tile. Group By
@@ -315,7 +316,7 @@ export default function ChartDisplaySettingsDrawer({
315316
size="xs"
316317
label="Series Limit"
317318
description={
318-
isRawSqlTimeChart
319+
isClientSideSeriesLimit
319320
? `Maximum number of series rendered, keeping those with the largest values. Leave empty for the default (${MAX_RENDERED_TIME_CHART_SERIES}); set 0 for unlimited.`
320321
: `Maximum number of series fetched for a group-by chart, keeping those with the largest values. Leave empty for the default (${MAX_RENDERED_TIME_CHART_SERIES}); set 0 for unlimited.`
321322
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,36 @@ describe('convertFormStateToSavedChartConfig', () => {
156156
expect(result).toMatchObject({ alternateRowBackground: true });
157157
});
158158

159+
it.each([0, 5])(
160+
'persists seriesLimit=%s for a promql+line config',
161+
seriesLimit => {
162+
const form: ChartEditorFormState = {
163+
configType: 'promql',
164+
displayType: DisplayType.Line,
165+
promqlExpression: 'up',
166+
connection: 'conn-1',
167+
seriesLimit,
168+
series: [],
169+
};
170+
const result = convertFormStateToSavedChartConfig(form, undefined);
171+
expect(result).toMatchObject({ seriesLimit });
172+
},
173+
);
174+
175+
it('carries seriesLimit into the queried config for a promql+line config', () => {
176+
const form: ChartEditorFormState = {
177+
configType: 'promql',
178+
displayType: DisplayType.Line,
179+
promqlExpression: 'up',
180+
connection: 'conn-1',
181+
seriesLimit: 5,
182+
series: [],
183+
};
184+
const dateRange: [Date, Date] = [new Date(0), new Date(1000)];
185+
const result = convertFormStateToChartConfig(form, dateRange, undefined);
186+
expect(result).toMatchObject({ configType: 'promql', seriesLimit: 5 });
187+
});
188+
159189
it('returns a raw SQL config for Line displayType', () => {
160190
const form: ChartEditorFormState = {
161191
configType: 'sql',

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export function convertFormStateToSavedChartConfig(
267267
'fillNulls',
268268
'alignDateRangeToGranularity',
269269
'alternateRowBackground',
270+
'seriesLimit',
270271
// 'alert', // TODO: Support alerts on PromQL (HDX-4636)
271272
]),
272273
promqlExpression: formPromqlExpressions(form),
@@ -366,6 +367,7 @@ export function convertFormStateToChartConfig(
366367
'fillNulls',
367368
'alignDateRangeToGranularity',
368369
'alternateRowBackground',
370+
'seriesLimit',
369371
]),
370372
promqlExpression: formPromqlExpressions(form),
371373
connection: source?.connection ?? form.connection ?? '',

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ import DisplaySwitcher from './charts/DisplaySwitcher';
6262
import HiddenSeriesIndicator from './charts/HiddenSeriesIndicator';
6363
import MVOptimizationIndicator from './MaterializedViews/MVOptimizationIndicator';
6464

65+
const PROMQL_REDUCE_CARDINALITY_HINT =
66+
'Narrow the label matchers, aggregate with sum by (...), or set a series limit to reduce the number of series.';
67+
6568
/** A single group column / value pair decoded from a chart series key. */
6669
export type SeriesGroupFilter = { column: string; value: string };
6770

@@ -953,6 +956,11 @@ function DBTimeChartComponent({
953956
// raise the cap (loadAllHandler is undefined otherwise), so the
954957
// notice never advertises a no-op click.
955958
onLoadAll={loadAllHandler}
959+
reduceCardinalityHint={
960+
isPromqlChartConfig(config)
961+
? PROMQL_REDUCE_CARDINALITY_HINT
962+
: undefined
963+
}
956964
/>,
957965
);
958966
}

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,80 @@ describe('ChartDisplaySettingsDrawer', () => {
273273
).not.toBeInTheDocument();
274274
});
275275

276+
describe('PromQL (client render cap)', () => {
277+
const promqlProps = { ...baseProps, configType: 'promql' as const };
278+
279+
it.each([DisplayType.Line, DisplayType.StackedBar])(
280+
'shows the Series Limit input for PromQL %s charts',
281+
displayType => {
282+
renderWithMantine(
283+
<ChartDisplaySettingsDrawer
284+
{...promqlProps}
285+
displayType={displayType}
286+
/>,
287+
);
288+
289+
const input = screen.getByRole('textbox', { name: /series limit/i });
290+
expect(input).toHaveAttribute(
291+
'placeholder',
292+
`Default (${MAX_RENDERED_TIME_CHART_SERIES})`,
293+
);
294+
},
295+
);
296+
297+
it('describes the limit as a render cap, not a fetch cap', () => {
298+
renderWithMantine(
299+
<ChartDisplaySettingsDrawer
300+
{...promqlProps}
301+
displayType={DisplayType.Line}
302+
/>,
303+
);
304+
305+
expect(
306+
screen.getByText(/maximum number of series rendered/i),
307+
).toBeInTheDocument();
308+
expect(screen.queryByText(/fetched/i)).not.toBeInTheDocument();
309+
});
310+
311+
it.each([DisplayType.Number, DisplayType.Table])(
312+
'does not show the Series Limit input for PromQL %s charts',
313+
displayType => {
314+
renderWithMantine(
315+
<ChartDisplaySettingsDrawer
316+
{...promqlProps}
317+
displayType={displayType}
318+
/>,
319+
);
320+
321+
expect(
322+
screen.queryByRole('textbox', { name: /series limit/i }),
323+
).not.toBeInTheDocument();
324+
},
325+
);
326+
327+
it('calls onChange with the entered seriesLimit when applied', async () => {
328+
const onChange = jest.fn();
329+
const user = userEvent.setup();
330+
331+
renderWithMantine(
332+
<ChartDisplaySettingsDrawer
333+
{...promqlProps}
334+
displayType={DisplayType.Line}
335+
onChange={onChange}
336+
/>,
337+
);
338+
339+
await user.type(
340+
screen.getByRole('textbox', { name: /series limit/i }),
341+
'5',
342+
);
343+
await user.click(screen.getByRole('button', { name: /apply/i }));
344+
345+
expect(onChange).toHaveBeenCalledTimes(1);
346+
expect(onChange.mock.calls[0][0]).toMatchObject({ seriesLimit: 5 });
347+
});
348+
});
349+
276350
it('calls onChange with the entered seriesLimit when applied', async () => {
277351
const onChange = jest.fn();
278352
const user = userEvent.setup();
@@ -338,6 +412,23 @@ describe('ChartDisplaySettingsDrawer', () => {
338412
},
339413
);
340414

415+
it.each([DisplayType.Pie, DisplayType.Bar])(
416+
'shows the Series Limit input for PromQL %s charts',
417+
displayType => {
418+
renderWithMantine(
419+
<ChartDisplaySettingsDrawer
420+
{...baseProps}
421+
configType="promql"
422+
displayType={displayType}
423+
/>,
424+
);
425+
426+
expect(
427+
screen.getByRole('textbox', { name: /series limit/i }),
428+
).toHaveAttribute('placeholder', 'Disabled (e.g. 10)');
429+
},
430+
);
431+
341432
it('does not show the Series Limit input for raw SQL pie charts', () => {
342433
renderWithMantine(
343434
<ChartDisplaySettingsDrawer

‎packages/app/src/components/__tests__/DBPieChart.test.tsx‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ describe('DBPieChart', () => {
351351
expect(second).toEqual(first);
352352
});
353353

354-
it('draws a slice per series, largest first', () => {
354+
const mockThreeSeries = () =>
355355
mockUseQueriedChartConfig.mockReturnValue({
356356
data: {
357357
data: [
@@ -368,12 +368,31 @@ describe('DBPieChart', () => {
368368
isError: false,
369369
});
370370

371-
renderWithMantine(<DBPieChart config={promqlConfig} />);
372-
373-
const labels = within(screen.getByTestId('pie-chart-legend'))
371+
const legendLabels = () =>
372+
within(screen.getByTestId('pie-chart-legend'))
374373
.getAllByTitle(/.+/)
375374
.map(el => el.getAttribute('title'));
376-
expect(labels).toEqual(['checkout', 'ad', 'cart']);
375+
376+
it('draws a slice per series, largest first', () => {
377+
mockThreeSeries();
378+
renderWithMantine(<DBPieChart config={promqlConfig} />);
379+
expect(legendLabels()).toEqual(['checkout', 'ad', 'cart']);
380+
});
381+
382+
it('keeps only the largest seriesLimit slices', () => {
383+
mockThreeSeries();
384+
renderWithMantine(
385+
<DBPieChart config={{ ...promqlConfig, seriesLimit: 2 }} />,
386+
);
387+
expect(legendLabels()).toEqual(['checkout', 'ad']);
388+
});
389+
390+
it('treats a seriesLimit of 0 as unlimited', () => {
391+
mockThreeSeries();
392+
renderWithMantine(
393+
<DBPieChart config={{ ...promqlConfig, seriesLimit: 0 }} />,
394+
);
395+
expect(legendLabels()).toEqual(['checkout', 'ad', 'cart']);
377396
});
378397
});
379398
});

0 commit comments

Comments
 (0)