grafana_app.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. import appEvents from 'app/core/app_events';
  9. export class GrafanaCtrl {
  10. /** @ngInject */
  11. constructor($scope, alertSrv, utilSrv, $rootScope, $controller, contextSrv) {
  12. $scope.init = function() {
  13. $scope.contextSrv = contextSrv;
  14. $scope._ = _;
  15. $rootScope.profilingEnabled = store.getBool('profilingEnabled');
  16. $rootScope.performance = { loadStart: new Date().getTime() };
  17. $rootScope.appSubUrl = config.appSubUrl;
  18. if ($rootScope.profilingEnabled) { $scope.initProfiling(); }
  19. alertSrv.init();
  20. utilSrv.init();
  21. $scope.dashAlerts = alertSrv;
  22. };
  23. $scope.initDashboard = function(dashboardData, viewScope) {
  24. $controller('DashboardCtrl', { $scope: viewScope }).init(dashboardData);
  25. };
  26. $rootScope.onAppEvent = function(name, callback, localScope) {
  27. var unbind = $rootScope.$on(name, callback);
  28. var callerScope = this;
  29. if (callerScope.$id === 1 && !localScope) {
  30. console.log('warning rootScope onAppEvent called without localscope');
  31. }
  32. if (localScope) {
  33. callerScope = localScope;
  34. }
  35. callerScope.$on('$destroy', unbind);
  36. };
  37. $rootScope.appEvent = function(name, payload) {
  38. $rootScope.$emit(name, payload);
  39. appEvents.emit(name, payload);
  40. };
  41. $rootScope.colors = [
  42. "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0",
  43. "#508642","#CCA300","#447EBC","#C15C17","#890F02","#0A437C","#6D1F62","#584477",
  44. "#B7DBAB","#F4D598","#70DBED","#F9BA8F","#F29191","#82B5D8","#E5A8E2","#AEA2E0",
  45. "#629E51","#E5AC0E","#64B0C8","#E0752D","#BF1B00","#0A50A1","#962D82","#614D93",
  46. "#9AC48A","#F2C96D","#65C5DB","#F9934E","#EA6460","#5195CE","#D683CE","#806EB7",
  47. "#3F6833","#967302","#2F575E","#99440A","#58140C","#052B51","#511749","#3F2B5B",
  48. "#E0F9D7","#FCEACA","#CFFAFF","#F9E2D2","#FCE2DE","#BADFF4","#F9D9F9","#DEDAF7"
  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. // something
  75. });
  76. $rootScope.performance.panels = [];
  77. $scope.$on('refresh', function() {
  78. if ($rootScope.performance.panels.length > 0) {
  79. var totalRender = 0;
  80. var totalQuery = 0;
  81. _.each($rootScope.performance.panels, function(panelTiming: any) {
  82. totalRender += panelTiming.render;
  83. totalQuery += panelTiming.query;
  84. });
  85. console.log('total query: ' + totalQuery);
  86. console.log('total render: ' + totalRender);
  87. console.log('avg render: ' + totalRender / $rootScope.performance.panels.length);
  88. }
  89. $rootScope.performance.panels = [];
  90. });
  91. $scope.onAppEvent('dashboard-loaded', function() {
  92. count = 0;
  93. setTimeout(function() {
  94. console.log("Dashboard::Performance Total Digests: " + count);
  95. console.log("Dashboard::Performance Total Watchers: " + $scope.getTotalWatcherCount());
  96. console.log("Dashboard::Performance Total ScopeCount: " + $rootScope.performance.scopeCount);
  97. var timeTaken = $rootScope.performance.allPanelsInitialized - $rootScope.performance.dashboardLoadStart;
  98. console.log("Dashboard::Performance - All panels initialized in " + timeTaken + " ms");
  99. // measure digest performance
  100. var rootDigestStart = window.performance.now();
  101. for (var i = 0; i < 30; i++) {
  102. $rootScope.$apply();
  103. }
  104. console.log("Dashboard::Performance Root Digest " + ((window.performance.now() - rootDigestStart) / 30));
  105. }, 3000);
  106. });
  107. };
  108. $scope.init();
  109. }
  110. }
  111. /** @ngInject */
  112. export function grafanaAppDirective(playlistSrv, contextSrv) {
  113. return {
  114. restrict: 'E',
  115. controller: GrafanaCtrl,
  116. link: (scope, elem) => {
  117. var ignoreSideMenuHide;
  118. var body = $('body');
  119. // handle sidemenu open state
  120. scope.$watch('contextSrv.sidemenu', newVal => {
  121. if (newVal !== undefined) {
  122. body.toggleClass('sidemenu-open', scope.contextSrv.sidemenu);
  123. if (!newVal) {
  124. contextSrv.setPinnedState(false);
  125. }
  126. }
  127. if (contextSrv.sidemenu) {
  128. ignoreSideMenuHide = true;
  129. setTimeout(() => {
  130. ignoreSideMenuHide = false;
  131. }, 300);
  132. }
  133. });
  134. scope.$watch('contextSrv.pinned', newVal => {
  135. if (newVal !== undefined) {
  136. body.toggleClass('sidemenu-pinned', newVal);
  137. }
  138. });
  139. // tooltip removal fix
  140. // manage page classes
  141. var pageClass;
  142. scope.$on("$routeChangeSuccess", function(evt, data) {
  143. if (pageClass) {
  144. body.removeClass(pageClass);
  145. }
  146. pageClass = data.$$route.pageClass;
  147. if (pageClass) {
  148. body.addClass(pageClass);
  149. }
  150. $("#tooltip, .tooltip").remove();
  151. });
  152. // handle document clicks that should hide things
  153. body.click(function(evt) {
  154. var target = $(evt.target);
  155. if (target.parents().length === 0) {
  156. return;
  157. }
  158. if (target.parents('.dash-playlist-actions').length === 0) {
  159. playlistSrv.stop();
  160. }
  161. // hide search
  162. if (body.find('.search-container').length > 0) {
  163. if (target.parents('.search-container').length === 0) {
  164. scope.$apply(function() {
  165. scope.appEvent('hide-dash-search');
  166. });
  167. }
  168. }
  169. // hide sidemenu
  170. if (!ignoreSideMenuHide && !contextSrv.pinned && body.find('.sidemenu').length > 0) {
  171. if (target.parents('.sidemenu').length === 0) {
  172. scope.$apply(function() {
  173. scope.contextSrv.toggleSideMenu();
  174. });
  175. }
  176. }
  177. // hide popovers
  178. var popover = elem.find('.popover');
  179. if (popover.length > 0 && target.parents('.graph-legend').length === 0) {
  180. popover.hide();
  181. }
  182. });
  183. }
  184. };
  185. }
  186. coreModule.directive('grafanaApp', grafanaAppDirective);