ShareSnapshotCtrl.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import angular, { ILocationService } from 'angular';
  2. import _ from 'lodash';
  3. import { BackendSrv } from 'app/core/services/backend_srv';
  4. import { TimeSrv } from '../../services/TimeSrv';
  5. import { DashboardModel } from '../../state/DashboardModel';
  6. import { PanelModel } from '../../state/PanelModel';
  7. export class ShareSnapshotCtrl {
  8. /** @ngInject */
  9. constructor(
  10. $scope: any,
  11. $rootScope: any,
  12. $location: ILocationService,
  13. backendSrv: BackendSrv,
  14. $timeout: any,
  15. timeSrv: TimeSrv
  16. ) {
  17. $scope.snapshot = {
  18. name: $scope.dashboard.title,
  19. expires: 0,
  20. timeoutSeconds: 4,
  21. };
  22. $scope.step = 1;
  23. $scope.expireOptions = [
  24. { text: '1 Hour', value: 60 * 60 },
  25. { text: '1 Day', value: 60 * 60 * 24 },
  26. { text: '7 Days', value: 60 * 60 * 24 * 7 },
  27. { text: 'Never', value: 0 },
  28. ];
  29. $scope.accessOptions = [
  30. { text: 'Anyone with the link', value: 1 },
  31. { text: 'Organization users', value: 2 },
  32. { text: 'Public on the web', value: 3 },
  33. ];
  34. $scope.init = () => {
  35. backendSrv.get('/api/snapshot/shared-options').then((options: { [x: string]: any }) => {
  36. $scope.sharingButtonText = options['externalSnapshotName'];
  37. $scope.externalEnabled = options['externalEnabled'];
  38. });
  39. };
  40. $scope.apiUrl = '/api/snapshots';
  41. $scope.createSnapshot = (external: any) => {
  42. $scope.dashboard.snapshot = {
  43. timestamp: new Date(),
  44. };
  45. if (!external) {
  46. $scope.dashboard.snapshot.originalUrl = $location.absUrl();
  47. }
  48. $scope.loading = true;
  49. $scope.snapshot.external = external;
  50. $scope.dashboard.startRefresh();
  51. $timeout(() => {
  52. $scope.saveSnapshot(external);
  53. }, $scope.snapshot.timeoutSeconds * 1000);
  54. };
  55. $scope.saveSnapshot = (external: any) => {
  56. const dash = $scope.dashboard.getSaveModelClone();
  57. $scope.scrubDashboard(dash);
  58. const cmdData = {
  59. dashboard: dash,
  60. name: dash.title,
  61. expires: $scope.snapshot.expires,
  62. external: external,
  63. };
  64. backendSrv.post($scope.apiUrl, cmdData).then(
  65. (results: { deleteUrl: any; url: any }) => {
  66. $scope.loading = false;
  67. $scope.deleteUrl = results.deleteUrl;
  68. $scope.snapshotUrl = results.url;
  69. $scope.step = 2;
  70. },
  71. () => {
  72. $scope.loading = false;
  73. }
  74. );
  75. };
  76. $scope.getSnapshotUrl = () => {
  77. return $scope.snapshotUrl;
  78. };
  79. $scope.scrubDashboard = (dash: DashboardModel) => {
  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, 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(annotation => {
  93. return annotation.enable;
  94. })
  95. .map((annotation: any) => {
  96. return {
  97. name: annotation.name,
  98. enable: annotation.enable,
  99. iconColor: annotation.iconColor,
  100. snapshotData: annotation.snapshotData,
  101. type: annotation.type,
  102. builtIn: annotation.builtIn,
  103. hide: annotation.hide,
  104. };
  105. })
  106. .value();
  107. // remove template queries
  108. _.each(dash.templating.list, variable => {
  109. variable.query = '';
  110. variable.options = variable.current;
  111. variable.refresh = false;
  112. });
  113. // snapshot single panel
  114. if ($scope.modeSharePanel) {
  115. const singlePanel = $scope.panel.getSaveModel();
  116. singlePanel.gridPos.w = 24;
  117. singlePanel.gridPos.x = 0;
  118. singlePanel.gridPos.y = 0;
  119. singlePanel.gridPos.h = 20;
  120. dash.panels = [singlePanel];
  121. }
  122. // cleanup snapshotData
  123. delete $scope.dashboard.snapshot;
  124. $scope.dashboard.forEachPanel((panel: PanelModel) => {
  125. delete panel.snapshotData;
  126. });
  127. _.each($scope.dashboard.annotations.list, annotation => {
  128. delete annotation.snapshotData;
  129. });
  130. };
  131. $scope.deleteSnapshot = () => {
  132. backendSrv.get($scope.deleteUrl).then(() => {
  133. $scope.step = 3;
  134. });
  135. };
  136. }
  137. }
  138. angular.module('grafana.controllers').controller('ShareSnapshotCtrl', ShareSnapshotCtrl);