datasource_srv.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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, $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(function(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. metricSources.push({ value: key, name: key, meta: value.meta });
  74. if (key === config.defaultDatasource) {
  75. metricSources.push({ value: null, name: 'default', meta: value.meta });
  76. }
  77. }
  78. });
  79. if (!options || !options.skipVariables) {
  80. this.addDataSourceVariables(metricSources);
  81. }
  82. metricSources.sort(function(a, b) {
  83. // these two should always be at the bottom
  84. if (a.meta.id === 'mixed' || a.meta.id === 'grafana') {
  85. return 1;
  86. }
  87. if (b.meta.id === 'mixed' || b.meta.id === 'grafana') {
  88. return -1;
  89. }
  90. if (a.name.toLowerCase() > b.name.toLowerCase()) {
  91. return 1;
  92. }
  93. if (a.name.toLowerCase() < b.name.toLowerCase()) {
  94. return -1;
  95. }
  96. return 0;
  97. });
  98. return metricSources;
  99. }
  100. addDataSourceVariables(list) {
  101. // look for data source variables
  102. for (var i = 0; i < this.templateSrv.variables.length; i++) {
  103. var variable = this.templateSrv.variables[i];
  104. if (variable.type !== 'datasource') {
  105. continue;
  106. }
  107. var first = variable.current.value;
  108. if (first === 'default') {
  109. first = config.defaultDatasource;
  110. }
  111. var ds = config.datasources[first];
  112. if (ds) {
  113. list.push({
  114. name: '$' + variable.name,
  115. value: '$' + variable.name,
  116. meta: ds.meta,
  117. });
  118. }
  119. }
  120. }
  121. }
  122. coreModule.service('datasourceSrv', DatasourceSrv);