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

199 lines
5.5 KiB
TypeScript
Raw Normal View History

// 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
// Services & Utils
2017-12-22 13:52:57 -06:00
import config from 'app/core/config';
import { importDataSourcePlugin } from './plugin_loader';
import { DataSourceSrv as DataSourceService, getDataSourceSrv as getDataSourceService } from '@grafana/runtime';
2017-12-22 13:52:57 -06:00
// Types
import { DataSourceApi, DataSourceSelectItem, ScopedVars, AppEvents } from '@grafana/data';
import { auto } from 'angular';
import { TemplateSrv } from '../templating/template_srv';
import { GrafanaRootScope } from 'app/routes/GrafanaCtrl';
2018-10-14 11:19:49 -05:00
// Pretend Datasource
import { expressionDatasource } from 'app/features/expressions/ExpressionDatasource';
export class DatasourceSrv implements DataSourceService {
datasources: Record<string, DataSourceApi>;
2017-12-22 13:52:57 -06:00
/** @ngInject */
constructor(
private $injector: auto.IInjectorService,
private $rootScope: GrafanaRootScope,
private templateSrv: TemplateSrv
) {
2017-12-22 13:52:57 -06:00
this.init();
}
init() {
this.datasources = {};
}
get(name?: string, scopedVars?: ScopedVars): Promise<DataSourceApi> {
2017-12-22 13:52:57 -06:00
if (!name) {
return this.get(config.defaultDatasource);
}
// Interpolation here is to support template variable in data source selection
name = this.templateSrv.replace(name, scopedVars, (value: any[], variable: any) => {
if (Array.isArray(value)) {
return value[0];
}
return value;
});
2017-12-22 13:52:57 -06:00
if (name === 'default') {
return this.get(config.defaultDatasource);
}
if (this.datasources[name]) {
return Promise.resolve(this.datasources[name]);
2017-12-22 13:52:57 -06:00
}
return this.loadDatasource(name);
}
loadDatasource(name: string): Promise<DataSourceApi<any, any>> {
// Expression Datasource (not a real datasource)
if (name === expressionDatasource.name) {
this.datasources[name] = expressionDatasource as any;
return Promise.resolve(expressionDatasource);
}
const dsConfig = config.datasources[name];
2017-12-22 13:52:57 -06:00
if (!dsConfig) {
return Promise.reject({ message: `Datasource named ${name} was not found` });
2017-12-22 13:52:57 -06:00
}
return importDataSourcePlugin(dsConfig.meta)
.then(dsPlugin => {
2017-12-22 13:52:57 -06:00
// check if its in cache now
if (this.datasources[name]) {
return this.datasources[name];
2017-12-22 13:52:57 -06:00
}
// If there is only one constructor argument it is instanceSettings
const useAngular = dsPlugin.DataSourceClass.length !== 1;
const instance: DataSourceApi = useAngular
? this.$injector.instantiate(dsPlugin.DataSourceClass, {
instanceSettings: dsConfig,
})
: new dsPlugin.DataSourceClass(dsConfig);
2017-12-22 13:52:57 -06:00
instance.components = dsPlugin.components;
instance.meta = dsConfig.meta;
// store in instance cache
2017-12-22 13:52:57 -06:00
this.datasources[name] = instance;
return instance;
2017-12-22 13:52:57 -06:00
})
2018-06-22 08:17:02 -05:00
.catch(err => {
this.$rootScope.appEvent(AppEvents.alertError, [dsConfig.name + ' plugin failed', err.toString()]);
return undefined;
2017-12-22 13:52:57 -06:00
});
}
getAll() {
const { datasources } = config;
return Object.keys(datasources).map(name => datasources[name]);
2017-12-22 13:52:57 -06:00
}
getExternal() {
const datasources = this.getAll().filter(ds => !ds.meta.builtIn);
return _.sortBy(datasources, ['name']);
}
2017-12-22 13:52:57 -06:00
getAnnotationSources() {
const sources: any[] = [];
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;
}
getMetricSources(options?: { skipVariables?: boolean }) {
2018-11-09 08:49:55 -06:00
const metricSources: DataSourceSelectItem[] = [];
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 === 'dashboard') {
metricSource.sort = String.fromCharCode(254);
} else if (value.meta.id === 'mixed') {
metricSource.sort = String.fromCharCode(255);
}
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: any[]) {
2017-12-22 13:52:57 -06:00
// 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
});
}
}
}
}
export const getDatasourceSrv = (): DatasourceSrv => {
return getDataSourceService() as DatasourceSrv;
};
2017-12-22 13:52:57 -06:00
coreModule.service('datasourceSrv', DatasourceSrv);
export default DatasourceSrv;