datasource_srv.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import _ from 'lodash';
  2. import coreModule from 'app/core/core_module';
  3. import config from 'app/core/config';
  4. import { importPluginModule } from './plugin_loader';
  5. export class DatasourceSrv {
  6. datasources: any;
  7. /** @ngInject */
  8. constructor(private $q, private $injector, private $rootScope, private templateSrv) {
  9. this.init();
  10. }
  11. init() {
  12. this.datasources = {};
  13. }
  14. get(name?) {
  15. if (!name) {
  16. return this.get(config.defaultDatasource);
  17. }
  18. name = this.templateSrv.replace(name);
  19. if (name === 'default') {
  20. return this.get(config.defaultDatasource);
  21. }
  22. if (this.datasources[name]) {
  23. return this.$q.when(this.datasources[name]);
  24. }
  25. return this.loadDatasource(name);
  26. }
  27. loadDatasource(name) {
  28. const dsConfig = config.datasources[name];
  29. if (!dsConfig) {
  30. return this.$q.reject({ message: 'Datasource named ' + name + ' was not found' });
  31. }
  32. const deferred = this.$q.defer();
  33. const pluginDef = dsConfig.meta;
  34. importPluginModule(pluginDef.module)
  35. .then(plugin => {
  36. // check if its in cache now
  37. if (this.datasources[name]) {
  38. deferred.resolve(this.datasources[name]);
  39. return;
  40. }
  41. // plugin module needs to export a constructor function named Datasource
  42. if (!plugin.Datasource) {
  43. throw new Error('Plugin module is missing Datasource constructor');
  44. }
  45. const instance = this.$injector.instantiate(plugin.Datasource, { instanceSettings: dsConfig });
  46. instance.meta = pluginDef;
  47. instance.name = name;
  48. this.datasources[name] = instance;
  49. deferred.resolve(instance);
  50. })
  51. .catch(err => {
  52. this.$rootScope.appEvent('alert-error', [dsConfig.name + ' plugin failed', err.toString()]);
  53. });
  54. return deferred.promise;
  55. }
  56. getAll() {
  57. return config.datasources;
  58. }
  59. getAnnotationSources() {
  60. const sources = [];
  61. this.addDataSourceVariables(sources);
  62. _.each(config.datasources, function(value) {
  63. if (value.meta && value.meta.annotations) {
  64. sources.push(value);
  65. }
  66. });
  67. return sources;
  68. }
  69. getExploreSources() {
  70. const { datasources } = config;
  71. const es = Object.keys(datasources)
  72. .map(name => datasources[name])
  73. .filter(ds => ds.meta && ds.meta.explore);
  74. return _.sortBy(es, ['name']);
  75. }
  76. getMetricSources(options) {
  77. const metricSources = [];
  78. _.each(config.datasources, function(value, key) {
  79. if (value.meta && value.meta.metrics) {
  80. let metricSource = { value: key, name: key, meta: value.meta, sort: key };
  81. //Make sure grafana and mixed are sorted at the bottom
  82. if (value.meta.id === 'grafana') {
  83. metricSource.sort = String.fromCharCode(253);
  84. } else if (value.meta.id === 'mixed') {
  85. metricSource.sort = String.fromCharCode(254);
  86. }
  87. metricSources.push(metricSource);
  88. if (key === config.defaultDatasource) {
  89. metricSource = { value: null, name: 'default', meta: value.meta, sort: key };
  90. metricSources.push(metricSource);
  91. }
  92. }
  93. });
  94. if (!options || !options.skipVariables) {
  95. this.addDataSourceVariables(metricSources);
  96. }
  97. metricSources.sort(function(a, b) {
  98. if (a.sort.toLowerCase() > b.sort.toLowerCase()) {
  99. return 1;
  100. }
  101. if (a.sort.toLowerCase() < b.sort.toLowerCase()) {
  102. return -1;
  103. }
  104. return 0;
  105. });
  106. return metricSources;
  107. }
  108. addDataSourceVariables(list) {
  109. // look for data source variables
  110. for (let i = 0; i < this.templateSrv.variables.length; i++) {
  111. const variable = this.templateSrv.variables[i];
  112. if (variable.type !== 'datasource') {
  113. continue;
  114. }
  115. let first = variable.current.value;
  116. if (first === 'default') {
  117. first = config.defaultDatasource;
  118. }
  119. const ds = config.datasources[first];
  120. if (ds) {
  121. const key = `$${variable.name}`;
  122. list.push({
  123. name: key,
  124. value: key,
  125. meta: ds.meta,
  126. sort: key,
  127. });
  128. }
  129. }
  130. }
  131. }
  132. coreModule.service('datasourceSrv', DatasourceSrv);
  133. export default DatasourceSrv;