Skip to content

Commit

Permalink
[Security Selection][Detection engine] fixes rule preview performance…
Browse files Browse the repository at this point in the history
… issues (elastic#164207)

## Summary

On every single keyboard type on query input, the whole preview part was
re-rendering. If there are too many alerts, it could take ~150ms-200ms,
for its re-render. Ultimately, making form almost unusable
So, in this PR I'm adding memoization for 2 Preview components, to
resolve this issue


### Before

<img width="2529" alt="Screenshot 2023-08-17 at 17 13 07"
src="https://github.com/elastic/kibana/assets/92328789/c0b137e6-828b-4068-b061-85a5b5ca99f1">

### After

<img width="2523" alt="Screenshot 2023-08-17 at 17 07 56"
src="https://github.com/elastic/kibana/assets/92328789/06a0983c-d8e0-40c1-9b48-fd4bc106e922">

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
vitaliidm and kibanamachine authored Aug 22, 2023
1 parent 433d4f2 commit cd8a94b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { isEqual } from 'lodash';
import * as i18n from './translations';
import { usePreviewRoute } from './use_preview_route';
import { PreviewHistogram } from './preview_histogram';
import { PreviewLogsComponent } from './preview_logs';
import { PreviewLogs } from './preview_logs';
import { useKibana } from '../../../../common/lib/kibana';
import { LoadingHistogram } from './loading_histogram';
import { useStartTransaction } from '../../../../common/lib/apm/use_start_transaction';
Expand Down Expand Up @@ -274,7 +274,7 @@ const RulePreviewComponent: React.FC<RulePreviewProps> = ({
timeframeOptions={previewData.timeframeOptions}
/>
)}
<PreviewLogsComponent logs={logs} hasNoiseWarning={hasNoiseWarning} isAborted={isAborted} />
<PreviewLogs logs={logs} hasNoiseWarning={hasNoiseWarning} isAborted={isAborted} />
</>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ interface PreviewHistogramProps {

const DEFAULT_HISTOGRAM_HEIGHT = 300;

export const PreviewHistogram = ({
const PreviewHistogramComponent = ({
previewId,
addNoiseWarning,
spaceId,
Expand Down Expand Up @@ -262,3 +262,6 @@ export const PreviewHistogram = ({
</>
);
};

export const PreviewHistogram = React.memo(PreviewHistogramComponent);
PreviewHistogram.displayName = 'PreviewHistogram';
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EuiCallOut, EuiText, EuiSpacer, EuiAccordion } from '@elastic/eui';
import type { RulePreviewLogs } from '../../../../../common/api/detection_engine';
import * as i18n from './translations';

interface PreviewLogsComponentProps {
interface PreviewLogsProps {
logs: RulePreviewLogs[];
hasNoiseWarning: boolean;
isAborted: boolean;
Expand Down Expand Up @@ -42,11 +42,7 @@ const addLogs = (
allLogs: SortedLogs[]
) => (logs.length ? [{ startedAt, logs, duration }, ...allLogs] : allLogs);

export const PreviewLogsComponent: React.FC<PreviewLogsComponentProps> = ({
logs,
hasNoiseWarning,
isAborted,
}) => {
const PreviewLogsComponent: React.FC<PreviewLogsProps> = ({ logs, hasNoiseWarning, isAborted }) => {
const sortedLogs = useMemo(
() =>
logs.reduce<{
Expand All @@ -73,6 +69,9 @@ export const PreviewLogsComponent: React.FC<PreviewLogsComponentProps> = ({
);
};

export const PreviewLogs = React.memo(PreviewLogsComponent);
PreviewLogs.displayName = 'PreviewLogs';

const LogAccordion: React.FC<LogAccordionProps> = ({ logs, isError, children }) => {
const firstLog = logs[0];
if (!(children || firstLog)) return null;
Expand Down

0 comments on commit cd8a94b

Please sign in to comment.