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

@@ -335,28 +335,20 @@ export class ZabbixDatasource {
} }
/** /**
* Test connection to Zabbix API * Test connection to Zabbix API and external history DB.
* @return {object} Connection status and Zabbix API version
*/ */
testDatasource() { testDatasource() {
let zabbixVersion; return this.zabbix.testDataSource()
return this.zabbix.getVersion() .then(result => {
.then(version => { const { zabbixVersion, dbConnectorStatus } = result;
zabbixVersion = version; let message = `Zabbix API version: ${zabbixVersion}`;
return this.zabbix.login(); if (dbConnectorStatus) {
}) message += `, DB connector type: ${dbConnectorStatus.dsType}`;
.then(() => {
if (this.enableDirectDBConnection) {
return this.zabbix.dbConnector.testDataSource();
} else {
return Promise.resolve();
} }
})
.then(() => {
return { return {
status: "success", status: "success",
title: "Success", title: "Success",
message: "Zabbix API version: " + zabbixVersion message: message
}; };
}) })
.catch(error => { .catch(error => {
@@ -372,7 +364,14 @@ export class ZabbixDatasource {
title: "Connection failed", title: "Connection failed",
message: "Connection failed: " + error.data.message message: "Connection failed: " + error.data.message
}; };
} else if (typeof(error) === 'string') {
return {
status: "error",
title: "Connection failed",
message: "Connection failed: " + error
};
} else { } else {
console.log(error);
return { return {
status: "error", status: "error",
title: "Connection failed", title: "Connection failed",

View File

@@ -1,7 +1,5 @@
import _ from 'lodash'; 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 * 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. * `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.datasourceSrv = datasourceSrv;
this.datasourceId = options.datasourceId; this.datasourceId = options.datasourceId;
this.datasourceName = options.datasourceName; this.datasourceName = options.datasourceName;
this.datasourceType = null; this.datasourceTypeId = null;
this.datasourceTypeName = null;
} }
loadDBDataSource() { loadDBDataSource() {
@@ -20,7 +19,9 @@ export default class DBConnector {
if (ds) { if (ds) {
return this.datasourceSrv.loadDatasource(ds.name) return this.datasourceSrv.loadDatasource(ds.name)
.then(ds => { .then(ds => {
this.datasourceType = ds.meta.id; this.datasourceName = ds.name;
this.datasourceTypeId = ds.meta.id;
this.datasourceTypeName = ds.meta.name;
return ds; return ds;
}); });
} else { } else {
@@ -32,20 +33,33 @@ export default class DBConnector {
* Send test request to datasource in order to ensure it's working. * Send test request to datasource in order to ensure it's working.
*/ */
testDataSource() { testDataSource() {
throw NOT_IMPLEMENTED; throw new ZabbixNotImplemented('testDataSource()');
} }
/** /**
* Get history data from external sources. * Get history data from external sources.
*/ */
getHistory() { getHistory() {
throw NOT_IMPLEMENTED; throw new ZabbixNotImplemented('getHistory()');
} }
/** /**
* Get trends data from external sources. * Get trends data from external sources.
*/ */
getTrends() { 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() { loadSQLDialect() {
if (this.datasourceType === supportedDatabases.postgres) { if (this.datasourceTypeId === supportedDatabases.postgres) {
this.sqlDialect = postgres; this.sqlDialect = postgres;
} else { } else {
this.sqlDialect = mysql; this.sqlDialect = mysql;

View File

@@ -4,6 +4,7 @@ import responseHandler from '../responseHandler';
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 { CachingProxy } from './proxy/cachingProxy'; 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',
@@ -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) { getItemsFromTarget(target, options) {
let parts = ['group', 'host', 'application', 'item']; let parts = ['group', 'host', 'application', 'item'];
let filters = _.map(parts, p => target[p].filter); let filters = _.map(parts, p => target[p].filter);