dashboard-from-script.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. define([
  2. 'angular',
  3. 'jquery',
  4. 'config',
  5. 'lodash',
  6. 'kbn',
  7. 'moment'
  8. ],
  9. function (angular, $, config, _, kbn, moment) {
  10. "use strict";
  11. var module = angular.module('grafana.routes');
  12. module.config(function($routeProvider) {
  13. $routeProvider
  14. .when('/dashboard/script/:jsFile', {
  15. templateUrl: 'app/partials/dashboard.html',
  16. controller : 'DashFromScriptProvider',
  17. });
  18. });
  19. module.controller('DashFromScriptProvider', function($scope, $rootScope, $http, $routeParams, alertSrv, $q) {
  20. var execute_script = function(result) {
  21. /*jshint -W054 */
  22. var script_func = new Function('ARGS','kbn','_','moment','window','document','$','jQuery', result.data);
  23. var script_result = script_func($routeParams, kbn, _ , moment, window, document, $, $);
  24. // Handle async dashboard scripts
  25. if (_.isFunction(script_result)) {
  26. var deferred = $q.defer();
  27. script_result(function(dashboard) {
  28. $rootScope.$apply(function() {
  29. deferred.resolve({ data: dashboard });
  30. });
  31. });
  32. return deferred.promise;
  33. }
  34. return { data: script_result };
  35. };
  36. var script_load = function(file) {
  37. var url = 'app/dashboards/'+file.replace(/\.(?!js)/,"/") + '?' + new Date().getTime();
  38. return $http({ url: url, method: "GET" })
  39. .then(execute_script)
  40. .then(null,function(err) {
  41. console.log('Script dashboard error '+ err);
  42. alertSrv.set('Error', "Could not load <i>scripts/"+file+"</i>. Please make sure it exists and returns a valid dashboard", 'error');
  43. return false;
  44. });
  45. };
  46. script_load($routeParams.jsFile).then(function(result) {
  47. $scope.emitAppEvent('setup-dashboard', result.data);
  48. });
  49. });
  50. });