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

[ResponseOps][Alerts] Fix DSL filter issues in search bar #193623

Merged
merged 38 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d7670dd
initial commit
js-jankisalvi Sep 20, 2024
1ad044a
transform filter to wrap in a query object
js-jankisalvi Sep 24, 2024
6195669
add tests for search bar
js-jankisalvi Sep 24, 2024
1ddb22a
added test for action alerts filter
js-jankisalvi Sep 25, 2024
924859f
add value validators test
js-jankisalvi Sep 25, 2024
e779800
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi Sep 25, 2024
670678b
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi Sep 25, 2024
02ed103
fix multiple filters bug
js-jankisalvi Sep 26, 2024
aace640
use cloneDeep
js-jankisalvi Sep 26, 2024
fd03949
Merge remote-tracking branch 'upstream' into dsl-filter-bug-fix
js-jankisalvi Sep 27, 2024
78e1ed9
Merge remote-tracking branch 'upstream' into dsl-filter-bug-fix
js-jankisalvi Sep 27, 2024
82677da
resolve conflicts
js-jankisalvi Sep 27, 2024
aaaa7fd
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi Sep 30, 2024
4a5d01e
functional test
js-jankisalvi Sep 30, 2024
719a841
fix lint
js-jankisalvi Sep 30, 2024
eb0ebd8
update unified search bar's onFiltersUpdated
js-jankisalvi Oct 3, 2024
e276245
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi Oct 3, 2024
b3aa283
lint fix
js-jankisalvi Oct 3, 2024
d84431e
Merge branch 'dsl-filter-bug-fix' of https://github.com/js-jankisalvi…
js-jankisalvi Oct 3, 2024
5046431
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Oct 3, 2024
27d014b
remove clonedeep
js-jankisalvi Oct 3, 2024
4de4114
Merge branch 'dsl-filter-bug-fix' of https://github.com/js-jankisalvi…
js-jankisalvi Oct 3, 2024
0cc7c9e
add tests to unified search bar
js-jankisalvi Oct 4, 2024
a99a408
clean up
js-jankisalvi Oct 4, 2024
136040d
mock dataViews
js-jankisalvi Oct 4, 2024
f22c955
add check for shouldExecuteFilterManagerUpdate
js-jankisalvi Oct 4, 2024
8c5edea
type fix
js-jankisalvi Oct 4, 2024
f348c2d
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi Oct 7, 2024
8b8a096
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi Oct 8, 2024
0041cf7
nitpick feedback addressed
js-jankisalvi Oct 8, 2024
8f6d240
Merge remote-tracking branch 'upstream' into dsl-filter-bug-fix
js-jankisalvi Oct 8, 2024
dd10186
resolve conflict
js-jankisalvi Oct 8, 2024
b663e2b
added suggestion
js-jankisalvi Oct 8, 2024
411fcf6
Revert "added suggestion"
js-jankisalvi Oct 10, 2024
dbcbceb
revert to previous solution to set filters in filtersManager in onFil…
js-jankisalvi Oct 10, 2024
e134d0f
add comment to transform query
js-jankisalvi Oct 10, 2024
9ca50df
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi Oct 11, 2024
758856f
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi Oct 13, 2024
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
172 changes: 172 additions & 0 deletions packages/kbn-alerts-ui-shared/src/alerts_search_bar/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import React from 'react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
import { Filter } from '@kbn/es-query';
import { ToastsStart } from '@kbn/core-notifications-browser';
import { useLoadRuleTypesQuery, useRuleAADFields, useAlertsDataView } from '../common/hooks';
import { AlertsSearchBar } from '.';
import { HttpStart } from '@kbn/core-http-browser';

const mockDataPlugin = dataPluginMock.createStartContract();
jest.mock('@kbn/kibana-utils-plugin/public');
jest.mock('../common/hooks');

jest.mocked(useAlertsDataView).mockReturnValue({
isLoading: false,
dataView: {
title: '.alerts-*',
fields: [
{
name: 'event.action',
type: 'string',
aggregatable: true,
searchable: true,
},
],
},
});

