Skip to content

Commit 4a2dc7a

Browse files
authored
feat: Proposal score calculation, add rounding to 1 decimal place | NPG-8046 (#611)
# Description Updated proposal community score calculation, add rounding to 1 decimal place. So now with the previous calculation result is `5.799999999999999` it will be rounded to `5.8`, and if result is `5.449999999999999` to `5.4`. ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) - [X] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ## How Has This Been Tested? - [ ] weighted_score_test_2 - [ ] weighted_score_test_3
1 parent 2e58820 commit 4a2dc7a

File tree

1 file changed

+88
-2
lines changed
  • src/catalyst-toolbox/catalyst-toolbox/src/proposal_score

1 file changed

+88
-2
lines changed

src/catalyst-toolbox/catalyst-toolbox/src/proposal_score/mod.rs

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ fn weighted_avarage_score(
9393
let allocated_weight = review_weight(allocated_weight, allocated_count);
9494
let not_allocated_weight = review_weight(not_allocated_weight, not_allocated_count);
9595

96-
let res = (total_allocated_rating as f64 * allocated_weight
96+
let mut res = (total_allocated_rating as f64 * allocated_weight
9797
+ total_not_allocated_rating as f64 * not_allocated_weight)
9898
/ (allocated_weight * allocated_count as f64
9999
+ not_allocated_weight * not_allocated_count as f64);
100100

101+
// round to 1 decimal place
102+
res = (10.0 * res).round() / 10.0;
101103
Ok(res)
102104
}
103105

@@ -119,7 +121,7 @@ mod tests {
119121
}
120122

121123
#[test]
122-
fn weighted_score_test() {
124+
fn weighted_score_test_1() {
123125
let allocated_weight = 0.8;
124126
let not_allocated_weight = 0.2;
125127

@@ -161,6 +163,90 @@ mod tests {
161163
assert!(weighted_avarage_score(0.5, 0.6, &[]).is_err());
162164
}
163165

166+
#[test]
167+
fn weighted_score_test_2() {
168+
let allocated_weight = 0.7;
169+
let not_allocated_weight = 0.3;
170+
171+
let reviews = vec![
172+
Review {
173+
rating: 1,
174+
allocated: false,
175+
},
176+
Review {
177+
rating: 2,
178+
allocated: false,
179+
},
180+
Review {
181+
rating: 3,
182+
allocated: false,
183+
},
184+
Review {
185+
rating: 4,
186+
allocated: false,
187+
},
188+
Review {
189+
rating: 5,
190+
allocated: false,
191+
},
192+
Review {
193+
rating: 6,
194+
allocated: true,
195+
},
196+
Review {
197+
rating: 8,
198+
allocated: true,
199+
},
200+
];
201+
202+
let result =
203+
weighted_avarage_score(allocated_weight, not_allocated_weight, &reviews).unwrap();
204+
// To be precise the result should be `5.799999999999999`, but we are rounding to 1 decimal place
205+
assert_eq!(result, 5.8);
206+
}
207+
208+
#[test]
209+
fn weighted_score_test_3() {
210+
let allocated_weight = 0.7;
211+
let not_allocated_weight = 0.3;
212+
213+
let reviews = vec![
214+
Review {
215+
rating: 1,
216+
allocated: false,
217+
},
218+
Review {
219+
rating: 2,
220+
allocated: false,
221+
},
222+
Review {
223+
rating: 3,
224+
allocated: false,
225+
},
226+
Review {
227+
rating: 4,
228+
allocated: false,
229+
},
230+
Review {
231+
rating: 5,
232+
allocated: false,
233+
},
234+
Review {
235+
rating: 6,
236+
allocated: true,
237+
},
238+
Review {
239+
rating: 7,
240+
allocated: true,
241+
},
242+
];
243+
244+
let result =
245+
weighted_avarage_score(allocated_weight, not_allocated_weight, &reviews).unwrap();
246+
// To be precise the result should be `5.449999999999999`, but we are rounding to 1 decimal place
247+
assert_eq!(result, 5.4);
248+
}
249+
164250
#[test]
165251
fn full_test() {
166252
let allocated_weight = 0.8;

0 commit comments

Comments
 (0)