shareSnapshotCtrl.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $scope.loading = true;
  38. $scope.snapshot.external = external;
  39. $rootScope.$broadcast('refresh');
  40. $timeout(function() {
  41. $scope.saveSnapshot(external);
  42. }, 4000);
  43. };
  44. $scope.saveSnapshot = function(external) {
  45. var dash = $scope.dashboard.getSaveModelClone();
  46. $scope.scrubDashboard(dash);
  47. var cmdData = {
  48. dashboard: dash,
  49. name: dash.title,
  50. expires: $scope.snapshot.expires,
  51. };
  52. var postUrl = external ? $scope.externalUrl + $scope.apiUrl : $scope.apiUrl;
  53. backendSrv.post(postUrl, cmdData).then(function(results) {
  54. $scope.loading = false;
  55. if (external) {
  56. $scope.deleteUrl = results.deleteUrl;
  57. $scope.snapshotUrl = results.url;
  58. $scope.saveExternalSnapshotRef(cmdData, results);
  59. } else {
  60. var url = $location.url();
  61. var baseUrl = $location.absUrl();
  62. if (url !== '/') {
  63. baseUrl = baseUrl.replace(url, '') + '/';
  64. }
  65. $scope.snapshotUrl = baseUrl + 'dashboard/snapshot/' + results.key;
  66. $scope.deleteUrl = baseUrl + 'api/snapshots-delete/' + results.deleteKey;
  67. }
  68. $scope.step = 2;
  69. }, function() {
  70. $scope.loading = false;
  71. });
  72. };
  73. $scope.scrubDashboard = function(dash) {
  74. // change title
  75. dash.title = $scope.snapshot.name;
  76. // make relative times absolute
  77. dash.time = timeSrv.timeRange();
  78. // remove panel queries & links
  79. dash.forEachPanel(function(panel) {
  80. panel.targets = [];
  81. panel.links = [];
  82. panel.datasource = null;
  83. });
  84. // remove annotation queries
  85. dash.annotations.list = _.chain(dash.annotations.list)
  86. .filter(function(annotation) {
  87. return annotation.enable;
  88. })
  89. .map(function(annotation) {
  90. return {
  91. name: annotation.name,
  92. enable: annotation.enable,
  93. snapshotData: annotation.snapshotData
  94. };
  95. }).value();
  96. // remove template queries
  97. _.each(dash.templating.list, function(variable) {
  98. variable.query = "";
  99. variable.options = variable.current;
  100. variable.refresh = false;
  101. });
  102. // snapshot single panel
  103. if ($scope.modeSharePanel) {
  104. var singlePanel = dash.getPanelById($scope.panel.id);
  105. singlePanel.span = 12;
  106. dash.rows = [{ height: '500px', span: 12, panels: [singlePanel] }];
  107. }
  108. // cleanup snapshotData
  109. delete $scope.dashboard.snapshot;
  110. $scope.dashboard.forEachPanel(function(panel) {
  111. delete panel.snapshotData;
  112. });
  113. _.each($scope.dashboard.annotations.list, function(annotation) {
  114. delete annotation.snapshotData;
  115. });
  116. };
  117. $scope.deleteSnapshot = function() {
  118. backendSrv.get($scope.deleteUrl).then(function() {
  119. $scope.step = 3;
  120. });
  121. };
  122. $scope.saveExternalSnapshotRef = function(cmdData, results) {
  123. // save external in local instance as well
  124. cmdData.external = true;
  125. cmdData.key = results.key;
  126. cmdData.deleteKey = results.deleteKey;
  127. backendSrv.post('/api/snapshots/', cmdData);
  128. };
  129. });
  130. });