datasource_srv.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. define([
  2. 'angular',
  3. 'lodash',
  4. '../core_module',
  5. 'app/core/config',
  6. ],
  7. function (angular, _, coreModule, config) {
  8. 'use strict';
  9. coreModule.default.service('datasourceSrv', function($q, $injector, $rootScope, templateSrv) {
  10. var self = this;
  11. this.init = function() {
  12. this.datasources = {};
  13. };
  14. this.get = function(name) {
  15. if (!name) {
  16. return this.get(config.defaultDatasource);
  17. }
  18. name = templateSrv.replace(name);
  19. if (this.datasources[name]) {
  20. return $q.when(this.datasources[name]);
  21. }
  22. return this.loadDatasource(name);
  23. };
  24. this.loadDatasource = function(name) {
  25. var dsConfig = config.datasources[name];
  26. if (!dsConfig) {
  27. return $q.reject({message: "Datasource named " + name + " was not found"});
  28. }
  29. var deferred = $q.defer();
  30. var pluginDef = dsConfig.meta;
  31. System.import(pluginDef.module).then(function(plugin) {
  32. // check if its in cache now
  33. if (self.datasources[name]) {
  34. deferred.resolve(self.datasources[name]);
  35. return;
  36. }
  37. // plugin module needs to export a constructor function named Datasource
  38. if (!plugin.Datasource) {
  39. throw "Plugin module is missing Datasource constructor";
  40. }
  41. var instance = $injector.instantiate(plugin.Datasource, {instanceSettings: dsConfig});
  42. instance.meta = pluginDef;
  43. instance.name = name;
  44. self.datasources[name] = instance;
  45. deferred.resolve(instance);
  46. }).catch(function(err) {
  47. $rootScope.appEvent('alert-error', [dsConfig.name + ' plugin failed', err.toString()]);
  48. });
  49. return deferred.promise;
  50. };
  51. this.getAll = function() {
  52. return config.datasources;
  53. };
  54. this.getAnnotationSources = function() {
  55. return _.reduce(config.datasources, function(memo, value) {
  56. if (value.meta && value.meta.annotations) {
  57. memo.push(value);
  58. }
  59. return memo;
  60. }, []);
  61. };
  62. this.getMetricSources = function(options) {
  63. var metricSources = [];
  64. _.each(config.datasources, function(value, key) {
  65. if (value.meta && value.meta.metrics) {
  66. metricSources.push({value: key, name: key, meta: value.meta});
  67. if (key === config.defaultDatasource) {
  68. metricSources.push({value: null, name: 'default', meta: value.meta});
  69. }
  70. }
  71. });
  72. if (!options || !options.skipVariables) {
  73. // look for data source variables
  74. for (var i = 0; i < templateSrv.variables.length; i++) {
  75. var variable = templateSrv.variables[i];
  76. if (variable.type !== 'datasource') {
  77. continue;
  78. }
  79. var first = variable.current.value;
  80. var ds = config.datasources[first];
  81. if (ds) {
  82. metricSources.push({
  83. name: '$' + variable.name,
  84. value: '$' + variable.name,
  85. meta: ds.meta,
  86. });
  87. }
  88. }
  89. }
  90. metricSources.sort(function(a, b) {
  91. if (a.meta.builtIn || a.name > b.name) {
  92. return 1;
  93. }
  94. if (a.name < b.name) {
  95. return -1;
  96. }
  97. return 0;
  98. });
  99. return metricSources;
  100. };
  101. this.init();
  102. });
  103. });