-
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.
[Security Solution] Deduplicate requests to ML (#153244)
**Resolves: #134826 ## Summary We've recently had a couple of SDHs related to the slow performance of Security Solution. The performance issues were related to slow responses from ML API. In this PR, I rewrite data-fetching hooks to React Query to leverage request deduplication and caching. That significantly reduces the number of outgoing HTTP requests to ML routes on page load. **Before** 9 HTTP requests to ML endpoints on initial page load, 5 of which are duplicates. ![Screenshot 2023-03-20 at 12 09 55](https://user-images.githubusercontent.com/1938181/226322831-b3f594c9-a08a-4c8a-acc9-247df8d4a028.png) **After** 4 HTTP requests to ML endpoints on initial page load, no duplicates. ![Screenshot 2023-03-20 at 11 59 33](https://user-images.githubusercontent.com/1938181/226322847-a69129a2-afcd-408d-911c-a7b767dc4fdb.png)
- Loading branch information
Showing
11 changed files
with
227 additions
and
119 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
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.