grafana/public/app/features/plugins/datasource_srv.ts

185 lines
4.6 KiB
TypeScript
Raw Normal View History

2018-10-14 11:19:49 -05:00
// Libraries
2017-12-22 13:52:57 -06:00
import _ from 'lodash';
import coreModule from 'app/core/core_module';
2018-10-14 11:19:49 -05:00
// Utils
2017-12-22 13:52:57 -06:00
import config from 'app/core/config';
import { importPluginModule } from './plugin_loader';
2018-10-14 11:19:49 -05:00
// Types
import { DataSourceApi } from 'app/types/series';
2018-10-30 08:38:34 -05:00
import { DataSource } from 'app/types';
2018-10-14 11:19:49 -05:00
2017-12-22 13:52:57 -06:00
export class DatasourceSrv {
2018-10-30 08:38:34 -05:00
datasources: { [name: string]: DataSource };
2017-12-22 13:52:57 -06:00
/** @ngInject */
constructor(private $q, private $injector, private $rootScope, private templateSrv) {
2017-12-22 13:52:57 -06:00
this.init();
}
init() {
this.datasources = {};
}
get(name?: string): Promise<DataSourceApi> {
2017-12-22 13:52:57 -06:00
if (!name) {
return this.get(config.defaultDatasource);
}
name = this.templateSrv.replace(name);
if (name === 'default') {
return this.get(config.defaultDatasource);
}
if (this.datasources[name]) {
return this.$q.when(this.datasources[name]);
2017-12-22 13:52:57 -06:00
}
return this.loadDatasource(name);
}
loadDatasource(name: string): Promise<DataSourceApi> {
const dsConfig = config.datasources[name];
2017-12-22 13:52:57 -06:00
if (!dsConfig) {
return this.$q.reject({ message: 'Datasource named ' + name + ' was not found' });
2017-12-22 13:52:57 -06:00
}
const deferred = this.$q.defer();
const pluginDef = dsConfig.meta;
2017-12-22 13:52:57 -06:00
importPluginModule(pluginDef.module)
2017-12-22 13:52:57 -06:00
.then(plugin => {
// check if its in cache now
if (this.datasources[name]) {
deferred.resolve(this.datasources[name]);
return;
2017-12-22 13:52:57 -06:00
}
// plugin module needs to export a constructor function named Datasource
if (!plugin.Datasource) {
throw new Error('Plugin module is missing Datasource constructor');
}
2018-10-30 08:38:34 -05:00
const instance: DataSource = this.$injector.instantiate(plugin.Datasource, { instanceSettings: dsConfig });
2017-12-22 13:52:57 -06:00
instance.meta = pluginDef;
instance.name = name;
2018-10-30 08:38:34 -05:00
instance.pluginExports = plugin;
2017-12-22 13:52:57 -06:00
this.datasources[name] = instance;
deferred.resolve(instance);
2017-12-22 13:52:57 -06:00
})
2018-06-22 08:17:02 -05:00
.catch(err => {
2017-12-22 13:52:57 -06:00
this.$rootScope.appEvent('alert-error', [dsConfig.name + ' plugin failed', err.toString()]);
});
return deferred.promise;
2017-12-22 13:52:57 -06:00
}
getAll() {
return config.datasources;
}
getAnnotationSources() {
const sources = [];
2017-12-22 13:52:57 -06:00
this.addDataSourceVariables(sources);
_.each(config.datasources, value => {
2017-12-22 13:52:57 -06:00
if (value.meta && value.meta.annotations) {
sources.push(value);
}
});
return sources;
}
getExploreSources() {
const { datasources } = config;
const es = Object.keys(datasources)
.map(name => datasources[name])
.filter(ds => ds.meta && ds.meta.explore);
return _.sortBy(es, ['name']);
}
2017-12-22 13:52:57 -06:00
getMetricSources(options) {
const metricSources = [];
2017-12-22 13:52:57 -06:00
_.each(config.datasources, (value, key) => {
2017-12-22 13:52:57 -06:00
if (value.meta && value.meta.metrics) {
let metricSource = { value: key, name: key, meta: value.meta, sort: key };
//Make sure grafana and mixed are sorted at the bottom
if (value.meta.id === 'grafana') {
metricSource.sort = String.fromCharCode(253);
} else if (value.meta.id === 'mixed') {
metricSource.sort = String.fromCharCode(254);
}
metricSources.push(metricSource);
2017-12-22 13:52:57 -06:00
if (key === config.defaultDatasource) {
metricSource = { value: null, name: 'default', meta: value.meta, sort: key };
metricSources.push(metricSource);
2017-12-22 13:52:57 -06:00
}
}
});
if (!options || !options.skipVariables) {
this.addDataSourceVariables(metricSources);
}
metricSources.sort((a, b) => {
if (a.sort.toLowerCase() > b.sort.toLowerCase()) {
2017-12-22 13:52:57 -06:00
return 1;
}
if (a.sort.toLowerCase() < b.sort.toLowerCase()) {
2017-12-22 13:52:57 -06:00
return -1;
}
return 0;
});
return metricSources;
}
addDataSourceVariables(list) {
// look for data source variables
for (let i = 0; i < this.templateSrv.variables.length; i++) {
const variable = this.templateSrv.variables[i];
2017-12-22 13:52:57 -06:00
if (variable.type !== 'datasource') {
continue;
}
let first = variable.current.value;
2017-12-22 13:52:57 -06:00
if (first === 'default') {
first = config.defaultDatasource;
}
const ds = config.datasources[first];
2017-12-22 13:52:57 -06:00
if (ds) {
const key = `$${variable.name}`;
2017-12-22 13:52:57 -06:00
list.push({
name: key,
value: key,
2017-12-22 13:52:57 -06:00
meta: ds.meta,
sort: key,
2017-12-22 13:52:57 -06:00
});
}
}
}
}
let singleton: DatasourceSrv;
export function setDatasourceSrv(srv: DatasourceSrv) {
singleton = srv;
}
export function getDatasourceSrv(): DatasourceSrv {
return singleton;
}
2017-12-22 13:52:57 -06:00
coreModule.service('datasourceSrv', DatasourceSrv);
export default DatasourceSrv;