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

@@ -43,16 +43,19 @@ System.register(['angular', 'lodash'], function (_export, _context) {
}
}, {
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 = _.groupBy(items, 'value_type');
var promises = _.map(grouped_items, function (items, value_type) {
var itemids = _.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);
});