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

[ML] AIOps Log Rate Analysis: adds controls for controlling which columns will be visible #184262

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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface FieldFilterApplyButtonProps {
tooltipContent?: string;
}

export const FieldFilterApplyButton: FC<FieldFilterApplyButtonProps> = ({
export const ItemFilterApplyButton: FC<FieldFilterApplyButtonProps> = ({
disabled,
onClick,
tooltipContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,40 @@ import {
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';

import { FieldFilterApplyButton } from './field_filter_apply_button';
import { ItemFilterApplyButton } from './item_filter_apply_button';

interface FieldFilterPopoverProps {
interface ItemFilterPopoverProps {
dataTestSubj: string;
disabled?: boolean;
disabledApplyButton?: boolean;
uniqueFieldNames: string[];
onChange: (skippedFields: string[]) => void;
disabledApplyTooltipContent?: string;
helpText: string;
itemSearchAriaLabel: string;
initialSkippedItems?: string[];
popoverButtonTitle: string;
selectedItemLimit?: number;
uniqueItemNames: string[];
onChange: (skippedItems: string[]) => void;
}

// This component is mostly inspired by EUI's Data Grid Column Selector
// https://github.com/elastic/eui/blob/main/src/components/datagrid/controls/column_selector.tsx
export const FieldFilterPopover: FC<FieldFilterPopoverProps> = ({
export const ItemFilterPopover: FC<ItemFilterPopoverProps> = ({
dataTestSubj,
disabled,
disabledApplyButton,
uniqueFieldNames,
disabledApplyTooltipContent,
helpText,
itemSearchAriaLabel,
initialSkippedItems = [],
popoverButtonTitle,
selectedItemLimit = 2,
uniqueItemNames,
onChange,
}) => {
const euiThemeContext = useEuiTheme();
// Inspired by https://github.com/elastic/eui/blob/main/src/components/datagrid/controls/_data_grid_column_selector.scss
const fieldSelectPopover = useMemo(
const itemSelectPopover = useMemo(
() => css`
${euiYScrollWithShadows(euiThemeContext, {})}
max-height: 400px;
Expand All @@ -55,97 +69,90 @@ export const FieldFilterPopover: FC<FieldFilterPopoverProps> = ({
);

const [isTouched, setIsTouched] = useState(false);
const [fieldSearchText, setFieldSearchText] = useState('');
const [skippedFields, setSkippedFields] = useState<string[]>([]);
const setFieldsFilter = (fieldNames: string[], checked: boolean) => {
let updatedSkippedFields = [...skippedFields];
const [itemSearchText, setItemSearchText] = useState('');
const [skippedItems, setSkippedItems] = useState<string[]>(initialSkippedItems);
const setItemsFilter = (itemNames: string[], checked: boolean) => {
let updatedSkippedItems = [...skippedItems];
if (!checked) {
updatedSkippedFields.push(...fieldNames);
updatedSkippedItems.push(...itemNames);
} else {
updatedSkippedFields = skippedFields.filter((d) => !fieldNames.includes(d));
updatedSkippedItems = skippedItems.filter((d) => !itemNames.includes(d));
}
setSkippedFields(updatedSkippedFields);
// Ensure there are no duplicates
setSkippedItems([...new Set(updatedSkippedItems)]);
setIsTouched(true);
};

const [isFieldSelectionPopoverOpen, setIsFieldSelectionPopoverOpen] = useState(false);
const onFieldSelectionButtonClick = () => setIsFieldSelectionPopoverOpen((isOpen) => !isOpen);
const closePopover = () => setIsFieldSelectionPopoverOpen(false);
const [isItemSelectionPopoverOpen, setIsItemSelectionPopoverOpen] = useState(false);
const onItemSelectionButtonClick = () => setIsItemSelectionPopoverOpen((isOpen) => !isOpen);
const closePopover = () => setIsItemSelectionPopoverOpen(false);

const filteredUniqueFieldNames = useMemo(() => {
return uniqueFieldNames.filter(
(d) => d.toLowerCase().indexOf(fieldSearchText.toLowerCase()) !== -1
const filteredUniqueItemNames = useMemo(() => {
return uniqueItemNames.filter(
(d) => d.toLowerCase().indexOf(itemSearchText.toLowerCase()) !== -1
);
}, [fieldSearchText, uniqueFieldNames]);
}, [itemSearchText, uniqueItemNames]);

// If the supplied list of unique field names changes, do a sanity check to only
// keep field names in the list of skipped fields that still are in the list of unique fields.
useEffect(() => {
setSkippedFields((previousSkippedFields) =>
previousSkippedFields.filter((d) => uniqueFieldNames.includes(d))
setSkippedItems((previousSkippedItems) =>
previousSkippedItems.filter((d) => uniqueItemNames.includes(d))
);
}, [uniqueFieldNames]);
}, [uniqueItemNames]);

const selectedFieldCount = uniqueFieldNames.length - skippedFields.length;
const selectedItemCount = uniqueItemNames.length - skippedItems.length;

return (
<EuiPopover
data-test-subj="aiopsFieldFilterPopover"
anchorPosition="downLeft"
panelPaddingSize="s"
panelStyle={{ minWidth: '20%' }}
button={
<EuiButton
data-test-subj="aiopsFieldFilterButton"
onClick={onFieldSelectionButtonClick}
data-test-subj={dataTestSubj}
onClick={onItemSelectionButtonClick}
disabled={disabled}
size="s"
iconType="arrowDown"
iconSide="right"
iconSize="s"
color="text"
>
<FormattedMessage
id="xpack.aiops.logRateAnalysis.page.fieldFilterButtonLabel"
defaultMessage="Filter fields"
/>
{popoverButtonTitle}
</EuiButton>
}
isOpen={isFieldSelectionPopoverOpen}
isOpen={isItemSelectionPopoverOpen}
closePopover={closePopover}
>
<EuiPopoverTitle>
<EuiText size="xs" color="subdued" style={{ maxWidth: '400px' }}>
<FormattedMessage
id="xpack.aiops.logRateAnalysis.page.fieldFilterHelpText"
defaultMessage="Deselect non-relevant fields to remove them from groups and click the Apply button to rerun the grouping. Use the search bar to filter the list, then select/deselect multiple fields with the actions below."
/>
{helpText}
</EuiText>
<EuiSpacer size="s" />
<EuiFieldText
compressed
placeholder={i18n.translate('xpack.aiops.analysis.fieldSelectorPlaceholder', {
defaultMessage: 'Search',
})}
aria-label={i18n.translate('xpack.aiops.analysis.fieldSelectorAriaLabel', {
defaultMessage: 'Filter fields',
})}
value={fieldSearchText}
onChange={(e: ChangeEvent<HTMLInputElement>) => setFieldSearchText(e.currentTarget.value)}
aria-label={itemSearchAriaLabel}
value={itemSearchText}
onChange={(e: ChangeEvent<HTMLInputElement>) => setItemSearchText(e.currentTarget.value)}
data-test-subj="aiopsFieldSelectorSearch"
/>
</EuiPopoverTitle>
<div css={fieldSelectPopover} data-test-subj="aiopsFieldSelectorFieldNameList">
{filteredUniqueFieldNames.map((fieldName) => (
<div css={itemSelectPopover} data-test-subj="aiopsFieldSelectorFieldNameList">
{filteredUniqueItemNames.map((fieldName) => (
<div key={fieldName} css={{ padding: '4px' }}>
<EuiSwitch
data-test-subj={`aiopsFieldSelectorFieldNameListItem${
!skippedFields.includes(fieldName) ? ' checked' : ''
!skippedItems.includes(fieldName) ? ' checked' : ''
}`}
className="euiSwitch--mini"
compressed
label={fieldName}
onChange={(e) => setFieldsFilter([fieldName], e.target.checked)}
checked={!skippedFields.includes(fieldName)}
onChange={(e) => setItemsFilter([fieldName], e.target.checked)}
checked={!skippedItems.includes(fieldName)}
/>
</div>
))}
Expand All @@ -162,19 +169,19 @@ export const FieldFilterPopover: FC<FieldFilterPopoverProps> = ({
<EuiButtonEmpty
size="xs"
flush="left"
onClick={() => setFieldsFilter(filteredUniqueFieldNames, true)}
disabled={fieldSearchText.length > 0 && filteredUniqueFieldNames.length === 0}
onClick={() => setItemsFilter(filteredUniqueItemNames, true)}
disabled={itemSearchText.length > 0 && filteredUniqueItemNames.length === 0}
data-test-subj="aiopsFieldSelectorSelectAllFieldsButton"
>
{fieldSearchText.length > 0 ? (
{itemSearchText.length > 0 ? (
<FormattedMessage
id="xpack.aiops.logRateAnalysis.page.fieldSelector.selectAllSearchedFields"
defaultMessage="Select filtered fields"
id="xpack.aiops.logRateAnalysis.page.fieldSelector.selectAllSearchedItems"
defaultMessage="Select filtered"
/>
) : (
<FormattedMessage
id="xpack.aiops.logRateAnalysis.page.fieldSelector.selectAllFields"
defaultMessage="Select all fields"
id="xpack.aiops.logRateAnalysis.page.fieldSelector.selectAllItems"
defaultMessage="Select all"
/>
)}
</EuiButtonEmpty>
Expand All @@ -183,39 +190,35 @@ export const FieldFilterPopover: FC<FieldFilterPopoverProps> = ({
<EuiButtonEmpty
size="xs"
flush="right"
onClick={() => setFieldsFilter(filteredUniqueFieldNames, false)}
disabled={fieldSearchText.length > 0 && filteredUniqueFieldNames.length === 0}
onClick={() => setItemsFilter(filteredUniqueItemNames, false)}
disabled={itemSearchText.length > 0 && filteredUniqueItemNames.length === 0}
data-test-subj="aiopsFieldSelectorDeselectAllFieldsButton"
>
{fieldSearchText.length > 0 ? (
{itemSearchText.length > 0 ? (
<FormattedMessage
id="xpack.aiops.logRateAnalysis.page.fieldSelector.deselectAllSearchedFields"
defaultMessage="Deselect filtered fields"
id="xpack.aiops.logRateAnalysis.page.fieldSelector.deselectAllSearchedItems"
defaultMessage="Deselect filtered"
/>
) : (
<FormattedMessage
id="xpack.aiops.logRateAnalysis.page.fieldSelector.deselectAllFields"
defaultMessage="Deselect all fields"
id="xpack.aiops.logRateAnalysis.page.fieldSelector.deselectAllItems"
defaultMessage="Deselect all"
/>
)}
</EuiButtonEmpty>
</EuiFlexItem>
</>
<EuiFlexItem grow={false}>
<FieldFilterApplyButton
<ItemFilterApplyButton
onClick={() => {
onChange(skippedFields);
setFieldSearchText('');
setIsFieldSelectionPopoverOpen(false);
onChange(skippedItems);
setItemSearchText('');
setIsItemSelectionPopoverOpen(false);
closePopover();
}}
disabled={disabledApplyButton || selectedFieldCount < 2 || !isTouched}
disabled={disabledApplyButton || selectedItemCount < selectedItemLimit || !isTouched}
tooltipContent={
selectedFieldCount < 2
? i18n.translate('xpack.aiops.analysis.fieldSelectorNotEnoughFieldsSelected', {
defaultMessage: 'Grouping requires at least 2 fields to be selected.',
})
: undefined
selectedItemCount < selectedItemLimit ? disabledApplyTooltipContent : undefined
}
/>
</EuiFlexItem>
Expand Down
Loading