shareSnapshotCtrl.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }, 3000);
  37. };
  38. $scope.saveSnapshot = function(external) {
  39. var dash = angular.copy($scope.dashboard);
  40. // change title
  41. dash.title = $scope.snapshot.name;
  42. // make relative times absolute
  43. dash.time = timeSrv.timeRange();
  44. // remove panel queries & links
  45. dash.forEachPanel(function(panel) {
  46. panel.targets = [];
  47. panel.links = [];
  48. });
  49. // remove annotations
  50. dash.annotations.list = [];
  51. // remove template queries
  52. _.each(dash.templating.list, function(variable) {
  53. variable.query = "";
  54. variable.refresh = false;
  55. });
  56. // cleanup snapshotData
  57. delete $scope.dashboard.snapshot;
  58. $scope.dashboard.forEachPanel(function(panel) {
  59. delete panel.snapshotData;
  60. });
  61. var cmdData = {
  62. dashboard: dash,
  63. expires: $scope.snapshot.expires,
  64. };
  65. var postUrl = external ? $scope.externalUrl + $scope.apiUrl : $scope.apiUrl;
  66. backendSrv.post(postUrl, cmdData).then(function(results) {
  67. $scope.loading = false;
  68. if (external) {
  69. $scope.deleteUrl = results.deleteUrl;
  70. $scope.snapshotUrl = results.url;
  71. $scope.saveExternalSnapshotRef(cmdData, results);
  72. } else {
  73. var baseUrl = $location.absUrl().replace($location.url(), "");
  74. $scope.snapshotUrl = baseUrl + '/dashboard/snapshot/' + results.key;
  75. $scope.deleteUrl = baseUrl + '/api/snapshots-delete/' + results.deleteKey;
  76. }
  77. $scope.step = 2;
  78. }, function() {
  79. $scope.loading = false;
  80. });
  81. };
  82. $scope.saveExternalSnapshotRef = function(cmdData, results) {
  83. // save external in local instance as well
  84. cmdData.external = true;
  85. cmdData.key = results.key;
  86. cmdData.deleteKey = results.deleteKey;
  87. backendSrv.post('/api/snapshots/', cmdData);
  88. };
  89. });
  90. });