dashLoadControllers.js 4.4 KB

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