diff --git a/.changeset/adhoc-filter-click-timechart.md b/.changeset/adhoc-filter-click-timechart.md new file mode 100644 index 0000000000..78c2baf334 --- /dev/null +++ b/.changeset/adhoc-filter-click-timechart.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +feat: Filter time chart series into adhoc dashboard filters diff --git a/packages/app/src/ChartUtils.tsx b/packages/app/src/ChartUtils.tsx index 6355045685..166e19f98d 100644 --- a/packages/app/src/ChartUtils.tsx +++ b/packages/app/src/ChartUtils.tsx @@ -664,6 +664,8 @@ export interface LineData { displayName: string; /** The original result column name this series' values were pulled from. */ valueColumnName: string; + /** The series' raw group-by values, in group column order. */ + groupValues: unknown[]; color: string; isDashed?: boolean; } @@ -814,6 +816,7 @@ function addResponseToFormattedData({ previousPeriodKey: `${currentPeriodKey}${PreviousPeriodSuffix}`, displayName: keyName, valueColumnName, + groupValues: groupColumnNames.map(name => row[name]), color: applyLogLevelColor ? logLevelColor(row[groupColumnNames[0]]) : undefined, diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index 11e581301c..8faba662ed 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -1298,6 +1298,7 @@ const Tile = ({ }); } } + onFilter={onFilter} /> )} {effectiveQueriedConfig?.displayType === DisplayType.Table && ( diff --git a/packages/app/src/__tests__/ChartUtils.test.ts b/packages/app/src/__tests__/ChartUtils.test.ts index 89ad6d1488..54fc44697c 100644 --- a/packages/app/src/__tests__/ChartUtils.test.ts +++ b/packages/app/src/__tests__/ChartUtils.test.ts @@ -155,6 +155,7 @@ describe('ChartUtils', () => { 'AVG(toFloat64OrDefault(toString(Duration))) (previous)', displayName: 'AVG(toFloat64OrDefault(toString(Duration)))', valueColumnName: 'AVG(toFloat64OrDefault(toString(Duration)))', + groupValues: [], isDashed: false, }, ]); @@ -246,6 +247,7 @@ describe('ChartUtils', () => { 'AVG(toFloat64OrDefault(toString(Duration))) · checkout (previous)', displayName: 'AVG(toFloat64OrDefault(toString(Duration))) · checkout', valueColumnName: 'AVG(toFloat64OrDefault(toString(Duration)))', + groupValues: ['checkout'], isDashed: false, }, { @@ -255,6 +257,7 @@ describe('ChartUtils', () => { previousPeriodKey: 'max · checkout (previous)', displayName: 'max · checkout', valueColumnName: 'max', + groupValues: ['checkout'], isDashed: false, }, { @@ -266,6 +269,7 @@ describe('ChartUtils', () => { 'AVG(toFloat64OrDefault(toString(Duration))) · shipping (previous)', displayName: 'AVG(toFloat64OrDefault(toString(Duration))) · shipping', valueColumnName: 'AVG(toFloat64OrDefault(toString(Duration)))', + groupValues: ['shipping'], isDashed: false, }, { @@ -275,6 +279,7 @@ describe('ChartUtils', () => { previousPeriodKey: 'max · shipping (previous)', displayName: 'max · shipping', valueColumnName: 'max', + groupValues: ['shipping'], isDashed: false, }, ]); @@ -336,6 +341,7 @@ describe('ChartUtils', () => { previousPeriodKey: 'info (previous)', displayName: 'info', valueColumnName: 'count()', + groupValues: ['info'], isDashed: false, }, { @@ -345,6 +351,7 @@ describe('ChartUtils', () => { previousPeriodKey: 'debug (previous)', displayName: 'debug', valueColumnName: 'count()', + groupValues: ['debug'], isDashed: false, }, { @@ -354,6 +361,7 @@ describe('ChartUtils', () => { previousPeriodKey: 'error (previous)', displayName: 'error', valueColumnName: 'count()', + groupValues: ['error'], isDashed: false, }, ]); @@ -444,6 +452,7 @@ describe('ChartUtils', () => { 'AVG(toFloat64OrDefault(toString(Duration))) · checkout (previous)', displayName: 'AVG(toFloat64OrDefault(toString(Duration))) · checkout', valueColumnName: 'AVG(toFloat64OrDefault(toString(Duration)))', + groupValues: ['checkout'], isDashed: false, }, { @@ -453,6 +462,7 @@ describe('ChartUtils', () => { previousPeriodKey: 'max · checkout (previous)', displayName: 'max · checkout', valueColumnName: 'max', + groupValues: ['checkout'], isDashed: false, }, { @@ -464,6 +474,7 @@ describe('ChartUtils', () => { 'AVG(toFloat64OrDefault(toString(Duration))) · shipping (previous)', displayName: 'AVG(toFloat64OrDefault(toString(Duration))) · shipping', valueColumnName: 'AVG(toFloat64OrDefault(toString(Duration)))', + groupValues: ['shipping'], isDashed: false, }, { @@ -473,6 +484,7 @@ describe('ChartUtils', () => { previousPeriodKey: 'max · shipping (previous)', displayName: 'max · shipping', valueColumnName: 'max', + groupValues: ['shipping'], isDashed: false, }, ]); @@ -672,11 +684,41 @@ describe('ChartUtils', () => { previousPeriodKey: 'foo · {"key":"val"} · [1,2,3] (previous)', displayName: 'foo · {"key":"val"} · [1,2,3]', valueColumnName: 'count()', + groupValues: ['foo', { key: 'val' }, [1, 2, 3]], isDashed: false, }, ]); }); + it('keeps raw group values, including nulls and values containing the key joiner', () => { + const path = `GET${ChartKeyJoiner}/api`; + const actual = formatResponseForTimeChart({ + currentPeriodResponse: { + data: [ + { + 'count()': 5, + severity: null, + span_name: path, + __hdx_time_bucket: '2025-11-26T11:12:00Z', + }, + ], + meta: [ + { name: 'count()', type: 'UInt64' }, + { name: 'severity', type: 'Nullable(String)' }, + { name: 'span_name', type: 'String' }, + { name: '__hdx_time_bucket', type: 'DateTime' }, + ], + }, + dateRange: [new Date(), new Date()], + granularity: '1 minute', + generateEmptyBuckets: false, + }); + + expect(actual.lineData).toHaveLength(1); + expect(actual.lineData[0].dataKey).toBe(`${ChartKeyJoiner}${path}`); + expect(actual.lineData[0].groupValues).toEqual([null, path]); + }); + it('should plot previous period data when provided, shifted to align with current period', () => { const currentPeriodResponse = { data: [ @@ -762,6 +804,7 @@ describe('ChartUtils', () => { dataKey: 'AVG(toFloat64OrDefault(toString(Duration)))', displayName: 'AVG(toFloat64OrDefault(toString(Duration)))', valueColumnName: 'AVG(toFloat64OrDefault(toString(Duration)))', + groupValues: [], isDashed: false, }, { @@ -772,6 +815,7 @@ describe('ChartUtils', () => { dataKey: 'AVG(toFloat64OrDefault(toString(Duration))) (previous)', displayName: 'AVG(toFloat64OrDefault(toString(Duration))) (previous)', valueColumnName: 'AVG(toFloat64OrDefault(toString(Duration)))', + groupValues: [], isDashed: true, }, ]); @@ -1839,6 +1883,7 @@ describe('getSeriesColorForGroup', () => { previousPeriodKey: `${currentPeriodKey} (previous)`, displayName: currentPeriodKey, valueColumnName: 'count()', + groupValues: [], color, isDashed, }); diff --git a/packages/app/src/__tests__/DBSearchPageQueryKey.test.tsx b/packages/app/src/__tests__/DBSearchPageQueryKey.test.tsx index bc4e34af36..28b8a3b200 100644 --- a/packages/app/src/__tests__/DBSearchPageQueryKey.test.tsx +++ b/packages/app/src/__tests__/DBSearchPageQueryKey.test.tsx @@ -9,6 +9,10 @@ import { DBTimeChart } from '@/components/DBTimeChart'; import SearchTotalCountChart from '@/components/SearchTotalCountChart'; // Mock the API and hooks +jest.mock('@/hooks/useResultColumnFilterKeys', () => ({ + useResultColumnFilterKeys: jest.fn(() => new Map()), +})); + jest.mock('@/api', () => ({ __esModule: true, default: { diff --git a/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts b/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts index 8c507533e6..2ccea9c3db 100644 --- a/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts +++ b/packages/app/src/__tests__/HDXMultiSeriesTimeChart.test.ts @@ -395,6 +395,7 @@ describe('scanYAxisValueRange', () => { previousPeriodKey: `${dataKey}.prev`, displayName: dataKey, valueColumnName: dataKey, + groupValues: [], color: '#a', }); @@ -417,6 +418,7 @@ describe('computeYAxisBounds', () => { previousPeriodKey: `${dataKey}.prev`, displayName: dataKey, valueColumnName: dataKey, + groupValues: [], color: '#a', }); @@ -836,6 +838,7 @@ describe('getVisibleLineData', () => { previousPeriodKey: `${dataKey}.prev`, displayName: displayName ?? dataKey, valueColumnName: dataKey, + groupValues: [], color: '#abcdef', }); @@ -903,6 +906,7 @@ describe('getSelectedLineData', () => { previousPeriodKey: `${dataKey}.prev`, displayName: displayName ?? dataKey, valueColumnName: dataKey, + groupValues: [], color: '#abcdef', }); @@ -949,6 +953,7 @@ describe('buildActiveClickSeries', () => { previousPeriodKey: `${dataKey}.prev`, displayName: displayName ?? dataKey, valueColumnName: dataKey, + groupValues: [], color: `#${dataKey}`, }); @@ -1022,6 +1027,7 @@ describe('buildActiveClickSeries', () => { previousPeriodKey: 'a.prev', displayName: 'Alpha (previous)', valueColumnName: 'a', + groupValues: [], color: '#a', isDashed: true, }; diff --git a/packages/app/src/components/DBTimeChart.tsx b/packages/app/src/components/DBTimeChart.tsx index 9593cabc95..99e2ff79d6 100644 --- a/packages/app/src/components/DBTimeChart.tsx +++ b/packages/app/src/components/DBTimeChart.tsx @@ -12,6 +12,7 @@ import { convertGranularityToSeconds, getAlignedDateRange, } from '@hyperdx/common-utils/dist/core/utils'; +import type { AdhocConditionEntry } from '@hyperdx/common-utils/dist/filters'; import { isBuilderChartConfig, isPromqlChartConfig, @@ -37,12 +38,16 @@ import { convertToTimeChartConfig, formatResponseForTimeChart, getPreviousDateRange, + type LineData, shouldFillNullsWithZero, tryExpandConfigVariables, useTimeChartSettings, } from '@/ChartUtils'; import { ChartAnnotation } from '@/components/charts/chartAnnotations'; -import { ChartSeriesTooltip } from '@/components/charts/ChartSeriesTooltip'; +import { + ChartSeriesTooltip, + type ChartSeriesTooltipProps, +} from '@/components/charts/ChartSeriesTooltip'; import { useChartTooltipZIndex } from '@/components/charts/ChartTooltip'; import { MAX_LOADABLE_TIME_CHART_SERIES, @@ -54,8 +59,9 @@ import { useQueriedChartConfig, } from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; +import { useResultColumnFilterKeys } from '@/hooks/useResultColumnFilterKeys'; import { useChartNumberFormats, useSource } from '@/source'; -import type { NumberFormat } from '@/types'; +import type { NumberFormat, TileFilterHandler } from '@/types'; import ChartContainer from './charts/ChartContainer'; import ChartErrorState, { @@ -143,6 +149,47 @@ export function decodeSeriesGroupFilters({ return groupFilters; } +/** + * The ad hoc filter entries that select one chart series: its PromQL labels, + * or a key per group-by column. Undefined when some group column has no key + * or can't be filtered on, since filtering on the rest would match more than + * the series. + */ +export function getSeriesFilterEntries({ + line, + groupColumns, + filterKeyByColumn, + promqlSeriesLabels, +}: { + line: Pick | undefined; + groupColumns: string[]; + filterKeyByColumn: ReadonlyMap; + promqlSeriesLabels?: Record>; +}): AdhocConditionEntry[] | undefined { + if (!line || groupColumns.length === 0) return undefined; + if (promqlSeriesLabels) { + // PromQL series are grouped by their name alone. + const labels = promqlSeriesLabels[String(line.groupValues[0])]; + // An ad hoc filter reaches every PromQL query, so a `__name__` condition + // would empty those on other metrics. + return labels + ? Object.entries(labels) + .filter(([key]) => key !== '__name__') + .map(([key, value]) => ({ key, value })) + : undefined; + } + const entries: AdhocConditionEntry[] = []; + for (const [index, column] of groupColumns.entries()) { + const key = filterKeyByColumn.get(column); + const value = line.groupValues[index]; + // NULL doesn't match `= ''`, and Map / Array / Tuple / JSON values are + // JSON in the series key, which their `toString()` wouldn't match. + if (!key || value == null || typeof value === 'object') return undefined; + entries.push({ key, value: String(value) }); + } + return entries; +} + // The interactive PINNED tooltip, rendered over the chart in a body-portaled // Mantine Popover anchored at the clicked point. Hover uses the recharts tooltip // in MemoChart instead; this is only for the click-locked state. @@ -151,6 +198,8 @@ function ChartTooltipOverlay({ buildSearchUrl, onDismiss, onFocusSeries, + getSeriesFilters, + onFilterSeries, onShowAllSeries, fallbackNumberFormat, numberFormatByKey, @@ -158,7 +207,7 @@ function ChartTooltipOverlay({ hiddenSeriesCount, onLoadAllSeries, expanded, -}: { +}: Pick & { payload: ActiveClickPayload | undefined; buildSearchUrl: (key?: string, value?: number) => string | null; onDismiss: () => void; @@ -289,6 +338,8 @@ function ChartTooltipOverlay({ buildSearchUrl={buildSearchUrl} onDismiss={onDismiss} onFocusSeries={onFocusSeries} + getSeriesFilters={getSeriesFilters} + onFilterSeries={onFilterSeries} onShowAllSeries={onShowAllSeries} hiddenSeriesCount={hiddenSeriesCount} onLoadAllSeries={onLoadAllSeries} @@ -336,6 +387,11 @@ type DBTimeChartComponentProps = { * behavior), which is all a standalone chart can do. */ onFocusSeries?: (filters: SeriesGroupFilter[]) => void; + /** + * When set, the pinned tooltip offers filter / exclude actions per series, + * with the series' group-by expressions (or PromQL labels) and values. + */ + onFilter?: TileFilterHandler; }; function DBTimeChartComponent({ @@ -362,6 +418,7 @@ function DBTimeChartComponent({ showDateRangeIndicator = true, errorVariant, onFocusSeries, + onFilter, }: DBTimeChartComponentProps) { const [selectedSeriesSet, setSelectedSeriesSet] = useState>( new Set(), @@ -886,6 +943,27 @@ function DBTimeChartComponent({ [onFocusSeries, groupColumns, isSingleValueColumn, handleToggleSeries], ); + const promqlSeriesLabels = data?.promqlSeriesLabels; + const filterKeyByColumn = useResultColumnFilterKeys({ + config: queriedConfig, + meta: data?.meta, + enabled: !!onFilter && !promqlSeriesLabels, + }); + const lineDataByKey = useMemo( + () => new Map(lineData.map(line => [line.dataKey, line])), + [lineData], + ); + const getSeriesFilters = useCallback( + (dataKey: string | undefined) => + getSeriesFilterEntries({ + line: dataKey != null ? lineDataByKey.get(dataKey) : undefined, + groupColumns, + filterKeyByColumn, + promqlSeriesLabels, + }), + [lineDataByKey, groupColumns, filterKeyByColumn, promqlSeriesLabels], + ); + const toolbarItemsMemo = useMemo(() => { const allToolbarItems = []; @@ -1033,6 +1111,8 @@ function DBTimeChartComponent({ // re-register its window listener on every re-render. onDismiss={dismissPinned} onFocusSeries={handleFocusSeries} + getSeriesFilters={getSeriesFilters} + onFilterSeries={onFilter} onShowAllSeries={ selectedSeriesSet.size > 0 ? handleClearSelectedSeries : undefined } diff --git a/packages/app/src/components/__tests__/DBTimeChart.test.tsx b/packages/app/src/components/__tests__/DBTimeChart.test.tsx index ee107d9ab7..bf460c3493 100644 --- a/packages/app/src/components/__tests__/DBTimeChart.test.tsx +++ b/packages/app/src/components/__tests__/DBTimeChart.test.tsx @@ -9,6 +9,7 @@ import DateRangeIndicator from '@/components/charts/DateRangeIndicator'; import { DBTimeChart, decodeSeriesGroupFilters, + getSeriesFilterEntries, } from '@/components/DBTimeChart'; import MVOptimizationIndicator from '@/components/MaterializedViews/MVOptimizationIndicator'; import { MAX_LOADABLE_TIME_CHART_SERIES } from '@/defaults'; @@ -25,6 +26,10 @@ jest.mock('@/hooks/useChartConfig', () => ({ getMinGranularitySeconds: jest.fn().mockReturnValue(undefined), })); +jest.mock('@/hooks/useResultColumnFilterKeys', () => ({ + useResultColumnFilterKeys: jest.fn(() => new Map()), +})); + jest.mock('@/hooks/useMVOptimizationExplanation', () => ({ useMVOptimizationExplanation: jest.fn().mockReturnValue({ data: undefined, @@ -671,6 +676,74 @@ describe('DBTimeChart', () => { }); }); + describe('getSeriesFilterEntries', () => { + const filterKeyByColumn = new Map([ + ['svc', 'ServiceName'], + ['SeverityText', 'SeverityText'], + ['attrs', 'LogAttributes'], + ]); + const entries = (groupValues: unknown[], groupColumns: string[]) => + getSeriesFilterEntries({ + line: { groupValues }, + groupColumns, + filterKeyByColumn, + }); + + it('keys each group value by its column filter key', () => { + expect(entries(['api', 'error'], ['svc', 'SeverityText'])).toEqual([ + { key: 'ServiceName', value: 'api' }, + { key: 'SeverityText', value: 'error' }, + ]); + }); + + it('stringifies a number group value', () => { + expect(entries([200], ['svc'])).toEqual([ + { key: 'ServiceName', value: '200' }, + ]); + }); + + it('returns nothing when a group column has no filter key', () => { + expect(entries(['api', 'x'], ['svc', 'unknown'])).toBeUndefined(); + }); + + it('returns nothing for a null group value', () => { + expect(entries(['api', null], ['svc', 'SeverityText'])).toBeUndefined(); + }); + + it('returns nothing for an object or array group value', () => { + expect(entries([{ a: '1' }], ['attrs'])).toBeUndefined(); + expect(entries([['a', 1]], ['attrs'])).toBeUndefined(); + }); + + it('returns nothing without a series or group columns', () => { + expect( + getSeriesFilterEntries({ + line: undefined, + groupColumns: ['svc'], + filterKeyByColumn, + }), + ).toBeUndefined(); + expect(entries([], [])).toBeUndefined(); + }); + + it('returns the labels of a PromQL series, except __name__', () => { + const seriesName = 'up{job="api"}'; + expect( + getSeriesFilterEntries({ + line: { groupValues: [seriesName] }, + groupColumns: ['series_name'], + filterKeyByColumn: new Map(), + promqlSeriesLabels: { + [seriesName]: { __name__: 'up', job: 'api', pod: 'a' }, + }, + }), + ).toEqual([ + { key: 'job', value: 'api' }, + { key: 'pod', value: 'a' }, + ]); + }); + }); + describe('decodeSeriesGroupFilters', () => { it('maps a single-group series key to a column/value filter', () => { expect( diff --git a/packages/app/src/components/charts/ChartSeriesTooltip.tsx b/packages/app/src/components/charts/ChartSeriesTooltip.tsx index eb31ec06c0..632063b80d 100644 --- a/packages/app/src/components/charts/ChartSeriesTooltip.tsx +++ b/packages/app/src/components/charts/ChartSeriesTooltip.tsx @@ -1,4 +1,5 @@ import Link from 'next/link'; +import type { AdhocConditionEntry } from '@hyperdx/common-utils/dist/filters'; import { ActionIcon, Group, @@ -12,6 +13,8 @@ import { IconArrowsMaximize, IconCheck, IconCopy, + IconFilter, + IconFilterX, IconFocusCentered, IconSearch, } from '@tabler/icons-react'; @@ -31,13 +34,50 @@ import { useChartTooltipActionZIndex, } from './ChartTooltip'; +/** An icon button in a series row's action cluster, labelled by its tooltip. */ +function SeriesAction({ + label, + testId, + icon, + onClick, + tooltipZIndex, +}: { + label: string; + testId: string; + icon: React.ReactNode; + onClick: () => void; + tooltipZIndex?: number; +}) { + return ( + + + {icon} + + + ); +} + /** * One pinned-tooltip series row: the shared ChartTooltipItem plus, when * `actions` is set, the per-series action cluster (Search / Copy / Focus). The * cluster is flexShrink:0 so it doesn't shift as the name truncates or the copy * icon swaps to a check (which would move the buttons out from under the cursor). * - * Search is hidden when `actions.drillInUrl` is undefined (source-less charts). + * Search is hidden when `actions.drillInUrl` is undefined (source-less charts), + * and Focus, Filter and Exclude when their handlers are. */ function SeriesRow({ name, @@ -58,8 +98,10 @@ function SeriesRow({ actions?: { /** Drill-in URL; when undefined the Search action is hidden (no source). */ drillInUrl?: string; - onDrillIn: () => void; - onFocus: () => void; + onDrillIn?: () => void; + onFocus?: () => void; + onFilter?: () => void; + onExclude?: () => void; }; /** z-index for the action tooltips, so they sit above the pinned popover. */ actionTooltipZIndex?: number; @@ -132,24 +174,33 @@ function SeriesRow({ )} - - } + onClick={actions.onFilter} + tooltipZIndex={actionTooltipZIndex} + /> + )} + {actions.onExclude != null && ( + } + onClick={actions.onExclude} + tooltipZIndex={actionTooltipZIndex} + /> + )} + {actions.onFocus != null && ( + } onClick={actions.onFocus} - > - - - + tooltipZIndex={actionTooltipZIndex} + /> + )} ); @@ -170,6 +221,18 @@ export type ChartSeriesTooltipProps = { onDismiss?: () => void; /** Focus a series by its raw key + display name. */ onFocusSeries?: (payload: { dataKey?: string; name: string }) => void; + /** + * The entries identifying a series, or undefined when it can't be filtered + * on. Paired with `onFilterSeries`. + */ + getSeriesFilters?: ( + dataKey: string | undefined, + ) => AdhocConditionEntry[] | undefined; + /** Filter on (`include`) or out (`exclude`) a series' entries. */ + onFilterSeries?: ( + entries: AdhocConditionEntry[], + action: 'include' | 'exclude', + ) => void; /** Clear an active series focus; renders a "Show All Series" footer action when set. */ onShowAllSeries?: () => void; /** @@ -219,6 +282,8 @@ export function ChartSeriesTooltip({ buildSearchUrl, onDismiss, onFocusSeries, + getSeriesFilters, + onFilterSeries, onShowAllSeries, hiddenSeriesCount = 0, onLoadAllSeries, @@ -255,7 +320,7 @@ export function ChartSeriesTooltip({ const totalHidden = tooltipHiddenCount + hiddenSeriesCount; // Per-series actions only make sense with more than one group (a single series - // is covered by the header/footer). + // is covered by the header/footer), except for filtering. const showPerSeriesActions = rows.length > 1; // buildSearchUrl is always supplied but returns null when drill-down isn't @@ -324,6 +389,25 @@ export function ChartSeriesTooltip({ (payload.valueColumnName != null ? numberFormatByKey.get(payload.valueColumnName) : undefined) ?? fallbackNumberFormat; + const seriesFilters = onFilterSeries + ? getSeriesFilters?.(payload.dataKey) + : undefined; + const filterSeries = + seriesFilters?.length && onFilterSeries + ? (action: 'include' | 'exclude') => { + onFilterSeries(seriesFilters, action); + onDismiss?.(); + } + : undefined; + const filterActions = filterSeries && { + onFilter: () => filterSeries('include'), + // Excluding a series with several keys can't be written as ANDed + // `!=` conditions, so it's single-key only. + onExclude: + seriesFilters?.length === 1 + ? () => filterSeries('exclude') + : undefined, + }; const seriesUrl = showPerSeriesActions && canDrillDown ? (buildSearchUrl!( @@ -353,8 +437,9 @@ export function ChartSeriesTooltip({ }); onDismiss?.(); }, + ...filterActions, } - : undefined + : filterActions } /> ); diff --git a/packages/app/src/components/charts/__tests__/ChartSeriesTooltip.test.tsx b/packages/app/src/components/charts/__tests__/ChartSeriesTooltip.test.tsx index b3de47662e..bf6d7c7cca 100644 --- a/packages/app/src/components/charts/__tests__/ChartSeriesTooltip.test.tsx +++ b/packages/app/src/components/charts/__tests__/ChartSeriesTooltip.test.tsx @@ -233,6 +233,96 @@ describe('ChartSeriesTooltip', () => { expect(onDismiss).toHaveBeenCalledTimes(1); }); + describe('series filters', () => { + const entriesByKey: Record = { + error: [{ key: 'SeverityText', value: 'error' }], + warn: [ + { key: 'SeverityText', value: 'warn' }, + { key: 'ServiceName', value: 'api' }, + ], + }; + const getSeriesFilters = (dataKey: string | undefined) => + dataKey ? entriesByKey[dataKey] : undefined; + + it('filters on a series and dismisses the tooltip', () => { + const onFilterSeries = jest.fn(); + const onDismiss = jest.fn(); + renderWithMantine( + , + ); + + fireEvent.click(screen.getByTestId('chart-filter-series-warn')); + expect(onFilterSeries).toHaveBeenCalledWith(entriesByKey.warn, 'include'); + expect(onDismiss).toHaveBeenCalled(); + }); + + it('offers exclude only for a series with one entry', () => { + const onFilterSeries = jest.fn(); + renderWithMantine( + , + ); + + expect(screen.queryByTestId('chart-exclude-series-warn')).toBeNull(); + fireEvent.click(screen.getByTestId('chart-exclude-series-error')); + expect(onFilterSeries).toHaveBeenCalledWith( + entriesByKey.error, + 'exclude', + ); + }); + + it('hides the actions for a series without entries, or without a handler', () => { + const { unmount } = renderWithMantine( + undefined} + onFilterSeries={jest.fn()} + />, + ); + expect(screen.queryByTestId('chart-filter-series-error')).toBeNull(); + unmount(); + + renderWithMantine( + , + ); + expect(screen.queryByTestId('chart-filter-series-error')).toBeNull(); + }); + + it('offers the filter actions for a lone series', () => { + renderWithMantine( + [{ key: 'ServiceName', value: 'api' }]} + onFilterSeries={jest.fn()} + />, + ); + + expect( + screen.getByTestId('chart-filter-series-count'), + ).toBeInTheDocument(); + expect( + screen.getByTestId('chart-exclude-series-count'), + ).toBeInTheDocument(); + expect(screen.queryByTestId('chart-focus-series-count')).toBeNull(); + }); + }); + it('renders no tooltip content when every series value is non-finite', () => { renderWithMantine( & { isComplete: boolean; + /** PromQL time series only: each series' labels, by series name. */ + promqlSeriesLabels?: Record>; }; /** diff --git a/packages/app/src/utils/__tests__/promqlChartQuery.test.ts b/packages/app/src/utils/__tests__/promqlChartQuery.test.ts index 5c497f0879..6a1d0c9acb 100644 --- a/packages/app/src/utils/__tests__/promqlChartQuery.test.ts +++ b/packages/app/src/utils/__tests__/promqlChartQuery.test.ts @@ -409,6 +409,76 @@ describe('queryPromqlChartConfig', () => { ); }); + describe('series labels of a time chart', () => { + const t0 = dateRange[0].getTime() / 1000; + const mockRange = (...metrics: Record[][]) => { + const mock = jest.mocked(prometheusApi.queryRange).mockReset(); + for (const series of metrics) { + mock.mockResolvedValueOnce({ + status: 'success', + data: { + resultType: 'matrix', + result: series.map(metric => ({ + metric, + values: [[t0, '1']], + })), + }, + }); + } + }; + const query = ( + promqlExpression: { expression: string }[], + legendTemplate?: string, + ) => + queryPromqlChartConfig( + { + configType: 'promql', + displayType: DisplayType.Line, + source: 'source-promql', + connection: 'conn', + granularity: '1 minute', + legendTemplate, + promqlExpression, + }, + dateRange, + new AbortController().signal, + {}, + ); + + it('keys each series by its name', async () => { + mockRange([ + { __name__: 'up', service: 'api' }, + { __name__: 'up', service: 'web' }, + ]); + const result = await query([{ expression: 'up' }]); + const names = result.data.map(row => row.series_name); + expect(result.promqlSeriesLabels).toEqual({ + [names[0]]: { __name__: 'up', service: 'api' }, + [names[1]]: { __name__: 'up', service: 'web' }, + }); + }); + + it('keys series by their legend-template name', async () => { + mockRange([{ service: 'api', pod: 'a' }]); + const result = await query([{ expression: 'up' }], '{{service}}'); + expect(result.promqlSeriesLabels).toEqual({ + api: { service: 'api', pod: 'a' }, + }); + }); + + it('covers every expression', async () => { + mockRange([{ service: 'api' }], [{ job: 'db' }]); + const result = await query([ + { expression: 'up' }, + { expression: 'down' }, + ]); + expect(Object.values(result.promqlSeriesLabels ?? {})).toEqual([ + { service: 'api' }, + { job: 'db' }, + ]); + }); + }); + it('runs a heatmap as a range query of its first expression', async () => { jest .mocked(prometheusApi.queryRange) diff --git a/packages/app/src/utils/promqlChartQuery.ts b/packages/app/src/utils/promqlChartQuery.ts index e7cb4fa2e6..183d0d926a 100644 --- a/packages/app/src/utils/promqlChartQuery.ts +++ b/packages/app/src/utils/promqlChartQuery.ts @@ -157,9 +157,11 @@ function toChartRows( const isBucketed = expressions.some(result => result.isBucketed); const data: Record[] = []; + const promqlSeriesLabels: Record> = {}; expressions.forEach(({ result: series }, expressionIndex) => { - series.forEach(({ values }, seriesIndex) => { + series.forEach(({ values, metric }, seriesIndex) => { const seriesName = seriesNames[expressionIndex][seriesIndex]; + promqlSeriesLabels[seriesName] = metric; for (const [ts, value] of values) { data.push( isBucketed @@ -185,6 +187,7 @@ function toChartRows( ], rows: data.length, isComplete: true, + promqlSeriesLabels, }; } diff --git a/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts b/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts index 4ee7afe229..d895c689db 100644 --- a/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts +++ b/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts @@ -4,6 +4,7 @@ * filter. Tiles no ad hoc filter reaches offer no filter actions. */ import { DisplayType } from '@hyperdx/common-utils/dist/types'; +import type { Locator } from '@playwright/test'; import { SidePanelComponent } from '../components/SidePanelComponent'; import { TableComponent } from '../components/TableComponent'; @@ -339,6 +340,168 @@ test.describe( }); }); + // One `` per returned series. + const seriesAreasOf = (tile: Locator) => tile.locator('.recharts-area'); + + const addLineTile = async (chartName: string, groupBy: string) => { + const tileCount = await dashboardPage.getTiles().count(); + await dashboardPage.addTile(); + await expect(dashboardPage.chartEditor.nameInput).toBeVisible(); + await dashboardPage.chartEditor.waitForDataToLoad(); + await dashboardPage.chartEditor.setChartType(DisplayType.Line); + await dashboardPage.chartEditor.setChartName(chartName); + await dashboardPage.chartEditor.selectSource(DEFAULT_LOGS_SOURCE_NAME); + await dashboardPage.chartEditor.setGroupBy(groupBy); + await dashboardPage.chartEditor.runQuery(); + await dashboardPage.chartEditor.save(); + await expect(dashboardPage.getTiles()).toHaveCount(tileCount + 1); + }; + + test('builder line chart series update the ad hoc filter', async ({ + page, + }) => { + const filterActions = + dashboardPage.getPinnedSeriesFilterActions('include'); + const excludeActions = + dashboardPage.getPinnedSeriesFilterActions('exclude'); + + await test.step('Add line charts grouped by one and two columns', async () => { + await dashboardPage.createNewDashboard(); + // Auto granularity leaves most one-minute buckets without every series + await dashboardPage.changeGranularity('30 Minutes Granularity'); + await addLineTile('Two keys', 'ServiceName, SeverityText'); + await addLineTile('One key', 'ServiceName'); + }); + + await test.step('Without an ad hoc filter, series offer no filter action', async () => { + await dashboardPage.pinTimeChart(dashboardPage.getTile(0)); + await expect(filterActions).toHaveCount(0); + await page.keyboard.press('Escape'); + await addAdhocFilters([ + { name: 'Conditions', sources: [DEFAULT_LOGS_SOURCE_NAME] }, + ]); + }); + + await test.step('Filtering on a series adds a condition per group-by value', async () => { + await dashboardPage.pinTimeChart(dashboardPage.getTile(0)); + await expect(excludeActions).toHaveCount(0); + await filterActions.first().click(); + await expect + .poll(() => adhocConditionsParam(page, 'Conditions')) + .toEqual([ + { key: 'ServiceName', operator: '=', value: expect.any(String) }, + { key: 'SeverityText', operator: '=', value: expect.any(String) }, + ]); + // Values that didn't match the series exactly would leave none or many. + await expect(seriesAreasOf(dashboardPage.getTile(0))).toHaveCount(1); + }); + + await test.step('Excluding a single-key series adds key != value', async () => { + await page.getByTestId('adhoc-filter-clear-Conditions').click(); + await expect.poll(() => filtersParam(page) ?? []).toEqual([]); + await dashboardPage.pinTimeChart(dashboardPage.getTile(1)); + await excludeActions.first().click(); + await expect + .poll(() => adhocConditionsParam(page, 'Conditions')) + .toEqual([ + { key: 'ServiceName', operator: '!=', value: expect.any(String) }, + ]); + const [{ value: service }] = adhocConditionsParam(page, 'Conditions'); + const excludedRow = page.getByTestId(`chart-copy-name-${service}`); + // Re-pin until the tooltip shows the refetched series. + await expect(async () => { + await page.keyboard.press('Escape'); + await dashboardPage.pinTimeChart(dashboardPage.getTile(1)); + await expect(excludedRow).toHaveCount(0, { timeout: 2000 }); + }).toPass({ timeout: 20000 }); + }); + }); + + test('raw SQL line chart series filter on the expression behind the alias', async ({ + page, + }) => { + const sql = `SELECT toStartOfInterval(Timestamp, INTERVAL {intervalSeconds:Int64} SECOND) AS ts, ServiceName AS svc, count() AS count FROM default.e2e_otel_logs WHERE ${TIME_FILTER} AND $__filters GROUP BY ts, svc ORDER BY ts ASC`; + + await dashboardPage.createNewDashboard(); + await dashboardPage.changeGranularity('30 Minutes Granularity'); + await dashboardPage.addTile(); + await expect(dashboardPage.chartEditor.nameInput).toBeVisible(); + await dashboardPage.chartEditor.waitForDataToLoad(); + await dashboardPage.chartEditor.setChartType(DisplayType.Line); + await dashboardPage.chartEditor.setChartName('Raw SQL line'); + await dashboardPage.chartEditor.switchToSqlMode(); + await dashboardPage.chartEditor.selectSource(DEFAULT_LOGS_SOURCE_NAME); + await dashboardPage.chartEditor.typeSqlQuery(sql); + await dashboardPage.chartEditor.runQuery(false); + await dashboardPage.chartEditor.save(); + await expect(dashboardPage.getTiles()).toHaveCount(1); + await addAdhocFilters([ + { name: 'Conditions', sources: [DEFAULT_LOGS_SOURCE_NAME] }, + ]); + + await dashboardPage.pinTimeChart(dashboardPage.getTile(0)); + await dashboardPage + .getPinnedSeriesFilterActions('include') + .first() + .click(); + await expect + .poll(() => adhocConditionsParam(page, 'Conditions')) + .toEqual([ + { key: 'ServiceName', operator: '=', value: expect.any(String) }, + ]); + await expect(seriesAreasOf(dashboardPage.getTile(0))).toHaveCount(1); + }); + + test('PromQL line chart series filter on their labels', async ({ + page, + }) => { + const tile = dashboardPage.getTile(0); + const seriesAreas = seriesAreasOf(tile); + + await test.step('Add a PromQL chart and an ad hoc filter on its source', async () => { + await dashboardPage.createNewDashboard(); + await dashboardPage.addTile(); + await expect(dashboardPage.chartEditor.nameInput).toBeVisible(); + await dashboardPage.chartEditor.waitForDataToLoad(); + await dashboardPage.chartEditor.switchToPromqlMode(); + await dashboardPage.chartEditor.selectSource(PROMQL_SOURCE_NAME); + await dashboardPage.chartEditor.setChartName('PromQL chart'); + await dashboardPage.chartEditor.replacePromqlExpression( + E2E_PROMQL_METRIC_NAME, + ); + await dashboardPage.chartEditor.save(); + await expect(dashboardPage.getTiles()).toHaveCount(1); + await dashboardPage.openEditFiltersModal(); + await dashboardPage.addAdhocFilterToDashboard( + 'Labels', + [PROMQL_SOURCE_NAME], + { sourceType: 'Prometheus' }, + ); + await dashboardPage.closeFiltersModal(); + await expect(seriesAreas.nth(1)).toBeAttached({ timeout: 20000 }); + }); + + await test.step('Filtering on a series adds its labels and narrows the chart', async () => { + await dashboardPage.pinTimeChart(tile); + await dashboardPage + .getPinnedSeriesFilterActions('include') + .first() + .click(); + await expect + .poll(() => filtersParam(page)) + .toEqual([ + { + type: 'adhoc', + name: 'Labels', + conditions: [ + { key: 'service', operator: '=', value: expect.any(String) }, + ], + }, + ]); + await expect(seriesAreas).toHaveCount(1); + }); + }); + test('search tile cells filter on the expressions behind complex columns', async ({ page, }) => { diff --git a/packages/app/tests/e2e/page-objects/DashboardPage.ts b/packages/app/tests/e2e/page-objects/DashboardPage.ts index 603df4fc17..c4a0035379 100644 --- a/packages/app/tests/e2e/page-objects/DashboardPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts @@ -1688,6 +1688,26 @@ export class DashboardPage { await (await this.getTileCellAction(tile, value, action)).click(); } + /** + * Click a time chart tile until its tooltip pins. Recharts only pins when the + * click lands on a bucket, so retry. + */ + async pinTimeChart(tile: Locator) { + const chart = tile.locator('.recharts-responsive-container'); + const pinnedRow = this.page.locator('[data-testid^="chart-copy-name-"]'); + await expect(async () => { + await chart.click(); + await expect(pinnedRow.first()).toBeVisible({ timeout: 2000 }); + }).toPass({ timeout: 30000 }); + } + + /** The pinned tooltip's filter (`include`) or exclude action per series. */ + getPinnedSeriesFilterActions(action: 'include' | 'exclude'): Locator { + const prefix = + action === 'include' ? 'chart-filter-series-' : 'chart-exclude-series-'; + return this.page.locator(`[data-testid^="${prefix}"]`); + } + /** The open condition editor of an ad hoc filter. */ getAdhocConditionEditor(filterName: string): Locator { return this.page.getByTestId(`adhoc-condition-editor-${filterName}`);