From 2e53b2ad56fde46409e16c0a194ff938adf09a5b Mon Sep 17 00:00:00 2001 From: Karl Godard Date: Wed, 23 Feb 2022 12:00:13 -0800 Subject: [PATCH] fix to cache bust issue on memoize functions. we were mutating the array, so the parameters ref wouldn't change when the array grew. also fixed isUserEntered calculation, and another inline function (#66) Co-authored-by: mitodrummer --- .../common/types/process_tree/index.ts | 1 + .../public/components/process_tree/helpers.ts | 2 +- .../components/process_tree/hooks.test.tsx | 29 +++++++++++++++++++ .../public/components/process_tree/hooks.ts | 14 +++++++-- .../public/components/process_tree/index.tsx | 2 +- 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 x-pack/plugins/session_view/public/components/process_tree/hooks.test.tsx diff --git a/x-pack/plugins/session_view/common/types/process_tree/index.ts b/x-pack/plugins/session_view/common/types/process_tree/index.ts index effcf1bad0614..c212c7c759cdd 100644 --- a/x-pack/plugins/session_view/common/types/process_tree/index.ts +++ b/x-pack/plugins/session_view/common/types/process_tree/index.ts @@ -152,6 +152,7 @@ export interface Process { parent: Process | undefined; autoExpand: boolean; searchMatched: string | null; // either false, or set to searchQuery + addEvent(event: ProcessEvent): void; hasOutput(): boolean; hasAlerts(): boolean; getAlerts(): ProcessEvent[]; diff --git a/x-pack/plugins/session_view/public/components/process_tree/helpers.ts b/x-pack/plugins/session_view/public/components/process_tree/helpers.ts index af670a0928588..97a1028190386 100644 --- a/x-pack/plugins/session_view/public/components/process_tree/helpers.ts +++ b/x-pack/plugins/session_view/public/components/process_tree/helpers.ts @@ -17,7 +17,7 @@ export const updateProcessMap = (processMap: ProcessMap, events: ProcessEvent[]) processMap[id] = process; } - process.events.push(event); + process.addEvent(event); }); return processMap; diff --git a/x-pack/plugins/session_view/public/components/process_tree/hooks.test.tsx b/x-pack/plugins/session_view/public/components/process_tree/hooks.test.tsx new file mode 100644 index 0000000000000..9cece96fe8467 --- /dev/null +++ b/x-pack/plugins/session_view/public/components/process_tree/hooks.test.tsx @@ -0,0 +1,29 @@ +/* + * 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 { EventAction } from '../../../common/types/process_tree'; +import { mockEvents } from '../../../common/mocks/constants/session_view_process.mock'; +import { ProcessImpl } from './hooks'; + +describe('ProcessTree hooks', () => { + describe('ProcessImpl.getDetails memoize will cache bust on new events', () => { + it('should return the exec event details when this.events changes', () => { + const process = new ProcessImpl(mockEvents[0].process.entity_id); + + process.addEvent(mockEvents[0]); + + let result = process.getDetails(); + + // push exec event + process.addEvent(mockEvents[1]); + + result = process.getDetails(); + + expect(result.event.action).toEqual(EventAction.exec); + }); + }); +}); diff --git a/x-pack/plugins/session_view/public/components/process_tree/hooks.ts b/x-pack/plugins/session_view/public/components/process_tree/hooks.ts index 9fea7c5ac9785..03873a5cebd54 100644 --- a/x-pack/plugins/session_view/public/components/process_tree/hooks.ts +++ b/x-pack/plugins/session_view/public/components/process_tree/hooks.ts @@ -43,6 +43,12 @@ export class ProcessImpl implements Process { this.searchMatched = null; } + addEvent(event: ProcessEvent) { + // rather than push new events on the array, we return a new one + // this helps the below memoizeOne functions to behave correctly. + this.events = this.events.concat(event); + } + // hideSameGroup will filter out any processes which have the same pgid as this process getChildren(hideSameGroup: boolean = false) { let children = this.children; @@ -75,7 +81,7 @@ export class ProcessImpl implements Process { } getAlerts() { - return this.events.filter(({ event }) => event.kind === EventKind.signal); + return this.filterEventsByKind(this.events, EventKind.signal); } hasExec() { @@ -99,7 +105,7 @@ export class ProcessImpl implements Process { const event = this.getDetails(); const { tty } = event.process; - return !!tty && process.pid !== event.process.group_leader.pid; + return !!tty && process.pid === event.process.group_leader.pid; } getMaxAlertLevel() { @@ -119,6 +125,10 @@ export class ProcessImpl implements Process { return events.filter(({ event }) => event.action === action); }); + filterEventsByKind = memoizeOne((events: ProcessEvent[], kind: EventKind) => { + return events.filter(({ event }) => event.kind === kind); + }); + getDetailsMemo = memoizeOne((events: ProcessEvent[]) => { const eventsPartition = events.reduce( (currEventsParition, processEvent) => { diff --git a/x-pack/plugins/session_view/public/components/process_tree/index.tsx b/x-pack/plugins/session_view/public/components/process_tree/index.tsx index 44f34cea6cba6..50319f164f006 100644 --- a/x-pack/plugins/session_view/public/components/process_tree/index.tsx +++ b/x-pack/plugins/session_view/public/components/process_tree/index.tsx @@ -148,7 +148,7 @@ export const ProcessTree = ({ function renderLoadMoreButton(text: JSX.Element, func: FetchFunction) { return ( - func()} isLoading={isFetching}> + {text} );