jest.mocked(useLoadRuleTypesQuery).mockReturnValue({
ruleTypesState: {
isInitialLoad: false,
data: new Map(),
isLoading: false,
error: null,
},
authorizedToReadAnyRules: false,
hasAnyAuthorizedRuleType: false,
authorizedRuleTypes: [],
authorizedToCreateAnyRules: false,
isSuccess: false,
});

jest.mocked(useRuleAADFields).mockReturnValue({
aadFields: [],
loading: false,
});

const unifiedSearchBarMock = jest.fn().mockImplementation((props) => (
<button
data-test-subj="querySubmitButton"
onClick={() => props.onQuerySubmit({ dateRange: { from: 'now', to: 'now' } })}
type="button"
>
{'Hello world'}
</button>
));

const toastsMock = { toasts: { addWarning: jest.fn() } } as unknown as ToastsStart;
const httpMock = {
post: jest.fn(),
} as unknown as HttpStart;

describe('AlertsSearchBar', () => {
beforeEach(() => {});
js-jankisalvi marked this conversation as resolved.
Show resolved Hide resolved

beforeEach(() => {
jest.clearAllMocks();
});

it('renders correctly', async () => {
render(
<AlertsSearchBar
rangeFrom="now/d"
rangeTo="now/d"
query=""
onQuerySubmit={jest.fn()}
onFiltersUpdated={jest.fn()}
appName={'test'}
featureIds={['observability', 'stackAlerts']}
unifiedSearchBar={unifiedSearchBarMock}
toasts={toastsMock}
http={httpMock}
dataViewsService={mockDataPlugin.dataViews}
/>
);
expect(await screen.findByTestId('querySubmitButton')).toBeInTheDocument();
});

it('calls onQuerySubmit correctly', async () => {
const onQuerySubmitMock = jest.fn();

render(
<AlertsSearchBar
rangeFrom="now/d"
rangeTo="now/d"
query=""
onQuerySubmit={onQuerySubmitMock}
onFiltersUpdated={jest.fn()}
unifiedSearchBar={unifiedSearchBarMock}
toasts={toastsMock}
http={httpMock}
dataViewsService={mockDataPlugin.dataViews}
appName={'test'}
featureIds={['observability', 'stackAlerts']}
/>
);

fireEvent.click(await screen.findByTestId('querySubmitButton'));

await waitFor(() => {
expect(onQuerySubmitMock).toHaveBeenCalled();
});
});

it('calls onFiltersUpdated correctly', async () => {
const onFiltersUpdatedMock = jest.fn();
const filters = [
{
meta: {
negate: false,
alias: null,
disabled: false,
type: 'custom',
key: 'query',
},
query: { bool: { filter: [{ term: { 'kibana.alert.rule.consumer': 'stackAlerts' } }] } },
$state: { store: 'appState' },
},
] as Filter[];
js-jankisalvi marked this conversation as resolved.
Show resolved Hide resolved

const newUnifiedSearchBarMock = jest.fn().mockImplementation((props) => (
<button
data-test-subj="filtersSubmitButton"
onClick={() => props.onFiltersUpdated(filters)}
type="button"
>
{'Hello world'}
</button>
));

render(
<AlertsSearchBar
rangeFrom="now/d"
rangeTo="now/d"
query=""
onQuerySubmit={jest.fn()}
onFiltersUpdated={onFiltersUpdatedMock}
unifiedSearchBar={newUnifiedSearchBarMock}
toasts={toastsMock}
http={httpMock}
dataViewsService={mockDataPlugin.dataViews}
appName={'test'}
featureIds={['observability', 'stackAlerts']}
/>
);

fireEvent.click(await screen.findByTestId('filtersSubmitButton'));

await waitFor(() => {
expect(onFiltersUpdatedMock).toHaveBeenCalledWith(filters);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const AlertsSearchBar = ({
submitOnBlur,
onQueryChange: onSearchQueryChange,
suggestionsAbstraction: isSecurity ? undefined : SA_ALERTS,
shouldExecuteFilterManagerUpdate: true,
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { httpServiceMock } from '@kbn/core/public/mocks';
import { RuleActionsAlertsFilter } from './rule_actions_alerts_filter';
import type { AlertsSearchBarProps } from '../../alerts_search_bar';
import { FilterStateStore } from '@kbn/es-query';
import { AlertsSearchBarProps, AlertsSearchBar } from '../../alerts_search_bar';
import { getAction } from '../../common/test_utils/actions_test_utils';
import userEvent from '@testing-library/user-event';
import { RuleActionsAlertsFilter } from './rule_actions_alerts_filter';

const http = httpServiceMock.createStartContract();

Expand All @@ -23,43 +23,7 @@ jest.mock('../hooks', () => ({
}));

jest.mock('../../alerts_search_bar', () => ({
AlertsSearchBar: ({ onFiltersUpdated, onQueryChange, onQuerySubmit }: AlertsSearchBarProps) => (
<div>
AlertsSearchBar
<button
onClick={() =>
onFiltersUpdated?.([
{
$state: { store: 'appState' as FilterStateStore },
meta: {},
},
])
}
>
Update Filter
</button>
<button
onClick={() =>
onQueryChange?.({
dateRange: { from: 'now', to: 'now' },
query: 'onQueryChange',
})
}
>
Update Query
</button>
<button
onClick={() =>
onQuerySubmit?.({
dateRange: { from: 'now', to: 'now' },
query: 'onQuerySubmit',
})
}
>
Submit Query
</button>
</div>
),
AlertsSearchBar: jest.fn(),
}));

const { useRuleFormState } = jest.requireMock('../hooks');
Expand Down Expand Up @@ -93,6 +57,46 @@ describe('ruleActionsAlertsFilter', () => {
});
});

(AlertsSearchBar as jest.Mock<any, any>).mockImplementation(
({ onFiltersUpdated, onQueryChange, onQuerySubmit }: AlertsSearchBarProps) => (
<div>
AlertsSearchBar
<button
onClick={() =>
onFiltersUpdated?.([
{
$state: { store: 'appState' as FilterStateStore },
js-jankisalvi marked this conversation as resolved.
Show resolved Hide resolved
meta: {},
},
])
}
>
Update Filter
</button>
<button
onClick={() =>
onQueryChange?.({
dateRange: { from: 'now', to: 'now' },
query: 'onQueryChange',
})
}
>
Update Query
</button>
<button
onClick={() =>
onQuerySubmit?.({
dateRange: { from: 'now', to: 'now' },
query: 'onQuerySubmit',
})
}
>
Submit Query
</button>
</div>
)
);

afterEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -166,18 +170,86 @@ describe('ruleActionsAlertsFilter', () => {

await userEvent.click(screen.getByText('Update Filter'));
expect(mockOnChange).toHaveBeenLastCalledWith({
filters: [{ $state: { store: 'appState' }, meta: {} }],
filters: [{ $state: { store: 'appState' }, meta: {}, query: {} }],
kql: 'test',
});
await userEvent.click(screen.getByText('Update Query'));
expect(mockOnChange).toHaveBeenLastCalledWith({
filters: [{ $state: { store: 'appState' }, meta: {} }],
filters: [{ $state: { store: 'appState' }, meta: {}, query: {} }],
kql: 'onQueryChange',
});
await userEvent.click(screen.getByText('Submit Query'));
expect(mockOnChange).toHaveBeenLastCalledWith({
filters: [{ $state: { store: 'appState' }, meta: {} }],
filters: [{ $state: { store: 'appState' }, meta: {}, query: {} }],
kql: 'onQuerySubmit',
});
});

test('renders filters correctly', async () => {
const filters = [
{
meta: {
negate: false,
alias: null,
disabled: false,
type: 'custom',
key: 'query',
},
query: { bool: { filter: [{ term: { 'kibana.alert.rule.consumer': 'stackAlerts' } }] } },
$state: { store: 'appState' as FilterStateStore },
js-jankisalvi marked this conversation as resolved.
Show resolved Hide resolved
},
];

(AlertsSearchBar as jest.Mock<any, any>).mockImplementation(
({ onFiltersUpdated, onQueryChange, onQuerySubmit }: AlertsSearchBarProps) => (
<div>
AlertsSearchBar
<button onClick={() => onFiltersUpdated?.(filters)}>Update Filter</button>
<button
onClick={() =>
onQueryChange?.({
dateRange: { from: 'now', to: 'now' },
query: 'onQueryChange',
})
}
>
Update Query
</button>
<button
onClick={() =>
onQuerySubmit?.({
dateRange: { from: 'now', to: 'now' },
query: 'onQuerySubmit',
})
}
>
Submit Query
</button>
</div>
)
);

render(
<RuleActionsAlertsFilter
action={getAction('1', {
alertsFilter: {
query: {
kql: '',
filters: [{ ...filters[0], meta: {} }],
},
},
})}
onChange={mockOnChange}
appName="stackAlerts"
featureIds={['stackAlerts']}
/>
);

await userEvent.click(screen.getByTestId('alertsFilterQueryToggle'));

expect(mockOnChange).toHaveBeenLastCalledWith(undefined);

await userEvent.click(screen.getByText('Update Filter'));
expect(mockOnChange).toHaveBeenLastCalledWith({ filters, kql: '' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,18 @@ export const RuleActionsAlertsFilter = ({
);

const onFiltersUpdated = useCallback(
(filters: Filter[]) => updateQuery({ filters }),
(filters: Filter[]) => {
const updatedFilters = filters.map((filter) => {
const { $state, meta, ...rest } = filter;
return {
$state,
meta,
query: filter?.query ? { ...filter.query } : { ...rest },
Copy link
Member

Choose a reason for hiding this comment

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

Can you please explain what you are trying to achieve here?
Filter type has only 3 properties: $state, meta, query?.
When deconstructing a filter the rest parameter should be just the query param.
In this ternary we are saying that if filter.query doesn't exist then use filter.query that doesn't exist.
So I'm not sure what we are updating here.
It looks like the Filter objects are "hiding" something more hidden from the current Typing. This is definitely not great and cause tech debt that is hard to find

Copy link
Member

Choose a reason for hiding this comment

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

On an subsequent check I see that the filter is currently constructed in the wrong way and in general it doesn't conform to its type: Filter

type Filter = {
  $state?: {
    store: FilterStateStore;
  };
  meta: FilterMeta;
  query?: Record<string, any>;
};

The parameter query is not populated with the custom DSL, but the custom DSL always pollutes the rest of the object. I see this as a wrong implementation (not yours but the current unified search implementation) that needs to be fixed to make it work correctly in all cases.
IMHO what we are trying to push here is a workaround of a bigger problem, than an actual fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The parameter query is not populated with the custom DSL, but the custom DSL always pollutes the rest of the object.

Exactly!
Above changes had to be done because when filters are updated via onFiltersUpdate, the DSL filter value is not wrapped in a query which throws validation error in the rule's action as it is expected to be a query object.

filter_query.bug.mov

IMHO what we are trying to push here is a workaround of a bigger problem, than an actual fix.

Agree, however as per my understanding not all plugins/ scenarios expects custom DSL value to be wrapped in a query object. Applying DSL filter to Alerts table works fine without query object.
Hence I fixed it wherever it was needed.
CC @cnasikas

Copy link
Member

Choose a reason for hiding this comment

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

if you don't mind I'd like to see what could happen if I change your code like that: 9c8b5a3
let the CI run and see of this change trigger any error in the use of the unified search. This definitely work with your use case, but I'd like to check if this causes problem in the surrounding

Copy link
Contributor Author

Choose a reason for hiding this comment

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

};
});

updateQuery({ filters: updatedFilters });
},
[updateQuery]
);

Expand Down
Loading