Skip to content

Commit

Permalink
Use reduce to iterate over events in getDetail
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizhou Wang committed Nov 25, 2021
1 parent c125580 commit 7627ae8
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions x-pack/plugins/session_view/public/hooks/use_process_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 7627ae8

Please sign in to comment.