Skip to content

Commit

Permalink
fix: use numeric types in scoring data object
Browse files Browse the repository at this point in the history
Signed-off-by: Dustin Popp <[email protected]>
  • Loading branch information
dpopp07 committed Sep 27, 2024
1 parent e80a708 commit 19e0365
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
12 changes: 5 additions & 7 deletions packages/validator/src/schemas/results-object.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,12 @@ $defs:
percentage:
type: integer
description: The percentage of total violations for a given severity that this rule comprises

# TODO: refactor the logic to use numbers for these.
MaxScore:
type: string
type: integer
description: A number describing the maximum quality score for an API after accounting for rule violations
# minimum: 0
# maximum: 100
minimum: 0
maximum: 100
Demerit:
type: string
type: number
description: A number describing the demerit impact a rule has on an API
# minimum: 0.01
minimum: 0.01
31 changes: 21 additions & 10 deletions packages/validator/src/scoring-tool/score.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ function scoreResults(result, metrics, logger) {

// Count up category totals as we go.
for (const [category, demerit] of Object.entries(data.demerits)) {
demeritSumsByCategory[category] += parseFloat(demerit);
demeritSumsByCategory[category] += demerit;
}

// After adding category totals, we can calculate the total impact for the given rule.
data.demerits.total = Object.values(data.demerits)
.reduce((sum, num) => sum + parseFloat(num), 0.0)
.toFixed(2);
data.demerits.total = includeDecimals(
Object.values(data.demerits).reduce((sum, num) => sum + num, 0.0),
2
);

scoringData.push(data);
});
Expand All @@ -62,11 +63,12 @@ function scoreResults(result, metrics, logger) {

// Average the categorized scores to produce the overall score.
// They are already weighted through their coefficients.
impactSummary.overall = (
impactSummary.overall = includeDecimals(
getCategories()
.map(c => parseFloat(impactSummary[c]))
.reduce((total, score) => total + score) / getCategories().length
).toFixed(0);
.map(c => impactSummary[c])
.reduce((total, score) => total + score) / getCategories().length,
0
);

return {
impactSummary,
Expand Down Expand Up @@ -97,7 +99,7 @@ function computeCategorizedScores(rule, count, metrics) {

// Base the categories on those defined for the given rule in the rubric.
rule.categories.forEach(c => {
result[c] = (baseScore * getCategoryCoefficient(c)).toFixed(2);
result[c] = includeDecimals(baseScore * getCategoryCoefficient(c), 2);
});

return result;
Expand Down Expand Up @@ -150,7 +152,16 @@ function tally(result, logger) {
// is the "/40", which is just scaling the input subjectively — it's
// a number we will keep tuning alongside tuning the demerit coefficients.
function computeOverallScore(demeritSum) {
return (100 - (100 * Math.atan(demeritSum / 40)) / Math.asin(1)).toFixed(0);
return includeDecimals(
100 - (100 * Math.atan(demeritSum / 40)) / Math.asin(1),
0
);
}

// In order to display numbers with meaningful precision,
// only use the first "number" of decimal places.
function includeDecimals(value, number) {
return parseFloat(value.toFixed(number));
}

module.exports = {
Expand Down
46 changes: 23 additions & 23 deletions packages/validator/test/scoring-tool/score.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,49 @@ describe('scoring-tool score tests', function () {

expect(results).toEqual({
impactSummary: {
usability: '83',
security: '100',
robustness: '82',
evolution: '88',
overall: '88',
usability: 83,
security: 100,
robustness: 82,
evolution: 88,
overall: 88,
},
scoringData: [
{
rule: 'ibm-avoid-property-name-collision',
count: 2,
func: '2×10÷operations',
demerits: {
usability: '5.00',
total: '5.00',
usability: 5.00,
total: 5.00,
},
},
{
rule: 'ibm-etag-header',
count: 3,
func: '3×1÷operations',
demerits: {
usability: '0.75',
robustness: '1.50',
total: '2.25',
usability: 0.75,
robustness: 1.50,
total: 2.25,
},
},
{
rule: 'ibm-no-array-responses',
count: 1,
func: '1×10÷operations',
demerits: {
evolution: '7.50',
total: '7.50',
evolution: 7.50,
total: 7.50,
},
},
{
rule: 'ibm-operation-summary',
count: 2,
func: '2×10÷operations',
demerits: {
usability: '5.00',
robustness: '10.00',
total: '15.00',
usability: 5.00,
robustness: 10.00,
total: 15.00,
},
},
],
Expand Down Expand Up @@ -119,8 +119,8 @@ describe('scoring-tool score tests', function () {

const scores = computeCategorizedScores(rubricEntry, 4, metrics);
expect(scores).toEqual({
usability: '2.00',
evolution: '6.00',
usability: 2.00,
evolution: 6.00,
});
});

Expand Down Expand Up @@ -183,12 +183,12 @@ describe('scoring-tool score tests', function () {
});

it('should computeOverallScore', function () {
expect(computeOverallScore(0)).toBe('100');
expect(computeOverallScore(3.4)).toBe('95');
expect(computeOverallScore(12.7)).toBe('80');
expect(computeOverallScore(58)).toBe('38');
expect(computeOverallScore(123.45)).toBe('20');
expect(computeOverallScore(700.08)).toBe('4');
expect(computeOverallScore(0)).toBe(100);
expect(computeOverallScore(3.4)).toBe(95);
expect(computeOverallScore(12.7)).toBe(80);
expect(computeOverallScore(58)).toBe(38);
expect(computeOverallScore(123.45)).toBe(20);
expect(computeOverallScore(700.08)).toBe(4);
});
});

Expand Down

0 comments on commit 19e0365

Please sign in to comment.