Skip to content

Commit 0691760

Browse files
authored
fix: Calculation of smaller values does not work #1661
Refactor calculation of metric percentage to also work with negative numbers Fix case of having same baseline and stretchgoal #1661 Add test for checking negative edge value #1661
1 parent 352aeb3 commit 0691760

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

frontend/cypress/e2e/check-in.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('okr check-in', () => {
156156
.setCheckInConfidence(5)
157157
.submit();
158158

159-
cy.contains('5%');
159+
cy.contains('Aktuell: 5%');
160160
cy.contains('!');
161161
cy.contains('5/10');
162162
cy.contains('Letztes Check-in (' + getCurrentDate() + ')');

frontend/cypress/e2e/scoring.cy.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ describe('okr scoring', () => {
4242
70,
4343
100,
4444
100
45+
],
46+
[
47+
0,
48+
-30,
49+
-70,
50+
-100,
51+
-70
52+
],
53+
[
54+
0,
55+
-30,
56+
-70,
57+
-100,
58+
-29.9
4559
]
4660
].forEach(([
4761
baseline,

frontend/cypress/support/helper/scoringSupport.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,41 +158,56 @@ function scoringValueFromPercentageOrdinal(percentage: number): ScoringValue {
158158
}
159159

160160
function colorFromPercentageMetric(keyResult: CheckInValue) {
161-
if (keyResult.lastCheckIn < keyResult.commitValue) {
161+
const compare = createComparator(keyResult);
162+
163+
if (compare(keyResult.lastCheckIn, keyResult.commitValue)) {
162164
return 'rgb(186, 56, 56)';
163-
} else if (keyResult.lastCheckIn < keyResult.targetValue) {
165+
} else if (compare(keyResult.lastCheckIn, keyResult.targetValue)) {
164166
return 'rgb(255, 214, 0)';
165-
} else if (keyResult.lastCheckIn < keyResult.stretchGoal) {
167+
} else if (compare(keyResult.lastCheckIn, keyResult.stretchGoal)) {
166168
return 'rgb(30, 138, 41)';
167-
} else if (keyResult.lastCheckIn >= keyResult.stretchGoal) {
169+
} else {
168170
return 'rgba(0, 0, 0, 0)';
169171
}
170172
}
171173

172-
function scoringValueFromPercentageMetric(keyResult: CheckInValue, percentage: number): ScoringValue {
173-
if (keyResult.lastCheckIn < keyResult.commitValue) {
174+
function scoringValueFromPercentageMetric(keyResult: CheckInValue,
175+
percentage: number): ScoringValue {
176+
const compare = createComparator(keyResult);
177+
178+
if (compare(keyResult.lastCheckIn, keyResult.commitValue)) {
174179
return {
175180
failPercent: percentage * (100 / 30),
176181
commitPercent: -1,
177182
targetPercent: -1
178183
};
179-
} else if (keyResult.lastCheckIn < keyResult.targetValue) {
184+
} else if (compare(keyResult.lastCheckIn, keyResult.targetValue)) {
180185
return {
181186
failPercent: 100,
182187
commitPercent: (percentage - 30) * (100 / 40),
183188
targetPercent: -1
184189
};
185-
} else if (keyResult.lastCheckIn < keyResult.stretchGoal) {
190+
} else if (compare(keyResult.lastCheckIn, keyResult.stretchGoal)) {
186191
return {
187192
failPercent: 100,
188193
commitPercent: 100,
189194
targetPercent: (percentage - 70) * (100 / 30)
190195
};
191-
} else if (keyResult.lastCheckIn >= keyResult.stretchGoal) {
196+
} else {
192197
return {
193198
failPercent: 0,
194199
commitPercent: 0,
195200
targetPercent: 0
196201
};
197202
}
198203
}
204+
205+
function createComparator(keyResult: CheckInValue) {
206+
const increasing = keyResult.baseline <= keyResult.stretchGoal;
207+
return (a: number, b: number) => {
208+
if (increasing) {
209+
return a < b;
210+
}
211+
return a > b;
212+
};
213+
}

frontend/src/app/shared/custom/scoring/scoring.component.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,31 @@ export class ScoringComponent implements OnInit, AfterViewInit, OnChanges {
105105
const keyResult = this.keyResult as KeyResultMetricMin;
106106
const lastCheckIn = keyResult.lastCheckIn?.value!;
107107

108-
if (lastCheckIn < keyResult.commitValue) {
108+
// Decide comparison direction based on goal orientation
109+
const increasing = keyResult.baseline <= keyResult.stretchGoal;
110+
const compare = (a: number, b: number) => {
111+
return increasing ? a < b : a > b;
112+
};
113+
114+
if (compare(lastCheckIn, keyResult.commitValue)) {
109115
this.stretched = false;
110-
this.failPercent = (lastCheckIn - keyResult.baseline) / (keyResult.commitValue - keyResult.baseline) * 100;
111-
} else if (lastCheckIn < keyResult.targetValue) {
116+
this.failPercent =
117+
(lastCheckIn - keyResult.baseline) /
118+
(keyResult.commitValue - keyResult.baseline) * 100;
119+
} else if (compare(lastCheckIn, keyResult.targetValue)) {
112120
this.stretched = false;
113121
this.failPercent = 100;
114-
this.commitPercent = (lastCheckIn - keyResult.commitValue) / (keyResult.targetValue - keyResult.commitValue) * 100;
115-
} else if (lastCheckIn < keyResult.stretchGoal) {
122+
this.commitPercent =
123+
(lastCheckIn - keyResult.commitValue) /
124+
(keyResult.targetValue - keyResult.commitValue) * 100;
125+
} else if (compare(lastCheckIn, keyResult.stretchGoal)) {
116126
this.stretched = false;
117127
this.failPercent = 100;
118128
this.commitPercent = 100;
119-
this.targetPercent = (lastCheckIn - keyResult.targetValue) / (keyResult.stretchGoal - keyResult.targetValue) * 100;
120-
} else if (lastCheckIn >= keyResult.stretchGoal) {
129+
this.targetPercent =
130+
(lastCheckIn - keyResult.targetValue) /
131+
(keyResult.stretchGoal - keyResult.targetValue) * 100;
132+
} else {
121133
this.stretched = true;
122134
}
123135
}

0 commit comments

Comments
 (0)