forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
122 lines (102 loc) · 3.41 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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);
};