grafana_app.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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') || config.buildInfo.env === 'development';
  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, contextSrv) {
  111. return {
  112. restrict: 'E',
  113. controller: GrafanaCtrl,
  114. link: (scope, elem) => {
  115. var ignoreSideMenuHide;
  116. var body = $('body');
  117. // handle sidemenu open state
  118. scope.$watch('contextSrv.sidemenu', newVal => {
  119. if (newVal !== undefined) {
  120. body.toggleClass('sidemenu-open', scope.contextSrv.sidemenu);
  121. if (!newVal) {
  122. contextSrv.setPinnedState(false);
  123. }
  124. }
  125. if (contextSrv.sidemenu) {
  126. ignoreSideMenuHide = true;
  127. setTimeout(() => {
  128. ignoreSideMenuHide = false;
  129. }, 300);
  130. }
  131. });
  132. scope.$watch('contextSrv.pinned', newVal => {
  133. if (newVal !== undefined) {
  134. body.toggleClass('sidemenu-pinned', newVal);
  135. }
  136. });
  137. // tooltip removal fix
  138. // manage page classes
  139. var pageClass;
  140. scope.$on("$routeChangeSuccess", function(evt, data) {
  141. if (pageClass) {
  142. body.removeClass(pageClass);
  143. }
  144. pageClass = data.$$route.pageClass;
  145. if (pageClass) {
  146. body.addClass(pageClass);
  147. }
  148. $("#tooltip, .tooltip").remove();
  149. });
  150. // handle document clicks that should hide things
  151. body.click(function(evt) {
  152. var target = $(evt.target);
  153. if (target.parents().length === 0) {
  154. return;
  155. }
  156. if (target.parents('.dash-playlist-actions').length === 0) {
  157. playlistSrv.stop();
  158. }
  159. // hide search
  160. if (body.find('.search-container').length > 0) {
  161. if (target.parents('.search-container').length === 0) {
  162. scope.$apply(function() {
  163. scope.appEvent('hide-dash-search');
  164. });
  165. }
  166. }
  167. // hide sidemenu
  168. if (!ignoreSideMenuHide && !contextSrv.pinned && body.find('.sidemenu').length > 0) {
  169. if (target.parents('.sidemenu').length === 0) {
  170. scope.$apply(function() {
  171. scope.contextSrv.toggleSideMenu();
  172. });
  173. }
  174. }
  175. // hide popovers
  176. var popover = elem.find('.popover');
  177. if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
  178. popover.hide();
  179. }
  180. });
  181. }
  182. };
  183. }
  184. coreModule.directive('grafanaApp', grafanaAppDirective);