dashboard_loader_srv.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import angular from 'angular';
  2. import moment from 'moment';
  3. import _ from 'lodash';
  4. import $ from 'jquery';
  5. import kbn from 'app/core/utils/kbn';
  6. import * as dateMath from 'app/core/utils/datemath';
  7. import impressionSrv from 'app/core/services/impression_srv';
  8. export class DashboardLoaderSrv {
  9. /** @ngInject */
  10. constructor(
  11. private backendSrv,
  12. private dashboardSrv,
  13. private datasourceSrv,
  14. private $http,
  15. private $q,
  16. private $timeout,
  17. contextSrv,
  18. private $routeParams,
  19. private $rootScope
  20. ) {}
  21. _dashboardLoadFailed(title, snapshot) {
  22. snapshot = snapshot || false;
  23. return {
  24. meta: {
  25. canStar: false,
  26. isSnapshot: snapshot,
  27. canDelete: false,
  28. canSave: false,
  29. canEdit: false,
  30. dashboardNotFound: true,
  31. },
  32. dashboard: { title: title },
  33. };
  34. }
  35. loadDashboard(type, slug) {
  36. var promise;
  37. if (type === 'script') {
  38. promise = this._loadScriptedDashboard(slug);
  39. } else if (type === 'snapshot') {
  40. promise = this.backendSrv
  41. .get('/api/snapshots/' + this.$routeParams.slug)
  42. .catch(() => {
  43. return this._dashboardLoadFailed('Snapshot not found', true);
  44. });
  45. } else {
  46. promise = this.backendSrv
  47. .getDashboard(this.$routeParams.type, this.$routeParams.slug)
  48. .then(result => {
  49. if (result.meta.isFolder) {
  50. this.$rootScope.appEvent('alert-error', ['Dashboard not found']);
  51. throw new Error('Dashboard not found');
  52. }
  53. return result;
  54. })
  55. .catch(() => {
  56. return this._dashboardLoadFailed('Not found', true);
  57. });
  58. }
  59. promise.then(function(result) {
  60. if (result.meta.dashboardNotFound !== true) {
  61. impressionSrv.addDashboardImpression(result.dashboard.id);
  62. }
  63. return result;
  64. });
  65. return promise;
  66. }
  67. _loadScriptedDashboard(file) {
  68. var url =
  69. 'public/dashboards/' +
  70. file.replace(/\.(?!js)/, '/') +
  71. '?' +
  72. new Date().getTime();
  73. return this.$http({ url: url, method: 'GET' })
  74. .then(this._executeScript)
  75. .then(
  76. function(result) {
  77. return {
  78. meta: {
  79. fromScript: true,
  80. canDelete: false,
  81. canSave: false,
  82. canStar: false,
  83. },
  84. dashboard: result.data,
  85. };
  86. },
  87. function(err) {
  88. console.log('Script dashboard error ' + err);
  89. this.$rootScope.appEvent('alert-error', [
  90. 'Script Error',
  91. 'Please make sure it exists and returns a valid dashboard',
  92. ]);
  93. return this._dashboardLoadFailed('Scripted dashboard');
  94. }
  95. );
  96. }
  97. _executeScript(result) {
  98. var services = {
  99. dashboardSrv: this.dashboardSrv,
  100. datasourceSrv: this.datasourceSrv,
  101. $q: this.$q,
  102. };
  103. /*jshint -W054 */
  104. var script_func = new Function(
  105. 'ARGS',
  106. 'kbn',
  107. 'dateMath',
  108. '_',
  109. 'moment',
  110. 'window',
  111. 'document',
  112. '$',
  113. 'jQuery',
  114. 'services',
  115. result.data
  116. );
  117. var script_result = script_func(
  118. this.$routeParams,
  119. kbn,
  120. dateMath,
  121. _,
  122. moment,
  123. window,
  124. document,
  125. $,
  126. $,
  127. services
  128. );
  129. // Handle async dashboard scripts
  130. if (_.isFunction(script_result)) {
  131. var deferred = this.$q.defer();
  132. script_result(dashboard => {
  133. this.$timeout(() => {
  134. deferred.resolve({ data: dashboard });
  135. });
  136. });
  137. return deferred.promise;
  138. }
  139. return { data: script_result };
  140. }
  141. }
  142. angular
  143. .module('grafana.services')
  144. .service('dashboardLoaderSrv', DashboardLoaderSrv);