-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Detections][Alerts] fixes saved_query rule validation issue (#142602) (
#143008) ## Summary - addresses #141758 - loads saved query on rules edit page before form rendering, thus prevents validation run on uncompleted data(saved query wasn't fetched before QueryBar component renders). Then fetched saved query passed to QueryBarDefineRule component ### Before https://user-images.githubusercontent.com/92328789/193884969-3647e06b-085a-4923-82e8-546bedabeb3e.mp4 ### After https://user-images.githubusercontent.com/92328789/193884323-629fb879-3a51-4412-8b63-2f36afc618cb.mov As side effect, it reduced number of requests for saved query: #### Before 2 requests were sent <img width="1995" alt="Screenshot 2022-10-05 at 09 39 09" src="https://user-images.githubusercontent.com/92328789/194018652-5612a032-e908-4e01-9858-2049c8e453e2.png"> #### After Only one request sent ### Checklist <img width="1996" alt="Screenshot 2022-10-05 at 09 37 12" src="https://user-images.githubusercontent.com/92328789/194018637-770a08df-832b-4838-a060-05058c436ed6.png"> Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios (cherry picked from commit 4feb397) Co-authored-by: Vitalii Dmyterko <[email protected]>
- Loading branch information
1 parent
17968d4
commit 80caa15
Showing
10 changed files
with
122 additions
and
74 deletions.
There are no files selected for viewing
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
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
55 changes: 0 additions & 55 deletions
55
...ty_solution/public/detections/pages/detection_engine/rules/details/use_get_saved_query.ts
This file was deleted.
Oops, something went wrong.
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
73 changes: 73 additions & 0 deletions
73
...s/security_solution/public/detections/pages/detection_engine/rules/use_get_saved_query.ts
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,73 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useMemo } from 'react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import type { Type } from '@kbn/securitysolution-io-ts-alerting-types'; | ||
|
||
import { useSavedQueryServices } from '../../../../common/utils/saved_query_services'; | ||
import type { DefineStepRule } from './types'; | ||
|
||
import { useAppToasts } from '../../../../common/hooks/use_app_toasts'; | ||
|
||
import { SAVED_QUERY_LOAD_ERROR_TOAST } from './translations'; | ||
|
||
interface UseGetSavedQuerySettings { | ||
onError?: (e: unknown) => void; | ||
ruleType: Type | undefined; | ||
} | ||
|
||
export const useGetSavedQuery = ( | ||
savedQueryId: string | undefined, | ||
{ ruleType, onError }: UseGetSavedQuerySettings | ||
) => { | ||
const savedQueryServices = useSavedQueryServices(); | ||
const { addError } = useAppToasts(); | ||
|
||
const defaultErrorHandler = (e: unknown) => { | ||
addError(e, { title: SAVED_QUERY_LOAD_ERROR_TOAST }); | ||
}; | ||
|
||
const query = useQuery( | ||
['detectionEngine', 'rule', 'savedQuery', savedQueryId], | ||
async () => { | ||
if (!savedQueryId) { | ||
return null; | ||
} | ||
|
||
return savedQueryServices.getSavedQuery(savedQueryId); | ||
}, | ||
{ | ||
onError: onError ?? defaultErrorHandler, | ||
// load saved query only if rule type === 'saved_query', as other rule types still can have saved_id property that is not used | ||
// Rule schema allows to save any rule with saved_id property, but it only used for saved_query rule type | ||
// In future we might look in possibility to restrict rule schema (breaking change!) and remove saved_id from the rest of rules through migration | ||
enabled: Boolean(savedQueryId) && ruleType === 'saved_query', | ||
retry: false, | ||
} | ||
); | ||
|
||
const savedQueryBar = useMemo<DefineStepRule['queryBar'] | null>( | ||
() => | ||
query.data | ||
? { | ||
saved_id: query.data.id, | ||
filters: query.data.attributes.filters ?? [], | ||
query: query.data.attributes.query, | ||
title: query.data.attributes.title, | ||
} | ||
: null, | ||
[query.data] | ||
); | ||
|
||
return { | ||
isSavedQueryLoading: savedQueryId ? query.isLoading : false, | ||
savedQueryBar, | ||
savedQuery: query.data ?? undefined, | ||
}; | ||
}; |