datasource_srv.ts 4.8 KB

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