[8.17] [Performance][Security Solution][3/4] - Sourcerer performance (#212482) (#212985)

# Backport

This will backport the following commits from `main` to `8.17`:
- [[Performance][Security Solution][3/4] - Sourcerer performance
(#212482)](https://github.com/elastic/kibana/pull/212482)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Michael
Olorunnisola","email":"michael.olorunnisola@elastic.co"},"sourceCommit":{"committedDate":"2025-03-03T21:06:38Z","message":"[Performance][Security
Solution][3/4] - Sourcerer performance (#212482)\n\n## Summary\nPart 3
of https://github.com/elastic/kibana/pull/212173\n\n### Testing\nFor
setup see testing section
here:\nhttps://github.com/elastic/kibana/pull/212173#issue-2870522020\n\nTesting
this work is a bit more `console.log` related, as the primary\naim of
many of these changes was to limit the amount of re-renders\ncaused by
state changes within many of the sourcerer utils. React is\nrelatively
fast at handling many of these re-renders, but considering\nthe
widespread usage of `useSourcererDataView` and the top level\nlocation
of `useInitSourcerer`, it was worth taking care of some low\nhanging
fruit.\n\n### Background\n\nThe changes in this PR centered around
limiting the number of re-renders\nthat were being caused unnecessarily
in `useSourcererDataView` and\n`useInitSourcerer` by identifying some
quick wins.\n\nComments regarding the changes can be found in the PR
diffs\n\n\n### Checklist\n\nCheck the PR satisfies following conditions.
\n\nReviewers should verify this PR satisfies this list as well.\n\n-
[x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n### Identify
risks\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"98b52804d500f31f2fc80f47f478427a52f20630","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Threat
Hunting:Investigations","backport:all-open","v8.18.0","v9.1.0","v8.19.0"],"title":"[Performance][Security
Solution][3/4] - Sourcerer
performance","number":212482,"url":"https://github.com/elastic/kibana/pull/212482","mergeCommit":{"message":"[Performance][Security
Solution][3/4] - Sourcerer performance (#212482)\n\n## Summary\nPart 3
of https://github.com/elastic/kibana/pull/212173\n\n### Testing\nFor
setup see testing section
here:\nhttps://github.com/elastic/kibana/pull/212173#issue-2870522020\n\nTesting
this work is a bit more `console.log` related, as the primary\naim of
many of these changes was to limit the amount of re-renders\ncaused by
state changes within many of the sourcerer utils. React is\nrelatively
fast at handling many of these re-renders, but considering\nthe
widespread usage of `useSourcererDataView` and the top level\nlocation
of `useInitSourcerer`, it was worth taking care of some low\nhanging
fruit.\n\n### Background\n\nThe changes in this PR centered around
limiting the number of re-renders\nthat were being caused unnecessarily
in `useSourcererDataView` and\n`useInitSourcerer` by identifying some
quick wins.\n\nComments regarding the changes can be found in the PR
diffs\n\n\n### Checklist\n\nCheck the PR satisfies following conditions.
\n\nReviewers should verify this PR satisfies this list as well.\n\n-
[x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n### Identify
risks\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"98b52804d500f31f2fc80f47f478427a52f20630"}},"sourceBranch":"main","suggestedTargetBranches":["9.0","8.18","8.x"],"targetPullRequestStates":[{"branch":"9.0","label":"v9.0.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.18","label":"v8.18.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/212482","number":212482,"mergeCommit":{"message":"[Performance][Security
Solution][3/4] - Sourcerer performance (#212482)\n\n## Summary\nPart 3
of https://github.com/elastic/kibana/pull/212173\n\n### Testing\nFor
setup see testing section
here:\nhttps://github.com/elastic/kibana/pull/212173#issue-2870522020\n\nTesting
this work is a bit more `console.log` related, as the primary\naim of
many of these changes was to limit the amount of re-renders\ncaused by
state changes within many of the sourcerer utils. React is\nrelatively
fast at handling many of these re-renders, but considering\nthe
widespread usage of `useSourcererDataView` and the top level\nlocation
of `useInitSourcerer`, it was worth taking care of some low\nhanging
fruit.\n\n### Background\n\nThe changes in this PR centered around
limiting the number of re-renders\nthat were being caused unnecessarily
in `useSourcererDataView` and\n`useInitSourcerer` by identifying some
quick wins.\n\nComments regarding the changes can be found in the PR
diffs\n\n\n### Checklist\n\nCheck the PR satisfies following conditions.
\n\nReviewers should verify this PR satisfies this list as well.\n\n-
[x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common scenarios\n### Identify
risks\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"98b52804d500f31f2fc80f47f478427a52f20630"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Michael Olorunnisola <michael.olorunnisola@elastic.co>
This commit is contained in:
Kibana Machine 2025-03-04 10:08:17 +11:00 committed by GitHub
parent 3176d535c7
commit 298f53986a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 50 additions and 26 deletions

View file

@ -13,7 +13,6 @@ import { SecuritySolutionAppWrapper } from '../../common/components/page';
import { HelpMenu } from '../../common/components/help_menu';
import { getScopeFromPath } from '../../sourcerer/containers/sourcerer_paths';
import { useSourcererDataView } from '../../sourcerer/containers';
import { GlobalHeader } from './global_header';
import { ConsoleManager } from '../../management/components/console/components/console_manager';
@ -34,13 +33,11 @@ interface HomePageProps {
const HomePageComponent: React.FC<HomePageProps> = ({ children }) => {
const { pathname } = useLocation();
useInitSourcerer(getScopeFromPath(pathname));
const { browserFields } = useInitSourcerer(getScopeFromPath(pathname));
useUrlState();
useUpdateBrowserTitle();
useUpdateExecutionContext();
const { browserFields } = useSourcererDataView(getScopeFromPath(pathname));
// side effect: this will attempt to upgrade the endpoint package if it is not up to date
// this will run when a user navigates to the Security Solution app and when they navigate between
// tabs in the app. This is useful for keeping the endpoint package as up to date as possible until

View file

@ -7,7 +7,7 @@
import { noop } from 'lodash/fp';
import type { Dispatch } from 'react';
import React, { useEffect, useReducer, createContext, useContext } from 'react';
import React, { useEffect, useReducer, createContext, useContext, useMemo } from 'react';
import { useAlertsPrivileges } from '../../containers/detection_engine/alerts/use_alerts_privileges';
import { useSignalIndex } from '../../containers/detection_engine/alerts/use_signal_index';
@ -353,19 +353,38 @@ export const useUserInfo = (): State => {
signalIndexMappingOutdated,
]);
return {
loading,
isSignalIndexExists,
isAuthenticated,
hasEncryptionKey,
canUserCRUD,
canUserREAD,
hasIndexManage,
hasIndexMaintenance,
hasIndexWrite,
hasIndexRead,
hasIndexUpdateDelete,
signalIndexName,
signalIndexMappingOutdated,
};
const userInfo = useMemo(
() => ({
loading,
isSignalIndexExists,
isAuthenticated,
hasEncryptionKey,
canUserCRUD,
canUserREAD,
hasIndexManage,
hasIndexMaintenance,
hasIndexWrite,
hasIndexRead,
hasIndexUpdateDelete,
signalIndexName,
signalIndexMappingOutdated,
}),
[
canUserCRUD,
canUserREAD,
hasEncryptionKey,
hasIndexMaintenance,
hasIndexManage,
hasIndexRead,
hasIndexUpdateDelete,
hasIndexWrite,
isAuthenticated,
isSignalIndexExists,
loading,
signalIndexMappingOutdated,
signalIndexName,
]
);
return userInfo;
};

View file

@ -67,10 +67,11 @@ export const useSourcererDataView = (
if (selectedDataView == null || missingPatterns.length > 0) {
// old way of fetching indices, legacy timeline
setLegacyPatterns(selectedPatterns);
} else {
} else if (legacyPatterns.length > 0) {
// Only create a new array reference if legacyPatterns is not empty
setLegacyPatterns([]);
}
}, [missingPatterns, selectedDataView, selectedPatterns]);
}, [legacyPatterns.length, missingPatterns, selectedDataView, selectedPatterns]);
const sourcererDataView = useMemo(() => {
const _dv =

View file

@ -178,7 +178,7 @@ export const useInitSourcerer = (
!loadingSignalIndex &&
signalIndexName != null &&
signalIndexNameSourcerer == null &&
(activeTimeline == null || activeTimeline.savedObjectId == null) &&
activeTimeline?.savedObjectId == null &&
initialTimelineSourcerer.current &&
defaultDataView.id.length > 0
) {
@ -209,7 +209,7 @@ export const useInitSourcerer = (
);
} else if (
signalIndexNameSourcerer != null &&
(activeTimeline == null || activeTimeline.savedObjectId == null) &&
activeTimeline?.savedObjectId == null &&
initialTimelineSourcerer.current &&
defaultDataView.id.length > 0
) {
@ -240,14 +240,14 @@ export const useInitSourcerer = (
);
}
}, [
activeTimeline,
activeTimeline?.savedObjectId,
defaultDataView,
dispatch,
loadingSignalIndex,
signalIndexName,
signalIndexNameSourcerer,
]);
const { dataViewId } = useSourcererDataView(scopeId);
const { dataViewId, browserFields } = useSourcererDataView(scopeId);
const updateSourcererDataView = useCallback(
(newSignalsIndex: string) => {
@ -370,4 +370,11 @@ export const useInitSourcerer = (
signalIndexName,
signalIndexNameSourcerer,
]);
return useMemo(
() => ({
browserFields,
}),
[browserFields]
);
};