datasource_srv.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. var dsConfig = config.datasources[name];
  29. if (!dsConfig) {
  30. return this.$q.reject({ message: 'Datasource named ' + name + ' was not found' });
  31. }
  32. var deferred = this.$q.defer();
  33. var 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. var 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. var 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. getMetricSources(options) {
  70. var metricSources = [];
  71. _.each(config.datasources, function(value, key) {
  72. if (value.meta && value.meta.metrics) {
  73. let metricSource = { value: key, name: key, meta: value.meta, sort: key };
  74. //Make sure grafana and mixed are sorted at the bottom
  75. if (value.meta.id === 'grafana') {
  76. metricSource.sort = String.fromCharCode(253);
  77. } else if (value.meta.id === 'mixed') {
  78. metricSource.sort = String.fromCharCode(254);
  79. }
  80. metricSources.push(metricSource);
  81. if (key === config.defaultDatasource) {
  82. metricSource = { value: null, name: 'default', meta: value.meta, sort: key };
  83. metricSources.push(metricSource);
  84. }
  85. }
  86. });
  87. if (!options || !options.skipVariables) {
  88. this.addDataSourceVariables(metricSources);
  89. }
  90. metricSources.sort(function(a, b) {
  91. if (a.sort.toLowerCase() > b.sort.toLowerCase()) {
  92. return 1;
  93. }
  94. if (a.sort.toLowerCase() < b.sort.toLowerCase()) {
  95. return -1;
  96. }
  97. return 0;
  98. });
  99. return metricSources;
  100. }
  101. addDataSourceVariables(list) {
  102. // look for data source variables
  103. for (var i = 0; i < this.templateSrv.variables.length; i++) {
  104. var variable = this.templateSrv.variables[i];
  105. if (variable.type !== 'datasource') {
  106. continue;
  107. }
  108. var first = variable.current.value;
  109. if (first === 'default') {
  110. first = config.defaultDatasource;
  111. }
  112. var ds = config.datasources[first];
  113. if (ds) {
  114. const key = `$${variable.name}`;
  115. list.push({
  116. name: key,
  117. value: key,
  118. meta: ds.meta,
  119. sort: key,
  120. });
  121. }
  122. }
  123. }
  124. }
  125. coreModule.service('datasourceSrv', DatasourceSrv);