Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Resolver] aria-level and aria-flowto support enhancements #71777

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,57 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { uniquePidForProcess, uniqueParentPidForProcess } from '../process_event';
import { IndexedProcessTree, AdjacentProcessMap } from '../../types';
import { uniquePidForProcess, uniqueParentPidForProcess, datetime } from '../process_event';
import { IndexedProcessTree } from '../../types';
import { ResolverEvent } from '../../../../common/endpoint/types';
import { levelOrder as baseLevelOrder } from '../../lib/tree_sequencers';

/**
* Create a new IndexedProcessTree from an array of ProcessEvents
* Create a new IndexedProcessTree from an array of ProcessEvents.
* siblings will be ordered by timestamp
*/
export function factory(processes: ResolverEvent[]): IndexedProcessTree {
export function factory(
// Array of processes to index as a tree
processes: ResolverEvent[]
): IndexedProcessTree {
const idToChildren = new Map<string | undefined, ResolverEvent[]>();
jonathan-buttner marked this conversation as resolved.
Show resolved Hide resolved
const idToValue = new Map<string, ResolverEvent>();
const idToAdjacent = new Map<string, AdjacentProcessMap>();

function emptyAdjacencyMap(id: string): AdjacentProcessMap {
return {
self: id,
parent: null,
firstChild: null,
previousSibling: null,
nextSibling: null,
level: 1,
};
}

const roots: ResolverEvent[] = [];

for (const process of processes) {
const uniqueProcessPid = uniquePidForProcess(process);
idToValue.set(uniqueProcessPid, process);

const currentProcessAdjacencyMap: AdjacentProcessMap =
idToAdjacent.get(uniqueProcessPid) || emptyAdjacencyMap(uniqueProcessPid);
idToAdjacent.set(uniqueProcessPid, currentProcessAdjacencyMap);

const uniqueParentPid = uniqueParentPidForProcess(process);
const currentProcessSiblings = idToChildren.get(uniqueParentPid);

if (currentProcessSiblings) {
const previousProcessId = uniquePidForProcess(
currentProcessSiblings[currentProcessSiblings.length - 1]
);
currentProcessSiblings.push(process);
/**
* Update adjacency maps for current and previous entries
*/
idToAdjacent.get(previousProcessId)!.nextSibling = uniqueProcessPid;
currentProcessAdjacencyMap.previousSibling = previousProcessId;
if (uniqueParentPid) {
currentProcessAdjacencyMap.parent = uniqueParentPid;
}
} else {
if (uniqueParentPid) {
idToChildren.set(uniqueParentPid, [process]);
/**
* Get the parent's map, otherwise set an empty one
*/
const parentAdjacencyMap =
idToAdjacent.get(uniqueParentPid) ||
(idToAdjacent.set(uniqueParentPid, emptyAdjacencyMap(uniqueParentPid)),
idToAdjacent.get(uniqueParentPid))!;
// set firstChild for parent
parentAdjacencyMap.firstChild = uniqueProcessPid;
// set parent for current
currentProcessAdjacencyMap.parent = uniqueParentPid || null;
} else {
// In this case (no unique parent id), it must be a root
roots.push(process);
// if its defined and not ''
if (uniqueParentPid) {
let siblings = idToChildren.get(uniqueParentPid);
if (!siblings) {
siblings = [];
idToChildren.set(uniqueParentPid, siblings);
}
siblings.push(process);
}
}

/**
* Scan adjacency maps from the top down and assign levels
*/
function traverseLevels(currentProcessMap: AdjacentProcessMap, level: number = 1): void {
const nextLevel = level + 1;
if (currentProcessMap.nextSibling) {
traverseLevels(idToAdjacent.get(currentProcessMap.nextSibling)!, level);
}
if (currentProcessMap.firstChild) {
traverseLevels(idToAdjacent.get(currentProcessMap.firstChild)!, nextLevel);
}
currentProcessMap.level = level;
}
// sort the children of each node
for (const siblings of idToChildren.values()) {
siblings.sort(function (firstEvent, secondEvent) {
jonathan-buttner marked this conversation as resolved.
Show resolved Hide resolved
const first: number = datetime(firstEvent);
const second: number = datetime(secondEvent);

for (const treeRoot of roots) {
traverseLevels(idToAdjacent.get(uniquePidForProcess(treeRoot))!);
// if either value is NaN, compare them differently
if (isNaN(first)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why would these ever be NaN or is this a "should never happen" scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

datetime tries to parse a Date from @timestamp (or somewhere else for legacy events.)
It then returns the getTime from that Date. If a date is 'Invalid Date', then getTime will return NaN.

maybe i should check for NaN in the body of datetime and return null instead.

// treat NaN as 1 and other values as 0, causing NaNs to have the highest value
return 1 - (isNaN(second) ? 1 : 0);
} else {
return first - second;
}
});
}

return {
idToChildren,
idToProcess: idToValue,
idToAdjacent,
};
}

Expand All @@ -109,6 +67,13 @@ export function children(tree: IndexedProcessTree, process: ResolverEvent): Reso
return currentProcessSiblings === undefined ? [] : currentProcessSiblings;
}

/**
* Get the indexed process event for the ID
*/
export function processEvent(tree: IndexedProcessTree, entityID: string): ResolverEvent | null {
return tree.idToProcess.get(entityID) ?? null;
}

/**
* Returns the parent ProcessEvent, if any, for the passed in `childProcess`
*/
Expand All @@ -124,6 +89,31 @@ export function parent(
}
}

/**
* Returns the following sibling
*/
export function nextSibling(
tree: IndexedProcessTree,
sibling: ResolverEvent
): ResolverEvent | undefined {
const parentNode = parent(tree, sibling);
if (parentNode) {
// The siblings of `sibling` are the children of its parent.
const siblings = children(tree, parentNode);

// Find the sibling
const index = siblings.indexOf(sibling);

// if the sibling wasn't found, or if it was the last element in the array, return undefined
if (index === -1 || index === siblings.length - 1) {
return undefined;
}

// return the next sibling
return siblings[index + 1];
}
}

/**
* Number of processes in the tree
*/
Expand All @@ -138,7 +128,10 @@ export function root(tree: IndexedProcessTree) {
if (size(tree) === 0) {
return null;
}
// any node will do
let current: ResolverEvent = tree.idToProcess.values().next().value;

// iteratively swap current w/ its parent
while (parent(tree, current) !== undefined) {
current = parent(tree, current)!;
}
Expand Down
Loading