From d78716c87376b21fafe5439280864559a14811dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Mon, 5 Jan 2015 15:51:02 +0100 Subject: [PATCH] Copy offenders from jsCount etc. to the requests rule --- lib/metadata/policies.js | 3 ++- lib/rulesChecker.js | 17 +++++++++++++++-- test/fixtures/rulesCheckerInput.json | 1 + test/fixtures/rulesCheckerOutput.json | 5 ++++- test/fixtures/rulesCheckerPolicies.json | 3 ++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/metadata/policies.js b/lib/metadata/policies.js index ab7ad78..91d3f4a 100644 --- a/lib/metadata/policies.js +++ b/lib/metadata/policies.js @@ -301,7 +301,8 @@ var policies = { "message": "

This is one of the most important performance rule. Every request is slowing down the page loading.

There are several technics to reduce their number:

", "isOkThreshold": 15, "isBadThreshold": 100, - "isAbnormalThreshold": 200 + "isAbnormalThreshold": 200, + "takeOffendersFrom": ["htmlCount", "jsCount", "cssCount", "imageCount", "webfontCount", "videoCount", "jsonCount", "jsonCount"] }, "htmlCount": { "tool": "phantomas", diff --git a/lib/rulesChecker.js b/lib/rulesChecker.js index bbbae49..f8eabd1 100644 --- a/lib/rulesChecker.js +++ b/lib/rulesChecker.js @@ -4,6 +4,7 @@ var RulesChecker = function() { 'use strict'; this.check = function(data, policies) { + /*jshint loopfunc:true */ var results = {}; @@ -23,9 +24,21 @@ var RulesChecker = function() { policy: policy }; - // Take DOMqueriesAvoidable's offenders from DOMqueriesDuplicated. + // Take DOMqueriesAvoidable's offenders from DOMqueriesDuplicated, for example. 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 && diff --git a/test/fixtures/rulesCheckerInput.json b/test/fixtures/rulesCheckerInput.json index fd05a45..e54cc5d 100644 --- a/test/fixtures/rulesCheckerInput.json +++ b/test/fixtures/rulesCheckerInput.json @@ -14,6 +14,7 @@ "metric1": [], "metric2": [], "metric3": ["offender1", "offender2"], + "metric4": ["offender3"], "metric5": [] } }, diff --git a/test/fixtures/rulesCheckerOutput.json b/test/fixtures/rulesCheckerOutput.json index ef696af..5327aa5 100644 --- a/test/fixtures/rulesCheckerOutput.json +++ b/test/fixtures/rulesCheckerOutput.json @@ -57,6 +57,7 @@ "isAbnormalThreshold": 5000 }, "value": 1000, + "offenders": ["offender3"], "bad": false, "abnormal": false, "score": 100, @@ -69,9 +70,11 @@ "message": "A great message", "isOkThreshold": 1000, "isBadThreshold": 3000, - "isAbnormalThreshold": 5000 + "isAbnormalThreshold": 5000, + "takeOffendersFrom": ["metric3", "metric4"] }, "value": 3000, + "offenders": ["offender1", "offender2", "offender3"], "bad": true, "abnormal": false, "score": 0, diff --git a/test/fixtures/rulesCheckerPolicies.json b/test/fixtures/rulesCheckerPolicies.json index 9c2f064..9147e01 100644 --- a/test/fixtures/rulesCheckerPolicies.json +++ b/test/fixtures/rulesCheckerPolicies.json @@ -38,7 +38,8 @@ "message": "A great message", "isOkThreshold": 1000, "isBadThreshold": 3000, - "isAbnormalThreshold": 5000 + "isAbnormalThreshold": 5000, + "takeOffendersFrom": ["metric3", "metric4"] }, "metric6": { "tool": "tool1",