db connector: ds loading refactor
This commit is contained in:
@@ -42,26 +42,33 @@ export class DBConnector {
|
||||
this.datasourceTypeName = null;
|
||||
}
|
||||
|
||||
loadDBDataSource() {
|
||||
if (!this.datasourceName && this.datasourceId !== undefined) {
|
||||
let ds = _.find(this.datasourceSrv.getAll(), {'id': this.datasourceId});
|
||||
static loadDatasource(dsId, dsName, datasourceSrv) {
|
||||
if (!dsName && dsId !== undefined) {
|
||||
let ds = _.find(datasourceSrv.getAll(), {'id': dsId});
|
||||
if (!ds) {
|
||||
return Promise.reject(`SQL Data Source with ID ${this.datasourceId} not found`);
|
||||
return Promise.reject(`Data Source with ID ${dsId} not found`);
|
||||
}
|
||||
this.datasourceName = ds.name;
|
||||
dsName = ds.name;
|
||||
}
|
||||
if (this.datasourceName) {
|
||||
return this.datasourceSrv.loadDatasource(this.datasourceName)
|
||||
.then(ds => {
|
||||
this.datasourceTypeId = ds.meta.id;
|
||||
this.datasourceTypeName = ds.meta.name;
|
||||
return ds;
|
||||
});
|
||||
if (dsName) {
|
||||
return datasourceSrv.loadDatasource(dsName);
|
||||
} else {
|
||||
return Promise.reject(`SQL Data Source name should be specified`);
|
||||
return Promise.reject(`Data Source name should be specified`);
|
||||
}
|
||||
}
|
||||
|
||||
loadDBDataSource() {
|
||||
return DBConnector.loadDatasource(this.datasourceId, this.datasourceName, this.datasourceSrv)
|
||||
.then(ds => {
|
||||
this.datasourceTypeId = ds.meta.id;
|
||||
this.datasourceTypeName = ds.meta.name;
|
||||
if (!this.datasourceName) {
|
||||
this.datasourceName = ds.name;
|
||||
}
|
||||
return ds;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Send test request to datasource in order to ensure it's working.
|
||||
*/
|
||||
|
||||
@@ -9,15 +9,17 @@ const supportedDatabases = {
|
||||
};
|
||||
|
||||
export class SQLConnector extends DBConnector {
|
||||
constructor(options, datasourceSrv, backendSrv) {
|
||||
constructor(options, datasourceSrv) {
|
||||
super(options, datasourceSrv);
|
||||
this.backendSrv = backendSrv;
|
||||
|
||||
this.limit = options.limit || DEFAULT_QUERY_LIMIT;
|
||||
this.sqlDialect = null;
|
||||
|
||||
super.loadDBDataSource()
|
||||
.then(() => this.loadSQLDialect());
|
||||
.then(ds => {
|
||||
this.backendSrv = ds.backendSrv;
|
||||
this.loadSQLDialect();
|
||||
});
|
||||
}
|
||||
|
||||
loadSQLDialect() {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import _ from 'lodash';
|
||||
import * as utils from '../utils';
|
||||
import responseHandler from '../responseHandler';
|
||||
import { CachingProxy } from './proxy/cachingProxy';
|
||||
import { ZabbixNotImplemented } from './connectors/dbConnector';
|
||||
import { DBConnector } from './connectors/dbConnector';
|
||||
import { ZabbixAPIConnector } from './connectors/zabbix_api/zabbixAPIConnector';
|
||||
import { SQLConnector } from './connectors/sql/sqlConnector';
|
||||
import { InfluxDBConnector } from './connectors/influxdb/influxdbConnector';
|
||||
import { CachingProxy } from './proxy/cachingProxy';
|
||||
import { ZabbixNotImplemented } from './connectors/dbConnector';
|
||||
|
||||
const REQUESTS_TO_PROXYFY = [
|
||||
'getHistory', 'getTrend', 'getGroups', 'getHosts', 'getApps', 'getItems', 'getMacros', 'getItemsByIDs',
|
||||
@@ -55,24 +55,27 @@ export class Zabbix {
|
||||
this.bindRequests();
|
||||
|
||||
if (enableDirectDBConnection) {
|
||||
let dbConnectorOptions = {
|
||||
datasourceId: dbConnectionDatasourceId,
|
||||
datasourceName: dbConnectionDatasourceName
|
||||
};
|
||||
this.dbConnector = new DBConnector(dbConnectorOptions, datasourceSrv);
|
||||
this.dbConnector.loadDBDataSource().then(ds => {
|
||||
if (ds.type === 'influxdb') {
|
||||
this.dbConnector = new InfluxDBConnector(dbConnectorOptions, datasourceSrv);
|
||||
} else {
|
||||
this.dbConnector = new SQLConnector(dbConnectorOptions, datasourceSrv, backendSrv);
|
||||
}
|
||||
}).then(() => {
|
||||
this.initDBConnector(dbConnectionDatasourceId, dbConnectionDatasourceName, datasourceSrv)
|
||||
.then(() => {
|
||||
this.getHistoryDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getHistory, 'getHistory', this.dbConnector);
|
||||
this.getTrendsDB = this.cachingProxy.proxyfyWithCache(this.dbConnector.getTrends, 'getTrends', this.dbConnector);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
initDBConnector(datasourceId, datasourceName, datasourceSrv) {
|
||||
return DBConnector.loadDatasource(datasourceId, datasourceName, datasourceSrv)
|
||||
.then(ds => {
|
||||
const options = { datasourceId, datasourceName };
|
||||
if (ds.type === 'influxdb') {
|
||||
this.dbConnector = new InfluxDBConnector(options, datasourceSrv);
|
||||
} else {
|
||||
this.dbConnector = new SQLConnector(options, datasourceSrv);
|
||||
}
|
||||
return this.dbConnector;
|
||||
});
|
||||
}
|
||||
|
||||
proxyfyRequests() {
|
||||
for (let request of REQUESTS_TO_PROXYFY) {
|
||||
this.zabbixAPI[request] = this.cachingProxy.proxyfy(this.zabbixAPI[request], request, this.zabbixAPI);
|
||||
|
||||
Reference in New Issue
Block a user