mysql-connector: imlement getTrends()
This commit is contained in:
29
dist/datasource-zabbix/datasource.js
vendored
29
dist/datasource-zabbix/datasource.js
vendored
@@ -355,19 +355,24 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
|
|||||||
options.consolidateBy = getConsolidateBy(target);
|
options.consolidateBy = getConsolidateBy(target);
|
||||||
|
|
||||||
if (useTrends) {
|
if (useTrends) {
|
||||||
var valueType = _this2.getTrendValueType(target);
|
if (_this2.enableDirectDBConnection) {
|
||||||
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
|
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo, options).then(function (history) {
|
||||||
return responseHandler.handleTrends(history, items, valueType);
|
return _this2.zabbix.dbConnector.handleGrafanaTSResponse(history, items);
|
||||||
}).then(function (timeseries) {
|
|
||||||
// Sort trend data, issue #202
|
|
||||||
_.forEach(timeseries, function (series) {
|
|
||||||
series.datapoints = _.sortBy(series.datapoints, function (point) {
|
|
||||||
return point[c.DATAPOINT_TS];
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
return timeseries;
|
var valueType = _this2.getTrendValueType(target);
|
||||||
});
|
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
|
||||||
|
return responseHandler.handleTrends(history, items, valueType);
|
||||||
|
}).then(function (timeseries) {
|
||||||
|
// Sort trend data, issue #202
|
||||||
|
_.forEach(timeseries, function (series) {
|
||||||
|
series.datapoints = _.sortBy(series.datapoints, function (point) {
|
||||||
|
return point[c.DATAPOINT_TS];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return timeseries;
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Use history
|
// Use history
|
||||||
if (_this2.enableDirectDBConnection) {
|
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) {
|
if (enableDirectDBConnection) {
|
||||||
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
|
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
|
||||||
this.getHistory = this.dbConnector.getHistory.bind(this.dbConnector);
|
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) {
|
System.register(['angular', 'lodash'], function (_export, _context) {
|
||||||
"use strict";
|
"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) {
|
function _classCallCheck(instance, Constructor) {
|
||||||
if (!(instance instanceof 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',
|
key: 'handleGrafanaTSResponse',
|
||||||
value: function handleGrafanaTSResponse(history, items) {
|
value: function handleGrafanaTSResponse(history, items) {
|
||||||
var addHostName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
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',
|
'3': 'history_uint',
|
||||||
'4': 'history_text'
|
'4': 'history_text'
|
||||||
};
|
};
|
||||||
|
TREND_TO_TABLE_MAP = {
|
||||||
|
'0': 'trends',
|
||||||
|
'3': 'trends_uint'
|
||||||
|
};
|
||||||
consolidateByFunc = {
|
consolidateByFunc = {
|
||||||
'avg': 'AVG',
|
'avg': 'AVG',
|
||||||
'min': 'MIN',
|
'min': 'MIN',
|
||||||
@@ -177,6 +212,11 @@ System.register(['angular', 'lodash'], function (_export, _context) {
|
|||||||
'sum': 'SUM',
|
'sum': 'SUM',
|
||||||
'count': 'COUNT'
|
'count': 'COUNT'
|
||||||
};
|
};
|
||||||
|
consolidateByTrendColumns = {
|
||||||
|
'avg': 'value_avg',
|
||||||
|
'min': 'value_min',
|
||||||
|
'max': 'value_max'
|
||||||
|
};
|
||||||
angular.module('grafana.services').factory('ZabbixDBConnector', ZabbixDBConnectorFactory);
|
angular.module('grafana.services').factory('ZabbixDBConnector', ZabbixDBConnectorFactory);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
29
dist/test/datasource-zabbix/datasource.js
vendored
29
dist/test/datasource-zabbix/datasource.js
vendored
@@ -215,19 +215,24 @@ var ZabbixAPIDatasource = function () {
|
|||||||
options.consolidateBy = getConsolidateBy(target);
|
options.consolidateBy = getConsolidateBy(target);
|
||||||
|
|
||||||
if (useTrends) {
|
if (useTrends) {
|
||||||
var valueType = _this2.getTrendValueType(target);
|
if (_this2.enableDirectDBConnection) {
|
||||||
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
|
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo, options).then(function (history) {
|
||||||
return _responseHandler2.default.handleTrends(history, items, valueType);
|
return _this2.zabbix.dbConnector.handleGrafanaTSResponse(history, items);
|
||||||
}).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];
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
return timeseries;
|
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 {
|
} else {
|
||||||
// Use history
|
// Use history
|
||||||
if (_this2.enableDirectDBConnection) {
|
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) {
|
if (enableDirectDBConnection) {
|
||||||
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
|
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
|
||||||
this.getHistory = this.dbConnector.getHistory.bind(this.dbConnector);
|
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'
|
'4': 'history_text'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var TREND_TO_TABLE_MAP = {
|
||||||
|
'0': 'trends',
|
||||||
|
'3': 'trends_uint'
|
||||||
|
};
|
||||||
|
|
||||||
var consolidateByFunc = {
|
var consolidateByFunc = {
|
||||||
'avg': 'AVG',
|
'avg': 'AVG',
|
||||||
'min': 'MIN',
|
'min': 'MIN',
|
||||||
@@ -31,6 +36,12 @@ var consolidateByFunc = {
|
|||||||
'count': 'COUNT'
|
'count': 'COUNT'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var consolidateByTrendColumns = {
|
||||||
|
'avg': 'value_avg',
|
||||||
|
'min': 'value_min',
|
||||||
|
'max': 'value_max'
|
||||||
|
};
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
|
function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
|
||||||
var ZabbixDBConnector = function () {
|
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',
|
key: 'handleGrafanaTSResponse',
|
||||||
value: function handleGrafanaTSResponse(history, items) {
|
value: function handleGrafanaTSResponse(history, items) {
|
||||||
var addHostName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
var addHostName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
||||||
|
|||||||
@@ -158,19 +158,21 @@ class ZabbixAPIDatasource {
|
|||||||
options.consolidateBy = getConsolidateBy(target);
|
options.consolidateBy = getConsolidateBy(target);
|
||||||
|
|
||||||
if (useTrends) {
|
if (useTrends) {
|
||||||
let valueType = this.getTrendValueType(target);
|
if (this.enableDirectDBConnection) {
|
||||||
getHistoryPromise = this.zabbix.getTrend(items, timeFrom, timeTo)
|
getHistoryPromise = this.zabbix.getTrend(items, timeFrom, timeTo, options)
|
||||||
.then(history => {
|
.then(history => this.zabbix.dbConnector.handleGrafanaTSResponse(history, items));
|
||||||
return responseHandler.handleTrends(history, items, valueType);
|
} else {
|
||||||
})
|
let valueType = this.getTrendValueType(target);
|
||||||
.then(timeseries => {
|
getHistoryPromise = this.zabbix.getTrend(items, timeFrom, timeTo)
|
||||||
// Sort trend data, issue #202
|
.then(history => responseHandler.handleTrends(history, items, valueType))
|
||||||
_.forEach(timeseries, series => {
|
.then(timeseries => {
|
||||||
series.datapoints = _.sortBy(series.datapoints, point => point[c.DATAPOINT_TS]);
|
// Sort trend data, issue #202
|
||||||
|
_.forEach(timeseries, series => {
|
||||||
|
series.datapoints = _.sortBy(series.datapoints, point => point[c.DATAPOINT_TS]);
|
||||||
|
});
|
||||||
|
return timeseries;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
return timeseries;
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// Use history
|
// Use history
|
||||||
if (this.enableDirectDBConnection) {
|
if (this.enableDirectDBConnection) {
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ function ZabbixFactory(zabbixAPIService, ZabbixCachingProxy, ZabbixDBConnector)
|
|||||||
if (enableDirectDBConnection) {
|
if (enableDirectDBConnection) {
|
||||||
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
|
this.dbConnector = new ZabbixDBConnector(sqlDatasourceId);
|
||||||
this.getHistory = this.dbConnector.getHistory.bind(this.dbConnector);
|
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'
|
'4': 'history_text'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const TREND_TO_TABLE_MAP = {
|
||||||
|
'0': 'trends',
|
||||||
|
'3': 'trends_uint'
|
||||||
|
};
|
||||||
|
|
||||||
const consolidateByFunc = {
|
const consolidateByFunc = {
|
||||||
'avg': 'AVG',
|
'avg': 'AVG',
|
||||||
'min': 'MIN',
|
'min': 'MIN',
|
||||||
@@ -18,6 +23,12 @@ const consolidateByFunc = {
|
|||||||
'count': 'COUNT'
|
'count': 'COUNT'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const consolidateByTrendColumns = {
|
||||||
|
'avg': 'value_avg',
|
||||||
|
'min': 'value_min',
|
||||||
|
'max': 'value_max'
|
||||||
|
};
|
||||||
|
|
||||||
/** @ngInject */
|
/** @ngInject */
|
||||||
function ZabbixDBConnectorFactory(datasourceSrv, backendSrv) {
|
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) {
|
handleGrafanaTSResponse(history, items, addHostName = true) {
|
||||||
return convertGrafanaTSResponse(history, items, addHostName);
|
return convertGrafanaTSResponse(history, items, addHostName);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user