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