dashLoadControllers.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'kbn',
  5. 'moment',
  6. 'jquery',
  7. ],
  8. function (angular, _, kbn, moment, $) {
  9. "use strict";
  10. var module = angular.module('grafana.routes');
  11. module.controller('DashFromDBCtrl', function($scope, $routeParams, backendSrv) {
  12. function dashboardLoadFailed(title) {
  13. $scope.initDashboard({meta: {}, dashboard: {title: title}}, $scope);
  14. }
  15. if (!$routeParams.slug) {
  16. backendSrv.get('/api/dashboards/home').then(function(result) {
  17. var meta = result.meta;
  18. meta.canSave = meta.canShare = meta.canEdit = meta.canStar = false;
  19. $scope.initDashboard(result, $scope);
  20. },function() {
  21. dashboardLoadFailed('Not found');
  22. });
  23. return;
  24. }
  25. return backendSrv.getDashboard($routeParams.slug).then(function(result) {
  26. $scope.initDashboard(result, $scope);
  27. }, function() {
  28. dashboardLoadFailed('Not found');
  29. });
  30. });
  31. module.controller('DashFromSnapshotCtrl', function($scope, $routeParams, backendSrv, contextSrv) {
  32. //don't show the sidemenu in snapshots.
  33. contextSrv.sidemenu = false;
  34. backendSrv.get('/api/snapshots/' + $routeParams.key).then(function(result) {
  35. $scope.initDashboard(result, $scope);
  36. }, function() {
  37. $scope.initDashboard({
  38. meta: {
  39. isSnapshot: true,
  40. canSave: false,
  41. canEdit: false,
  42. },
  43. dashboard: {
  44. title: 'Snapshot not found'
  45. }
  46. }, $scope);
  47. });
  48. });
  49. module.controller('DashFromImportCtrl', function($scope, $location, alertSrv) {
  50. if (!window.grafanaImportDashboard) {
  51. alertSrv.set('Not found', 'Cannot reload page with unsaved imported dashboard', 'warning', 7000);
  52. $location.path('');
  53. return;
  54. }
  55. $scope.initDashboard({
  56. meta: { canShare: false, canStar: false },
  57. dashboard: window.grafanaImportDashboard
  58. }, $scope);
  59. });
  60. module.controller('NewDashboardCtrl', function($scope) {
  61. $scope.initDashboard({
  62. meta: { canStar: false, canShare: false },
  63. dashboard: {
  64. title: "New dashboard",
  65. rows: [{ height: '250px', panels:[] }]
  66. },
  67. }, $scope);
  68. });
  69. module.controller('DashFromFileCtrl', function($scope, $rootScope, $http, $routeParams) {
  70. var file_load = function(file) {
  71. return $http({
  72. url: "public/dashboards/"+file.replace(/\.(?!json)/,"/")+'?' + new Date().getTime(),
  73. method: "GET",
  74. transformResponse: function(response) {
  75. return angular.fromJson(response);
  76. }
  77. }).then(function(result) {
  78. if(!result) {
  79. return false;
  80. }
  81. return result.data;
  82. },function() {
  83. $scope.appEvent('alert-error', ["Dashboard load failed", "Could not load "+file+". Please make sure it exists"]);
  84. return false;
  85. });
  86. };
  87. file_load($routeParams.jsonFile).then(function(result) {
  88. $scope.initDashboard({
  89. meta: { canSave: false, canDelete: false },
  90. dashboard: result
  91. }, $scope);
  92. });
  93. });
  94. module.controller('DashFromScriptCtrl', function($scope, $rootScope, $http, $routeParams, $q, dashboardSrv, datasourceSrv, $timeout) {
  95. var execute_script = function(result) {
  96. var services = {
  97. dashboardSrv: dashboardSrv,
  98. datasourceSrv: datasourceSrv,
  99. $q: $q,
  100. };
  101. /*jshint -W054 */
  102. var script_func = new Function('ARGS','kbn','_','moment','window','document','$','jQuery', 'services', result.data);
  103. var script_result = script_func($routeParams, kbn, _ , moment, window, document, $, $, services);
  104. // Handle async dashboard scripts
  105. if (_.isFunction(script_result)) {
  106. var deferred = $q.defer();
  107. script_result(function(dashboard) {
  108. $timeout(function() {
  109. deferred.resolve({ data: dashboard });
  110. });
  111. });
  112. return deferred.promise;
  113. }
  114. return { data: script_result };
  115. };
  116. var script_load = function(file) {
  117. var url = 'public/dashboards/'+file.replace(/\.(?!js)/,"/") + '?' + new Date().getTime();
  118. return $http({ url: url, method: "GET" })
  119. .then(execute_script)
  120. .then(null,function(err) {
  121. console.log('Script dashboard error '+ err);
  122. $scope.appEvent('alert-error', ["Script Error", "Please make sure it exists and returns a valid dashboard"]);
  123. return false;
  124. });
  125. };
  126. script_load($routeParams.jsFile).then(function(result) {
  127. $scope.initDashboard({
  128. meta: {fromScript: true, canDelete: false, canSave: false},
  129. dashboard: result.data
  130. }, $scope);
  131. });
  132. });
  133. });