mysql-connector: test MySQL data source during ds config

This commit is contained in:
Alexander Zobnin
2017-07-25 13:02:59 +03:00
parent ffeaeab988
commit 4ec84b21e5
8 changed files with 86 additions and 17 deletions

View File

@@ -392,6 +392,12 @@ var ZabbixAPIDatasource = function () {
return this.zabbix.getVersion().then(function (version) {
zabbixVersion = version;
return _this5.zabbix.login();
}).then(function () {
if (_this5.enableDirectDBConnection) {
return _this5.zabbix.dbConnector.testSQLDataSource();
} else {
return Promise.resolve();
}
}).then(function () {
return {
status: "success",
@@ -405,6 +411,12 @@ var ZabbixAPIDatasource = function () {
title: error.message,
message: error.data
};
} else if (error.data && error.data.message) {
return {
status: "error",
title: "Connection failed",
message: error.data.message
};
} else {
return {
status: "error",

View File

@@ -55,11 +55,14 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
this.sqlDataSourceId = sqlDataSourceId;
this.limit = limit || DEFAULT_QUERY_LIMIT;
// Try to load DS with given id to check it's exist
this.loadSQLDataSource(sqlDataSourceId);
}
/**
* Try to load DS with given id to check it's exist.
* @param {*} datasourceId ID of SQL data source
*/
_createClass(ZabbixDBConnector, [{
key: 'loadSQLDataSource',
value: function loadSQLDataSource(datasourceId) {
@@ -72,6 +75,17 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
return Promise.reject('SQL Data Source with ID ' + datasourceId + ' not found');
}
}
/**
* Try to invoke test query for one of Zabbix database tables.
*/
}, {
key: 'testSQLDataSource',
value: function testSQLDataSource() {
var testQuery = 'SELECT itemid AS metric, clock AS time_sec, value_avg AS value FROM trends_uint LIMIT 1';
return this.invokeSQLQuery(testQuery);
}
}, {
key: 'getHistory',
value: function getHistory(items, timeFrom, timeTill, options) {
@@ -91,7 +105,7 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
var itemids = _lodash2.default.map(items, 'itemid').join(', ');
var table = HISTORY_TO_TABLE_MAP[value_type];
var query = '\n SELECT itemid AS metric, clock AS time_sec, ' + aggFunction + '(value) as value\n FROM ' + table + '\n WHERE itemid IN (' + itemids + ')\n AND clock > ' + timeFrom + ' AND clock < ' + timeTill + '\n GROUP BY time_sec DIV ' + intervalSec + ', metric\n ';
var query = '\n SELECT itemid AS metric, clock AS time_sec, ' + aggFunction + '(value) AS value\n FROM ' + table + '\n WHERE itemid IN (' + itemids + ')\n AND clock > ' + timeFrom + ' AND clock < ' + timeTill + '\n GROUP BY time_sec DIV ' + intervalSec + ', metric\n ';
query = compactSQLQuery(query);
return _this.invokeSQLQuery(query);
@@ -122,7 +136,7 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
var valueColumn = _lodash2.default.includes(['avg', 'min', 'max'], consolidateBy) ? consolidateBy : 'avg';
valueColumn = consolidateByTrendColumns[valueColumn];
var query = '\n SELECT itemid AS metric, clock AS time_sec, ' + aggFunction + '(' + valueColumn + ') as value\n FROM ' + table + '\n WHERE itemid IN (' + itemids + ')\n AND clock > ' + timeFrom + ' AND clock < ' + timeTill + '\n GROUP BY time_sec DIV ' + intervalSec + ', metric\n ';
var query = '\n SELECT itemid AS metric, clock AS time_sec, ' + aggFunction + '(' + valueColumn + ') AS value\n FROM ' + table + '\n WHERE itemid IN (' + itemids + ')\n AND clock > ' + timeFrom + ' AND clock < ' + timeTill + '\n GROUP BY time_sec DIV ' + intervalSec + ', metric\n ';
query = compactSQLQuery(query);
return _this2.invokeSQLQuery(query);