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

@@ -173,7 +173,7 @@ class ZabbixAPIDatasource {
} else {
// Use history
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));
} else {
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
let grouped_items = _.groupBy(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 query = `
SELECT itemid AS metric, clock AS time_sec, value
SELECT itemid AS metric, clock AS time_sec, ${aggFunction}(value) as value
FROM ${table}
WHERE itemid IN (${itemids})
AND clock > ${timeFrom} AND clock < ${timeTill}
GROUP BY time_sec DIV ${intervalSec}, metric
`;
return this.invokeSQLQuery(query);