Implements a general score calculator
This commit is contained in:
@@ -7,7 +7,6 @@ var RulesChecker = function() {
|
||||
this.check = function(data, policies) {
|
||||
|
||||
var results = {};
|
||||
var err = null;
|
||||
|
||||
debug('Starting checking rules');
|
||||
|
||||
|
||||
+24
-4
@@ -1,8 +1,10 @@
|
||||
var Q = require('q');
|
||||
var debug = require('debug')('ylt:yellowlabtools');
|
||||
var Q = require('q');
|
||||
var debug = require('debug')('ylt:runner');
|
||||
|
||||
var phantomasWrapper = require('./tools/phantomasWrapper');
|
||||
var rulesChecker = require('./rulesChecker');
|
||||
var scoreCalculator = require('./scoreCalculator');
|
||||
|
||||
var phantomasWrapper = require('./tools/phantomasWrapper');
|
||||
var rulesChecker = require('./rulesChecker');
|
||||
|
||||
var Runner = function(params) {
|
||||
'use strict';
|
||||
@@ -27,6 +29,24 @@ var Runner = function(params) {
|
||||
data.rules = rulesChecker.check(data, policies);
|
||||
|
||||
|
||||
// Scores calculator
|
||||
var scoreProfileGeneric = require('./metadata/scoreProfileGeneric.json');
|
||||
data.scoreProfiles = {
|
||||
generic : scoreCalculator.calculate(data, scoreProfileGeneric)
|
||||
};
|
||||
|
||||
|
||||
// Get the JS Execution Tree from offenders and put in the main object
|
||||
try {
|
||||
data.javascriptExecutionTree = JSON.parse(data.toolsResults.phantomas.offenders.javascriptExecutionTree[0]);
|
||||
} catch(e) {
|
||||
debug('Could not find nor parse phantomas.offenders.javascriptExecutionTree');
|
||||
}
|
||||
|
||||
delete data.toolsResults.phantomas.metrics.javascriptExecutionTree;
|
||||
delete data.toolsResults.phantomas.offenders.javascriptExecutionTree;
|
||||
|
||||
//Finished!
|
||||
deferred.resolve(data);
|
||||
|
||||
}).fail(function(err) {
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
var Q = require('q');
|
||||
var debug = require('debug')('ylt:scoreCalculator');
|
||||
|
||||
var ScoreCalculator = function() {
|
||||
'use strict';
|
||||
|
||||
this.calculate = function(data, profile) {
|
||||
|
||||
var results = {
|
||||
categories: {}
|
||||
};
|
||||
var weight;
|
||||
var categoryName;
|
||||
|
||||
debug('Starting calculating scores');
|
||||
|
||||
// Calculate categories
|
||||
for (categoryName in profile.categories) {
|
||||
var categoryResult = {
|
||||
label: profile.categories[categoryName].label
|
||||
};
|
||||
|
||||
var sum = 0;
|
||||
var totalWeight = 0;
|
||||
var rules = [];
|
||||
|
||||
for (var policyName in profile.categories[categoryName].policies) {
|
||||
weight = profile.categories[categoryName].policies[policyName];
|
||||
|
||||
if (data.rules[policyName]) {
|
||||
sum += data.rules[policyName].score * 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(sum / totalWeight);
|
||||
}
|
||||
|
||||
categoryResult.rules = rules;
|
||||
results.categories[categoryName] = categoryResult;
|
||||
}
|
||||
|
||||
|
||||
// Calculate general score
|
||||
var globalSum = 0;
|
||||
var globalTotalWeight = 0;
|
||||
|
||||
for (categoryName in profile.globalScore) {
|
||||
weight = profile.globalScore[categoryName];
|
||||
|
||||
if (results.categories[categoryName]) {
|
||||
globalSum += results.categories[categoryName].categoryScore * weight;
|
||||
} else {
|
||||
globalSum += 100 * weight;
|
||||
}
|
||||
globalTotalWeight += profile.globalScore[categoryName];
|
||||
}
|
||||
|
||||
if (globalTotalWeight === 0) {
|
||||
results.globalScore = 100;
|
||||
} else {
|
||||
results.globalScore = Math.round(globalSum / globalTotalWeight);
|
||||
}
|
||||
|
||||
|
||||
|
||||
debug('Score calculation finished:');
|
||||
debug(results);
|
||||
|
||||
return results;
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = new ScoreCalculator();
|
||||
@@ -16,7 +16,7 @@ var apiLimitsMiddleware = function(req, res, next) {
|
||||
if (!runsTable.accepts(req.connection.remoteAddress)) {
|
||||
// Sorry :/
|
||||
debug('Too many tests launched from IP address %s', req.connection.remoteAddress);
|
||||
res.status(429).send('Too Many Requests');
|
||||
res.status(429).send('Too many requests');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ var apiLimitsMiddleware = function(req, res, next) {
|
||||
if (!callsTable.accepts(req.connection.remoteAddress)) {
|
||||
// Sorry :/
|
||||
debug('Too many API requests from IP address %s', req.connection.remoteAddress);
|
||||
res.status(429).send('Too Many Requests');
|
||||
res.status(429).send('Too many requests');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user