dashboardLoaderSrv.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. define([
  2. 'angular',
  3. 'moment',
  4. 'lodash',
  5. 'jquery',
  6. 'app/core/utils/kbn',
  7. 'app/core/utils/datemath',
  8. './impressionStore',
  9. ],
  10. function (angular, moment, _, $, kbn, dateMath, impressionStore) {
  11. 'use strict';
  12. var module = angular.module('grafana.services');
  13. module.service('dashboardLoaderSrv', function(backendSrv,
  14. dashboardSrv,
  15. datasourceSrv,
  16. $http, $q, $timeout,
  17. contextSrv, $routeParams,
  18. $rootScope) {
  19. var self = this;
  20. this._dashboardLoadFailed = function(title) {
  21. return {meta: {canStar: false, canDelete: false, canSave: false}, dashboard: {title: title}};
  22. };
  23. this.loadDashboard = function(type, slug) {
  24. var promise;
  25. if (type === 'script') {
  26. promise = this._loadScriptedDashboard(slug);
  27. } else if (type === 'snapshot') {
  28. promise = backendSrv.get('/api/snapshots/' + $routeParams.slug).catch(function() {
  29. return {meta:{isSnapshot: true, canSave: false, canEdit: false}, dashboard: {title: 'Snapshot not found'}};
  30. });
  31. } else {
  32. promise = backendSrv.getDashboard($routeParams.type, $routeParams.slug)
  33. .catch(function() {
  34. return self._dashboardLoadFailed("Not found");
  35. });
  36. }
  37. promise.then(function(result) {
  38. impressionStore.impressions.addDashboardImpression(slug);
  39. return result;
  40. });
  41. return promise;
  42. };
  43. this._loadScriptedDashboard = function(file) {
  44. var url = 'public/dashboards/'+file.replace(/\.(?!js)/,"/") + '?' + new Date().getTime();
  45. return $http({ url: url, method: "GET" })
  46. .then(this._executeScript).then(function(result) {
  47. return { meta: { fromScript: true, canDelete: false, canSave: false, canStar: false}, dashboard: result.data };
  48. }, function(err) {
  49. console.log('Script dashboard error '+ err);
  50. $rootScope.appEvent('alert-error', ["Script Error", "Please make sure it exists and returns a valid dashboard"]);
  51. return self._dashboardLoadFailed('Scripted dashboard');
  52. });
  53. };
  54. this._executeScript = function(result) {
  55. var services = {
  56. dashboardSrv: dashboardSrv,
  57. datasourceSrv: datasourceSrv,
  58. $q: $q,
  59. };
  60. /*jshint -W054 */
  61. var script_func = new Function('ARGS','kbn','dateMath','_','moment','window','document','$','jQuery', 'services', result.data);
  62. var script_result = script_func($routeParams, kbn, dateMath, _ , moment, window, document, $, $, services);
  63. // Handle async dashboard scripts
  64. if (_.isFunction(script_result)) {
  65. var deferred = $q.defer();
  66. script_result(function(dashboard) {
  67. $timeout(function() {
  68. deferred.resolve({ data: dashboard });
  69. });
  70. });
  71. return deferred.promise;
  72. }
  73. return { data: script_result };
  74. };
  75. });
  76. });