datasource_srv.js 3.2 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, key, 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({
  67. value: key === config.defaultDatasource ? null : key,
  68. name: key,
  69. meta: value.meta,
  70. });
  71. }
  72. });
  73. if (!options || !options.skipVariables) {
  74. // look for data source variables
  75. for (var i = 0; i < templateSrv.variables.length; i++) {
  76. var variable = templateSrv.variables[i];
  77. if (variable.type !== 'datasource') {
  78. continue;
  79. }
  80. var first = variable.current.value;
  81. var ds = config.datasources[first];
  82. if (ds) {
  83. metricSources.push({
  84. name: '$' + variable.name,
  85. value: '$' + variable.name,
  86. meta: ds.meta,
  87. });
  88. }
  89. }
  90. }
  91. metricSources.sort(function(a, b) {
  92. if (a.meta.builtIn || a.name > b.name) {
  93. return 1;
  94. }
  95. if (a.name < b.name) {
  96. return -1;
  97. }
  98. return 0;
  99. });
  100. return metricSources;
  101. };
  102. this.init();
  103. });
  104. });