shareSnapshotCtrl.js 4.4 KB

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