Skip to content

Commit

Permalink
fix(DGT-528): Fix the rounding issue of QualityBar percentages
Browse files Browse the repository at this point in the history
  • Loading branch information
dlcaldeira authored May 24, 2024
1 parent e66f4c5 commit 6ff9f03
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-camels-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@talend/design-system": patch
---

DGT-528: Fix QualityBar rounding issue when the invalid or empty percentages were rounded to 0. Set a miminum value for the rounding to prevent UI inconsistencies
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const getQualityPercentagesRounded = (
empty: number = 0,
valid: number = 0,
na: number = 0,
placeholder = 0,
placeholder: number = 0,
): Required<QualityBarPercentages> => {
const output: Required<QualityBarPercentages> = {
empty: 0,
Expand All @@ -45,30 +45,30 @@ export const getQualityPercentagesRounded = (
valid: 0,
};

let sumValues = 0;
let sumRounded = 0;
const digitMultiplier = Math.pow(10, digits);
const multiplier = 100 * digitMultiplier;

const total = invalid + empty + valid + na + placeholder;

sumValues = (invalid * multiplier) / total;
output.invalid = Math.round(sumValues - sumRounded) / digitMultiplier;
sumRounded = Math.round(sumValues);
if (total === 0) {
return output;
}

const minPercentage = 1 / digitMultiplier;

output.invalid = +(invalid > 0 ? Math.max((invalid * 100) / total, minPercentage) : 0).toFixed(
digits,
);

sumValues += (empty * multiplier) / total;
output.empty = Math.round(sumValues - sumRounded) / digitMultiplier;
sumRounded = Math.round(sumValues);
output.empty = +(empty > 0 ? Math.max((empty * 100) / total, minPercentage) : 0).toFixed(digits);

sumValues += (valid * multiplier) / total;
output.valid = Math.round(sumValues - sumRounded) / digitMultiplier;
sumRounded = Math.round(sumValues);
output.na = +(na > 0 ? Math.max((na * 100) / total, minPercentage) : 0).toFixed(digits);

sumValues += (na * multiplier) / total;
output.na = Math.round(sumValues - sumRounded) / digitMultiplier;
output.placeholder = +(
placeholder > 0 ? Math.max((placeholder * 100) / total, minPercentage) : 0
).toFixed(digits);

sumValues += (placeholder * multiplier) / total;
output.placeholder = Math.round(sumValues - sumRounded) / digitMultiplier;
output.valid = +(100 - output.invalid - output.empty - output.na - output.placeholder).toFixed(
digits,
);

return output;
};

0 comments on commit 6ff9f03

Please sign in to comment.