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