Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.11] [SecuritySolution] Fix topN legend actions - filter in / out in timeline (#170127) #170269

Merged
merged 11 commits into from
Nov 1, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { layeredXyVisFunction } from '.';
import { createMockExecutionContext } from '@kbn/expressions-plugin/common/mocks';
import { sampleArgs, sampleExtendedLayer } from '../__mocks__';
import { XY_VIS } from '../constants';
import { shouldShowLegendActionDefault } from '../helpers/visualization';

describe('layeredXyVis', () => {
test('it renders with the specified data and args', async () => {
Expand All @@ -30,6 +31,7 @@ describe('layeredXyVis', () => {
syncTooltips: false,
syncCursor: true,
canNavigateToLens: false,
shouldShowLegendAction: shouldShowLegendActionDefault,
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
validateAxes,
} from './validate';
import { appendLayerIds, getDataLayers } from '../helpers';
import { shouldShowLegendActionDefault } from '../helpers/visualization';

export const layeredXyVisFn: LayeredXyVisFn['fn'] = async (data, args, handlers) => {
const layers = appendLayerIds(args.layers ?? [], 'layers');
Expand Down Expand Up @@ -66,6 +67,7 @@ export const layeredXyVisFn: LayeredXyVisFn['fn'] = async (data, args, handlers)
syncTooltips: handlers?.isSyncTooltipsEnabled?.() ?? false,
syncCursor: handlers?.isSyncCursorEnabled?.() ?? true,
overrides: handlers.variables?.overrides as XYRender['value']['overrides'],
shouldShowLegendAction: handlers?.shouldShowLegendAction ?? shouldShowLegendActionDefault,
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { xyVisFunction } from '.';
import { createMockExecutionContext } from '@kbn/expressions-plugin/common/mocks';
import { sampleArgs, sampleLayer } from '../__mocks__';
import { XY_VIS } from '../constants';
import { shouldShowLegendActionDefault } from '../helpers/visualization';

describe('xyVis', () => {
test('it renders with the specified data and args', async () => {
Expand Down Expand Up @@ -42,6 +43,7 @@ describe('xyVis', () => {
syncColors: false,
syncTooltips: false,
syncCursor: true,
shouldShowLegendAction: shouldShowLegendActionDefault,
},
});
});
Expand Down Expand Up @@ -352,6 +354,7 @@ describe('xyVis', () => {
syncColors: false,
syncTooltips: false,
syncCursor: true,
shouldShowLegendAction: shouldShowLegendActionDefault,
},
});
});
Expand Down Expand Up @@ -401,6 +404,7 @@ describe('xyVis', () => {
syncTooltips: false,
syncCursor: true,
overrides,
shouldShowLegendAction: shouldShowLegendActionDefault,
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
validateAxes,
} from './validate';
import { logDatatable } from '../utils';
import { shouldShowLegendActionDefault } from '../helpers/visualization';

const createDataLayer = (args: XYArgs, table: Datatable): DataLayerConfigResult => {
const accessors = getAccessors<string | ExpressionValueVisDimension, XYArgs>(args, table);
Expand Down Expand Up @@ -139,6 +140,7 @@ export const xyVisFn: XyVisFn['fn'] = async (data, args, handlers) => {
syncTooltips: handlers?.isSyncTooltipsEnabled?.() ?? false,
syncCursor: handlers?.isSyncCursorEnabled?.() ?? true,
overrides: handlers.variables?.overrides as XYRender['value']['overrides'],
shouldShowLegendAction: handlers?.shouldShowLegendAction ?? shouldShowLegendActionDefault,
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export function isTimeChart(layers: CommonXYDataLayerConfigResult[]) {
(!l.xScaleType || l.xScaleType === XScaleTypes.TIME)
);
}

export const shouldShowLegendActionDefault = () => true;
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ describe('getLegendAction', function () {
formattedColumns: {},
},
},
{}
{},
() => true
);
let wrapper: ReactWrapper<LegendActionProps>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const getLegendAction = (
fieldFormats: LayersFieldFormats,
formattedDatatables: DatatablesWithFormatInfo,
titles: LayersAccessorsTitles,
shouldShowLegendAction?: (actionId: string) => boolean,
singleTable?: boolean
): LegendAction =>
React.memo(({ series: [xySeries] }) => {
Expand Down Expand Up @@ -109,6 +110,7 @@ export const getLegendAction = (
}
onFilter={filterHandler}
legendCellValueActions={legendCellValueActions}
shouldShowLegendAction={shouldShowLegendAction}
/>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
* Side Public License, v 1.
*/

import React, { useState } from 'react';
import React, { useState, useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiContextMenuPanelDescriptor, EuiIcon, EuiPopover, EuiContextMenu } from '@elastic/eui';
import {
EuiContextMenuPanelDescriptor,
EuiIcon,
EuiPopover,
EuiContextMenu,
EuiContextMenuPanelItemDescriptor,
} from '@elastic/eui';
import { useLegendAction } from '@elastic/charts';
import type { CellValueAction } from '../types';
import { shouldShowLegendActionDefault } from '../../common/helpers/visualization';

export type LegendCellValueActions = Array<
Omit<CellValueAction, 'execute'> & { execute: () => void }
Expand All @@ -29,57 +36,70 @@ export interface LegendActionPopoverProps {
* Compatible actions to be added to the popover actions
*/
legendCellValueActions?: LegendCellValueActions;
shouldShowLegendAction?: (actionId: string) => boolean;
}

export const LegendActionPopover: React.FunctionComponent<LegendActionPopoverProps> = ({
label,
onFilter,
legendCellValueActions = [],
shouldShowLegendAction = shouldShowLegendActionDefault,
}) => {
const [popoverOpen, setPopoverOpen] = useState(false);
const [ref, onClose] = useLegendAction<HTMLDivElement>();

const legendCellValueActionPanelItems = legendCellValueActions.map((action) => ({
name: action.displayName,
'data-test-subj': `legend-${label}-${action.id}`,
icon: <EuiIcon type={action.iconType} size="m" />,
onClick: () => {
action.execute();
setPopoverOpen(false);
},
}));

const panels: EuiContextMenuPanelDescriptor[] = [
{
id: 'main',
title: label,
items: [
{
name: i18n.translate('expressionXY.legend.filterForValueButtonAriaLabel', {
defaultMessage: 'Filter for',
}),
'data-test-subj': `legend-${label}-filterIn`,
icon: <EuiIcon type="plusInCircle" size="m" />,
onClick: () => {
setPopoverOpen(false);
onFilter();
},
const panels: EuiContextMenuPanelDescriptor[] = useMemo(() => {
const defaultActions = [
{
id: 'filterIn',
displayName: i18n.translate('expressionXY.legend.filterForValueButtonAriaLabel', {
defaultMessage: 'Filter for',
}),
'data-test-subj': `legend-${label}-filterIn`,
iconType: 'plusInCircle',
execute: () => {
setPopoverOpen(false);
onFilter();
},
{
name: i18n.translate('expressionXY.legend.filterOutValueButtonAriaLabel', {
defaultMessage: 'Filter out',
}),
'data-test-subj': `legend-${label}-filterOut`,
icon: <EuiIcon type="minusInCircle" size="m" />,
},
{
id: 'filterOut',
displayName: i18n.translate('expressionXY.legend.filterOutValueButtonAriaLabel', {
defaultMessage: 'Filter out',
}),
'data-test-subj': `legend-${label}-filterOut`,
iconType: 'minusInCircle',
execute: () => {
setPopoverOpen(false);
onFilter({ negate: true });
},
},
];

const legendCellValueActionPanelItems = [...defaultActions, ...legendCellValueActions].reduce<
EuiContextMenuPanelItemDescriptor[]
>((acc, action) => {
if (shouldShowLegendAction(action.id)) {
acc.push({
name: action.displayName,
'data-test-subj': `legend-${label}-${action.id}`,
icon: <EuiIcon type={action.iconType} size="m" />,
onClick: () => {
action.execute();
setPopoverOpen(false);
onFilter({ negate: true });
},
},
...legendCellValueActionPanelItems,
],
},
];
});
}
return acc;
}, []);
return [
{
id: 'main',
title: label,
items: legendCellValueActionPanelItems,
},
];
}, [label, legendCellValueActions, onFilter, shouldShowLegendAction]);

const Button = (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export type XYChartRenderProps = Omit<XYChartProps, 'canNavigateToLens'> & {
renderComplete: () => void;
uiState?: PersistedState;
timeFormat: string;
shouldShowLegendAction?: (actionId: string) => boolean;
};

function nonNullable<T>(v: T): v is NonNullable<T> {
Expand Down Expand Up @@ -206,6 +207,7 @@ export function XYChart({
uiState,
timeFormat,
overrides,
shouldShowLegendAction,
}: XYChartRenderProps) {
const {
legend,
Expand Down Expand Up @@ -838,6 +840,7 @@ export function XYChart({
fieldFormats,
formattedDatatables,
titles,
shouldShowLegendAction,
singleTable
)
: undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export const getXyChartRenderer = ({
syncCursor={config.syncCursor}
uiState={handlers.uiState as PersistedState}
renderComplete={renderComplete}
shouldShowLegendAction={handlers.shouldShowLegendAction}
/>
</div>
</I18nProvider>
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/expressions/common/execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export interface ExecutionContext<
* Logs datatable.
*/
logDatatable?(name: string, datatable: Datatable): void;

shouldShowLegendAction?: (actionId: string) => boolean;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ export interface IInterpreterRenderHandlers {
uiState?: unknown;

getExecutionContext(): KibanaExecutionContext | undefined;
shouldShowLegendAction?: (actionId: string) => boolean;
}
1 change: 1 addition & 0 deletions src/plugins/expressions/public/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class ExpressionLoader {
hasCompatibleActions: params?.hasCompatibleActions,
getCompatibleCellValueActions: params?.getCompatibleCellValueActions,
executionContext: params?.executionContext,
shouldShowLegendAction: params?.shouldShowLegendAction,
});
this.render$ = this.renderHandler.render$;
this.update$ = this.renderHandler.update$;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface ReactExpressionRendererProps
error?: ExpressionRenderError | null
) => React.ReactElement | React.ReactElement[];
padding?: 'xs' | 's' | 'm' | 'l' | 'xl';
shouldShowLegendAction?: (actionId: string) => boolean;
}

export type ReactExpressionRendererType = React.ComponentType<ReactExpressionRendererProps>;
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/expressions/public/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface ExpressionRenderHandlerParams {
hasCompatibleActions?: (event: ExpressionRendererEvent) => Promise<boolean>;
getCompatibleCellValueActions?: (data: object[]) => Promise<unknown[]>;
executionContext?: KibanaExecutionContext;
shouldShowLegendAction?: (actionId: string) => boolean;
}

type UpdateValue = IInterpreterRenderUpdateParams<IExpressionLoaderParams>;
Expand Down Expand Up @@ -66,6 +67,7 @@ export class ExpressionRenderHandler {
hasCompatibleActions = async () => false,
getCompatibleCellValueActions = async () => [],
executionContext,
shouldShowLegendAction,
}: ExpressionRenderHandlerParams = {}
) {
this.element = element;
Expand Down Expand Up @@ -118,6 +120,9 @@ export class ExpressionRenderHandler {
},
hasCompatibleActions,
getCompatibleCellValueActions,
shouldShowLegendAction: (actionId: string) => {
return shouldShowLegendAction?.(actionId) ?? true;
},
};
}

Expand Down
1 change: 1 addition & 0 deletions src/plugins/expressions/public/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface IExpressionLoaderParams {
* By default, it equals 1000.
*/
throttle?: number;
shouldShowLegendAction?: (actionId: string) => boolean;
}

export interface ExpressionRenderError extends Error {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/lens/public/embeddable/embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ interface LensBaseEmbeddableInput extends EmbeddableInput {
onTableRowClick?: (
data: Simplify<LensTableRowContextMenuEvent['data'] & PreventableEvent>
) => void;
shouldShowLegendAction?: (actionId: string) => boolean;
}

export type LensByValueInput = {
Expand Down Expand Up @@ -1103,6 +1104,7 @@ export class Embeddable
}}
noPadding={this.visDisplayOptions.noPadding}
docLinks={this.deps.coreStart.docLinks}
shouldShowLegendAction={input.shouldShowLegendAction}
/>
</KibanaThemeProvider>
<MessagesBadge
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/lens/public/embeddable/expression_wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface ExpressionWrapperProps {
lensInspector: LensInspector;
noPadding?: boolean;
docLinks: CoreStart['docLinks'];
shouldShowLegendAction?: (actionId: string) => boolean;
}

export function ExpressionWrapper({
Expand All @@ -73,6 +74,7 @@ export function ExpressionWrapper({
lensInspector,
noPadding,
docLinks,
shouldShowLegendAction,
}: ExpressionWrapperProps) {
if (!expression) return null;
return (
Expand Down Expand Up @@ -104,6 +106,7 @@ export function ExpressionWrapper({
onEvent={handleEvent}
hasCompatibleActions={hasCompatibleActions}
getCompatibleCellValueActions={getCompatibleCellValueActions}
shouldShowLegendAction={shouldShowLegendAction}
/>
</div>
</I18nProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export { createFilterInCellActionFactory } from './cell_action/filter_in';
export { createFilterOutCellActionFactory } from './cell_action/filter_out';
export { createFilterInDiscoverCellActionFactory } from './discover/filter_in';
export { createFilterOutDiscoverCellActionFactory } from './discover/filter_out';
export { createTimelineHistogramFilterInLegendActionFactory } from './lens/filter_in_timeline';
export { createFilterInHistogramLegendActionFactory } from './lens/filter_in';
export { createTimelineHistogramFilterOutLegendActionFactory } from './lens/filter_out_timeline';
export { createFilterOutHistogramLegendActionFactory } from './lens/filter_out';
Loading
Loading