grafana_ctrl.js 4.4 KB

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