datasource_srv.ts 5.3 KB

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