Skip to content

Commit 15b77b1

Browse files
committed
Refactored the changes
Signed-off-by: Maksim Gaponov <[email protected]>
1 parent 046da80 commit 15b77b1

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

packages/jaeger-ui/src/components/TracePage/TraceStatistics/tableValues.tsx

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import memoizeOne from 'memoize-one';
1516
import * as _ from 'lodash';
1617
import DRange from 'drange';
1718
import { Trace, Span } from '../../../types/trace';
@@ -21,23 +22,25 @@ import colorGenerator from '../../../utils/color-generator';
2122
const serviceName = 'Service Name';
2223
const operationName = 'Operation Name';
2324

24-
let parentChildOfMap: Record<string, Span[]>;
25+
function parentChildOfMap(allSpans: Span[]): Record<string, Span[]> {
26+
let parentChildOfMap = {};
27+
allSpans.forEach(s => {
28+
if (s.references) {
29+
// Filter for CHILD_OF we don't want to calculate FOLLOWS_FROM (prod-cons)
30+
const parentIDs = s.references.filter(r => r.refType === 'CHILD_OF').map(r => r.spanID);
31+
parentIDs.forEach((pID: string) => {
32+
parentChildOfMap[pID] = parentChildOfMap[pID] || [];
33+
parentChildOfMap[pID].push(s);
34+
});
35+
}
36+
});
37+
return parentChildOfMap;
38+
}
39+
40+
const memoizedParentChildOfMap = memoizeOne(parentChildOfMap);
2541

2642
function getChildOfSpans(parentID: string, allSpans: Span[]): Span[] {
27-
if (!parentChildOfMap) {
28-
parentChildOfMap = {};
29-
allSpans.forEach(s => {
30-
if (s.references) {
31-
// Filter for CHILD_OF we don't want to calculate FOLLOWS_FROM (prod-cons)
32-
const parentIDs = s.references.filter(r => r.refType === 'CHILD_OF').map(r => r.spanID);
33-
parentIDs.forEach((pID: string) => {
34-
parentChildOfMap[pID] = parentChildOfMap[pID] || [];
35-
parentChildOfMap[pID].push(s);
36-
});
37-
}
38-
});
39-
}
40-
return parentChildOfMap[parentID] || [];
43+
return memoizedParentChildOfMap(allSpans)[parentID] || [];
4144
}
4245

4346
function getChildOfDrange(parentID: string, allSpans: Span[]) {
@@ -50,10 +53,20 @@ function getChildOfDrange(parentID: string, allSpans: Span[]) {
5053
}
5154

5255
function computeSelfTime(span: Span, allSpans: Span[]): number {
53-
const cdr = new DRange(span.startTime, span.startTime + span.duration - 1).intersect(
56+
// We want to represent spans as half-open intervals like [startTime, startTime + duration).
57+
// This way the subtraction preserves the right boundaries. However, DRange treats all
58+
// intervals as inclusive. For example,
59+
// range(1, 10).subtract(4, 8) => range([1, 3], [9-10])
60+
// length=(3-1)+(10-9)=2+1=3
61+
// In other words, we took an interval of length=10-1=9 and subtracted length=8-4=4.
62+
// We should've ended up with length 9-4=5, but we got 3.
63+
// To work around that, we multiply start/end times by 10 and subtract one from the end.
64+
// So instead of [1-10] we get [10-99]. This makes the intervals work like half-open.
65+
if (!span.hasChildren) return span.duration;
66+
const spanRange = new DRange(span.startTime, span.startTime + span.duration - 1).subtract(
5467
getChildOfDrange(span.spanID, allSpans)
5568
);
56-
return span.duration - cdr.length;
69+
return spanRange.length;
5770
}
5871

5972
function computeColumnValues(trace: Trace, span: Span, allSpans: Span[], resultValue: any) {
@@ -107,7 +120,6 @@ function valueFirstDropdown(selectedTagKey: string, trace: Trace) {
107120
let color = '';
108121
let allDiffColumnValues = [];
109122
const allSpans = trace.spans;
110-
parentChildOfMap = null;
111123
// all possibilities that can be displayed
112124
if (selectedTagKey === serviceName) {
113125
const temp = _.chain(allSpans)
@@ -271,7 +283,6 @@ function buildDetail(
271283
isDetail: boolean,
272284
trace: Trace
273285
) {
274-
parentChildOfMap = null;
275286
const newColumnValues = [];
276287
for (let j = 0; j < diffNamesA.length; j++) {
277288
let color = '';
@@ -337,7 +348,6 @@ function buildDetail(
337348
function generateDetailRest(allColumnValues: ITableSpan[], selectedTagKeySecond: string, trace: Trace) {
338349
const allSpans = trace.spans;
339350
const newTable = [];
340-
parentChildOfMap = null;
341351
for (let i = 0; i < allColumnValues.length; i++) {
342352
newTable.push(allColumnValues[i]);
343353
if (!allColumnValues[i].isDetail) {

0 commit comments

Comments
 (0)