resultsCtrl.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. var app = angular.module('Results', []);
  2. app.controller('ResultsCtrl', function ($scope) {
  3. // Grab results from nodeJS served page
  4. $scope.phantomasResults = window._phantomas_results;
  5. $scope.phantomasMetadata = window._phantomas_metadata.metrics;
  6. $scope.view = 'summary';
  7. if ($scope.phantomasResults.metrics && $scope.phantomasResults.offenders && $scope.phantomasResults.offenders.javascriptExecutionTree) {
  8. // Get the execution tree from the offenders
  9. $scope.javascript = JSON.parse($scope.phantomasResults.offenders.javascriptExecutionTree);
  10. // Sort globalVariables offenders alphabetically
  11. $scope.phantomasResults.offenders.globalVariables.sort();
  12. initSummaryView();
  13. initExecutionView();
  14. initMetricsView();
  15. }
  16. $scope.setView = function(viewName) {
  17. $scope.view = viewName;
  18. };
  19. $scope.onNodeDetailsClick = function(node) {
  20. var isOpen = node.data.showDetails;
  21. if (!isOpen) {
  22. // Close all other nodes
  23. $scope.javascript.children.forEach(function(currentNode) {
  24. currentNode.data.showDetails = false;
  25. });
  26. // Parse the backtrace
  27. if (!node.data.parsedBacktrace) {
  28. node.data.parsedBacktrace = parseBacktrace(node.data.backtrace);
  29. }
  30. }
  31. node.data.showDetails = !isOpen;
  32. };
  33. function initSummaryView() {
  34. // Read the main elements of the tree and sum the total time
  35. $scope.totalJSTime = 0;
  36. treeRunner($scope.javascript, function(node) {
  37. if (node.data.time) {
  38. $scope.totalJSTime += node.data.time;
  39. }
  40. if (node.data.type !== 'main') {
  41. // Don't check the children
  42. return false;
  43. }
  44. });
  45. // Read all the duplicated queries and calculate a more appropriated score
  46. $scope.duplicatedQueriesCountAll = 0;
  47. if ($scope.phantomasResults.offenders.DOMqueriesDuplicated) {
  48. var regex = /\): *(\d+) queries$/;
  49. $scope.phantomasResults.offenders.DOMqueriesDuplicated.forEach(function(query) {
  50. var regexResult = regex.exec(query);
  51. if (regexResult) {
  52. $scope.duplicatedQueriesCountAll += parseInt(regexResult[1], 10) - 1;
  53. }
  54. });
  55. }
  56. // Check if the CSS was correctly parsed
  57. $scope.cssParsingError = (!$scope.phantomasResults.metrics.cssRules && $scope.phantomasResults.metrics.cssCount > 0);
  58. // Grab the notes
  59. $scope.notations = {
  60. domComplexity: getDomComplexityScore(),
  61. domManipulations: getDomManipulationsScore(),
  62. duplicatedDomQueries: getDuplicatedDomQueriesScore(),
  63. eventsBound: getEventsBoundScore(),
  64. jsBadPractices: getJSBadPracticesScore(),
  65. jQueryLoading: getJQueryLoadingScore(),
  66. cssComplexity: getCSSComplexityScore(),
  67. badCss: getBadCssScore(),
  68. requests: requestsScore(),
  69. network: networkScore()
  70. };
  71. }
  72. function initExecutionView() {
  73. $scope.slowRequestsOn = false;
  74. $scope.slowRequestsLimit = 5;
  75. if (!$scope.javascript.children) {
  76. return;
  77. }
  78. // Now read the tree and display it on a timeline
  79. // Split the timeline into 200 intervals
  80. var numberOfIntervals = 200;
  81. var lastEvent = $scope.javascript.children[$scope.javascript.children.length - 1];
  82. $scope.endTime = lastEvent.data.timestamp + (lastEvent.data.time || 0);
  83. $scope.timelineIntervalDuration = $scope.endTime / numberOfIntervals;
  84. // Pre-filled array of 100 elements
  85. $scope.timeline = Array.apply(null, new Array(numberOfIntervals)).map(Number.prototype.valueOf,0);
  86. treeRunner($scope.javascript, function(node) {
  87. if (node.data.time) {
  88. // If a node is between two intervals, split it. That's the meaning of the following dirty algorithm.
  89. var startInterval = Math.floor(node.data.timestamp / $scope.timelineIntervalDuration);
  90. var endInterval = Math.floor((node.data.timestamp + node.data.time) / $scope.timelineIntervalDuration);
  91. if (startInterval === endInterval) {
  92. $scope.timeline[startInterval] += node.data.time;
  93. } else {
  94. var timeToDispatch = node.data.time;
  95. var startIntervalPart = ((startInterval + 1) * $scope.timelineIntervalDuration) - node.data.timestamp;
  96. $scope.timeline[startInterval] += startIntervalPart;
  97. timeToDispatch -= startIntervalPart;
  98. var currentInterval = startInterval;
  99. while(currentInterval < endInterval && currentInterval + 1 < numberOfIntervals) {
  100. currentInterval ++;
  101. var currentIntervalPart = Math.min(timeToDispatch, $scope.timelineIntervalDuration);
  102. $scope.timeline[currentInterval] = currentIntervalPart;
  103. timeToDispatch -= currentIntervalPart;
  104. }
  105. }
  106. }
  107. if (node.data.type !== 'main') {
  108. // Don't check the children
  109. return false;
  110. }
  111. });
  112. $scope.timelineMax = Math.max.apply(Math, $scope.timeline);
  113. }
  114. function initMetricsView() {
  115. // Get the Phantomas modules from metadata
  116. $scope.metricsModule = {};
  117. for (var metricName in $scope.phantomasMetadata) {
  118. var metric = $scope.phantomasMetadata[metricName];
  119. if (!$scope.metricsModule[metric.module]) {
  120. $scope.metricsModule[metric.module] = {};
  121. }
  122. $scope.metricsModule[metric.module][metricName] = metric;
  123. }
  124. }
  125. function getDomComplexityScore() {
  126. var note = 'A';
  127. var score = $scope.phantomasResults.metrics.DOMelementsCount +
  128. Math.pow($scope.phantomasResults.metrics.DOMelementMaxDepth, 2) +
  129. $scope.phantomasResults.metrics.iframesCount * 50 +
  130. $scope.phantomasResults.metrics.DOMidDuplicated * 25;
  131. if (score > 1000) {
  132. note = 'B';
  133. }
  134. if (score > 1500) {
  135. note = 'C';
  136. }
  137. if (score > 2000) {
  138. note = 'D';
  139. }
  140. if (score > 3000) {
  141. note = 'E';
  142. }
  143. if (score > 4000) {
  144. note = 'F';
  145. }
  146. return note;
  147. }
  148. function getDomManipulationsScore() {
  149. var note = 'A';
  150. var score = $scope.phantomasResults.metrics.DOMinserts +
  151. $scope.phantomasResults.metrics.DOMqueries * 0.5 +
  152. $scope.totalJSTime;
  153. if (score > 100) {
  154. note = 'B';
  155. }
  156. if (score > 200) {
  157. note = 'C';
  158. }
  159. if (score > 300) {
  160. note = 'D';
  161. }
  162. if (score > 500) {
  163. note = 'E';
  164. }
  165. if (score > 800) {
  166. note = 'F';
  167. }
  168. return note;
  169. }
  170. function getDuplicatedDomQueriesScore() {
  171. var note = 'A';
  172. var score = $scope.duplicatedQueriesCountAll;
  173. if (score > 10) {
  174. note = 'B';
  175. }
  176. if (score > 50) {
  177. note = 'C';
  178. }
  179. if (score > 100) {
  180. note = 'D';
  181. }
  182. if (score > 200) {
  183. note = 'E';
  184. }
  185. if (score > 500) {
  186. note = 'F';
  187. }
  188. return note;
  189. }
  190. function getEventsBoundScore() {
  191. var note = 'A';
  192. var score = $scope.phantomasResults.metrics.eventsBound;
  193. if (score > 50) {
  194. note = 'B';
  195. }
  196. if (score > 100) {
  197. note = 'C';
  198. }
  199. if (score > 200) {
  200. note = 'D';
  201. }
  202. if (score > 500) {
  203. note = 'E';
  204. }
  205. if (score > 1000) {
  206. note = 'F';
  207. }
  208. return note;
  209. }
  210. function getJSBadPracticesScore() {
  211. var note = 'A';
  212. var score = $scope.phantomasResults.metrics.documentWriteCalls * 3 +
  213. $scope.phantomasResults.metrics.evalCalls * 2 +
  214. $scope.phantomasResults.metrics.jsErrors * 10 +
  215. $scope.phantomasResults.metrics.consoleMessages / 2 +
  216. $scope.phantomasResults.metrics.globalVariables / 10;
  217. if (score > 5) {
  218. note = 'B';
  219. }
  220. if (score > 10) {
  221. note = 'C';
  222. }
  223. if (score > 15) {
  224. note = 'D';
  225. }
  226. if (score > 25) {
  227. note = 'E';
  228. }
  229. if (score > 40) {
  230. note = 'F';
  231. }
  232. return note;
  233. }
  234. function getJQueryLoadingScore() {
  235. var note = 'NA';
  236. if ($scope.phantomasResults.metrics.jQueryDifferentVersions > 1) {
  237. note = 'F';
  238. } else if ($scope.phantomasResults.metrics.jQueryVersion) {
  239. if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.10.') === 0 ||
  240. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.11.') === 0 ||
  241. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.12.') === 0 ||
  242. $scope.phantomasResults.metrics.jQueryVersion.indexOf('2.0.') === 0 ||
  243. $scope.phantomasResults.metrics.jQueryVersion.indexOf('2.1.') === 0 ||
  244. $scope.phantomasResults.metrics.jQueryVersion.indexOf('2.2.') === 0) {
  245. note = 'A';
  246. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.8.') === 0 ||
  247. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.9.') === 0) {
  248. note = 'B';
  249. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.6.') === 0 ||
  250. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.7.') === 0) {
  251. note = 'C';
  252. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.4.') === 0 ||
  253. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.5.') === 0) {
  254. note = 'D';
  255. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.2.') === 0 ||
  256. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.3.') === 0) {
  257. note = 'E';
  258. }
  259. }
  260. return note;
  261. }
  262. function getCSSComplexityScore() {
  263. if ($scope.cssParsingError) {
  264. return 'F';
  265. } else if (!$scope.phantomasResults.metrics.cssRules) {
  266. return 'NA';
  267. }
  268. var note = 'A';
  269. var score = $scope.phantomasResults.metrics.cssRules +
  270. $scope.phantomasResults.metrics.cssComplexSelectors * 10;
  271. if (score > 500) {
  272. note = 'B';
  273. }
  274. if (score > 1000) {
  275. note = 'C';
  276. }
  277. if (score > 2000) {
  278. note = 'D';
  279. }
  280. if (score > 4500) {
  281. note = 'E';
  282. }
  283. if (score > 7000) {
  284. note = 'F';
  285. }
  286. return note;
  287. }
  288. function getBadCssScore() {
  289. if ($scope.cssParsingError) {
  290. return 'F';
  291. } else if (!$scope.phantomasResults.metrics.cssRules) {
  292. return 'NA';
  293. }
  294. var note = 'A';
  295. var score = $scope.phantomasResults.metrics.cssDuplicatedSelectors +
  296. $scope.phantomasResults.metrics.cssEmptyRules +
  297. $scope.phantomasResults.metrics.cssExpressions * 10 +
  298. $scope.phantomasResults.metrics.cssImportants * 2 +
  299. $scope.phantomasResults.metrics.cssOldIEFixes * 10 +
  300. $scope.phantomasResults.metrics.cssOldPropertyPrefixes +
  301. $scope.phantomasResults.metrics.cssUniversalSelectors * 5 +
  302. $scope.phantomasResults.metrics.cssRedundantBodySelectors;
  303. if (score > 20) {
  304. note = 'B';
  305. }
  306. if (score > 50) {
  307. note = 'C';
  308. }
  309. if (score > 100) {
  310. note = 'D';
  311. }
  312. if (score > 200) {
  313. note = 'E';
  314. }
  315. if (score > 500) {
  316. note = 'F';
  317. }
  318. return note;
  319. }
  320. function requestsScore() {
  321. var note = 'A';
  322. var score = $scope.phantomasResults.metrics.requests;
  323. if (score > 30) {
  324. note = 'B';
  325. }
  326. if (score > 45) {
  327. note = 'C';
  328. }
  329. if (score > 60) {
  330. note = 'D';
  331. }
  332. if (score > 80) {
  333. note = 'E';
  334. }
  335. if (score > 100) {
  336. note = 'F';
  337. }
  338. return note;
  339. }
  340. function networkScore() {
  341. var note = 'A';
  342. var score = $scope.phantomasResults.metrics.notFound * 25 +
  343. $scope.phantomasResults.metrics.closedConnections * 10 +
  344. $scope.phantomasResults.metrics.multipleRequests * 10 +
  345. $scope.phantomasResults.metrics.cachingDisabled * 2 +
  346. $scope.phantomasResults.metrics.cachingNotSpecified +
  347. $scope.phantomasResults.metrics.cachingTooShort / 2 +
  348. $scope.phantomasResults.metrics.domains;
  349. if (score > 20) {
  350. note = 'B';
  351. }
  352. if (score > 40) {
  353. note = 'C';
  354. }
  355. if (score > 60) {
  356. note = 'D';
  357. }
  358. if (score > 80) {
  359. note = 'E';
  360. }
  361. if (score > 100) {
  362. note = 'F';
  363. }
  364. return note;
  365. }
  366. function parseBacktrace(str) {
  367. if (!str) {
  368. return null;
  369. }
  370. var out = [];
  371. var splited = str.split(' / ');
  372. splited.forEach(function(trace) {
  373. var result = /^(\S*)\s?\(?(https?:\/\/\S+):(\d+)\)?$/g.exec(trace);
  374. if (result && result[2].length > 0) {
  375. var filePath = result[2];
  376. var chunks = filePath.split('/');
  377. var fileName = chunks[chunks.length - 1];
  378. out.push({
  379. fnName: result[1],
  380. fileName: fileName,
  381. filePath: filePath,
  382. line: result[3]
  383. });
  384. }
  385. });
  386. return out;
  387. }
  388. // Goes on every node of the tree and calls the function fn. If fn returns false on a node, its children won't be checked.
  389. function treeRunner(node, fn) {
  390. if (fn(node) !== false && node.children) {
  391. node.children.forEach(function(child) {
  392. treeRunner(child, fn);
  393. });
  394. }
  395. }
  396. });