-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
js-jankisalvi
merged 38 commits into
elastic:main
from
js-jankisalvi:dsl-filter-bug-fix
Oct 13, 2024
Merged
Changes from 30 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
d7670dd
initial commit
js-jankisalvi 1ad044a
transform filter to wrap in a query object
js-jankisalvi 6195669
add tests for search bar
js-jankisalvi 1ddb22a
added test for action alerts filter
js-jankisalvi 924859f
add value validators test
js-jankisalvi e779800
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi 670678b
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi 02ed103
fix multiple filters bug
js-jankisalvi aace640
use cloneDeep
js-jankisalvi fd03949
Merge remote-tracking branch 'upstream' into dsl-filter-bug-fix
js-jankisalvi 78e1ed9
Merge remote-tracking branch 'upstream' into dsl-filter-bug-fix
js-jankisalvi 82677da
resolve conflicts
js-jankisalvi aaaa7fd
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi 4a5d01e
functional test
js-jankisalvi 719a841
fix lint
js-jankisalvi eb0ebd8
update unified search bar's onFiltersUpdated
js-jankisalvi e276245
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi b3aa283
lint fix
js-jankisalvi d84431e
Merge branch 'dsl-filter-bug-fix' of https://github.com/js-jankisalvi…
js-jankisalvi 5046431
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine 27d014b
remove clonedeep
js-jankisalvi 4de4114
Merge branch 'dsl-filter-bug-fix' of https://github.com/js-jankisalvi…
js-jankisalvi 0cc7c9e
add tests to unified search bar
js-jankisalvi a99a408
clean up
js-jankisalvi 136040d
mock dataViews
js-jankisalvi f22c955
add check for shouldExecuteFilterManagerUpdate
js-jankisalvi 8c5edea
type fix
js-jankisalvi f348c2d
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi 8b8a096
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi 0041cf7
nitpick feedback addressed
js-jankisalvi 8f6d240
Merge remote-tracking branch 'upstream' into dsl-filter-bug-fix
js-jankisalvi dd10186
resolve conflict
js-jankisalvi b663e2b
added suggestion
js-jankisalvi 411fcf6
Revert "added suggestion"
js-jankisalvi dbcbceb
revert to previous solution to set filters in filtersManager in onFil…
js-jankisalvi e134d0f
add comment to transform query
js-jankisalvi 9ca50df
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi 758856f
Merge branch 'main' into dsl-filter-bug-fix
js-jankisalvi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
170 changes: 170 additions & 0 deletions
170
packages/kbn-alerts-ui-shared/src/alerts_search_bar/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* 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, FilterStateStore } 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(() => { | ||
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: Filter[] = [ | ||
{ | ||
meta: { | ||
negate: false, | ||
alias: null, | ||
disabled: false, | ||
type: 'custom', | ||
key: 'query', | ||
}, | ||
query: { bool: { filter: [{ term: { 'kibana.alert.rule.consumer': 'stackAlerts' } }] } }, | ||
$state: { store: FilterStateStore.APP_STATE }, | ||
}, | ||
]; | ||
|
||
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thequery
param.In this ternary we are saying that if
filter.query
doesn't exist then usefilter.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 findThere was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 aquery
object.filter_query.bug.mov
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added