datasource_srv.ts 4.6 KB

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