datasource_srv.ts 4.9 KB

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