datasource_srv.ts 4.4 KB

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