datasource_srv.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. instance.exploreComponents = plugin.ExploreComponents;
  53. this.datasources[name] = instance;
  54. deferred.resolve(instance);
  55. })
  56. .catch(err => {
  57. this.$rootScope.appEvent('alert-error', [dsConfig.name + ' plugin failed', err.toString()]);
  58. });
  59. return deferred.promise;
  60. }
  61. getAll() {
  62. return config.datasources;
  63. }
  64. getAnnotationSources() {
  65. const sources = [];
  66. this.addDataSourceVariables(sources);
  67. _.each(config.datasources, value => {
  68. if (value.meta && value.meta.annotations) {
  69. sources.push(value);
  70. }
  71. });
  72. return sources;
  73. }
  74. getExploreSources() {
  75. const { datasources } = config;
  76. const es = Object.keys(datasources)
  77. .map(name => datasources[name])
  78. .filter(ds => ds.meta && ds.meta.explore);
  79. return _.sortBy(es, ['name']);
  80. }
  81. getMetricSources(options) {
  82. const metricSources = [];
  83. _.each(config.datasources, (value, key) => {
  84. if (value.meta && value.meta.metrics) {
  85. let metricSource = { value: key, name: key, meta: value.meta, sort: key };
  86. //Make sure grafana and mixed are sorted at the bottom
  87. if (value.meta.id === 'grafana') {
  88. metricSource.sort = String.fromCharCode(253);
  89. } else if (value.meta.id === 'mixed') {
  90. metricSource.sort = String.fromCharCode(254);
  91. }
  92. metricSources.push(metricSource);
  93. if (key === config.defaultDatasource) {
  94. metricSource = { value: null, name: 'default', meta: value.meta, sort: key };
  95. metricSources.push(metricSource);
  96. }
  97. }
  98. });
  99. if (!options || !options.skipVariables) {
  100. this.addDataSourceVariables(metricSources);
  101. }
  102. metricSources.sort((a, b) => {
  103. if (a.sort.toLowerCase() > b.sort.toLowerCase()) {
  104. return 1;
  105. }
  106. if (a.sort.toLowerCase() < b.sort.toLowerCase()) {
  107. return -1;
  108. }
  109. return 0;
  110. });
  111. return metricSources;
  112. }
  113. addDataSourceVariables(list) {
  114. // look for data source variables
  115. for (let i = 0; i < this.templateSrv.variables.length; i++) {
  116. const variable = this.templateSrv.variables[i];
  117. if (variable.type !== 'datasource') {
  118. continue;
  119. }
  120. let first = variable.current.value;
  121. if (first === 'default') {
  122. first = config.defaultDatasource;
  123. }
  124. const ds = config.datasources[first];
  125. if (ds) {
  126. const key = `$${variable.name}`;
  127. list.push({
  128. name: key,
  129. value: key,
  130. meta: ds.meta,
  131. sort: key,
  132. });
  133. }
  134. }
  135. }
  136. }
  137. let singleton: DatasourceSrv;
  138. export function setDatasourceSrv(srv: DatasourceSrv) {
  139. singleton = srv;
  140. }
  141. export function getDatasourceSrv(): DatasourceSrv {
  142. return singleton;
  143. }
  144. coreModule.service('datasourceSrv', DatasourceSrv);
  145. export default DatasourceSrv;