-
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
[Security Solution] Deduplicate requests to ML #153244
Merged
Merged
Changes from all commits
Commits
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
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
42 changes: 42 additions & 0 deletions
42
...ugins/security_solution/public/common/components/ml/hooks/use_fetch_jobs_summary_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,42 @@ | ||
/* | ||
* 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 type { MlSummaryJob } from '@kbn/ml-plugin/public'; | ||
import type { UseQueryOptions } from '@tanstack/react-query'; | ||
import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { useCallback } from 'react'; | ||
import type { GetJobsSummaryArgs } from '../api/get_jobs_summary'; | ||
import { getJobsSummary } from '../api/get_jobs_summary'; | ||
|
||
const ONE_MINUTE = 60000; | ||
export const GET_JOBS_SUMMARY_QUERY_KEY = ['POST', '/api/ml/jobs/jobs_summary']; | ||
|
||
export const useFetchJobsSummaryQuery = ( | ||
queryArgs: Omit<GetJobsSummaryArgs, 'signal'>, | ||
options?: UseQueryOptions<MlSummaryJob[]> | ||
) => { | ||
return useQuery<MlSummaryJob[]>( | ||
[GET_JOBS_SUMMARY_QUERY_KEY, queryArgs], | ||
async ({ signal }) => getJobsSummary({ signal, ...queryArgs }), | ||
{ | ||
refetchIntervalInBackground: false, | ||
staleTime: ONE_MINUTE * 5, | ||
retry: false, | ||
...options, | ||
} | ||
); | ||
}; | ||
|
||
export const useInvalidateFetchJobsSummaryQuery = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useCallback(() => { | ||
queryClient.invalidateQueries(GET_JOBS_SUMMARY_QUERY_KEY, { | ||
refetchType: 'active', | ||
}); | ||
}, [queryClient]); | ||
}; |
14 changes: 0 additions & 14 deletions
14
x-pack/plugins/security_solution/public/common/components/ml/hooks/use_get_jobs_summary.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
39 changes: 39 additions & 0 deletions
39
...ns/security_solution/public/common/components/ml_popover/hooks/use_fetch_modules_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,39 @@ | ||
/* | ||
* 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 type { UseQueryOptions } from '@tanstack/react-query'; | ||
import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { useCallback } from 'react'; | ||
import { getModules } from '../api'; | ||
import type { GetModulesProps, Module } from '../types'; | ||
|
||
const ONE_MINUTE = 60000; | ||
export const GET_MODULES_QUERY_KEY = ['GET', '/api/ml/modules/get_module/:moduleId']; | ||
|
||
export const useFetchModulesQuery = ( | ||
queryArgs: Omit<GetModulesProps, 'signal'>, | ||
options?: UseQueryOptions<Module[]> | ||
) => { | ||
return useQuery<Module[]>( | ||
[GET_MODULES_QUERY_KEY, queryArgs], | ||
async ({ signal }) => getModules({ signal, ...queryArgs }), | ||
{ | ||
refetchIntervalInBackground: false, | ||
staleTime: ONE_MINUTE * 5, | ||
retry: false, | ||
...options, | ||
} | ||
); | ||
}; | ||
|
||
export const useInvalidateFetchModulesQuery = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useCallback(() => { | ||
queryClient.invalidateQueries(GET_MODULES_QUERY_KEY, { refetchType: 'active' }); | ||
}, [queryClient]); | ||
}; |
39 changes: 39 additions & 0 deletions
39
...security_solution/public/common/components/ml_popover/hooks/use_fetch_recognizer_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,39 @@ | ||
/* | ||
* 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 type { UseQueryOptions } from '@tanstack/react-query'; | ||
import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { useCallback } from 'react'; | ||
import { checkRecognizer } from '../api'; | ||
import type { CheckRecognizerProps, RecognizerModule } from '../types'; | ||
|
||
const ONE_MINUTE = 60000; | ||
export const GET_RECOGNIZER_QUERY_KEY = ['GET', '/api/ml/modules/recognize/:indexPatterns']; | ||
|
||
export const useFetchRecognizerQuery = ( | ||
queryArgs: Omit<CheckRecognizerProps, 'signal'>, | ||
options?: UseQueryOptions<RecognizerModule[]> | ||
) => { | ||
return useQuery<RecognizerModule[]>( | ||
[GET_RECOGNIZER_QUERY_KEY, queryArgs], | ||
async ({ signal }) => checkRecognizer({ signal, ...queryArgs }), | ||
{ | ||
refetchIntervalInBackground: false, | ||
staleTime: ONE_MINUTE * 5, | ||
retry: false, | ||
...options, | ||
} | ||
); | ||
}; | ||
|
||
export const useInvalidateFetchRecognizerQuery = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useCallback(() => { | ||
queryClient.invalidateQueries(GET_RECOGNIZER_QUERY_KEY, { refetchType: 'active' }); | ||
}, [queryClient]); | ||
}; |
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.
Could you move job filter logic to inside
useMemo
and prevent unnecessary renders?