mysql-connector: imlement getTrends()

This commit is contained in:
Alexander Zobnin
2017-07-22 21:28:27 +03:00
parent 0da9aefccc
commit 75f041f841
12 changed files with 180 additions and 40 deletions

View File

@@ -215,19 +215,24 @@ var ZabbixAPIDatasource = function () {
options.consolidateBy = getConsolidateBy(target);
if (useTrends) {
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleTrends(history, items, valueType);
}).then(function (timeseries) {
// Sort trend data, issue #202
_lodash2.default.forEach(timeseries, function (series) {
series.datapoints = _lodash2.default.sortBy(series.datapoints, function (point) {
return point[c.DATAPOINT_TS];
});
if (_this2.enableDirectDBConnection) {
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo, options).then(function (history) {
return _this2.zabbix.dbConnector.handleGrafanaTSResponse(history, items);
});
return timeseries;
});
} else {
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleTrends(history, items, valueType);
}).then(function (timeseries) {
// Sort trend data, issue #202
_lodash2.default.forEach(timeseries, function (series) {
series.datapoints = _lodash2.default.sortBy(series.datapoints, function (point) {
return point[c.DATAPOINT_TS];
});
});
return timeseries;
});
}
} else {
// Use history
if (_this2.enableDirectDBConnection) {

View File

@@ -73,6 +73,7 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector)
if (enableDirectDBConnection) {
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
this.getHistory = this.dbConnector.getHistory.bind(this.dbConnector);
this.getTrend = this.dbConnector.getTrends.bind(this.dbConnector);
}
}

View File

@@ -23,6 +23,11 @@ var HISTORY_TO_TABLE_MAP = {
'4': 'history_text'
};
var TREND_TO_TABLE_MAP = {
'0': 'trends',
'3': 'trends_uint'
};
var consolidateByFunc = {
'avg': 'AVG',
'min': 'MIN',
@@ -31,6 +36,12 @@ var consolidateByFunc = {
'count': 'COUNT'
};
var consolidateByTrendColumns = {
'avg': 'value_avg',
'min': 'value_min',
'max': 'value_max'
};
/** @ngInject */
function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
var ZabbixDBConnector = function () {
@@ -91,6 +102,37 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
});
}
}, {
key: 'getTrends',
value: function getTrends(items, timeFrom, timeTill, options) {
var _this2 = this;
var intervalMs = options.intervalMs,
consolidateBy = options.consolidateBy;
var intervalSec = Math.ceil(intervalMs / 1000);
consolidateBy = consolidateBy || 'avg';
var aggFunction = consolidateByFunc[consolidateBy];
// 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 = TREND_TO_TABLE_MAP[value_type];
var valueColumn = _lodash2.default.includes(['avg', 'min', 'max'], consolidateBy) ? consolidateBy : 'avg';
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 ';
query = compactSQLQuery(query);
return _this2.invokeSQLQuery(query);
});
return Promise.all(promises).then(function (results) {
return _lodash2.default.flatten(results);
});
}
}, {
key: 'handleGrafanaTSResponse',
value: function handleGrafanaTSResponse(history, items) {
var addHostName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;