From 978c93194fb7d38db492b9f8b33306fc977a4300 Mon Sep 17 00:00:00 2001 From: Zizhou Wang Date: Tue, 30 Nov 2021 18:24:26 -0500 Subject: [PATCH] Move ProcessImpl back to use_process_tree --- .../constants/session_view_process.mock.ts | 25 +++-- .../common/types/process_tree/index.ts | 83 ----------------- .../components/SessionViewPage/index.tsx | 2 +- .../public/hooks/use_process_tree.ts | 91 ++++++++++++++++++- 4 files changed, 103 insertions(+), 98 deletions(-) diff --git a/x-pack/plugins/session_view/common/mocks/constants/session_view_process.mock.ts b/x-pack/plugins/session_view/common/mocks/constants/session_view_process.mock.ts index 950f173722427..f99f9f1bfabbc 100644 --- a/x-pack/plugins/session_view/common/mocks/constants/session_view_process.mock.ts +++ b/x-pack/plugins/session_view/common/mocks/constants/session_view_process.mock.ts @@ -419,9 +419,9 @@ const mockAlerts = [ }, ]; -export const sessionViewBasicProcessMock: Process = { +const processMock: Process = { id: '3f44d854-fe8d-5666-abc9-9efe7f970b4b', - events: mockEvents, + events: [], children: [], autoExpand: false, searchMatched: null, @@ -429,26 +429,25 @@ export const sessionViewBasicProcessMock: Process = { 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, }; 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 52abfc2b238b2..18fc08feea18c 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 @@ -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; - } -} diff --git a/x-pack/plugins/session_view/public/components/SessionViewPage/index.tsx b/x-pack/plugins/session_view/public/components/SessionViewPage/index.tsx index 9479aa252ede8..eae4ffc20c390 100644 --- a/x-pack/plugins/session_view/public/components/SessionViewPage/index.tsx +++ b/x-pack/plugins/session_view/public/components/SessionViewPage/index.tsx @@ -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[]; 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 50327093c387e..becd2804d6252 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 @@ -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; @@ -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,