mysql-connector: imlement getTrends()
This commit is contained in:
7
dist/datasource-zabbix/datasource.js
vendored
7
dist/datasource-zabbix/datasource.js
vendored
@@ -355,6 +355,11 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
|
||||
options.consolidateBy = getConsolidateBy(target);
|
||||
|
||||
if (useTrends) {
|
||||
if (_this2.enableDirectDBConnection) {
|
||||
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo, options).then(function (history) {
|
||||
return _this2.zabbix.dbConnector.handleGrafanaTSResponse(history, items);
|
||||
});
|
||||
} else {
|
||||
var valueType = _this2.getTrendValueType(target);
|
||||
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
|
||||
return responseHandler.handleTrends(history, items, valueType);
|
||||
@@ -365,9 +370,9 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
|
||||
return point[c.DATAPOINT_TS];
|
||||
});
|
||||
});
|
||||
|
||||
return timeseries;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Use history
|
||||
if (_this2.enableDirectDBConnection) {
|
||||
|
||||
2
dist/datasource-zabbix/datasource.js.map
vendored
2
dist/datasource-zabbix/datasource.js.map
vendored
File diff suppressed because one or more lines are too long
1
dist/datasource-zabbix/zabbix.js
vendored
1
dist/datasource-zabbix/zabbix.js
vendored
@@ -68,6 +68,7 @@ System.register(['angular', 'lodash', './utils', './zabbixAPI.service.js', './za
|
||||
if (enableDirectDBConnection) {
|
||||
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
|
||||
this.getHistory = this.dbConnector.getHistory.bind(this.dbConnector);
|
||||
this.getTrend = this.dbConnector.getTrends.bind(this.dbConnector);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
2
dist/datasource-zabbix/zabbix.js.map
vendored
2
dist/datasource-zabbix/zabbix.js.map
vendored
File diff suppressed because one or more lines are too long
42
dist/datasource-zabbix/zabbixDBConnector.js
vendored
42
dist/datasource-zabbix/zabbixDBConnector.js
vendored
@@ -3,7 +3,7 @@
|
||||
System.register(['angular', 'lodash'], function (_export, _context) {
|
||||
"use strict";
|
||||
|
||||
var angular, _, _createClass, DEFAULT_QUERY_LIMIT, HISTORY_TO_TABLE_MAP, consolidateByFunc;
|
||||
var angular, _, _createClass, DEFAULT_QUERY_LIMIT, HISTORY_TO_TABLE_MAP, TREND_TO_TABLE_MAP, consolidateByFunc, consolidateByTrendColumns;
|
||||
|
||||
function _classCallCheck(instance, Constructor) {
|
||||
if (!(instance instanceof Constructor)) {
|
||||
@@ -71,6 +71,37 @@ System.register(['angular', 'lodash'], function (_export, _context) {
|
||||
});
|
||||
}
|
||||
}, {
|
||||
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 = _.groupBy(items, 'value_type');
|
||||
var promises = _.map(grouped_items, function (items, value_type) {
|
||||
var itemids = _.map(items, 'itemid').join(', ');
|
||||
var table = TREND_TO_TABLE_MAP[value_type];
|
||||
var valueColumn = _.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 _.flatten(results);
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: 'handleGrafanaTSResponse',
|
||||
value: function handleGrafanaTSResponse(history, items) {
|
||||
var addHostName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
||||
@@ -170,6 +201,10 @@ System.register(['angular', 'lodash'], function (_export, _context) {
|
||||
'3': 'history_uint',
|
||||
'4': 'history_text'
|
||||
};
|
||||
TREND_TO_TABLE_MAP = {
|
||||
'0': 'trends',
|
||||
'3': 'trends_uint'
|
||||
};
|
||||
consolidateByFunc = {
|
||||
'avg': 'AVG',
|
||||
'min': 'MIN',
|
||||
@@ -177,6 +212,11 @@ System.register(['angular', 'lodash'], function (_export, _context) {
|
||||
'sum': 'SUM',
|
||||
'count': 'COUNT'
|
||||
};
|
||||
consolidateByTrendColumns = {
|
||||
'avg': 'value_avg',
|
||||
'min': 'value_min',
|
||||
'max': 'value_max'
|
||||
};
|
||||
angular.module('grafana.services').factory('ZabbixDBConnector', ZabbixDBConnectorFactory);
|
||||
}
|
||||
};
|
||||
|
||||
File diff suppressed because one or more lines are too long
7
dist/test/datasource-zabbix/datasource.js
vendored
7
dist/test/datasource-zabbix/datasource.js
vendored
@@ -215,6 +215,11 @@ var ZabbixAPIDatasource = function () {
|
||||
options.consolidateBy = getConsolidateBy(target);
|
||||
|
||||
if (useTrends) {
|
||||
if (_this2.enableDirectDBConnection) {
|
||||
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo, options).then(function (history) {
|
||||
return _this2.zabbix.dbConnector.handleGrafanaTSResponse(history, items);
|
||||
});
|
||||
} else {
|
||||
var valueType = _this2.getTrendValueType(target);
|
||||
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
|
||||
return _responseHandler2.default.handleTrends(history, items, valueType);
|
||||
@@ -225,9 +230,9 @@ var ZabbixAPIDatasource = function () {
|
||||
return point[c.DATAPOINT_TS];
|
||||
});
|
||||
});
|
||||
|
||||
return timeseries;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Use history
|
||||
if (_this2.enableDirectDBConnection) {
|
||||
|
||||
1
dist/test/datasource-zabbix/zabbix.js
vendored
1
dist/test/datasource-zabbix/zabbix.js
vendored
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
42
dist/test/datasource-zabbix/zabbixDBConnector.js
vendored
42
dist/test/datasource-zabbix/zabbixDBConnector.js
vendored
@@ -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;
|
||||
|
||||
@@ -158,19 +158,21 @@ class ZabbixAPIDatasource {
|
||||
options.consolidateBy = getConsolidateBy(target);
|
||||
|
||||
if (useTrends) {
|
||||
if (this.enableDirectDBConnection) {
|
||||
getHistoryPromise = this.zabbix.getTrend(items, timeFrom, timeTo, options)
|
||||
.then(history => this.zabbix.dbConnector.handleGrafanaTSResponse(history, items));
|
||||
} else {
|
||||
let valueType = this.getTrendValueType(target);
|
||||
getHistoryPromise = this.zabbix.getTrend(items, timeFrom, timeTo)
|
||||
.then(history => {
|
||||
return responseHandler.handleTrends(history, items, valueType);
|
||||
})
|
||||
.then(history => responseHandler.handleTrends(history, items, valueType))
|
||||
.then(timeseries => {
|
||||
// Sort trend data, issue #202
|
||||
_.forEach(timeseries, series => {
|
||||
series.datapoints = _.sortBy(series.datapoints, point => point[c.DATAPOINT_TS]);
|
||||
});
|
||||
|
||||
return timeseries;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Use history
|
||||
if (this.enableDirectDBConnection) {
|
||||
|
||||
@@ -45,6 +45,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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,11 @@ const HISTORY_TO_TABLE_MAP = {
|
||||
'4': 'history_text'
|
||||
};
|
||||
|
||||
const TREND_TO_TABLE_MAP = {
|
||||
'0': 'trends',
|
||||
'3': 'trends_uint'
|
||||
};
|
||||
|
||||
const consolidateByFunc = {
|
||||
'avg': 'AVG',
|
||||
'min': 'MIN',
|
||||
@@ -18,6 +23,12 @@ const consolidateByFunc = {
|
||||
'count': 'COUNT'
|
||||
};
|
||||
|
||||
const consolidateByTrendColumns = {
|
||||
'avg': 'value_avg',
|
||||
'min': 'value_min',
|
||||
'max': 'value_max'
|
||||
};
|
||||
|
||||
/** @ngInject */
|
||||
function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
|
||||
|
||||
@@ -75,6 +86,38 @@ function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
|
||||
});
|
||||
}
|
||||
|
||||
getTrends(items, timeFrom, timeTill, options) {
|
||||
let {intervalMs, consolidateBy} = options;
|
||||
let intervalSec = Math.ceil(intervalMs / 1000);
|
||||
|
||||
consolidateBy = consolidateBy || 'avg';
|
||||
let aggFunction = consolidateByFunc[consolidateBy];
|
||||
|
||||
// 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) => {
|
||||
let itemids = _.map(items, 'itemid').join(', ');
|
||||
let table = TREND_TO_TABLE_MAP[value_type];
|
||||
let valueColumn = _.includes(['avg', 'min', 'max'], consolidateBy) ? consolidateBy : 'avg';
|
||||
valueColumn = consolidateByTrendColumns[valueColumn];
|
||||
|
||||
let query = `
|
||||
SELECT itemid AS metric, clock AS time_sec, ${aggFunction}(${valueColumn}) as value
|
||||
FROM ${table}
|
||||
WHERE itemid IN (${itemids})
|
||||
AND clock > ${timeFrom} AND clock < ${timeTill}
|
||||
GROUP BY time_sec DIV ${intervalSec}, metric
|
||||
`;
|
||||
|
||||
query = compactSQLQuery(query);
|
||||
return this.invokeSQLQuery(query);
|
||||
});
|
||||
|
||||
return Promise.all(promises).then(results => {
|
||||
return _.flatten(results);
|
||||
});
|
||||
}
|
||||
|
||||
handleGrafanaTSResponse(history, items, addHostName = true) {
|
||||
return convertGrafanaTSResponse(history, items, addHostName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user