refactor testDataSource() method

This commit is contained in:
Alexander Zobnin
2018-08-28 17:22:50 +03:00
parent 8a51d1d854
commit 056d9bcd26
4 changed files with 83 additions and 24 deletions

View File

@@ -1,7 +1,5 @@
import _ from 'lodash';
const NOT_IMPLEMENTED = 'Method should be implemented in subclass of DBConnector';
/**
* Base class for external history database connectors. Subclasses should implement `getHistory()`, `getTrends()` and
* `testDataSource()` methods, which describe how to fetch data from source other than Zabbix API.
@@ -12,7 +10,8 @@ export default class DBConnector {
this.datasourceSrv = datasourceSrv;
this.datasourceId = options.datasourceId;
this.datasourceName = options.datasourceName;
this.datasourceType = null;
this.datasourceTypeId = null;
this.datasourceTypeName = null;
}
loadDBDataSource() {
@@ -20,7 +19,9 @@ export default class DBConnector {
if (ds) {
return this.datasourceSrv.loadDatasource(ds.name)
.then(ds => {
this.datasourceType = ds.meta.id;
this.datasourceName = ds.name;
this.datasourceTypeId = ds.meta.id;
this.datasourceTypeName = ds.meta.name;
return ds;
});
} else {
@@ -32,20 +33,33 @@ export default class DBConnector {
* Send test request to datasource in order to ensure it's working.
*/
testDataSource() {
throw NOT_IMPLEMENTED;
throw new ZabbixNotImplemented('testDataSource()');
}
/**
* Get history data from external sources.
*/
getHistory() {
throw NOT_IMPLEMENTED;
throw new ZabbixNotImplemented('getHistory()');
}
/**
* Get trends data from external sources.
*/
getTrends() {
throw NOT_IMPLEMENTED;
throw new ZabbixNotImplemented('getTrends()');
}
}
// Define Zabbix DB Connector exception type for non-implemented methods
export class ZabbixNotImplemented {
constructor(methodName) {
this.code = null;
this.name = 'ZabbixNotImplemented';
this.message = `Zabbix DB Connector Error: method ${methodName || ''} should be implemented in subclass of DBConnector`;
}
toString() {
return this.message;
}
}

View File

@@ -48,7 +48,7 @@ export class SQLConnector extends DBConnector {
}
loadSQLDialect() {
if (this.datasourceType === supportedDatabases.postgres) {
if (this.datasourceTypeId === supportedDatabases.postgres) {
this.sqlDialect = postgres;
} else {
this.sqlDialect = mysql;

View File

@@ -4,6 +4,7 @@ import responseHandler from '../responseHandler';
import { ZabbixAPIConnector } from './connectors/zabbix_api/zabbixAPIConnector';
import { SQLConnector } from './connectors/sql/sqlConnector';
import { CachingProxy } from './proxy/cachingProxy';
import { ZabbixNotImplemented } from './connectors/dbConnector';
const REQUESTS_TO_PROXYFY = [
'getHistory', 'getTrend', 'getGroups', 'getHosts', 'getApps', 'getItems', 'getMacros', 'getItemsByIDs',
@@ -73,6 +74,51 @@ export class Zabbix {
}
}
/**
* Perform test query for Zabbix API and external history DB.
* @return {object} test result object:
* ```
{
zabbixVersion,
dbConnectorStatus: {
dsType,
dsName
}
}
```
*/
testDataSource() {
let zabbixVersion;
let dbConnectorStatus;
return this.getVersion()
.then(version => {
zabbixVersion = version;
return this.login();
})
.then(() => {
if (this.enableDirectDBConnection) {
return this.dbConnector.testDataSource();
} else {
return Promise.resolve();
}
})
.catch(error => {
if (error instanceof ZabbixNotImplemented) {
return Promise.resolve();
}
return Promise.reject(error);
})
.then(testResult => {
if (testResult) {
dbConnectorStatus = {
dsType: this.dbConnector.datasourceTypeName,
dsName: this.dbConnector.datasourceName
};
}
return { zabbixVersion, dbConnectorStatus };
});
}
getItemsFromTarget(target, options) {
let parts = ['group', 'host', 'application', 'item'];
let filters = _.map(parts, p => target[p].filter);