shareSnapshotCtrl.js 4.5 KB

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