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

@@ -526,6 +526,12 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
return this.zabbix.getVersion().then(function (version) { return this.zabbix.getVersion().then(function (version) {
zabbixVersion = version; zabbixVersion = version;
return _this5.zabbix.login(); return _this5.zabbix.login();
}).then(function () {
if (_this5.enableDirectDBConnection) {
return _this5.zabbix.dbConnector.testSQLDataSource();
} else {
return Promise.resolve();
}
}).then(function () { }).then(function () {
return { return {
status: "success", status: "success",
@@ -539,6 +545,12 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
title: error.message, title: error.message,
message: error.data message: error.data
}; };
} else if (error.data && error.data.message) {
return {
status: "error",
title: "Connection failed",
message: error.data.message
};
} else { } else {
return { return {
status: "error", status: "error",

File diff suppressed because one or more lines are too long

View File

@@ -24,11 +24,14 @@ System.register(['angular', 'lodash'], function (_export, _context) {
this.sqlDataSourceId = sqlDataSourceId; this.sqlDataSourceId = sqlDataSourceId;
this.limit = limit || DEFAULT_QUERY_LIMIT; 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, [{ _createClass(ZabbixDBConnector, [{
key: 'loadSQLDataSource', key: 'loadSQLDataSource',
value: function loadSQLDataSource(datasourceId) { value: function loadSQLDataSource(datasourceId) {
@@ -41,6 +44,12 @@ System.register(['angular', 'lodash'], function (_export, _context) {
return Promise.reject('SQL Data Source with ID ' + datasourceId + ' not found'); return Promise.reject('SQL Data Source with ID ' + datasourceId + ' not found');
} }
} }
}, {
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', key: 'getHistory',
value: function getHistory(items, timeFrom, timeTill, options) { value: function getHistory(items, timeFrom, timeTill, options) {
@@ -60,7 +69,7 @@ System.register(['angular', 'lodash'], function (_export, _context) {
var itemids = _.map(items, 'itemid').join(', '); var itemids = _.map(items, 'itemid').join(', ');
var table = HISTORY_TO_TABLE_MAP[value_type]; 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); query = compactSQLQuery(query);
return _this.invokeSQLQuery(query); return _this.invokeSQLQuery(query);
@@ -91,7 +100,7 @@ System.register(['angular', 'lodash'], function (_export, _context) {
var valueColumn = _.includes(['avg', 'min', 'max'], consolidateBy) ? consolidateBy : 'avg'; var valueColumn = _.includes(['avg', 'min', 'max'], consolidateBy) ? consolidateBy : 'avg';
valueColumn = consolidateByTrendColumns[valueColumn]; 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); query = compactSQLQuery(query);
return _this2.invokeSQLQuery(query); return _this2.invokeSQLQuery(query);

File diff suppressed because one or more lines are too long

View File

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

View File

@@ -55,11 +55,14 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
this.sqlDataSourceId = sqlDataSourceId; this.sqlDataSourceId = sqlDataSourceId;
this.limit = limit || DEFAULT_QUERY_LIMIT; 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, [{ _createClass(ZabbixDBConnector, [{
key: 'loadSQLDataSource', key: 'loadSQLDataSource',
value: function loadSQLDataSource(datasourceId) { value: function loadSQLDataSource(datasourceId) {
@@ -72,6 +75,17 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
return Promise.reject('SQL Data Source with ID ' + datasourceId + ' not found'); 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', key: 'getHistory',
value: function getHistory(items, timeFrom, timeTill, options) { value: function getHistory(items, timeFrom, timeTill, options) {
@@ -91,7 +105,7 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
var itemids = _lodash2.default.map(items, 'itemid').join(', '); var itemids = _lodash2.default.map(items, 'itemid').join(', ');
var table = HISTORY_TO_TABLE_MAP[value_type]; 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); query = compactSQLQuery(query);
return _this.invokeSQLQuery(query); return _this.invokeSQLQuery(query);
@@ -122,7 +136,7 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
var valueColumn = _lodash2.default.includes(['avg', 'min', 'max'], consolidateBy) ? consolidateBy : 'avg'; var valueColumn = _lodash2.default.includes(['avg', 'min', 'max'], consolidateBy) ? consolidateBy : 'avg';
valueColumn = consolidateByTrendColumns[valueColumn]; 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); query = compactSQLQuery(query);
return _this2.invokeSQLQuery(query); return _this2.invokeSQLQuery(query);

View File

@@ -318,6 +318,13 @@ class ZabbixAPIDatasource {
zabbixVersion = version; zabbixVersion = version;
return this.zabbix.login(); return this.zabbix.login();
}) })
.then(() => {
if (this.enableDirectDBConnection) {
return this.zabbix.dbConnector.testSQLDataSource();
} else {
return Promise.resolve();
}
})
.then(() => { .then(() => {
return { return {
status: "success", status: "success",
@@ -332,6 +339,12 @@ class ZabbixAPIDatasource {
title: error.message, title: error.message,
message: error.data message: error.data
}; };
} else if (error.data && error.data.message) {
return {
status: "error",
title: "Connection failed",
message: error.data.message
};
} else { } else {
return { return {
status: "error", status: "error",

View File

@@ -39,11 +39,12 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
this.sqlDataSourceId = sqlDataSourceId; this.sqlDataSourceId = sqlDataSourceId;
this.limit = limit || DEFAULT_QUERY_LIMIT; 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
*/
loadSQLDataSource(datasourceId) { loadSQLDataSource(datasourceId) {
let ds = _.find(datasourceSrv.getAll(), {'id': datasourceId}); let ds = _.find(datasourceSrv.getAll(), {'id': datasourceId});
if (ds) { if (ds) {
@@ -56,6 +57,14 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
} }
} }
/**
* Try to invoke test query for one of Zabbix database tables.
*/
testSQLDataSource() {
let testQuery = `SELECT itemid AS metric, clock AS time_sec, value_avg AS value FROM trends_uint LIMIT 1`;
return this.invokeSQLQuery(testQuery);
}
getHistory(items, timeFrom, timeTill, options) { getHistory(items, timeFrom, timeTill, options) {
let {intervalMs, consolidateBy} = options; let {intervalMs, consolidateBy} = options;
let intervalSec = Math.ceil(intervalMs / 1000); let intervalSec = Math.ceil(intervalMs / 1000);
@@ -70,7 +79,7 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
let table = HISTORY_TO_TABLE_MAP[value_type]; let table = HISTORY_TO_TABLE_MAP[value_type];
let query = ` let query = `
SELECT itemid AS metric, clock AS time_sec, ${aggFunction}(value) as value SELECT itemid AS metric, clock AS time_sec, ${aggFunction}(value) AS value
FROM ${table} FROM ${table}
WHERE itemid IN (${itemids}) WHERE itemid IN (${itemids})
AND clock > ${timeFrom} AND clock < ${timeTill} AND clock > ${timeFrom} AND clock < ${timeTill}
@@ -102,7 +111,7 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
valueColumn = consolidateByTrendColumns[valueColumn]; valueColumn = consolidateByTrendColumns[valueColumn];
let query = ` let query = `
SELECT itemid AS metric, clock AS time_sec, ${aggFunction}(${valueColumn}) as value SELECT itemid AS metric, clock AS time_sec, ${aggFunction}(${valueColumn}) AS value
FROM ${table} FROM ${table}
WHERE itemid IN (${itemids}) WHERE itemid IN (${itemids})
AND clock > ${timeFrom} AND clock < ${timeTill} AND clock > ${timeFrom} AND clock < ${timeTill}