Skip to content

Commit

Permalink
Move ProcessImpl back to use_process_tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizhou Wang committed Nov 30, 2021
1 parent f250707 commit 978c931
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -419,36 +419,35 @@ const mockAlerts = [
},
];

export const sessionViewBasicProcessMock: Process = {
const processMock: Process = {
id: '3f44d854-fe8d-5666-abc9-9efe7f970b4b',
events: mockEvents,
events: [],
children: [],
autoExpand: false,
searchMatched: null,
parent: undefined,
hasOutput: () => false,
hasAlerts: () => false,
getAlerts: () => [],
hasExec: () => true,
hasExec: () => false,
getOutput: () => '',
getDetails: () => ({} as ProcessEvent),
isUserEntered: () => true,
isUserEntered: () => false,
getMaxAlertLevel: () => null,
};

export const sessionViewBasicProcessMock: Process = {
...processMock,
events: mockEvents,
hasExec: () => true,
isUserEntered: () => true,
};

export const sessionViewAlertProcessMock: Process = {
id: '3f44d854-fe8d-5666-abc9-9efe7f970b4b',
...processMock,
events: [...mockEvents, ...mockAlerts],
children: [],
autoExpand: false,
searchMatched: null,
parent: undefined,
hasOutput: () => false,
hasAlerts: () => true,
getAlerts: () => mockEvents,
hasExec: () => true,
getOutput: () => '',
getDetails: () => ({} as ProcessEvent),
isUserEntered: () => true,
getMaxAlertLevel: () => null,
};
83 changes: 0 additions & 83 deletions x-pack/plugins/session_view/common/types/process_tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,86 +125,3 @@ export interface Process {
isUserEntered(): boolean;
getMaxAlertLevel(): number | null;
}

export class ProcessImpl implements Process {
id: string;
events: ProcessEvent[];
children: Process[];
parent: Process | undefined;
autoExpand: boolean;
searchMatched: string | null;

constructor(id: string) {
this.id = id;
this.events = [];
this.children = [];
this.autoExpand = false;
this.searchMatched = null;
}

hasOutput() {
// TODO: schema undecided
return !!this.events.find(({ event }) => event.action === EventAction.output);
}

hasAlerts() {
return !!this.events.find(({ event }) => event.kind === EventKind.signal);
}

getAlerts() {
return this.events.filter(({ event }) => event.kind === EventKind.signal);
}

hasExec() {
return !!this.events.find(({ event }) => event.action === EventAction.exec);
}

hasExited() {
return !!this.events.find(({ event }) => event.action === EventAction.exit);
}

getDetails() {
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)
);

if (eventsPartition.exec.length) {
return eventsPartition.exec[eventsPartition.exec.length - 1];
}

if (eventsPartition.fork.length) {
return eventsPartition.fork[eventsPartition.fork.length - 1];
}

return {} as ProcessEvent;
}

getOutput() {
return this.events.reduce((output, event) => {
if (event.event.action === EventAction.output) {
output += ''; // TODO: schema unknown
}

return output;
}, '');
}

isUserEntered() {
const event = this.getDetails();
const { interactive, pgid, parent } = event?.process || {};

return interactive && pgid !== parent.pgid;
}

getMaxAlertLevel() {
// TODO:
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CoreStart } from '../../../../../../src/core/public';
import { RECENT_SESSION_ROUTE, BASE_PATH } from '../../../common/constants';

import { SessionView } from '../SessionView';
import { ProcessEvent } from '../../hooks/use_process_tree';
import { ProcessEvent } from '../../../common/types/process_tree';

interface RecentSessionResults {
hits: any[];
Expand Down
91 changes: 90 additions & 1 deletion x-pack/plugins/session_view/public/hooks/use_process_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*/
import _ from 'lodash';
import { useState, useEffect } from 'react';
import { EventKind, Process, ProcessImpl, ProcessEvent } from '../../common/types/process_tree';
import {
EventAction,
EventKind,
EventActionPartition,
Process,
ProcessEvent,
} from '../../common/types/process_tree';

interface UseProcessTreeDeps {
sessionEntityId: string;
Expand All @@ -19,6 +25,89 @@ type ProcessMap = {
[key: string]: Process;
};

class ProcessImpl implements Process {
id: string;
events: ProcessEvent[];
children: Process[];
parent: Process | undefined;
autoExpand: boolean;
searchMatched: string | null;

constructor(id: string) {
this.id = id;
this.events = [];
this.children = [];
this.autoExpand = false;
this.searchMatched = null;
}

hasOutput() {
// TODO: schema undecided
return !!this.events.find(({ event }) => event.action === EventAction.output);
}

hasAlerts() {
return !!this.events.find(({ event }) => event.kind === EventKind.signal);
}

getAlerts() {
return this.events.filter(({ event }) => event.kind === EventKind.signal);
}

hasExec() {
return !!this.events.find(({ event }) => event.action === EventAction.exec);
}

hasExited() {
return !!this.events.find(({ event }) => event.action === EventAction.exit);
}

getDetails() {
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)
);

if (eventsPartition.exec.length) {
return eventsPartition.exec[eventsPartition.exec.length - 1];
}

if (eventsPartition.fork.length) {
return eventsPartition.fork[eventsPartition.fork.length - 1];
}

return {} as ProcessEvent;
}

getOutput() {
return this.events.reduce((output, event) => {
if (event.event.action === EventAction.output) {
output += ''; // TODO: schema unknown
}

return output;
}, '');
}

isUserEntered() {
const event = this.getDetails();
const { interactive, pgid, parent } = event?.process || {};

return interactive && pgid !== parent.pgid;
}

getMaxAlertLevel() {
// TODO:
return null;
}
}

export const useProcessTree = ({
sessionEntityId,
forward,
Expand Down

0 comments on commit 978c931

Please sign in to comment.