dashLoadControllers.js 4.1 KB

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