panelSrv.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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() {
  35. $scope.panel.targets.push({target: ''});
  36. };
  37. $scope.removeDataQuery = function (query) {
  38. $scope.panel.targets = _.without($scope.panel.targets, query);
  39. $scope.get_data();
  40. };
  41. $scope.setDatasource = function(datasource) {
  42. $scope.panel.datasource = datasource;
  43. $scope.datasource = null;
  44. $scope.get_data();
  45. };
  46. $scope.toggleEditorHelp = function(index) {
  47. if ($scope.editorHelpIndex === index) {
  48. $scope.editorHelpIndex = null;
  49. return;
  50. }
  51. $scope.editorHelpIndex = index;
  52. };
  53. $scope.isNewPanel = function() {
  54. return $scope.panel.title === config.new_panel_title;
  55. };
  56. $scope.toggleFullscreen = function(edit) {
  57. $scope.dashboardViewState.update({ fullscreen: true, edit: edit, panelId: $scope.panel.id });
  58. };
  59. $scope.otherPanelInFullscreenMode = function() {
  60. return $scope.dashboardViewState.fullscreen && !$scope.fullscreen;
  61. };
  62. $scope.getCurrentDatasource = function() {
  63. if ($scope.datasource) {
  64. return $q.when($scope.datasource);
  65. }
  66. return datasourceSrv.get($scope.panel.datasource);
  67. };
  68. $scope.get_data = function() {
  69. if ($scope.otherPanelInFullscreenMode()) { return; }
  70. if ($scope.panel.snapshotData) {
  71. if ($scope.loadSnapshot) {
  72. $scope.loadSnapshot($scope.panel.snapshotData);
  73. }
  74. return;
  75. }
  76. delete $scope.panelMeta.error;
  77. $scope.panelMeta.loading = true;
  78. $scope.getCurrentDatasource().then(function(datasource) {
  79. $scope.datasource = datasource;
  80. return $scope.refreshData($scope.datasource) || $q.when({});
  81. }).then(function() {
  82. $scope.panelMeta.loading = false;
  83. }, function(err) {
  84. console.log('Panel data error:', err);
  85. $scope.panelMeta.loading = false;
  86. $scope.panelMeta.error = err.message || "Timeseries data request error";
  87. $scope.inspector.error = err;
  88. });
  89. };
  90. if ($scope.refreshData) {
  91. $scope.$on("refresh", $scope.get_data);
  92. }
  93. // Post init phase
  94. $scope.fullscreen = false;
  95. $scope.editor = { index: 1 };
  96. $scope.dashboardViewState.registerPanel($scope);
  97. $scope.datasources = datasourceSrv.getMetricSources();
  98. if (!$scope.skipDataOnInit) {
  99. $timeout(function() {
  100. $scope.get_data();
  101. }, 30);
  102. }
  103. };
  104. });
  105. });