fix regex-based text filter
This commit is contained in:
2
dist/datasource-zabbix/datasource.js
vendored
2
dist/datasource-zabbix/datasource.js
vendored
@@ -503,7 +503,7 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
|
||||
if (items.length) {
|
||||
return _this4.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
|
||||
if (target.resultFormat === 'table') {
|
||||
return responseHandler.handleHistoryAsTable(history, items);
|
||||
return responseHandler.handleHistoryAsTable(history, items, target);
|
||||
} else {
|
||||
return responseHandler.handleText(history, items, target);
|
||||
}
|
||||
|
||||
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
8
dist/datasource-zabbix/responseHandler.js
vendored
8
dist/datasource-zabbix/responseHandler.js
vendored
@@ -72,7 +72,7 @@ System.register(['lodash', 'app/core/table_model', './constants'], function (_ex
|
||||
|
||||
var convertTextCallback = _.partial(convertText, target);
|
||||
return convertHistory(history, items, addHostName, convertTextCallback);
|
||||
}function handleHistoryAsTable(history, items) {
|
||||
}function handleHistoryAsTable(history, items, target) {
|
||||
var table = new TableModel();
|
||||
table.addColumn({ text: 'Host' });
|
||||
table.addColumn({ text: 'Item' });
|
||||
@@ -84,6 +84,12 @@ System.register(['lodash', 'app/core/table_model', './constants'], function (_ex
|
||||
var itemHistory = grouped_history[item.itemid] || [];
|
||||
var lastPoint = _.last(itemHistory);
|
||||
var lastValue = lastPoint ? lastPoint.value : null;
|
||||
|
||||
// Regex-based extractor
|
||||
if (target.textFilter) {
|
||||
lastValue = extractText(lastValue, target.textFilter, target.useCaptureGroups);
|
||||
}
|
||||
|
||||
var host = _.first(item.hosts);
|
||||
host = host ? host.name : "";
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 : "";
|
||||
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user