forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request elastic#22 from Elastic-AWP-Platform/refactor-use-…
…process-tree Refactor use process tree and unit tests
- Loading branch information
Showing
8 changed files
with
316 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
x-pack/plugins/session_view/public/components/ProcessTree/helpers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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 { | ||
mockEvents, | ||
mockProcessMap, | ||
} from '../../../common/mocks/constants/session_view_process.mock'; | ||
import { Process, ProcessMap } from '../../../common/types/process_tree'; | ||
import { | ||
updateProcessMap, | ||
buildProcessTree, | ||
searchProcessTree, | ||
autoExpandProcessTree, | ||
} from './helpers'; | ||
|
||
const SESSION_ENTITY_ID = '3d0192c6-7c54-5ee6-a110-3539a7cf42bc'; | ||
const SEARCH_QUERY = 'vi'; | ||
const SEARCH_RESULT_PROCESS_ID = '8e4daeb2-4a4e-56c4-980e-f0dcfdbc3727'; | ||
|
||
describe('process tree hook helpers tests', () => { | ||
let processMap: ProcessMap; | ||
|
||
beforeEach(() => { | ||
processMap = {}; | ||
}); | ||
|
||
it('updateProcessMap works', () => { | ||
processMap = updateProcessMap(processMap, mockEvents); | ||
|
||
// processes are added to processMap | ||
mockEvents.forEach((event) => { | ||
expect(processMap[event.process.entity_id]).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
it('buildProcessTree works', () => { | ||
processMap = mockProcessMap; | ||
const orphans: Process[] = []; | ||
processMap = buildProcessTree(processMap, mockEvents, orphans, SESSION_ENTITY_ID); | ||
|
||
const sessionLeaderChildrenIds = new Set( | ||
processMap[SESSION_ENTITY_ID].children.map((child) => child.id) | ||
); | ||
|
||
// processes are added under their parent's childrean array in processMap | ||
mockEvents.forEach((event) => { | ||
expect(sessionLeaderChildrenIds.has(event.process.entity_id)); | ||
}); | ||
}); | ||
|
||
it('searchProcessTree works', () => { | ||
const searchResults = searchProcessTree(mockProcessMap, SEARCH_QUERY); | ||
|
||
// search returns the process with search query in its event args | ||
expect(searchResults[0].id).toBe(SEARCH_RESULT_PROCESS_ID); | ||
}); | ||
|
||
it('autoExpandProcessTree works', () => { | ||
processMap = mockProcessMap; | ||
// mock what buildProcessTree does | ||
const childProcesses = Object.values(processMap).filter( | ||
(process) => process.id !== SESSION_ENTITY_ID | ||
); | ||
processMap[SESSION_ENTITY_ID].children = childProcesses; | ||
|
||
expect(processMap[SESSION_ENTITY_ID].autoExpand).toBeFalsy(); | ||
processMap = autoExpandProcessTree(processMap); | ||
// session leader should have autoExpand to be true | ||
expect(processMap[SESSION_ENTITY_ID].autoExpand).toBeTruthy(); | ||
}); | ||
}); |
122 changes: 122 additions & 0 deletions
122
x-pack/plugins/session_view/public/components/ProcessTree/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* 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 { Process, ProcessEvent, ProcessMap } from '../../../common/types/process_tree'; | ||
import { ProcessImpl } from './hooks'; | ||
|
||
export const updateProcessMap = (processMap: ProcessMap, events: ProcessEvent[]) => { | ||
events.forEach((event) => { | ||
const { entity_id: id } = event.process; | ||
let process = processMap[id]; | ||
|
||
if (!process) { | ||
process = new ProcessImpl(id); | ||
processMap[id] = process; | ||
} | ||
|
||
process.events.push(event); | ||
}); | ||
|
||
return processMap; | ||
}; | ||
|
||
export const buildProcessTree = ( | ||
processMap: ProcessMap, | ||
events: ProcessEvent[], | ||
orphans: Process[], | ||
sessionEntityId: string, | ||
backwardDirection: boolean = false | ||
) => { | ||
events.forEach((event) => { | ||
const process = processMap[event.process.entity_id]; | ||
const parentProcess = processMap[event.process.parent?.entity_id]; | ||
|
||
if (parentProcess) { | ||
process.parent = parentProcess; // handy for recursive operations (like auto expand) | ||
|
||
if (!parentProcess.children.includes(process) && parentProcess.id !== process.id) { | ||
if (backwardDirection) { | ||
parentProcess.children.unshift(process); | ||
} else { | ||
parentProcess.children.push(process); | ||
} | ||
} | ||
} else if (process.id !== sessionEntityId && !orphans.includes(process)) { | ||
// if no parent process, process is probably orphaned | ||
orphans.push(process); | ||
} | ||
}); | ||
|
||
return processMap; | ||
}; | ||
|
||
export const searchProcessTree = (processMap: ProcessMap, searchQuery: string | undefined) => { | ||
const results = []; | ||
|
||
if (searchQuery) { | ||
for (const processId of Object.keys(processMap)) { | ||
const process = processMap[processId]; | ||
const event = process.getDetails(); | ||
const { working_directory: workingDirectory, args } = event.process; | ||
|
||
// TODO: the text we search is the same as what we render. | ||
// should this be customizable?? | ||
const text = `${workingDirectory} ${args.join(' ')}`; | ||
|
||
process.searchMatched = text.includes(searchQuery) ? searchQuery : null; | ||
|
||
if (process.searchMatched) { | ||
results.push(process); | ||
} | ||
} | ||
} else { | ||
for (const processId of Object.keys(processMap)) { | ||
processMap[processId].searchMatched = null; | ||
processMap[processId].autoExpand = false; | ||
} | ||
} | ||
|
||
return results; | ||
}; | ||
|
||
export const autoExpandProcessTree = (processMap: ProcessMap) => { | ||
for (const processId of Object.keys(processMap)) { | ||
const process = processMap[processId]; | ||
|
||
if (process.searchMatched || process.isUserEntered()) { | ||
let { parent } = process; | ||
|
||
while (parent && parent.id !== parent.parent?.id) { | ||
parent.autoExpand = true; | ||
parent = parent.parent; | ||
} | ||
} | ||
} | ||
|
||
return processMap; | ||
}; | ||
|
||
export const processNewEvents = ( | ||
eventsProcessMap: ProcessMap, | ||
events: ProcessEvent[] | undefined, | ||
orphans: Process[], | ||
sessionEntityId: string, | ||
backwardDirection: boolean = false | ||
) => { | ||
if (!events || events.length === 0) { | ||
return eventsProcessMap; | ||
} | ||
|
||
const updatedProcessMap = updateProcessMap(eventsProcessMap, events); | ||
const builtProcessMap = buildProcessTree( | ||
updatedProcessMap, | ||
events, | ||
orphans, | ||
sessionEntityId, | ||
backwardDirection | ||
); | ||
return autoExpandProcessTree(builtProcessMap); | ||
}; |
Oops, something went wrong.