datasource_srv.ts 4.6 KB

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