mysql-connector: aggregate values by time interval on server side

This commit is contained in:
Alexander Zobnin
2017-07-21 22:22:16 +03:00
parent 32e8806158
commit 0369fb83a5
8 changed files with 21 additions and 11 deletions

View File

@@ -230,7 +230,7 @@ var ZabbixAPIDatasource = function () {
} else {
// Use history
if (_this2.enableDirectDBConnection) {
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo, options.intervalMs).then(function (history) {
return _this2.zabbix.dbConnector.handleHistory(history, items);
});
} else {

View File

@@ -55,16 +55,19 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
}
}, {
key: 'getHistory',
value: function getHistory(items, timeFrom, timeTill) {
value: function getHistory(items, timeFrom, timeTill, intervalMs) {
var _this = this;
var intervalSec = Math.ceil(intervalMs / 1000);
var aggFunction = 'AVG';
// Group items by value type and perform request for each value type
var grouped_items = _lodash2.default.groupBy(items, 'value_type');
var promises = _lodash2.default.map(grouped_items, function (items, value_type) {
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, value\n FROM ' + table + '\n WHERE itemid IN (' + itemids + ')\n AND clock > ' + timeFrom + ' AND clock < ' + timeTill + '\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 ';
return _this.invokeSQLQuery(query);
});