|
@@ -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();
|