grafanaCtrl.js 3.6 KB

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