rulesChecker.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var debug = require('debug')('ylt:ruleschecker');
  2. var RulesChecker = function() {
  3. 'use strict';
  4. this.check = function(data, policies) {
  5. /*jshint loopfunc:true */
  6. var results = {};
  7. debug('Starting checking rules');
  8. for (var metricName in policies) {
  9. var policy = policies[metricName];
  10. var rule;
  11. if (policy.tool &&
  12. data.toolsResults[policy.tool] &&
  13. data.toolsResults[policy.tool].metrics &&
  14. (data.toolsResults[policy.tool].metrics[metricName] || data.toolsResults[policy.tool].metrics[metricName] === 0)) {
  15. rule = {
  16. value: data.toolsResults[policy.tool].metrics[metricName],
  17. policy: policy
  18. };
  19. var offenders = [];
  20. // Take DOMqueriesAvoidable's offenders from DOMqueriesDuplicated, for example.
  21. if (policy.takeOffendersFrom) {
  22. var fromList = policy.takeOffendersFrom;
  23. // takeOffendersFrom option can be a string or an array of strings.
  24. if (typeof fromList === 'string') {
  25. fromList = [fromList];
  26. }
  27. fromList.forEach(function(from) {
  28. if (data.toolsResults[policy.tool] &&
  29. data.toolsResults[policy.tool].offenders &&
  30. data.toolsResults[policy.tool].offenders[from]) {
  31. offenders = offenders.concat(data.toolsResults[policy.tool].offenders[from]);
  32. }
  33. });
  34. data.toolsResults[policy.tool].offenders[metricName] = offenders;
  35. } else if (data.toolsResults[policy.tool] &&
  36. data.toolsResults[policy.tool].offenders &&
  37. data.toolsResults[policy.tool].offenders[metricName]) {
  38. offenders = data.toolsResults[policy.tool].offenders[metricName];
  39. }
  40. // It is possible to declare a transformation function for the offenders.
  41. // The function should take an array of strings as single parameter and return a string.
  42. if (policy.offendersTransformFn) {
  43. rule.offendersCount = offenders.length;
  44. offenders = policy.offendersTransformFn(offenders);
  45. delete policy.offendersTransformFn;
  46. }
  47. if (offenders && offenders.length > 0) {
  48. rule.offenders = offenders;
  49. }
  50. rule.bad = rule.value > policy.isOkThreshold;
  51. rule.abnormal = policy.isAbnormalThreshold && rule.value >= policy.isAbnormalThreshold;
  52. // A value between 0 (bad) and 100 (very good).
  53. var score = (policy.isBadThreshold - rule.value) * 100 / (policy.isBadThreshold - policy.isOkThreshold);
  54. rule.score = Math.min(Math.max(Math.round(score), 0), 100);
  55. // A value between 0 (abnormal) and negative-infinity (your website is a blackhole)
  56. var abnormalityScore = (policy.isAbnormalThreshold - rule.value) * 100 / (policy.isAbnormalThreshold - policy.isOkThreshold);
  57. rule.abnormalityScore = Math.min(Math.round(abnormalityScore), 0);
  58. results[metricName] = rule;
  59. debug('Metric %s calculated. Score: %d', metricName, rule.score);
  60. } else if (policy.scoreFn) {
  61. debug('Custom score function for %s', metricName);
  62. // Custom score function
  63. rule = policy.scoreFn(data);
  64. // Check returned values (if the result is null, just don't save)
  65. if (rule) {
  66. rule.policy = {
  67. label: policy.label,
  68. message: policy.message
  69. };
  70. results[metricName] = rule;
  71. debug('Metric %s calculated. Score: %d', metricName, rule.score);
  72. } else {
  73. debug('Metric %s is null. Ignored.', metricName);
  74. }
  75. } else {
  76. debug('Metric %s not found for tool %s', metricName, policy.tool);
  77. }
  78. }
  79. debug('Rules checking finished');
  80. return results;
  81. };
  82. };
  83. module.exports = new RulesChecker();