p_grafanaCtrl.js 3.9 KB

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