datasource_srv.ts 4.5 KB

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