Browse Source

Better scoring algorithm

Gaël Métais 10 years ago
parent
commit
b186677a53
2 changed files with 39 additions and 28 deletions
  1. 11 3
      lib/metadata/policies.js
  2. 28 25
      lib/scoreCalculator.js

+ 11 - 3
lib/metadata/policies.js

@@ -170,9 +170,9 @@ var policies = {
         "tool": "phantomas",
         "label": "CSS syntax error",
         "message": "<p>Yellow Lab Tools failed to parse a CSS file. I doubt the problem comes from the css parser.</p><p>Maybe a <a href=\"http://jigsaw.w3.org/css-validator\" target=\"_blank\">CSS validator</a> can help you.</p>",
-        "isOkThreshold": 1,
-        "isBadThreshold": 2,
-        "isAbnormalThreshold": 3
+        "isOkThreshold": 0,
+        "isBadThreshold": 1,
+        "isAbnormalThreshold": 1
     },
     "cssRules": {
         "tool": "phantomas",
@@ -390,6 +390,14 @@ var policies = {
         "isBadThreshold": 12,
         "isAbnormalThreshold": 25
     },
+    "cachingNotSpecified": {
+        "tool": "phantomas",
+        "label": "Caching not specified",
+        "message": "<p>When no caching is specified, each browser will handle it differently. Most of the time, it will automatically add a cache for you, but a poor one. You'd better handle it yourself.</p>",
+        "isOkThreshold": 5,
+        "isBadThreshold": 20,
+        "isAbnormalThreshold": 40
+    },
     "cachingTooShort": {
         "tool": "phantomas",
         "label": "Caching too short",

+ 28 - 25
lib/scoreCalculator.js

@@ -9,8 +9,9 @@ var ScoreCalculator = function() {
         var results = {
             categories: {}
         };
-        var weight;
+        var categoryScore;
         var categoryName;
+        var weight;
 
         debug('Starting calculating scores');
 
@@ -20,31 +21,24 @@ var ScoreCalculator = function() {
                 label: profile.categories[categoryName].label
             };
 
-            var sum = 0;
-            var totalWeight = 0;
+            categoryScore = new ScoreMerger();
             var rules = [];
+            var policyScore;
 
             for (var policyName in profile.categories[categoryName].policies) {
                 weight = profile.categories[categoryName].policies[policyName];
                 
                 if (data.rules[policyName]) {
-                    var policyScore = data.rules[policyName].score + data.rules[policyName].abnormalityScore;
-                    sum += policyScore * weight;
+                    policyScore = data.rules[policyName].score + (data.rules[policyName].abnormalityScore * 2);
+                    categoryScore.push(policyScore, weight);
                 } else {
-                    // Max value if rule is not here
-                    sum += 100 * weight;
                     debug('Warning: could not find rule %s', policyName);
                 }
 
-                totalWeight += weight;
                 rules.push(policyName);
             }
 
-            if (totalWeight === 0) {
-                categoryResult.categoryScore = 100;
-            } else {
-                categoryResult.categoryScore = Math.round(Math.max(sum, 0) / totalWeight);
-            }
+            categoryResult.categoryScore = categoryScore.getScore();
 
             categoryResult.rules = rules;
             results.categories[categoryName] = categoryResult;
@@ -52,26 +46,17 @@ var ScoreCalculator = function() {
 
 
         // Calculate general score
-        var globalSum = 0;
-        var globalTotalWeight = 0;
+        var globalScore = new ScoreMerger();
 
         for (categoryName in profile.globalScore) {
             weight = profile.globalScore[categoryName];
 
             if (results.categories[categoryName]) {
-                globalSum += results.categories[categoryName].categoryScore * weight;
-            } else {
-                globalSum += 100 * weight;
+                globalScore.push(results.categories[categoryName].categoryScore, weight);
             }
-            globalTotalWeight += profile.globalScore[categoryName];
-        }
-
-        if (globalTotalWeight === 0) {
-            results.globalScore = 100;
-        } else {
-            results.globalScore = Math.round(globalSum / globalTotalWeight);
         }
 
+        results.globalScore = Math.round(globalScore.getScore());
 
 
         debug('Score calculation finished:');
@@ -79,6 +64,24 @@ var ScoreCalculator = function() {
 
         return results;
     };
+
+
+    var ScoreMerger = function() {
+        var sum = 0;
+        var totalWeight = 0;
+
+        this.push = function(score, weight) {
+            sum += (100 - score) * weight;
+            totalWeight += weight;
+        };
+
+        this.getScore = function() {
+            if (totalWeight === 0) {
+                return 100;
+            }
+            return Math.round(100 - (sum / totalWeight));
+        };
+    };
 };
 
 module.exports = new ScoreCalculator();