share_snapshot_ctrl.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. export class ShareSnapshotCtrl {
  4. constructor($scope, $rootScope, $location, backendSrv, $timeout, timeSrv) {
  5. $scope.snapshot = {
  6. name: $scope.dashboard.title,
  7. expires: 0,
  8. timeoutSeconds: 4,
  9. };
  10. $scope.step = 1;
  11. $scope.expireOptions = [
  12. { text: '1 Hour', value: 60 * 60 },
  13. { text: '1 Day', value: 60 * 60 * 24 },
  14. { text: '7 Days', value: 60 * 60 * 24 * 7 },
  15. { text: 'Never', value: 0 },
  16. ];
  17. $scope.accessOptions = [
  18. { text: 'Anyone with the link', value: 1 },
  19. { text: 'Organization users', value: 2 },
  20. { text: 'Public on the web', value: 3 },
  21. ];
  22. $scope.init = function() {
  23. backendSrv.get('/api/snapshot/shared-options').then(function(options) {
  24. $scope.externalUrl = options['externalSnapshotURL'];
  25. $scope.sharingButtonText = options['externalSnapshotName'];
  26. $scope.externalEnabled = options['externalEnabled'];
  27. });
  28. };
  29. $scope.apiUrl = '/api/snapshots';
  30. $scope.createSnapshot = function(external) {
  31. $scope.dashboard.snapshot = {
  32. timestamp: new Date(),
  33. };
  34. if (!external) {
  35. $scope.dashboard.snapshot.originalUrl = $location.absUrl();
  36. }
  37. $scope.loading = true;
  38. $scope.snapshot.external = external;
  39. $rootScope.$broadcast('refresh');
  40. $timeout(function() {
  41. $scope.saveSnapshot(external);
  42. }, $scope.snapshot.timeoutSeconds * 1000);
  43. };
  44. $scope.saveSnapshot = function(external) {
  45. var dash = $scope.dashboard.getSaveModelClone();
  46. $scope.scrubDashboard(dash);
  47. var cmdData = {
  48. dashboard: dash,
  49. name: dash.title,
  50. expires: $scope.snapshot.expires,
  51. };
  52. var postUrl = external ? $scope.externalUrl + $scope.apiUrl : $scope.apiUrl;
  53. backendSrv.post(postUrl, cmdData).then(
  54. function(results) {
  55. $scope.loading = false;
  56. if (external) {
  57. $scope.deleteUrl = results.deleteUrl;
  58. $scope.snapshotUrl = results.url;
  59. $scope.saveExternalSnapshotRef(cmdData, results);
  60. } else {
  61. var url = $location.url();
  62. var baseUrl = $location.absUrl();
  63. if (url !== '/') {
  64. baseUrl = baseUrl.replace(url, '') + '/';
  65. }
  66. $scope.snapshotUrl = baseUrl + 'dashboard/snapshot/' + results.key;
  67. $scope.deleteUrl = baseUrl + 'api/snapshots-delete/' + results.deleteKey;
  68. }
  69. $scope.step = 2;
  70. },
  71. function() {
  72. $scope.loading = false;
  73. }
  74. );
  75. };
  76. $scope.getSnapshotUrl = function() {
  77. return $scope.snapshotUrl;
  78. };
  79. $scope.scrubDashboard = function(dash) {
  80. // change title
  81. dash.title = $scope.snapshot.name;
  82. // make relative times absolute
  83. dash.time = timeSrv.timeRange();
  84. // remove panel queries & links
  85. _.each(dash.panels, function(panel) {
  86. panel.targets = [];
  87. panel.links = [];
  88. panel.datasource = null;
  89. });
  90. // remove annotation queries
  91. dash.annotations.list = _.chain(dash.annotations.list)
  92. .filter(function(annotation) {
  93. return annotation.enable;
  94. })
  95. .map(function(annotation) {
  96. return {
  97. name: annotation.name,
  98. enable: annotation.enable,
  99. iconColor: annotation.iconColor,
  100. snapshotData: annotation.snapshotData,
  101. };
  102. })
  103. .value();
  104. // remove template queries
  105. _.each(dash.templating.list, function(variable) {
  106. variable.query = '';
  107. variable.options = variable.current;
  108. variable.refresh = false;
  109. });
  110. // snapshot single panel
  111. if ($scope.modeSharePanel) {
  112. var singlePanel = $scope.panel.getSaveModel();
  113. singlePanel.gridPos.w = 24;
  114. singlePanel.gridPos.x = 0;
  115. singlePanel.gridPos.y = 0;
  116. singlePanel.gridPos.h = 20;
  117. dash.panels = [singlePanel];
  118. }
  119. // cleanup snapshotData
  120. delete $scope.dashboard.snapshot;
  121. $scope.dashboard.forEachPanel(function(panel) {
  122. delete panel.snapshotData;
  123. });
  124. _.each($scope.dashboard.annotations.list, function(annotation) {
  125. delete annotation.snapshotData;
  126. });
  127. };
  128. $scope.deleteSnapshot = function() {
  129. backendSrv.get($scope.deleteUrl).then(function() {
  130. $scope.step = 3;
  131. });
  132. };
  133. $scope.saveExternalSnapshotRef = function(cmdData, results) {
  134. // save external in local instance as well
  135. cmdData.external = true;
  136. cmdData.key = results.key;
  137. cmdData.deleteKey = results.deleteKey;
  138. backendSrv.post('/api/snapshots/', cmdData);
  139. };
  140. }
  141. }
  142. angular.module('grafana.controllers').controller('ShareSnapshotCtrl', ShareSnapshotCtrl);