share_snapshot_ctrl.ts 4.8 KB

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