grafana_app.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import store from 'app/core/store';
  4. import _ from 'lodash';
  5. import angular from 'angular';
  6. import $ from 'jquery';
  7. import coreModule from 'app/core/core_module';
  8. export class GrafanaCtrl {
  9. /** @ngInject */
  10. constructor($scope, alertSrv, utilSrv, $rootScope, $controller, contextSrv) {
  11. $scope.init = function() {
  12. $scope.contextSrv = contextSrv;
  13. $scope._ = _;
  14. $rootScope.profilingEnabled = store.getBool('profilingEnabled');
  15. $rootScope.performance = { loadStart: new Date().getTime() };
  16. $rootScope.appSubUrl = config.appSubUrl;
  17. if ($rootScope.profilingEnabled) { $scope.initProfiling(); }
  18. alertSrv.init();
  19. utilSrv.init();
  20. $scope.dashAlerts = alertSrv;
  21. };
  22. $scope.initDashboard = function(dashboardData, viewScope) {
  23. $controller('DashboardCtrl', { $scope: viewScope }).init(dashboardData);
  24. };
  25. $rootScope.onAppEvent = function(name, callback, localScope) {
  26. var unbind = $rootScope.$on(name, callback);
  27. var callerScope = this;
  28. if (callerScope.$id === 1 && !localScope) {
  29. console.log('warning rootScope onAppEvent called without localscope');
  30. }
  31. if (localScope) {
  32. callerScope = localScope;
  33. }
  34. callerScope.$on('$destroy', unbind);
  35. };
  36. $rootScope.appEvent = function(name, payload) {
  37. $rootScope.$emit(name, payload);
  38. };
  39. $rootScope.colors = [
  40. "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0",
  41. "#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C","#6D1F62","#584477",
  42. "#B7DBAB","#F4D598","#70DBED","#F9BA8F","#F29191","#82B5D8","#E5A8E2","#AEA2E0",
  43. "#629E51","#E5AC0E","#64B0C8","#E0752D","#BF1B00","#0A50A1","#962D82","#614D93",
  44. "#9AC48A","#F2C96D","#65C5DB","#F9934E","#EA6460","#5195CE","#D683CE","#806EB7",
  45. "#3F6833","#967302","#2F575E","#99440A","#58140C","#052B51","#511749","#3F2B5B",
  46. "#E0F9D7","#FCEACA","#CFFAFF","#F9E2D2","#FCE2DE","#BADFF4","#F9D9F9","#DEDAF7"
  47. ];
  48. $scope.getTotalWatcherCount = function() {
  49. var count = 0;
  50. var scopes = 0;
  51. var root = $(document.getElementsByTagName('body'));
  52. var f = function (element) {
  53. if (element.data().hasOwnProperty('$scope')) {
  54. scopes++;
  55. angular.forEach(element.data().$scope.$$watchers, function () {
  56. count++;
  57. });
  58. }
  59. angular.forEach(element.children(), function (childElement) {
  60. f($(childElement));
  61. });
  62. };
  63. f(root);
  64. $rootScope.performance.scopeCount = scopes;
  65. return count;
  66. };
  67. $scope.initProfiling = function() {
  68. var count = 0;
  69. $scope.$watch(function digestCounter() {
  70. count++;
  71. }, function() {
  72. // something
  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: any) {
  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. }
  109. /** @ngInject */
  110. export function grafanaAppDirective(playlistSrv) {
  111. return {
  112. restrict: 'E',
  113. controller: GrafanaCtrl,
  114. link: (scope, elem) => {
  115. var ignoreSideMenuHide;
  116. // handle sidemenu open state
  117. scope.$watch('contextSrv.sidemenu', newVal => {
  118. if (newVal !== undefined) {
  119. elem.toggleClass('sidemenu-open', scope.contextSrv.sidemenu);
  120. }
  121. if (scope.contextSrv.sidemenu) {
  122. ignoreSideMenuHide = true;
  123. setTimeout(() => {
  124. ignoreSideMenuHide = false;
  125. }, 300);
  126. }
  127. });
  128. // tooltip removal fix
  129. scope.$on("$routeChangeSuccess", function() {
  130. $("#tooltip, .tooltip").remove();
  131. });
  132. // handle document clicks that should hide things
  133. elem.click(function(evt) {
  134. var target = $(evt.target);
  135. if (target.parents().length === 0) {
  136. return;
  137. }
  138. if (target.parents('.dash-playlist-actions').length === 0) {
  139. playlistSrv.stop();
  140. }
  141. // hide search
  142. if (elem.find('.search-container').length > 0) {
  143. if (target.parents('.search-container').length === 0) {
  144. scope.appEvent('hide-dash-search');
  145. }
  146. }
  147. // hide sidemenu
  148. if (!ignoreSideMenuHide && elem.find('.sidemenu').length > 0) {
  149. if (target.parents('.sidemenu').length === 0) {
  150. scope.$apply(() => scope.contextSrv.toggleSideMenu());
  151. }
  152. }
  153. // hide popovers
  154. var popover = elem.find('.popover');
  155. if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
  156. popover.hide();
  157. }
  158. });
  159. }
  160. };
  161. }
  162. coreModule.directive('grafanaApp', grafanaAppDirective);