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

@@ -357,7 +357,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
} else { } else {
// Use history // Use history
if (_this2.enableDirectDBConnection) { 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); return _this2.zabbix.dbConnector.handleHistory(history, items);
}); });
} else { } else {

File diff suppressed because one or more lines are too long

View File

@@ -43,16 +43,19 @@ System.register(['angular', 'lodash'], function (_export, _context) {
} }
}, { }, {
key: 'getHistory', key: 'getHistory',
value: function getHistory(items, timeFrom, timeTill) { value: function getHistory(items, timeFrom, timeTill, intervalMs) {
var _this = this; var _this = this;
var intervalSec = Math.ceil(intervalMs / 1000);
var aggFunction = 'AVG';
// Group items by value type and perform request for each value type // Group items by value type and perform request for each value type
var grouped_items = _.groupBy(items, 'value_type'); var grouped_items = _.groupBy(items, 'value_type');
var promises = _.map(grouped_items, function (items, value_type) { var promises = _.map(grouped_items, function (items, value_type) {
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, 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); return _this.invokeSQLQuery(query);
}); });

File diff suppressed because one or more lines are too long

View File

@@ -230,7 +230,7 @@ var ZabbixAPIDatasource = function () {
} else { } else {
// Use history // Use history
if (_this2.enableDirectDBConnection) { 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); return _this2.zabbix.dbConnector.handleHistory(history, items);
}); });
} else { } else {

View File

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

View File

@@ -173,7 +173,7 @@ class ZabbixAPIDatasource {
} else { } else {
// Use history // Use history
if (this.enableDirectDBConnection) { if (this.enableDirectDBConnection) {
getHistoryPromise = this.zabbix.getHistory(items, timeFrom, timeTo) getHistoryPromise = this.zabbix.getHistory(items, timeFrom, timeTo, options.intervalMs)
.then(history => this.zabbix.dbConnector.handleHistory(history, items)); .then(history => this.zabbix.dbConnector.handleHistory(history, items));
} else { } else {
getHistoryPromise = this.zabbix.getHistory(items, timeFrom, timeTo) getHistoryPromise = this.zabbix.getHistory(items, timeFrom, timeTo)

View File

@@ -37,7 +37,10 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
} }
} }
getHistory(items, timeFrom, timeTill) { getHistory(items, timeFrom, timeTill, intervalMs) {
let intervalSec = Math.ceil(intervalMs / 1000);
let aggFunction = 'AVG';
// Group items by value type and perform request for each value type // Group items by value type and perform request for each value type
let grouped_items = _.groupBy(items, 'value_type'); let grouped_items = _.groupBy(items, 'value_type');
let promises = _.map(grouped_items, (items, value_type) => { let promises = _.map(grouped_items, (items, value_type) => {
@@ -45,10 +48,11 @@ 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, 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}
GROUP BY time_sec DIV ${intervalSec}, metric
`; `;
return this.invokeSQLQuery(query); return this.invokeSQLQuery(query);