-
Notifications
You must be signed in to change notification settings - Fork 336
/
Copy pathutils.ts
46 lines (45 loc) · 1.37 KB
/
utils.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
import { ISpanItem } from "./types";
export type SpanTreeNode<TSpan> = {
span: TSpan;
children: SpanTreeNode<TSpan>[];
};
/**
* Create a span tree from a list of spans
* @param spans
*/
export function createSpanTree<TSpan extends ISpanItem>(
spans: TSpan[]
): SpanTreeNode<TSpan>[] {
// A map of spanId to span tree node
const spanMap = spans.reduce((acc, span) => {
acc.set(span.context.spanId, {
span,
children: [],
});
return acc;
}, new Map<string, SpanTreeNode<TSpan>>());
const rootSpans: SpanTreeNode<TSpan>[] = [];
for (const span of spans) {
const spanTreeItem = spanMap.get(span.context.spanId);
if (span.parentId === null || !spanMap.has(span.parentId)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
rootSpans.push(spanTreeItem!);
} else {
const parentSpanNode = spanMap.get(span.parentId);
if (parentSpanNode) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parentSpanNode.children.push(spanTreeItem!);
}
}
}
// We must sort the children of each span by their start time
// So that the children are in the correct order
for (const spanNode of spanMap.values()) {
spanNode.children.sort(
(a, b) =>
new Date(a.span.startTime).valueOf() -
new Date(b.span.startTime).valueOf()
);
}
return rootSpans;
}