-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElementAnalyzer.js
154 lines (128 loc) · 5.59 KB
/
ElementAnalyzer.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as Constants from '../Constants';
export class ElementAnalyzer {
static generateUniqueElementList(elements, uniqueElements = []) {
if (!Array.isArray(elements)) {
elements = [elements];
}
for (let element of elements) {
if (uniqueElements.indexOf(element.name) < 0) {
uniqueElements.push(element.name);
}
// recursion
if (element.children && element.children.length > 0) {
this.generateUniqueElementList(element.children, uniqueElements);
}
}
return uniqueElements;
}
static findSmallestAndBiggestMetricValueByMetricName(elements, metricName) {
if (typeof elements != 'object' || elements == null) {
throw new Error('elements is not an object or null!');
}
if (!Array.isArray(elements)) {
elements = [elements];
}
var min = Number.MAX_VALUE;
var max = Number.MIN_VALUE;
for (let element of elements) {
// investigate only FILEs, because only files can have different sizes and colors
if (element.type == Constants.ELEMENT_TYPE_FILE) {
var commit1Metrics = element.commit1Metrics || null;
var commit2Metrics = element.commit2Metrics || null;
var big = this.getMaxMetricValueByMetricName(commit1Metrics, commit2Metrics, metricName);
if (big > max) {
max = big;
}
var small = this.getMinMetricValueByMetricName(commit1Metrics, commit2Metrics, metricName);
if (small < min) {
min = small;
}
}
// recursion
if (element.children && element.children.length > 0) {
var result = this.findSmallestAndBiggestMetricValueByMetricName(element.children, metricName);
if (result.max > max) {
max = result.max;
}
if (result.min < min) {
min = result.min;
}
}
}
return {
min: min,
max: max
};
}
static getMinMetricValueByMetricName(commit1Metrics, commit2Metrics, metricName) {
if (commit1Metrics == null && commit2Metrics == null) {
throw new Error(`No metric objects given`);
}
if (commit1Metrics == null) {
return commit2Metrics[metricName];
} else if (commit2Metrics == null) {
return commit1Metrics[metricName];
} else {
return commit1Metrics[metricName] < commit2Metrics[metricName] ? commit1Metrics[metricName] : commit2Metrics[metricName];
}
}
static getMaxMetricValueByMetricName(commit1Metrics, commit2Metrics, metricName) {
if (commit1Metrics == null && commit2Metrics == null) {
throw new Error(`No metric objects given`);
}
if (commit1Metrics == null) {
return commit2Metrics[metricName];
} else if (commit2Metrics == null) {
return commit1Metrics[metricName];
} else {
return commit1Metrics[metricName] > commit2Metrics[metricName] ? commit1Metrics[metricName] : commit2Metrics[metricName];
}
}
static hasChildrenForCurrentCommit(element, isFullscreen, screenPosition) {
var found = false;
for (let child of element.children) {
if (this.hasMetricValuesForCurrentCommit(child, isFullscreen, screenPosition)) {
found = true;
}
// recursion
if (child.children && child.children.length > 0 && !found) {
found = this.hasChildrenForCurrentCommit(child, isFullscreen, screenPosition);
}
}
return found;
}
static hasMetricValuesForCurrentCommit(element, isFullscreen, screenPosition) {
// when in fullscreen mode, metrics for at least one commit should be present
if (isFullscreen) {
return element.commit1Metrics != null || element.commit2Metrics != null;
}
if (screenPosition == Constants.LEFT_SCREEN) {
return element.commit1Metrics != null;
} else if (screenPosition == Constants.RIGHT_SCREEN) {
return element.commit2Metrics != null;
} else {
throw new Error(`Unknown screen position ${screenPosition}!`);
}
}
static getMetricValueOfElementAndCommitType(element, metricName, commitType, screenPosition) {
if (screenPosition == Constants.LEFT_SCREEN) {
if (commitType == Constants.COMMIT_TYPE_CURRENT) {
return element.commit1Metrics ? element.commit1Metrics[metricName] : undefined;
} else if (commitType == Constants.COMMIT_TYPE_OTHER) {
return element.commit2Metrics ? element.commit2Metrics[metricName] : undefined;
} else {
throw new Error(`Unknown commitType ${commitType}!`);
}
} else if (screenPosition == Constants.RIGHT_SCREEN) {
if (commitType == Constants.COMMIT_TYPE_CURRENT) {
return element.commit2Metrics ? element.commit2Metrics[metricName] : undefined;
} else if (commitType == Constants.COMMIT_TYPE_OTHER) {
return element.commit1Metrics ? element.commit1Metrics[metricName] : undefined;
} else {
throw new Error(`Unknown commitType ${commitType}!`);
}
} else {
throw new Error(`Unknown screen position ${screenPosition}!`);
}
}
}