grafana_ctrl.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'jquery',
  5. '../core_module',
  6. 'app/core/config',
  7. 'app/core/store',
  8. ],
  9. function (angular, _, $, coreModule, config, store) {
  10. "use strict";
  11. coreModule.controller('GrafanaCtrl', function($scope, alertSrv, utilSrv, $rootScope, $controller, contextSrv) {
  12. $scope.init = function() {
  13. $scope.contextSrv = contextSrv;
  14. $scope._ = _;
  15. $rootScope.profilingEnabled = store.getBool('profilingEnabled');
  16. $rootScope.performance = { loadStart: new Date().getTime() };
  17. $rootScope.appSubUrl = config.appSubUrl;
  18. if ($rootScope.profilingEnabled) { $scope.initProfiling(); }
  19. alertSrv.init();
  20. utilSrv.init();
  21. $scope.dashAlerts = alertSrv;
  22. };
  23. $scope.initDashboard = function(dashboardData, viewScope) {
  24. $controller('DashboardCtrl', { $scope: viewScope }).init(dashboardData);
  25. };
  26. $rootScope.onAppEvent = function(name, callback, localScope) {
  27. var unbind = $rootScope.$on(name, callback);
  28. var callerScope = this;
  29. if (callerScope.$id === 1 && !localScope) {
  30. console.log('warning rootScope onAppEvent called without localscope');
  31. }
  32. if (localScope) {
  33. callerScope = localScope;
  34. }
  35. callerScope.$on('$destroy', unbind);
  36. };
  37. $rootScope.appEvent = function(name, payload) {
  38. $rootScope.$emit(name, payload);
  39. };
  40. $rootScope.colors = [
  41. "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0", //1
  42. "#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C","#6D1F62","#584477", //2
  43. "#B7DBAB","#F4D598","#70DBED","#F9BA8F","#F29191","#82B5D8","#E5A8E2","#AEA2E0", //3
  44. "#629E51","#E5AC0E","#64B0C8","#E0752D","#BF1B00","#0A50A1","#962D82","#614D93", //4
  45. "#9AC48A","#F2C96D","#65C5DB","#F9934E","#EA6460","#5195CE","#D683CE","#806EB7", //5
  46. "#3F6833","#967302","#2F575E","#99440A","#58140C","#052B51","#511749","#3F2B5B", //6
  47. "#E0F9D7","#FCEACA","#CFFAFF","#F9E2D2","#FCE2DE","#BADFF4","#F9D9F9","#DEDAF7" //7
  48. ];
  49. $scope.getTotalWatcherCount = function() {
  50. var count = 0;
  51. var scopes = 0;
  52. var root = $(document.getElementsByTagName('body'));
  53. var f = function (element) {
  54. if (element.data().hasOwnProperty('$scope')) {
  55. scopes++;
  56. angular.forEach(element.data().$scope.$$watchers, function () {
  57. count++;
  58. });
  59. }
  60. angular.forEach(element.children(), function (childElement) {
  61. f($(childElement));
  62. });
  63. };
  64. f(root);
  65. $rootScope.performance.scopeCount = scopes;
  66. return count;
  67. };
  68. $scope.initProfiling = function() {
  69. var count = 0;
  70. $scope.$watch(function digestCounter() {
  71. count++;
  72. }, function() {
  73. });
  74. $rootScope.performance.panels = [];
  75. $scope.$on('refresh', function() {
  76. if ($rootScope.performance.panels.length > 0) {
  77. var totalRender = 0;
  78. var totalQuery = 0;
  79. _.each($rootScope.performance.panels, function(panelTiming) {
  80. totalRender += panelTiming.render;
  81. totalQuery += panelTiming.query;
  82. });
  83. console.log('total query: ' + totalQuery);
  84. console.log('total render: ' + totalRender);
  85. console.log('avg render: ' + totalRender / $rootScope.performance.panels.length);
  86. }
  87. $rootScope.performance.panels = [];
  88. });
  89. $scope.onAppEvent('dashboard-loaded', function() {
  90. count = 0;
  91. setTimeout(function() {
  92. console.log("Dashboard::Performance Total Digests: " + count);
  93. console.log("Dashboard::Performance Total Watchers: " + $scope.getTotalWatcherCount());
  94. console.log("Dashboard::Performance Total ScopeCount: " + $rootScope.performance.scopeCount);
  95. var timeTaken = $rootScope.performance.allPanelsInitialized - $rootScope.performance.dashboardLoadStart;
  96. console.log("Dashboard::Performance - All panels initialized in " + timeTaken + " ms");
  97. // measure digest performance
  98. var rootDigestStart = window.performance.now();
  99. for (var i = 0; i < 30; i++) {
  100. $rootScope.$apply();
  101. }
  102. console.log("Dashboard::Performance Root Digest " + ((window.performance.now() - rootDigestStart) / 30));
  103. }, 3000);
  104. });
  105. };
  106. $scope.init();
  107. });
  108. });