Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/promql-series-limit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/app': patch
---

feat: support a series limit on PromQL line, stacked bar, pie and bar charts

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).
3 changes: 2 additions & 1 deletion packages/app/src/ChartUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ export function formatResponseForCategoricalChart(
data: ResponseJSON<Record<string, unknown>>,
getColor: (index: number, label: string) => string,
applyDefaultOrder: boolean = true,
maxGroups: number = DEFAULT_MAX_CATEGORICAL_GROUPS,
): Array<{ label: string; value: number; color: string }> {
if (data.meta == null) {
throw new Error('No meta data found in response');
Expand Down Expand Up @@ -587,7 +588,7 @@ export function formatResponseForCategoricalChart(
}

return labelsAndValues
.slice(0, DEFAULT_MAX_CATEGORICAL_GROUPS)
.slice(0, Math.min(maxGroups, DEFAULT_MAX_CATEGORICAL_GROUPS))
Comment thread
karl-power marked this conversation as resolved.
Resolved
.map((entry, index) => ({
...entry,
color: getColor(index, entry.label),
Expand Down
41 changes: 41 additions & 0 deletions packages/app/src/__tests__/ChartUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,47 @@ describe('ChartUtils', () => {
{ label: 'svc', value: 5, color: 'color-0-svc' },
]);
});

describe('maxGroups', () => {
const manyGroups = (count: number) => ({
data: Array.from({ length: count }, (_, i) => ({
value: i,
series_name: `s${i}`,
})),
meta: [
{ name: 'value', type: 'Float64' },
{ name: 'series_name', type: 'String' },
],
});

it('keeps the largest maxGroups groups', () => {
const result = formatResponseForCategoricalChart(
manyGroups(5),
getColor,
true,
2,
);
expect(result.map(e => e.label)).toEqual(['s4', 's3']);
});

it('caps at 500 groups by default', () => {
const result = formatResponseForCategoricalChart(
manyGroups(600),
getColor,
);
expect(result).toHaveLength(500);
});

it('cannot raise the 500-group cap', () => {
const result = formatResponseForCategoricalChart(
manyGroups(600),
getColor,
true,
1000,
);
expect(result).toHaveLength(500);
});
});
});

describe('findNearestSeriesKey', () => {
Expand Down
19 changes: 10 additions & 9 deletions packages/app/src/components/ChartDisplaySettingsDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,12 @@ export default function ChartDisplaySettingsDrawer({

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

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

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

// Table display options. Alternate Row Background is purely presentational
// (it stripes rendered rows), so it applies to any table tile. Group By
Expand Down Expand Up @@ -315,7 +316,7 @@ export default function ChartDisplaySettingsDrawer({
size="xs"
label="Series Limit"
description={
isRawSqlTimeChart
isClientSideSeriesLimit
? `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.`
: `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.`
}
Expand Down
30 changes: 30 additions & 0 deletions packages/app/src/components/ChartEditor/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,36 @@ describe('convertFormStateToSavedChartConfig', () => {
expect(result).toMatchObject({ alternateRowBackground: true });
});

it.each([0, 5])(
'persists seriesLimit=%s for a promql+line config',
seriesLimit => {
const form: ChartEditorFormState = {
configType: 'promql',
displayType: DisplayType.Line,
promqlExpression: 'up',
connection: 'conn-1',
seriesLimit,
series: [],
};
const result = convertFormStateToSavedChartConfig(form, undefined);
expect(result).toMatchObject({ seriesLimit });
},
);

it('carries seriesLimit into the queried config for a promql+line config', () => {
const form: ChartEditorFormState = {
configType: 'promql',
displayType: DisplayType.Line,
promqlExpression: 'up',
connection: 'conn-1',
seriesLimit: 5,
series: [],
};
const dateRange: [Date, Date] = [new Date(0), new Date(1000)];
const result = convertFormStateToChartConfig(form, dateRange, undefined);
expect(result).toMatchObject({ configType: 'promql', seriesLimit: 5 });
});

it('returns a raw SQL config for Line displayType', () => {
const form: ChartEditorFormState = {
configType: 'sql',
Expand Down
2 changes: 2 additions & 0 deletions packages/app/src/components/ChartEditor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export function convertFormStateToSavedChartConfig(
'fillNulls',
'alignDateRangeToGranularity',
'alternateRowBackground',
'seriesLimit',
// 'alert', // TODO: Support alerts on PromQL (HDX-4636)
]),
promqlExpression: formPromqlExpressions(form),
Expand Down Expand Up @@ -366,6 +367,7 @@ export function convertFormStateToChartConfig(
'fillNulls',
'alignDateRangeToGranularity',
'alternateRowBackground',
'seriesLimit',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — Changing the client-side PromQL Series Limit re-runs the Prometheus query

seriesLimit now goes into the PromQL queried config. convertToTimeChartConfig (packages/app/src/ChartUtils.tsx:239) only removes it for builder configs, and DBTimeChart puts the whole queriedConfig in the useQueriedChartConfig queryKey (DBTimeChart.tsx:496-498). So editing a cap that only affects rendering fetches from Prometheus again. Fix: drop seriesLimit from non-builder configs in convertToTimeChartConfig. DBTimeChart reads the cap from config.seriesLimit, not queriedConfig, so the render cap still works.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

]),
promqlExpression: formPromqlExpressions(form),
connection: source?.connection ?? form.connection ?? '',
Expand Down
8 changes: 8 additions & 0 deletions packages/app/src/components/DBTimeChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ import DisplaySwitcher from './charts/DisplaySwitcher';
import HiddenSeriesIndicator from './charts/HiddenSeriesIndicator';
import MVOptimizationIndicator from './MaterializedViews/MVOptimizationIndicator';

const PROMQL_REDUCE_CARDINALITY_HINT =
'Narrow the label matchers, aggregate with sum by (...), or set a series limit to reduce the number of series.';

/** A single group column / value pair decoded from a chart series key. */
export type SeriesGroupFilter = { column: string; value: string };

Expand Down Expand Up @@ -953,6 +956,11 @@ function DBTimeChartComponent({
// raise the cap (loadAllHandler is undefined otherwise), so the
// notice never advertises a no-op click.
onLoadAll={loadAllHandler}
reduceCardinalityHint={
isPromqlChartConfig(config)
Comment thread
karl-power marked this conversation as resolved.
Resolved
? PROMQL_REDUCE_CARDINALITY_HINT
: undefined
}
/>,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,80 @@ describe('ChartDisplaySettingsDrawer', () => {
).not.toBeInTheDocument();
});

describe('PromQL (client render cap)', () => {
const promqlProps = { ...baseProps, configType: 'promql' as const };

it.each([DisplayType.Line, DisplayType.StackedBar])(
'shows the Series Limit input for PromQL %s charts',
displayType => {
renderWithMantine(
<ChartDisplaySettingsDrawer
{...promqlProps}
displayType={displayType}
/>,
);

const input = screen.getByRole('textbox', { name: /series limit/i });
expect(input).toHaveAttribute(
'placeholder',
`Default (${MAX_RENDERED_TIME_CHART_SERIES})`,
);
},
);

it('describes the limit as a render cap, not a fetch cap', () => {
renderWithMantine(
<ChartDisplaySettingsDrawer
{...promqlProps}
displayType={DisplayType.Line}
/>,
);

expect(
screen.getByText(/maximum number of series rendered/i),
).toBeInTheDocument();
expect(screen.queryByText(/fetched/i)).not.toBeInTheDocument();
});

it.each([DisplayType.Number, DisplayType.Table])(
'does not show the Series Limit input for PromQL %s charts',
displayType => {
renderWithMantine(
<ChartDisplaySettingsDrawer
{...promqlProps}
displayType={displayType}
/>,
);

expect(
screen.queryByRole('textbox', { name: /series limit/i }),
).not.toBeInTheDocument();
},
);

it('calls onChange with the entered seriesLimit when applied', async () => {
const onChange = jest.fn();
const user = userEvent.setup();

renderWithMantine(
<ChartDisplaySettingsDrawer
{...promqlProps}
displayType={DisplayType.Line}
onChange={onChange}
/>,
);

await user.type(
screen.getByRole('textbox', { name: /series limit/i }),
'5',
);
await user.click(screen.getByRole('button', { name: /apply/i }));

expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.mock.calls[0][0]).toMatchObject({ seriesLimit: 5 });
});
});

it('calls onChange with the entered seriesLimit when applied', async () => {
const onChange = jest.fn();
const user = userEvent.setup();
Expand Down Expand Up @@ -338,6 +412,23 @@ describe('ChartDisplaySettingsDrawer', () => {
},
);

it.each([DisplayType.Pie, DisplayType.Bar])(
'shows the Series Limit input for PromQL %s charts',
displayType => {
renderWithMantine(
<ChartDisplaySettingsDrawer
{...baseProps}
configType="promql"
displayType={displayType}
/>,
);

expect(
screen.getByRole('textbox', { name: /series limit/i }),
).toHaveAttribute('placeholder', 'Disabled (e.g. 10)');
},
);

it('does not show the Series Limit input for raw SQL pie charts', () => {
renderWithMantine(
<ChartDisplaySettingsDrawer
Expand Down
29 changes: 24 additions & 5 deletions packages/app/src/components/__tests__/DBPieChart.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ describe('DBPieChart', () => {
expect(second).toEqual(first);
});

it('draws a slice per series, largest first', () => {
const mockThreeSeries = () =>
mockUseQueriedChartConfig.mockReturnValue({
data: {
data: [
Expand All @@ -368,12 +368,31 @@ describe('DBPieChart', () => {
isError: false,
});

renderWithMantine(<DBPieChart config={promqlConfig} />);

const labels = within(screen.getByTestId('pie-chart-legend'))
const legendLabels = () =>
within(screen.getByTestId('pie-chart-legend'))
.getAllByTitle(/.+/)
.map(el => el.getAttribute('title'));
expect(labels).toEqual(['checkout', 'ad', 'cart']);

it('draws a slice per series, largest first', () => {
mockThreeSeries();
renderWithMantine(<DBPieChart config={promqlConfig} />);
expect(legendLabels()).toEqual(['checkout', 'ad', 'cart']);
});

it('keeps only the largest seriesLimit slices', () => {
mockThreeSeries();
renderWithMantine(
<DBPieChart config={{ ...promqlConfig, seriesLimit: 2 }} />,
);
expect(legendLabels()).toEqual(['checkout', 'ad']);
});

it('treats a seriesLimit of 0 as unlimited', () => {
mockThreeSeries();
renderWithMantine(
<DBPieChart config={{ ...promqlConfig, seriesLimit: 0 }} />,
);
expect(legendLabels()).toEqual(['checkout', 'ad', 'cart']);
});
});
});
20 changes: 18 additions & 2 deletions packages/app/src/components/charts/CategoricalChart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useMemo } from 'react';
import { hasNonEmptyOrderBy } from '@hyperdx/common-utils/dist/core/utils';
import {
hasNonEmptyOrderBy,
hasPositiveSeriesLimit,
} from '@hyperdx/common-utils/dist/core/utils';
import {
isBuilderChartConfig,
isPromqlChartConfig,
Expand Down Expand Up @@ -130,8 +133,21 @@ export function useCategoricalChart({
isBuilderChartConfig(queriedConfig) &&
hasNonEmptyOrderBy(queriedConfig.orderBy);

// Builder charts apply seriesLimit as a SQL LIMIT. Prometheus returns
// every series, so a PromQL limit trims the largest-first rows here.
const maxGroups =
Comment thread
karl-power marked this conversation as resolved.
Resolved
isPromqlChartConfig(queriedConfig) &&
hasPositiveSeriesLimit(queriedConfig.seriesLimit)
? queriedConfig.seriesLimit
: undefined;

return [
formatResponseForCategoricalChart(data, getColorProps, !hasOrderBy),
formatResponseForCategoricalChart(
data,
getColorProps,
!hasOrderBy,
maxGroups,
),
null,
];
} catch (error) {
Expand Down
Loading
Loading