fix regex-based text filter

This commit is contained in:
Alexander Zobnin
2018-01-27 13:26:14 +03:00
parent 7a517be6e1
commit 48a003a930
8 changed files with 26 additions and 14 deletions

View File

@@ -291,7 +291,7 @@ class ZabbixAPIDatasource {
return this.zabbix.getHistory(items, timeFrom, timeTo)
.then(history => {
if (target.resultFormat === 'table') {
return responseHandler.handleHistoryAsTable(history, items);
return responseHandler.handleHistoryAsTable(history, items, target);
} else {
return responseHandler.handleText(history, items, target);
}

View File

@@ -54,7 +54,7 @@ function handleText(history, items, target, addHostName = true) {
return convertHistory(history, items, addHostName, convertTextCallback);
}
function handleHistoryAsTable(history, items) {
function handleHistoryAsTable(history, items, target) {
let table = new TableModel();
table.addColumn({text: 'Host'});
table.addColumn({text: 'Item'});
@@ -66,6 +66,12 @@ function handleHistoryAsTable(history, items) {
let itemHistory = grouped_history[item.itemid] || [];
let lastPoint = _.last(itemHistory);
let lastValue = lastPoint ? lastPoint.value : null;
// Regex-based extractor
if (target.textFilter) {
lastValue = extractText(lastValue, target.textFilter, target.useCaptureGroups);
}
let host = _.first(item.hosts);
host = host ? host.name : "";

View File

@@ -123,6 +123,8 @@ describe('ZabbixDatasource', () => {
host: {filter: "Zabbix server"},
application: {filter: ""},
item: {filter: "System information"},
textFilter: "",
useCaptureGroups: true,
mode: 2,
resultFormat: "table"
}
@@ -145,13 +147,11 @@ describe('ZabbixDatasource', () => {
});
});
it('should extract value if regex is specified', (done) => {
it('should extract value if regex with capture group is used', (done) => {
ctx.options.targets[0].textFilter = "Linux (.*)";
ctx.ds.query(ctx.options).then(result => {
let tableData = result.data[0];
expect(tableData.rows).toEqual([
['Zabbix server', 'System information', 'system.uname', 'last']
]);
expect(tableData.rows[0][3]).toEqual('last');
done();
});
});