panelSrv.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. ],
  6. function (angular, _, config) {
  7. 'use strict';
  8. var module = angular.module('grafana.services');
  9. module.service('panelSrv', function($rootScope, $timeout, datasourceSrv, $q) {
  10. this.init = function($scope) {
  11. if (!$scope.panel.span) { $scope.panel.span = 12; }
  12. $scope.inspector = {};
  13. $scope.editPanel = function() {
  14. $scope.toggleFullscreen(true);
  15. };
  16. $scope.sharePanel = function() {
  17. $scope.appEvent('show-modal', {
  18. src: './app/features/dashboard/partials/shareModal.html',
  19. scope: $scope.$new()
  20. });
  21. };
  22. $scope.editPanelJson = function() {
  23. $scope.appEvent('show-json-editor', { object: $scope.panel, updateHandler: $scope.replacePanel });
  24. };
  25. $scope.duplicatePanel = function() {
  26. $scope.dashboard.duplicatePanel($scope.panel, $scope.row);
  27. };
  28. $scope.updateColumnSpan = function(span) {
  29. $scope.updatePanelSpan($scope.panel, span);
  30. $timeout(function() {
  31. $scope.$broadcast('render');
  32. });
  33. };
  34. $scope.addDataQuery = function(datasource) {
  35. var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  36. var target = {};
  37. if (datasource) {
  38. target.datasource = datasource.name;
  39. }
  40. target.refId = _.find(letters, function(refId) {
  41. return _.every($scope.panel.targets, function(other) {
  42. return other.refId !== refId;
  43. });
  44. });
  45. $scope.panel.targets.push(target);
  46. };
  47. $scope.removeDataQuery = function (query) {
  48. $scope.panel.targets = _.without($scope.panel.targets, query);
  49. $scope.get_data();
  50. };
  51. $scope.setDatasource = function(datasource) {
  52. // switching to mixed
  53. if (datasource.meta.mixed) {
  54. _.each($scope.panel.targets, function(target) {
  55. target.datasource = $scope.panel.datasource;
  56. if (target.datasource === null) {
  57. target.datasource = config.defaultDatasource;
  58. }
  59. });
  60. }
  61. // switching from mixed
  62. else if ($scope.datasource && $scope.datasource.meta.mixed) {
  63. _.each($scope.panel.targets, function(target) {
  64. delete target.datasource;
  65. });
  66. }
  67. $scope.panel.datasource = datasource.value;
  68. $scope.datasource = null;
  69. $scope.get_data();
  70. };
  71. $scope.toggleEditorHelp = function(index) {
  72. if ($scope.editorHelpIndex === index) {
  73. $scope.editorHelpIndex = null;
  74. return;
  75. }
  76. $scope.editorHelpIndex = index;
  77. };
  78. $scope.isNewPanel = function() {
  79. return $scope.panel.title === config.new_panel_title;
  80. };
  81. $scope.toggleFullscreen = function(edit) {
  82. $scope.dashboardViewState.update({ fullscreen: true, edit: edit, panelId: $scope.panel.id });
  83. };
  84. $scope.otherPanelInFullscreenMode = function() {
  85. return $scope.dashboardViewState.fullscreen && !$scope.fullscreen;
  86. };
  87. $scope.getCurrentDatasource = function() {
  88. if ($scope.datasource) {
  89. return $q.when($scope.datasource);
  90. }
  91. return datasourceSrv.get($scope.panel.datasource);
  92. };
  93. $scope.panelRenderingComplete = function() {
  94. $rootScope.performance.panelsRendered++;
  95. };
  96. $scope.get_data = function() {
  97. if ($scope.otherPanelInFullscreenMode()) { return; }
  98. if ($scope.panel.snapshotData) {
  99. if ($scope.loadSnapshot) {
  100. $scope.loadSnapshot($scope.panel.snapshotData);
  101. }
  102. return;
  103. }
  104. delete $scope.panelMeta.error;
  105. $scope.panelMeta.loading = true;
  106. $scope.getCurrentDatasource().then(function(datasource) {
  107. $scope.datasource = datasource;
  108. return $scope.refreshData($scope.datasource) || $q.when({});
  109. }).then(function() {
  110. $scope.panelMeta.loading = false;
  111. }, function(err) {
  112. console.log('Panel data error:', err);
  113. $scope.panelMeta.loading = false;
  114. $scope.panelMeta.error = err.message || "Timeseries data request error";
  115. $scope.inspector.error = err;
  116. });
  117. };
  118. if ($scope.refreshData) {
  119. $scope.$on("refresh", $scope.get_data);
  120. }
  121. // Post init phase
  122. $scope.fullscreen = false;
  123. $scope.editor = { index: 1 };
  124. $scope.dashboardViewState.registerPanel($scope);
  125. $scope.datasources = datasourceSrv.getMetricSources();
  126. if (!$scope.skipDataOnInit) {
  127. $timeout(function() {
  128. $scope.get_data();
  129. }, 30);
  130. }
  131. };
  132. });
  133. });