shareSnapshotCtrl.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.externalUrl = '//snapshots-origin.raintank.io';
  26. $scope.apiUrl = '/api/snapshots';
  27. $scope.createSnapshot = function(external) {
  28. $scope.dashboard.snapshot = {
  29. timestamp: new Date()
  30. };
  31. $scope.loading = true;
  32. $scope.snapshot.external = external;
  33. $rootScope.$broadcast('refresh');
  34. $timeout(function() {
  35. $scope.saveSnapshot(external);
  36. }, 4000);
  37. };
  38. $scope.saveSnapshot = function(external) {
  39. var dash = $scope.dashboard.getSaveModelClone();
  40. $scope.scrubDashboard(dash);
  41. var cmdData = {
  42. dashboard: dash,
  43. expires: $scope.snapshot.expires,
  44. };
  45. var postUrl = external ? $scope.externalUrl + $scope.apiUrl : $scope.apiUrl;
  46. backendSrv.post(postUrl, cmdData).then(function(results) {
  47. $scope.loading = false;
  48. if (external) {
  49. $scope.deleteUrl = results.deleteUrl;
  50. $scope.snapshotUrl = results.url;
  51. $scope.saveExternalSnapshotRef(cmdData, results);
  52. } else {
  53. var url = $location.url();
  54. var baseUrl = $location.absUrl();
  55. if (url !== '/') {
  56. baseUrl = baseUrl.replace(url, '') + '/';
  57. }
  58. $scope.snapshotUrl = baseUrl + 'dashboard/snapshot/' + results.key;
  59. $scope.deleteUrl = baseUrl + 'api/snapshots-delete/' + results.deleteKey;
  60. }
  61. $scope.step = 2;
  62. }, function() {
  63. $scope.loading = false;
  64. });
  65. };
  66. $scope.scrubDashboard = function(dash) {
  67. // change title
  68. dash.title = $scope.snapshot.name;
  69. // make relative times absolute
  70. dash.time = timeSrv.timeRange();
  71. // remove panel queries & links
  72. dash.forEachPanel(function(panel) {
  73. panel.targets = [];
  74. panel.links = [];
  75. panel.datasource = null;
  76. });
  77. // remove annotations
  78. dash.annotations.list = [];
  79. // remove template queries
  80. _.each(dash.templating.list, function(variable) {
  81. variable.query = "";
  82. variable.options = [];
  83. variable.refresh = false;
  84. });
  85. // snapshot single panel
  86. if ($scope.modeSharePanel) {
  87. var singlePanel = dash.getPanelById($scope.panel.id);
  88. singlePanel.span = 12;
  89. dash.rows = [{ height: '500px', span: 12, panels: [singlePanel] }];
  90. }
  91. // cleanup snapshotData
  92. delete $scope.dashboard.snapshot;
  93. $scope.dashboard.forEachPanel(function(panel) {
  94. delete panel.snapshotData;
  95. });
  96. };
  97. $scope.deleteSnapshot = function() {
  98. backendSrv.get($scope.deleteUrl).then(function() {
  99. $scope.step = 3;
  100. });
  101. };
  102. $scope.saveExternalSnapshotRef = function(cmdData, results) {
  103. // save external in local instance as well
  104. cmdData.external = true;
  105. cmdData.key = results.key;
  106. cmdData.deleteKey = results.deleteKey;
  107. backendSrv.post('/api/snapshots/', cmdData);
  108. };
  109. });
  110. });