datasource_srv.ts 4.7 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 { importPluginModule } from './plugin_loader';
  7. // Types
  8. import { DataSourceApi, DataSourceSelectItem } 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): Promise<DataSourceApi> {
  19. if (!name) {
  20. return this.get(config.defaultDatasource);
  21. }
  22. name = this.templateSrv.replace(name);
  23. if (name === 'default') {
  24. return this.get(config.defaultDatasource);
  25. }
  26. if (this.datasources[name]) {
  27. return this.$q.when(this.datasources[name]);
  28. }
  29. return this.loadDatasource(name);
  30. }
  31. loadDatasource(name: string): Promise<DataSourceApi> {
  32. // if there are multiple datasources provided, just use the first one
  33. const re = /{([^,}]+).*/;
  34. name = name.replace(re, '$1');
  35. const dsConfig = config.datasources[name];
  36. if (!dsConfig) {
  37. return this.$q.reject({ message: 'Datasource named ' + name + ' was not found' });
  38. }
  39. const deferred = this.$q.defer();
  40. const pluginDef = dsConfig.meta;
  41. importPluginModule(pluginDef.module)
  42. .then(plugin => {
  43. // check if its in cache now
  44. if (this.datasources[name]) {
  45. deferred.resolve(this.datasources[name]);
  46. return;
  47. }
  48. // plugin module needs to export a constructor function named Datasource
  49. if (!plugin.Datasource) {
  50. throw new Error('Plugin module is missing Datasource constructor');
  51. }
  52. const instance: DataSourceApi = this.$injector.instantiate(plugin.Datasource, { instanceSettings: dsConfig });
  53. instance.meta = pluginDef;
  54. instance.name = name;
  55. instance.pluginExports = plugin;
  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;