resultsCtrl.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. jsDomManipulations: getJsDomManipulationsScore(),
  62. jsBadPractices: getJSBadPracticesScore(),
  63. jQueryLoading: getJQueryLoadingScore(),
  64. cssComplexity: getCSSComplexityScore(),
  65. badCss: getBadCssScore(),
  66. requests: requestsScore(),
  67. network: networkScore()
  68. };
  69. }
  70. function initExecutionView() {
  71. $scope.slowRequestsOn = false;
  72. $scope.slowRequestsLimit = 5;
  73. if (!$scope.javascript.children) {
  74. return;
  75. }
  76. // Now read the tree and display it on a timeline
  77. // Split the timeline into 200 intervals
  78. var numberOfIntervals = 200;
  79. var lastEvent = $scope.javascript.children[$scope.javascript.children.length - 1];
  80. $scope.endTime = lastEvent.data.timestamp + (lastEvent.data.time || 0);
  81. $scope.timelineIntervalDuration = $scope.endTime / numberOfIntervals;
  82. // Pre-filled array of 100 elements
  83. $scope.timeline = Array.apply(null, new Array(numberOfIntervals)).map(Number.prototype.valueOf,0);
  84. treeRunner($scope.javascript, function(node) {
  85. if (node.data.time) {
  86. // If a node is between two intervals, split it. That's the meaning of the following dirty algorithm.
  87. var startInterval = Math.floor(node.data.timestamp / $scope.timelineIntervalDuration);
  88. var endInterval = Math.floor((node.data.timestamp + node.data.time) / $scope.timelineIntervalDuration);
  89. if (startInterval === endInterval) {
  90. $scope.timeline[startInterval] += node.data.time;
  91. } else {
  92. var timeToDispatch = node.data.time;
  93. var startIntervalPart = ((startInterval + 1) * $scope.timelineIntervalDuration) - node.data.timestamp;
  94. $scope.timeline[startInterval] += startIntervalPart;
  95. timeToDispatch -= startIntervalPart;
  96. var currentInterval = startInterval;
  97. while(currentInterval < endInterval && currentInterval + 1 < numberOfIntervals) {
  98. currentInterval ++;
  99. var currentIntervalPart = Math.min(timeToDispatch, $scope.timelineIntervalDuration);
  100. $scope.timeline[currentInterval] = currentIntervalPart;
  101. timeToDispatch -= currentIntervalPart;
  102. }
  103. }
  104. }
  105. if (node.data.type !== 'main') {
  106. // Don't check the children
  107. return false;
  108. }
  109. });
  110. $scope.timelineMax = Math.max.apply(Math, $scope.timeline);
  111. }
  112. function initMetricsView() {
  113. // Get the Phantomas modules from metadata
  114. $scope.metricsModule = {};
  115. for (var metricName in $scope.phantomasMetadata) {
  116. var metric = $scope.phantomasMetadata[metricName];
  117. if (!$scope.metricsModule[metric.module]) {
  118. $scope.metricsModule[metric.module] = {};
  119. }
  120. $scope.metricsModule[metric.module][metricName] = metric;
  121. }
  122. }
  123. function getDomComplexityScore() {
  124. var note = 'A';
  125. var score = $scope.phantomasResults.metrics.DOMelementsCount +
  126. Math.pow($scope.phantomasResults.metrics.DOMelementMaxDepth, 2) +
  127. $scope.phantomasResults.metrics.iframesCount * 50 +
  128. $scope.phantomasResults.metrics.DOMidDuplicated * 25;
  129. if (score > 1000) {
  130. note = 'B';
  131. }
  132. if (score > 1500) {
  133. note = 'C';
  134. }
  135. if (score > 2000) {
  136. note = 'D';
  137. }
  138. if (score > 3000) {
  139. note = 'E';
  140. }
  141. if (score > 4000) {
  142. note = 'F';
  143. }
  144. return note;
  145. }
  146. function getJsDomManipulationsScore() {
  147. var note = 'A';
  148. var score = $scope.phantomasResults.metrics.DOMinserts * 2 +
  149. $scope.phantomasResults.metrics.DOMqueries +
  150. $scope.duplicatedQueriesCountAll * 2 +
  151. $scope.phantomasResults.metrics.eventsBound;
  152. if (score > 300) {
  153. note = 'B';
  154. }
  155. if (score > 500) {
  156. note = 'C';
  157. }
  158. if (score > 700) {
  159. note = 'D';
  160. }
  161. if (score > 1000) {
  162. note = 'E';
  163. }
  164. if (score > 1400) {
  165. note = 'F';
  166. }
  167. return note;
  168. }
  169. function getJSBadPracticesScore() {
  170. var note = 'A';
  171. var score = $scope.phantomasResults.metrics.documentWriteCalls * 3 +
  172. $scope.phantomasResults.metrics.evalCalls * 2 +
  173. $scope.phantomasResults.metrics.jsErrors * 10 +
  174. $scope.phantomasResults.metrics.consoleMessages / 2 +
  175. $scope.phantomasResults.metrics.globalVariables / 20;
  176. if (score > 5) {
  177. note = 'B';
  178. }
  179. if (score > 10) {
  180. note = 'C';
  181. }
  182. if (score > 15) {
  183. note = 'D';
  184. }
  185. if (score > 25) {
  186. note = 'E';
  187. }
  188. if (score > 40) {
  189. note = 'F';
  190. }
  191. return note;
  192. }
  193. function getJQueryLoadingScore() {
  194. var note = 'NA';
  195. if ($scope.phantomasResults.metrics.jQueryDifferentVersions > 1) {
  196. note = 'F';
  197. } else if ($scope.phantomasResults.metrics.jQueryVersion) {
  198. if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.10.') === 0 ||
  199. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.11.') === 0 ||
  200. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.12.') === 0 ||
  201. $scope.phantomasResults.metrics.jQueryVersion.indexOf('2.0.') === 0 ||
  202. $scope.phantomasResults.metrics.jQueryVersion.indexOf('2.1.') === 0 ||
  203. $scope.phantomasResults.metrics.jQueryVersion.indexOf('2.2.') === 0) {
  204. note = 'A';
  205. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.8.') === 0 ||
  206. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.9.') === 0) {
  207. note = 'B';
  208. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.6.') === 0 ||
  209. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.7.') === 0) {
  210. note = 'C';
  211. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.4.') === 0 ||
  212. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.5.') === 0) {
  213. note = 'D';
  214. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.2.') === 0 ||
  215. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.3.') === 0) {
  216. note = 'E';
  217. }
  218. }
  219. return note;
  220. }
  221. function getCSSComplexityScore() {
  222. if ($scope.cssParsingError) {
  223. return 'F';
  224. } else if (!$scope.phantomasResults.metrics.cssRules) {
  225. return 'NA';
  226. }
  227. var note = 'A';
  228. var score = $scope.phantomasResults.metrics.cssRules +
  229. $scope.phantomasResults.metrics.cssComplexSelectors * 10;
  230. if (score > 500) {
  231. note = 'B';
  232. }
  233. if (score > 1000) {
  234. note = 'C';
  235. }
  236. if (score > 2000) {
  237. note = 'D';
  238. }
  239. if (score > 4500) {
  240. note = 'E';
  241. }
  242. if (score > 7000) {
  243. note = 'F';
  244. }
  245. return note;
  246. }
  247. function getBadCssScore() {
  248. if ($scope.cssParsingError) {
  249. return 'F';
  250. } else if (!$scope.phantomasResults.metrics.cssRules) {
  251. return 'NA';
  252. }
  253. var note = 'A';
  254. var score = $scope.phantomasResults.metrics.cssDuplicatedSelectors +
  255. $scope.phantomasResults.metrics.cssEmptyRules +
  256. $scope.phantomasResults.metrics.cssExpressions * 10 +
  257. $scope.phantomasResults.metrics.cssImportants * 2 +
  258. $scope.phantomasResults.metrics.cssOldIEFixes * 10 +
  259. $scope.phantomasResults.metrics.cssOldPropertyPrefixes +
  260. $scope.phantomasResults.metrics.cssUniversalSelectors * 5 +
  261. $scope.phantomasResults.metrics.cssRedundantBodySelectors;
  262. if (score > 20) {
  263. note = 'B';
  264. }
  265. if (score > 50) {
  266. note = 'C';
  267. }
  268. if (score > 100) {
  269. note = 'D';
  270. }
  271. if (score > 200) {
  272. note = 'E';
  273. }
  274. if (score > 500) {
  275. note = 'F';
  276. }
  277. return note;
  278. }
  279. function requestsScore() {
  280. var note = 'A';
  281. var score = $scope.phantomasResults.metrics.requests;
  282. if (score > 30) {
  283. note = 'B';
  284. }
  285. if (score > 45) {
  286. note = 'C';
  287. }
  288. if (score > 60) {
  289. note = 'D';
  290. }
  291. if (score > 80) {
  292. note = 'E';
  293. }
  294. if (score > 100) {
  295. note = 'F';
  296. }
  297. return note;
  298. }
  299. function networkScore() {
  300. var note = 'A';
  301. var score = $scope.phantomasResults.metrics.notFound * 25 +
  302. $scope.phantomasResults.metrics.closedConnections * 10 +
  303. $scope.phantomasResults.metrics.multipleRequests * 10 +
  304. $scope.phantomasResults.metrics.cachingDisabled * 2 +
  305. $scope.phantomasResults.metrics.cachingNotSpecified +
  306. $scope.phantomasResults.metrics.cachingTooShort / 2 +
  307. $scope.phantomasResults.metrics.domains;
  308. if (score > 20) {
  309. note = 'B';
  310. }
  311. if (score > 40) {
  312. note = 'C';
  313. }
  314. if (score > 60) {
  315. note = 'D';
  316. }
  317. if (score > 80) {
  318. note = 'E';
  319. }
  320. if (score > 100) {
  321. note = 'F';
  322. }
  323. return note;
  324. }
  325. function parseBacktrace(str) {
  326. if (!str) {
  327. return null;
  328. }
  329. var out = [];
  330. var splited = str.split(' / ');
  331. splited.forEach(function(trace) {
  332. var result = /^(\S*)\s?\(?(https?:\/\/\S+):(\d+)\)?$/g.exec(trace);
  333. if (result && result[2].length > 0) {
  334. var filePath = result[2];
  335. var chunks = filePath.split('/');
  336. var fileName = chunks[chunks.length - 1];
  337. out.push({
  338. fnName: result[1],
  339. fileName: fileName,
  340. filePath: filePath,
  341. line: result[3]
  342. });
  343. }
  344. });
  345. return out;
  346. }
  347. // 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.
  348. function treeRunner(node, fn) {
  349. if (fn(node) !== false && node.children) {
  350. node.children.forEach(function(child) {
  351. treeRunner(child, fn);
  352. });
  353. }
  354. }
  355. });