dashLoadControllers.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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({meta: {}, model: window.grafanaImportDashboard }, $scope);
  54. });
  55. module.controller('NewDashboardCtrl', function($scope) {
  56. $scope.initDashboard({
  57. meta: {},
  58. model: {
  59. title: "New dashboard",
  60. rows: [{ height: '250px', panels:[] }]
  61. },
  62. }, $scope);
  63. });
  64. module.controller('DashFromFileCtrl', function($scope, $rootScope, $http, $routeParams) {
  65. var file_load = function(file) {
  66. return $http({
  67. url: "public/dashboards/"+file.replace(/\.(?!json)/,"/")+'?' + new Date().getTime(),
  68. method: "GET",
  69. transformResponse: function(response) {
  70. return angular.fromJson(response);
  71. }
  72. }).then(function(result) {
  73. if(!result) {
  74. return false;
  75. }
  76. return result.data;
  77. },function() {
  78. $scope.appEvent('alert-error', ["Dashboard load failed", "Could not load <i>dashboards/"+file+"</i>. Please make sure it exists"]);
  79. return false;
  80. });
  81. };
  82. file_load($routeParams.jsonFile).then(function(result) {
  83. $scope.initDashboard({meta: {fromFile: true}, model: result}, $scope);
  84. });
  85. });
  86. module.controller('DashFromScriptCtrl', function($scope, $rootScope, $http, $routeParams, $q, dashboardSrv, datasourceSrv, $timeout) {
  87. var execute_script = function(result) {
  88. var services = {
  89. dashboardSrv: dashboardSrv,
  90. datasourceSrv: datasourceSrv,
  91. $q: $q,
  92. };
  93. /*jshint -W054 */
  94. var script_func = new Function('ARGS','kbn','_','moment','window','document','$','jQuery', 'services', result.data);
  95. var script_result = script_func($routeParams, kbn, _ , moment, window, document, $, $, services);
  96. // Handle async dashboard scripts
  97. if (_.isFunction(script_result)) {
  98. var deferred = $q.defer();
  99. script_result(function(dashboard) {
  100. $timeout(function() {
  101. deferred.resolve({ data: dashboard });
  102. });
  103. });
  104. return deferred.promise;
  105. }
  106. return { data: script_result };
  107. };
  108. var script_load = function(file) {
  109. var url = 'public/dashboards/'+file.replace(/\.(?!js)/,"/") + '?' + new Date().getTime();
  110. return $http({ url: url, method: "GET" })
  111. .then(execute_script)
  112. .then(null,function(err) {
  113. console.log('Script dashboard error '+ err);
  114. $scope.appEvent('alert-error', ["Script Error", "Please make sure it exists and returns a valid dashboard"]);
  115. return false;
  116. });
  117. };
  118. script_load($routeParams.jsFile).then(function(result) {
  119. $scope.initDashboard({meta: {fromScript: true, canDelete: false}, model: result.data}, $scope);
  120. });
  121. });
  122. });