datasource_srv.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 { importDataSourcePlugin } from './plugin_loader';
  7. import { DataSourceSrv as DataSourceService, getDataSourceSrv as getDataSourceService } from '@grafana/runtime';
  8. // Types
  9. import { DataSourceApi, DataSourceSelectItem, ScopedVars } from '@grafana/ui';
  10. import { auto } from 'angular';
  11. import { TemplateSrv } from '../templating/template_srv';
  12. export class DatasourceSrv implements DataSourceService {
  13. datasources: { [name: string]: DataSourceApi };
  14. /** @ngInject */
  15. constructor(
  16. private $q: any,
  17. private $injector: auto.IInjectorService,
  18. private $rootScope: any,
  19. private templateSrv: TemplateSrv
  20. ) {
  21. this.init();
  22. }
  23. init() {
  24. this.datasources = {};
  25. }
  26. get(name?: string, scopedVars?: ScopedVars): Promise<DataSourceApi> {
  27. if (!name) {
  28. return this.get(config.defaultDatasource);
  29. }
  30. // Interpolation here is to support template variable in data source selection
  31. name = this.templateSrv.replace(name, scopedVars, (value: any[], variable: any) => {
  32. if (Array.isArray(value)) {
  33. return value[0];
  34. }
  35. return value;
  36. });
  37. if (name === 'default') {
  38. return this.get(config.defaultDatasource);
  39. }
  40. if (this.datasources[name]) {
  41. return this.$q.when(this.datasources[name]);
  42. }
  43. return this.loadDatasource(name);
  44. }
  45. loadDatasource(name: string): Promise<DataSourceApi> {
  46. const dsConfig = config.datasources[name];
  47. if (!dsConfig) {
  48. return this.$q.reject({ message: 'Datasource named ' + name + ' was not found' });
  49. }
  50. const deferred = this.$q.defer();
  51. importDataSourcePlugin(dsConfig.meta)
  52. .then(dsPlugin => {
  53. // check if its in cache now
  54. if (this.datasources[name]) {
  55. deferred.resolve(this.datasources[name]);
  56. return;
  57. }
  58. // If there is only one constructor argument it is instanceSettings
  59. const useAngular = dsPlugin.DataSourceClass.length !== 1;
  60. const instance: DataSourceApi = useAngular
  61. ? this.$injector.instantiate(dsPlugin.DataSourceClass, {
  62. instanceSettings: dsConfig,
  63. })
  64. : new dsPlugin.DataSourceClass(dsConfig);
  65. instance.components = dsPlugin.components;
  66. instance.meta = dsConfig.meta;
  67. // store in instance cache
  68. this.datasources[name] = instance;
  69. deferred.resolve(instance);
  70. })
  71. .catch(err => {
  72. this.$rootScope.appEvent('alert-error', [dsConfig.name + ' plugin failed', err.toString()]);
  73. });
  74. return deferred.promise;
  75. }
  76. getAll() {
  77. const { datasources } = config;
  78. return Object.keys(datasources).map(name => datasources[name]);
  79. }
  80. getExternal() {
  81. const datasources = this.getAll().filter(ds => !ds.meta.builtIn);
  82. return _.sortBy(datasources, ['name']);
  83. }
  84. getAnnotationSources() {
  85. const sources: any[] = [];
  86. this.addDataSourceVariables(sources);
  87. _.each(config.datasources, value => {
  88. if (value.meta && value.meta.annotations) {
  89. sources.push(value);
  90. }
  91. });
  92. return sources;
  93. }
  94. getMetricSources(options?: { skipVariables?: boolean }) {
  95. const metricSources: DataSourceSelectItem[] = [];
  96. _.each(config.datasources, (value, key) => {
  97. if (value.meta && value.meta.metrics) {
  98. let metricSource = { value: key, name: key, meta: value.meta, sort: key };
  99. //Make sure grafana and mixed are sorted at the bottom
  100. if (value.meta.id === 'grafana') {
  101. metricSource.sort = String.fromCharCode(253);
  102. } else if (value.meta.id === 'dashboard') {
  103. metricSource.sort = String.fromCharCode(254);
  104. } else if (value.meta.id === 'mixed') {
  105. metricSource.sort = String.fromCharCode(255);
  106. }
  107. metricSources.push(metricSource);
  108. if (key === config.defaultDatasource) {
  109. metricSource = { value: null, name: 'default', meta: value.meta, sort: key };
  110. metricSources.push(metricSource);
  111. }
  112. }
  113. });
  114. if (!options || !options.skipVariables) {
  115. this.addDataSourceVariables(metricSources);
  116. }
  117. metricSources.sort((a, b) => {
  118. if (a.sort.toLowerCase() > b.sort.toLowerCase()) {
  119. return 1;
  120. }
  121. if (a.sort.toLowerCase() < b.sort.toLowerCase()) {
  122. return -1;
  123. }
  124. return 0;
  125. });
  126. return metricSources;
  127. }
  128. addDataSourceVariables(list: any[]) {
  129. // look for data source variables
  130. for (let i = 0; i < this.templateSrv.variables.length; i++) {
  131. const variable = this.templateSrv.variables[i];
  132. if (variable.type !== 'datasource') {
  133. continue;
  134. }
  135. let first = variable.current.value;
  136. if (first === 'default') {
  137. first = config.defaultDatasource;
  138. }
  139. const ds = config.datasources[first];
  140. if (ds) {
  141. const key = `$${variable.name}`;
  142. list.push({
  143. name: key,
  144. value: key,
  145. meta: ds.meta,
  146. sort: key,
  147. });
  148. }
  149. }
  150. }
  151. }
  152. export function getDatasourceSrv(): DatasourceSrv {
  153. return getDataSourceService() as DatasourceSrv;
  154. }
  155. coreModule.service('datasourceSrv', DatasourceSrv);
  156. export default DatasourceSrv;