Skip to content

Commit 7fe867b

Browse files
committed
Fix time based mode
1 parent 19bdf46 commit 7fe867b

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/bin/SpanTree.tsx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,45 @@ const sortSpans = (spans: Span[], mode: string): Span[] => {
1515
if (mode === "logical") {
1616
const spanMap = new Map<string, Span>();
1717

18-
// Step 1: Convert raw objects into Span instances
18+
// Step 1: Convert raw objects to Span instances
1919
spans.forEach(span => {
2020
if (!spanMap.has(span.spanId)) {
21-
const newSpan = Object.assign(new Span(span.name, span.parentSpanId), span);
21+
const newSpan = new Span(span.name, span.parentSpanId);
22+
Object.assign(newSpan, span);
2223
newSpan.children = [];
2324
spanMap.set(span.spanId, newSpan);
2425
}
2526
});
2627

2728
const rootSpans: Span[] = [];
2829

29-
// Step 2: Link children correctly and prevent duplicates
30+
// Step 2: Link children to parents
3031
spans.forEach(span => {
3132
if (span.parentSpanId && spanMap.has(span.parentSpanId)) {
32-
const parent = spanMap.get(span.parentSpanId);
33-
const child = spanMap.get(span.spanId);
34-
if (parent && child && !parent.children.includes(child)) {
35-
parent.children.push(child); // ✅ Only push unique children
36-
}
33+
spanMap.get(span.parentSpanId)!.children.push(spanMap.get(span.spanId)!);
3734
}
3835
});
3936

40-
// Step 3: Collect only true root spans
37+
// Step 3: Collect only true root spans (avoiding duplicates)
4138
spans.forEach(span => {
4239
if (!span.parentSpanId) {
4340
const rootSpan = spanMap.get(span.spanId);
44-
if (rootSpan && !rootSpans.includes(rootSpan)) {
41+
if (rootSpan) {
4542
rootSpans.push(rootSpan);
4643
}
4744
}
4845
});
4946

50-
return rootSpans; // ✅ Only root spans are returned, no duplicates
47+
return rootSpans;
5148
} else {
52-
// ✅ Sort spans by time for time-based mode
53-
return spans.sort((a, b) => a.startTime - b.startTime);
49+
// ✅ Time Mode: Flatten spans and sort only by `startTime`
50+
return spans
51+
.map(span => new Span(span.name, span.parentSpanId)) // Convert to Span instances
52+
.sort((a, b) => a.startTime - b.startTime); // Sort purely by start time
5453
}
5554
};
5655

5756

58-
5957
/**
6058
* **Recursive Renderer**
6159
* - Uses box-drawing characters (│ ├ └) for structured layout.

0 commit comments

Comments
 (0)