shareSnapshotCtrl.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.getSnapshotUrl = function() {
  78. return $scope.snapshotUrl;
  79. };
  80. $scope.scrubDashboard = function(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. dash.forEachPanel(function(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(function(annotation) {
  94. return annotation.enable;
  95. })
  96. .map(function(annotation) {
  97. return {
  98. name: annotation.name,
  99. enable: annotation.enable,
  100. iconColor: annotation.iconColor,
  101. snapshotData: annotation.snapshotData
  102. };
  103. }).value();
  104. // remove template queries
  105. _.each(dash.templating.list, function(variable) {
  106. variable.query = "";
  107. variable.options = variable.current;
  108. variable.refresh = false;
  109. });
  110. // snapshot single panel
  111. if ($scope.modeSharePanel) {
  112. var singlePanel = dash.getPanelById($scope.panel.id);
  113. singlePanel.span = 12;
  114. dash.rows = [{ height: '500px', span: 12, panels: [singlePanel] }];
  115. }
  116. // cleanup snapshotData
  117. delete $scope.dashboard.snapshot;
  118. $scope.dashboard.forEachPanel(function(panel) {
  119. delete panel.snapshotData;
  120. });
  121. _.each($scope.dashboard.annotations.list, function(annotation) {
  122. delete annotation.snapshotData;
  123. });
  124. };
  125. $scope.deleteSnapshot = function() {
  126. backendSrv.get($scope.deleteUrl).then(function() {
  127. $scope.step = 3;
  128. });
  129. };
  130. $scope.saveExternalSnapshotRef = function(cmdData, results) {
  131. // save external in local instance as well
  132. cmdData.external = true;
  133. cmdData.key = results.key;
  134. cmdData.deleteKey = results.deleteKey;
  135. backendSrv.post('/api/snapshots/', cmdData);
  136. };
  137. });
  138. });