瀏覽代碼

Copy offenders from jsCount etc. to the requests rule

Gaël Métais 10 年之前
父節點
當前提交
d78716c873

+ 2 - 1
lib/metadata/policies.js

@@ -301,7 +301,8 @@ var policies = {
         "message": "<p>This is one of the most important performance rule. Every request is slowing down the page loading.</p><p>There are several technics to reduce their number:<ul><li>Concatenate JS files</li><li>Concatenate CSS files</li><li>Embed or inline small JS or CSS files in the HTML</li><li>Create sprites or icon fonts</li><li>Base64 encode small images in HTML or stylesheets</li><li>Use lazyloading for images</li></ul></p>",
         "message": "<p>This is one of the most important performance rule. Every request is slowing down the page loading.</p><p>There are several technics to reduce their number:<ul><li>Concatenate JS files</li><li>Concatenate CSS files</li><li>Embed or inline small JS or CSS files in the HTML</li><li>Create sprites or icon fonts</li><li>Base64 encode small images in HTML or stylesheets</li><li>Use lazyloading for images</li></ul></p>",
         "isOkThreshold": 15,
         "isOkThreshold": 15,
         "isBadThreshold": 100,
         "isBadThreshold": 100,
-        "isAbnormalThreshold": 200
+        "isAbnormalThreshold": 200,
+        "takeOffendersFrom": ["htmlCount", "jsCount", "cssCount", "imageCount", "webfontCount", "videoCount", "jsonCount", "jsonCount"]
     },
     },
     "htmlCount": {
     "htmlCount": {
         "tool": "phantomas",
         "tool": "phantomas",

+ 15 - 2
lib/rulesChecker.js

@@ -4,6 +4,7 @@ var RulesChecker = function() {
     'use strict';
     'use strict';
 
 
     this.check = function(data, policies) {
     this.check = function(data, policies) {
+        /*jshint loopfunc:true */
 
 
         var results = {};
         var results = {};
 
 
@@ -23,9 +24,21 @@ var RulesChecker = function() {
                         policy: policy
                         policy: policy
                     };
                     };
 
 
-                    // Take DOMqueriesAvoidable's offenders from DOMqueriesDuplicated.
+                    // Take DOMqueriesAvoidable's offenders from DOMqueriesDuplicated, for example.
                     if (policy.takeOffendersFrom) {
                     if (policy.takeOffendersFrom) {
-                        data.toolsResults[policy.tool].offenders[metricName] = data.toolsResults[policy.tool].offenders[policy.takeOffendersFrom];
+                        var fromList = policy.takeOffendersFrom;
+                        var offenders = [];
+                        
+                        // takeOffendersFrom option can be a string or an array of strings.
+                        if (typeof fromList === 'string') {
+                            fromList = [fromList];
+                        }
+                        
+                        fromList.forEach(function(from) {
+                            offenders = offenders.concat(data.toolsResults[policy.tool].offenders[from]);
+                        });
+
+                        data.toolsResults[policy.tool].offenders[metricName] = offenders;
                     }
                     }
 
 
                     if (data.toolsResults[policy.tool].offenders &&
                     if (data.toolsResults[policy.tool].offenders &&

+ 1 - 0
test/fixtures/rulesCheckerInput.json

@@ -14,6 +14,7 @@
                 "metric1": [],
                 "metric1": [],
                 "metric2": [],
                 "metric2": [],
                 "metric3": ["offender1", "offender2"],
                 "metric3": ["offender1", "offender2"],
+                "metric4": ["offender3"],
                 "metric5": []
                 "metric5": []
             }
             }
         },
         },

+ 4 - 1
test/fixtures/rulesCheckerOutput.json

@@ -57,6 +57,7 @@
             "isAbnormalThreshold": 5000
             "isAbnormalThreshold": 5000
         },
         },
         "value": 1000,
         "value": 1000,
+        "offenders": ["offender3"],
         "bad": false,
         "bad": false,
         "abnormal": false,
         "abnormal": false,
         "score": 100,
         "score": 100,
@@ -69,9 +70,11 @@
             "message": "A great message",
             "message": "A great message",
             "isOkThreshold": 1000,
             "isOkThreshold": 1000,
             "isBadThreshold": 3000,
             "isBadThreshold": 3000,
-            "isAbnormalThreshold": 5000
+            "isAbnormalThreshold": 5000,
+            "takeOffendersFrom": ["metric3", "metric4"]
         },
         },
         "value": 3000,
         "value": 3000,
+        "offenders": ["offender1", "offender2", "offender3"],
         "bad": true,
         "bad": true,
         "abnormal": false,
         "abnormal": false,
         "score": 0,
         "score": 0,

+ 2 - 1
test/fixtures/rulesCheckerPolicies.json

@@ -38,7 +38,8 @@
         "message": "A great message",
         "message": "A great message",
         "isOkThreshold": 1000,
         "isOkThreshold": 1000,
         "isBadThreshold": 3000,
         "isBadThreshold": 3000,
-        "isAbnormalThreshold": 5000
+        "isAbnormalThreshold": 5000,
+        "takeOffendersFrom": ["metric3", "metric4"]
     },
     },
     "metric6": {
     "metric6": {
         "tool": "tool1",
         "tool": "tool1",