From c12558055616dd1c3ea9dd1c91940a0ecadadb3b Mon Sep 17 00:00:00 2001 From: Zizhou Wang Date: Thu, 25 Nov 2021 11:56:38 -0500 Subject: [PATCH 1/2] Check if same id when adding process under parent --- .../plugins/session_view/public/hooks/use_process_tree.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/session_view/public/hooks/use_process_tree.ts b/x-pack/plugins/session_view/public/hooks/use_process_tree.ts index 75e39f17078cd..40c245f774fd5 100644 --- a/x-pack/plugins/session_view/public/hooks/use_process_tree.ts +++ b/x-pack/plugins/session_view/public/hooks/use_process_tree.ts @@ -164,6 +164,12 @@ class ProcessImpl implements Process { ({ event }) => event.action === EventAction.exec || event.action === EventAction.fork ); + const execs = execsForks.filter(({ event }) => event.action === EventAction.exec); + + if (execs.length) { + return execs[execs.length - 1]; + } + if (execsForks.length === 0) { // eslint-disable-next-line no-debugger debugger; @@ -249,7 +255,7 @@ export const useProcessTree = ({ if (parentProcess) { process.parent = parentProcess; // handy for recursive operations (like auto expand) - if (!parentProcess.children.includes(process)) { + if (!parentProcess.children.includes(process) && parentProcess.id !== process.id) { if (backwardDirection) { parentProcess.children.unshift(process); } else { From 7627ae80cfe3974f0d22f71a2a377edaf3a1f86c Mon Sep 17 00:00:00 2001 From: Zizhou Wang Date: Thu, 25 Nov 2021 15:13:11 -0500 Subject: [PATCH 2/2] Use reduce to iterate over events in getDetail --- .../public/hooks/use_process_tree.ts | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/session_view/public/hooks/use_process_tree.ts b/x-pack/plugins/session_view/public/hooks/use_process_tree.ts index 40c245f774fd5..823c9d64c784a 100644 --- a/x-pack/plugins/session_view/public/hooks/use_process_tree.ts +++ b/x-pack/plugins/session_view/public/hooks/use_process_tree.ts @@ -26,6 +26,13 @@ export enum EventAction { output = 'output', } +interface EventActionPartition { + fork: ProcessEvent[]; + exec: ProcessEvent[]; + exit: ProcessEvent[]; + output: ProcessEvent[]; +} + interface User { id: string; name: string; @@ -160,22 +167,31 @@ class ProcessImpl implements Process { } getDetails() { - const execsForks = this.events.filter( - ({ event }) => event.action === EventAction.exec || event.action === EventAction.fork + const eventsPartition = this.events.reduce( + (currEventsParition, processEvent) => { + currEventsParition[processEvent.event.action]?.push(processEvent); + return currEventsParition; + }, + Object.values(EventAction).reduce((currActions, action) => { + currActions[action] = [] as ProcessEvent[]; + return currActions; + }, {} as EventActionPartition) ); - const execs = execsForks.filter(({ event }) => event.action === EventAction.exec); + if (!(eventsPartition.exec.length || eventsPartition.fork.length)) { + // eslint-disable-next-line no-debugger + debugger; + } - if (execs.length) { - return execs[execs.length - 1]; + if (eventsPartition.exec.length) { + return eventsPartition.exec[eventsPartition.exec.length - 1]; } - if (execsForks.length === 0) { - // eslint-disable-next-line no-debugger - debugger; + if (eventsPartition.fork.length) { + return eventsPartition.fork[eventsPartition.fork.length - 1]; } - return execsForks[execsForks.length - 1]; + return {} as ProcessEvent; } getOutput() {