resultsCtrl.js 14 KB

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