shareSnapshotCtrl.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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*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 = 'http://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 = angular.copy($scope.dashboard);
  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 baseUrl = $location.absUrl().replace($location.url(), "");
  54. $scope.snapshotUrl = baseUrl + '/dashboard/snapshot/' + results.key;
  55. $scope.deleteUrl = baseUrl + '/api/snapshots-delete/' + results.deleteKey;
  56. }
  57. $scope.step = 2;
  58. }, function() {
  59. $scope.loading = false;
  60. });
  61. };
  62. $scope.scrubDashboard = function(dash) {
  63. // change title
  64. dash.title = $scope.snapshot.name;
  65. // make relative times absolute
  66. dash.time = timeSrv.timeRange();
  67. // remove panel queries & links
  68. dash.forEachPanel(function(panel) {
  69. panel.targets = [];
  70. panel.links = [];
  71. });
  72. // remove annotations
  73. dash.annotations.list = [];
  74. // remove template queries
  75. _.each(dash.templating.list, function(variable) {
  76. variable.query = "";
  77. variable.options = [];
  78. variable.refresh = false;
  79. });
  80. // snapshot single panel
  81. if ($scope.modeSharePanel) {
  82. var singlePanel = dash.getPanelById($scope.panel.id);
  83. singlePanel.span = 12;
  84. dash.rows = [{ height: '500px', span: 12, panels: [singlePanel] }];
  85. }
  86. // cleanup snapshotData
  87. delete $scope.dashboard.snapshot;
  88. $scope.dashboard.forEachPanel(function(panel) {
  89. delete panel.snapshotData;
  90. });
  91. };
  92. $scope.saveExternalSnapshotRef = function(cmdData, results) {
  93. // save external in local instance as well
  94. cmdData.external = true;
  95. cmdData.key = results.key;
  96. cmdData.deleteKey = results.deleteKey;
  97. backendSrv.post('/api/snapshots/', cmdData);
  98. };
  99. });
  100. });