share_snapshot_ctrl.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.sharingButtonText = options['externalSnapshotName'];
  26. $scope.externalEnabled = options['externalEnabled'];
  27. });
  28. };
  29. $scope.apiUrl = '/api/snapshots';
  30. $scope.createSnapshot = 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. $scope.dashboard.startRefresh();
  40. $timeout(() => {
  41. $scope.saveSnapshot(external);
  42. }, $scope.snapshot.timeoutSeconds * 1000);
  43. };
  44. $scope.saveSnapshot = external => {
  45. const dash = $scope.dashboard.getSaveModelClone();
  46. $scope.scrubDashboard(dash);
  47. const cmdData = {
  48. dashboard: dash,
  49. name: dash.title,
  50. expires: $scope.snapshot.expires,
  51. external: external,
  52. };
  53. backendSrv.post($scope.apiUrl, cmdData).then(
  54. results => {
  55. $scope.loading = false;
  56. $scope.deleteUrl = results.deleteUrl;
  57. $scope.snapshotUrl = results.url;
  58. $scope.step = 2;
  59. },
  60. () => {
  61. $scope.loading = false;
  62. }
  63. );
  64. };
  65. $scope.getSnapshotUrl = () => {
  66. return $scope.snapshotUrl;
  67. };
  68. $scope.scrubDashboard = dash => {
  69. // change title
  70. dash.title = $scope.snapshot.name;
  71. // make relative times absolute
  72. dash.time = timeSrv.timeRange();
  73. // remove panel queries & links
  74. _.each(dash.panels, panel => {
  75. panel.targets = [];
  76. panel.links = [];
  77. panel.datasource = null;
  78. });
  79. // remove annotation queries
  80. dash.annotations.list = _.chain(dash.annotations.list)
  81. .filter(annotation => {
  82. return annotation.enable;
  83. })
  84. .map(annotation => {
  85. return {
  86. name: annotation.name,
  87. enable: annotation.enable,
  88. iconColor: annotation.iconColor,
  89. snapshotData: annotation.snapshotData,
  90. type: annotation.type,
  91. builtIn: annotation.builtIn,
  92. hide: annotation.hide,
  93. };
  94. })
  95. .value();
  96. // remove template queries
  97. _.each(dash.templating.list, variable => {
  98. variable.query = '';
  99. variable.options = variable.current;
  100. variable.refresh = false;
  101. });
  102. // snapshot single panel
  103. if ($scope.modeSharePanel) {
  104. const singlePanel = $scope.panel.getSaveModel();
  105. singlePanel.gridPos.w = 24;
  106. singlePanel.gridPos.x = 0;
  107. singlePanel.gridPos.y = 0;
  108. singlePanel.gridPos.h = 20;
  109. dash.panels = [singlePanel];
  110. }
  111. // cleanup snapshotData
  112. delete $scope.dashboard.snapshot;
  113. $scope.dashboard.forEachPanel(panel => {
  114. delete panel.snapshotData;
  115. });
  116. _.each($scope.dashboard.annotations.list, annotation => {
  117. delete annotation.snapshotData;
  118. });
  119. };
  120. $scope.deleteSnapshot = () => {
  121. backendSrv.get($scope.deleteUrl).then(() => {
  122. $scope.step = 3;
  123. });
  124. };
  125. }
  126. }
  127. angular.module('grafana.controllers').controller('ShareSnapshotCtrl', ShareSnapshotCtrl);