shareSnapshotCtrl.js 3.9 KB

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