Skip to content

Commit

Permalink
fix to cache bust issue on memoize functions. we were mutating the ar…
Browse files Browse the repository at this point in the history
…ray, so the parameters ref wouldn't change when the array grew. also fixed isUserEntered calculation, and another inline function (elastic#66)

Co-authored-by: mitodrummer <karlgodard@elastic.co>
  • Loading branch information
mitodrummer and mitodrummer authored Feb 23, 2022
1 parent ed725ce commit 2e53b2a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const updateProcessMap = (processMap: ProcessMap, events: ProcessEvent[])
processMap[id] = process;
}

process.events.push(event);
process.addEvent(event);
});

return processMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const ProcessTree = ({

function renderLoadMoreButton(text: JSX.Element, func: FetchFunction) {
return (
<EuiButton fullWidth onClick={() => func()} isLoading={isFetching}>
<EuiButton fullWidth onClick={func} isLoading={isFetching}>
{text}
</EuiButton>
);
Expand Down

0 comments on commit 2e53b2a

Please sign in to comment.