resultsCtrl.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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.view = 'summary';
  6. if ($scope.phantomasResults.metrics && $scope.phantomasResults.offenders && $scope.phantomasResults.offenders.javascriptExecutionTree) {
  7. // Get the execution tree from the offenders
  8. $scope.javascript = JSON.parse($scope.phantomasResults.offenders.javascriptExecutionTree);
  9. // Sort globalVariables offenders alphabetically
  10. if ($scope.phantomasResults.offenders.globalVariables) {
  11. $scope.phantomasResults.offenders.globalVariables.sort();
  12. }
  13. initSummaryView();
  14. initJSTimelineView();
  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. $scope.inBodyDomManipulations = 0;
  37. treeRunner($scope.javascript, function(node) {
  38. if (node.data.time) {
  39. $scope.totalJSTime += node.data.time;
  40. }
  41. if (node.data.timestamp < $scope.phantomasResults.metrics.domInteractive &&
  42. node.data.type !== 'jQuery - onDOMReady') {
  43. $scope.inBodyDomManipulations ++;
  44. }
  45. if (node.data.type !== 'main') {
  46. // Don't check the children
  47. return false;
  48. }
  49. });
  50. // Grab the notes
  51. $scope.notations = {
  52. domComplexity: getDomComplexityScore(),
  53. jsDomManipulations: getJsDomManipulationsScore(),
  54. jsBadPractices: getJSBadPracticesScore(),
  55. jQueryLoading: getJQueryLoadingScore(),
  56. cssComplexity: getCSSComplexityScore(),
  57. badCss: getBadCssScore(),
  58. requests: requestsScore(),
  59. network: networkScore()
  60. };
  61. }
  62. function initJSTimelineView() {
  63. if (!$scope.javascript.children) {
  64. return;
  65. }
  66. // Read the execution tree and adjust the navigation timings (cause their not very well synchronised)
  67. treeRunner($scope.javascript, function(node) {
  68. switch(node.data.type) {
  69. case 'domInteractive':
  70. $scope.phantomasResults.metrics.domInteractive = node.data.timestamp;
  71. break;
  72. case 'domContentLoaded':
  73. $scope.phantomasResults.metrics.domContentLoaded = node.data.timestamp;
  74. break;
  75. case 'domContentLoadedEnd':
  76. $scope.phantomasResults.metrics.domContentLoadedEnd = node.data.timestamp;
  77. break;
  78. case 'domComplete':
  79. $scope.phantomasResults.metrics.domComplete = node.data.timestamp;
  80. break;
  81. }
  82. if (node.data.type !== 'main') {
  83. // Don't check the children
  84. return false;
  85. }
  86. });
  87. // Now read the tree and display it on a timeline
  88. // Split the timeline into 200 intervals
  89. var numberOfIntervals = 199;
  90. var lastEvent = $scope.javascript.children[$scope.javascript.children.length - 1];
  91. $scope.endTime = lastEvent.data.timestamp + (lastEvent.data.time || 0);
  92. $scope.timelineIntervalDuration = $scope.endTime / numberOfIntervals;
  93. // Pre-fill array of as many elements as there are milleseconds
  94. var millisecondsArray = Array.apply(null, new Array($scope.endTime + 1)).map(Number.prototype.valueOf,0);
  95. // Create the milliseconds array from the execution tree
  96. treeRunner($scope.javascript, function(node) {
  97. if (node.data.time !== undefined) {
  98. // Ignore artefacts (durations > 100ms)
  99. var time = Math.min(node.data.time, 100) || 1;
  100. for (var i=node.data.timestamp, max=node.data.timestamp + time ; i<max ; i++) {
  101. millisecondsArray[i] |= 1;
  102. }
  103. }
  104. if (node.data.type !== 'main') {
  105. // Don't check the children
  106. return false;
  107. }
  108. });
  109. // Pre-fill array of 200 elements
  110. $scope.timeline = Array.apply(null, new Array(numberOfIntervals + 1)).map(Number.prototype.valueOf,0);
  111. // Create the timeline from the milliseconds array
  112. millisecondsArray.forEach(function(value, timestamp) {
  113. if (value === 1) {
  114. $scope.timeline[Math.floor(timestamp / $scope.timelineIntervalDuration)] += 1;
  115. }
  116. });
  117. // Get the maximum value of the array (needed for display)
  118. $scope.timelineMax = Math.max.apply(Math, $scope.timeline);
  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. $scope.phantomasResults.metrics.DOMidDuplicated * 25;
  126. if (score > 1000) {
  127. note = 'B';
  128. }
  129. if (score > 1500) {
  130. note = 'C';
  131. }
  132. if (score > 2000) {
  133. note = 'D';
  134. }
  135. if (score > 3000) {
  136. note = 'E';
  137. }
  138. if (score > 4000) {
  139. note = 'F';
  140. }
  141. return note;
  142. }
  143. function getJsDomManipulationsScore() {
  144. var note = 'A';
  145. var score = $scope.phantomasResults.metrics.DOMinserts * 2 +
  146. $scope.phantomasResults.metrics.DOMqueries +
  147. $scope.phantomasResults.metrics.DOMqueriesAvoidable * 2 +
  148. $scope.phantomasResults.metrics.eventsBound;
  149. if (score > 300) {
  150. note = 'B';
  151. }
  152. if (score > 500) {
  153. note = 'C';
  154. }
  155. if (score > 700) {
  156. note = 'D';
  157. }
  158. if (score > 1000) {
  159. note = 'E';
  160. }
  161. if (score > 1400) {
  162. note = 'F';
  163. }
  164. return note;
  165. }
  166. function getJSBadPracticesScore() {
  167. var note = 'A';
  168. var score = $scope.phantomasResults.metrics.documentWriteCalls * 3 +
  169. $scope.phantomasResults.metrics.evalCalls * 2 +
  170. $scope.phantomasResults.metrics.jsErrors * 10 +
  171. $scope.phantomasResults.metrics.consoleMessages / 2 +
  172. $scope.phantomasResults.metrics.globalVariables / 20 +
  173. Math.sqrt($scope.inBodyDomManipulations);
  174. if (score > 10) {
  175. note = 'B';
  176. }
  177. if (score > 15) {
  178. note = 'C';
  179. }
  180. if (score > 20) {
  181. note = 'D';
  182. }
  183. if (score > 30) {
  184. note = 'E';
  185. }
  186. if (score > 45) {
  187. note = 'F';
  188. }
  189. return note;
  190. }
  191. function getJQueryLoadingScore() {
  192. var note = 'NA';
  193. if ($scope.phantomasResults.metrics.jQueryDifferentVersions > 1) {
  194. note = 'F';
  195. } else if ($scope.phantomasResults.metrics.jQueryVersion) {
  196. if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.11.') === 0 ||
  197. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.12.') === 0 ||
  198. $scope.phantomasResults.metrics.jQueryVersion.indexOf('2.1.') === 0 ||
  199. $scope.phantomasResults.metrics.jQueryVersion.indexOf('2.2.') === 0) {
  200. note = 'A';
  201. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.9.') === 0 ||
  202. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.10.') === 0 ||
  203. $scope.phantomasResults.metrics.jQueryVersion.indexOf('2.0.') === 0) {
  204. note = 'B';
  205. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.7.') === 0 ||
  206. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.8.') === 0) {
  207. note = 'C';
  208. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.5.') === 0 ||
  209. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.6.') === 0) {
  210. note = 'D';
  211. } else if ($scope.phantomasResults.metrics.jQueryVersion.indexOf('1.2.') === 0 ||
  212. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.3.') === 0 ||
  213. $scope.phantomasResults.metrics.jQueryVersion.indexOf('1.4.') === 0) {
  214. note = 'E';
  215. }
  216. }
  217. return note;
  218. }
  219. function getCSSComplexityScore() {
  220. if (!$scope.phantomasResults.metrics.cssRules) {
  221. return 'NA';
  222. }
  223. var note = 'A';
  224. var score = $scope.phantomasResults.metrics.cssRules +
  225. $scope.phantomasResults.metrics.cssComplexSelectors * 5 +
  226. $scope.phantomasResults.metrics.cssComplexSelectorsByAttribute * 10;
  227. if (score > 800) {
  228. note = 'B';
  229. }
  230. if (score > 1200) {
  231. note = 'C';
  232. }
  233. if (score > 2500) {
  234. note = 'D';
  235. }
  236. if (score > 4000) {
  237. note = 'E';
  238. }
  239. if (score > 6000) {
  240. note = 'F';
  241. }
  242. return note;
  243. }
  244. function getBadCssScore() {
  245. if ($scope.phantomasResults.metrics.cssParsingErrors) {
  246. return 'F';
  247. } else if (!$scope.phantomasResults.metrics.cssRules) {
  248. return 'NA';
  249. }
  250. var note = 'A';
  251. var score = $scope.phantomasResults.metrics.cssDuplicatedSelectors +
  252. $scope.phantomasResults.metrics.cssDuplicatedProperties +
  253. $scope.phantomasResults.metrics.cssEmptyRules +
  254. $scope.phantomasResults.metrics.cssExpressions * 10 +
  255. $scope.phantomasResults.metrics.cssImportants * 2 +
  256. $scope.phantomasResults.metrics.cssOldIEFixes * 10 +
  257. $scope.phantomasResults.metrics.cssOldPropertyPrefixes +
  258. $scope.phantomasResults.metrics.cssUniversalSelectors * 5 +
  259. $scope.phantomasResults.metrics.cssRedundantBodySelectors * 0.5 +
  260. $scope.phantomasResults.metrics.cssRedundantChildNodesSelectors * 0.5 +
  261. $scope.phantomasResults.metrics.cssImports * 50;
  262. if (score > 50) {
  263. note = 'B';
  264. }
  265. if (score > 100) {
  266. note = 'C';
  267. }
  268. if (score > 200) {
  269. note = 'D';
  270. }
  271. if (score > 500) {
  272. note = 'E';
  273. }
  274. if (score > 1000) {
  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. });