@@ -60,40 +60,32 @@ function computeSelfTime(parentSpan: Span, allSpans: Span[]): number {
60
60
const childEndsBeforePreviousChild = childEndTime < previousChildEndTime ;
61
61
62
62
// parent |..................|
63
- // child |.......|
63
+ // child |.......| - previousChild
64
64
// child |.....| - childEndsBeforePreviousChild is true, skipped
65
65
// child |......| - childStartsAfterParentEnded is true, skipped
66
66
if ( childStartsAfterParentEnded || childEndsBeforePreviousChild ) {
67
67
continue ;
68
68
}
69
69
70
- let nonOverlappingStartTime = child . startTime ;
71
- let nonOverlappingDuration = child . duration ;
72
- const childStartsBeforePreviousChildEnds = child . startTime < previousChildEndTime ;
73
-
74
70
// parent |.....................|
75
- // child |.......|
76
- // child |.....| - childStartsBeforePreviousChildEnds is true
77
- // child |.....| - childStartsBeforePreviousChildEnds is false
78
- if ( childStartsBeforePreviousChildEnds ) {
79
- const diff = previousChildEndTime - child . startTime ;
80
- nonOverlappingDuration = child . duration - diff ;
81
- nonOverlappingStartTime = previousChildEndTime ;
82
- }
71
+ // child |.......| - previousChild
72
+ // child |.....| - nonOverlappingStartTime is previousChildEndTime
73
+ // child |.....| - nonOverlappingStartTime is child.startTime
74
+ const nonOverlappingStartTime = Math . max ( previousChildEndTime , child . startTime ) ;
75
+ const childEndTimeOrParentEndTime = Math . min ( parentSpanEndTime , childEndTime ) ;
76
+
77
+ const nonOverlappingDuration = childEndTimeOrParentEndTime - nonOverlappingStartTime ;
78
+ parentSpanSelfTime -= nonOverlappingDuration ;
79
+
83
80
// last span which can be included in self time calculation, because it ends after parent span ends
84
81
// parent |......................|
85
82
// child |.....| - last span included in self time calculation
86
83
// child |.........| - skipped
87
- else if ( childEndTime > parentSpanEndTime ) {
88
- const diff = childEndTime - parentSpanEndTime ;
89
-
90
- nonOverlappingDuration = child . duration - diff ;
91
- parentSpanSelfTime -= nonOverlappingDuration ;
84
+ if ( childEndTimeOrParentEndTime === parentSpanEndTime ) {
92
85
break ;
93
86
}
94
87
95
- parentSpanSelfTime -= nonOverlappingDuration ;
96
- previousChildEndTime = nonOverlappingStartTime + nonOverlappingDuration ;
88
+ previousChildEndTime = childEndTime ;
97
89
}
98
90
99
91
return parentSpanSelfTime ;
0 commit comments