datasource_srv.ts 4.6 KB

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