Fix potential bug in key array params expanding
This commit is contained in:
31
dist/datasource-zabbix/specs/utils_specs.js
vendored
31
dist/datasource-zabbix/specs/utils_specs.js
vendored
@@ -16,6 +16,11 @@ describe('Utils', () => {
|
||||
name: `CPU $2 time - $3`,
|
||||
key: `system.cpu.util[,system,avg1]`,
|
||||
expected: "CPU system time - avg1"
|
||||
},
|
||||
{
|
||||
name: `CPU - $1 - $2 - $3`,
|
||||
key: `system.cpu.util[,system,avg1]`,
|
||||
expected: "CPU - - system - avg1"
|
||||
}
|
||||
];
|
||||
|
||||
@@ -56,5 +61,31 @@ describe('Utils', () => {
|
||||
});
|
||||
done();
|
||||
});
|
||||
|
||||
it('should properly expand array params', (done) => {
|
||||
let test_cases = [
|
||||
{
|
||||
name: `CPU $2 - $3 time`,
|
||||
key: `system.cpu.util[,[user,system],avg1]`,
|
||||
expected: "CPU user,system - avg1 time"
|
||||
},
|
||||
{
|
||||
name: `CPU $2 - $3 time`,
|
||||
key: `system.cpu.util[,["user,system",iowait],avg1]`,
|
||||
expected: `CPU "user,system",iowait - avg1 time`
|
||||
},
|
||||
{
|
||||
name: `CPU - $2 - $3 - $4`,
|
||||
key: `system.cpu.util[,[],["user,system",iowait],avg1]`,
|
||||
expected: `CPU - - "user,system",iowait - avg1`
|
||||
}
|
||||
];
|
||||
|
||||
_.each(test_cases, test_case => {
|
||||
let expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
13
dist/datasource-zabbix/utils.js
vendored
13
dist/datasource-zabbix/utils.js
vendored
@@ -31,15 +31,22 @@ System.register(['lodash', 'moment'], function (_export, _context) {
|
||||
function splitKeyParams(paramStr) {
|
||||
var params = [];
|
||||
var quoted = false;
|
||||
var in_array = false;
|
||||
var split_symbol = ',';
|
||||
var param = '';
|
||||
|
||||
_.forEach(paramStr, function (symbol) {
|
||||
if (symbol === '"' && !quoted) {
|
||||
quoted = true;
|
||||
if (symbol === '"' && in_array) {
|
||||
param += symbol;
|
||||
} else if (symbol === '"' && quoted) {
|
||||
quoted = false;
|
||||
} else if (symbol === split_symbol && !quoted) {
|
||||
} else if (symbol === '"' && !quoted) {
|
||||
quoted = true;
|
||||
} else if (symbol === '[' && !quoted) {
|
||||
in_array = true;
|
||||
} else if (symbol === ']' && !quoted) {
|
||||
in_array = false;
|
||||
} else if (symbol === split_symbol && !quoted && !in_array) {
|
||||
params.push(param);
|
||||
param = '';
|
||||
} else {
|
||||
|
||||
2
dist/datasource-zabbix/utils.js.map
vendored
2
dist/datasource-zabbix/utils.js.map
vendored
File diff suppressed because one or more lines are too long
26
dist/test/datasource-zabbix/specs/utils_specs.js
vendored
26
dist/test/datasource-zabbix/specs/utils_specs.js
vendored
@@ -25,6 +25,10 @@ describe('Utils', function () {
|
||||
name: 'CPU $2 time - $3',
|
||||
key: 'system.cpu.util[,system,avg1]',
|
||||
expected: "CPU system time - avg1"
|
||||
}, {
|
||||
name: 'CPU - $1 - $2 - $3',
|
||||
key: 'system.cpu.util[,system,avg1]',
|
||||
expected: "CPU - - system - avg1"
|
||||
}];
|
||||
|
||||
_lodash2.default.each(test_cases, function (test_case) {
|
||||
@@ -59,5 +63,27 @@ describe('Utils', function () {
|
||||
});
|
||||
done();
|
||||
});
|
||||
|
||||
it('should properly expand array params', function (done) {
|
||||
var test_cases = [{
|
||||
name: 'CPU $2 - $3 time',
|
||||
key: 'system.cpu.util[,[user,system],avg1]',
|
||||
expected: "CPU user,system - avg1 time"
|
||||
}, {
|
||||
name: 'CPU $2 - $3 time',
|
||||
key: 'system.cpu.util[,["user,system",iowait],avg1]',
|
||||
expected: 'CPU "user,system",iowait - avg1 time'
|
||||
}, {
|
||||
name: 'CPU - $2 - $3 - $4',
|
||||
key: 'system.cpu.util[,[],["user,system",iowait],avg1]',
|
||||
expected: 'CPU - - "user,system",iowait - avg1'
|
||||
}];
|
||||
|
||||
_lodash2.default.each(test_cases, function (test_case) {
|
||||
var expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
13
dist/test/datasource-zabbix/utils.js
vendored
13
dist/test/datasource-zabbix/utils.js
vendored
@@ -49,15 +49,22 @@ function expandItemName(name, key) {
|
||||
function splitKeyParams(paramStr) {
|
||||
var params = [];
|
||||
var quoted = false;
|
||||
var in_array = false;
|
||||
var split_symbol = ',';
|
||||
var param = '';
|
||||
|
||||
_lodash2.default.forEach(paramStr, function (symbol) {
|
||||
if (symbol === '"' && !quoted) {
|
||||
quoted = true;
|
||||
if (symbol === '"' && in_array) {
|
||||
param += symbol;
|
||||
} else if (symbol === '"' && quoted) {
|
||||
quoted = false;
|
||||
} else if (symbol === split_symbol && !quoted) {
|
||||
} else if (symbol === '"' && !quoted) {
|
||||
quoted = true;
|
||||
} else if (symbol === '[' && !quoted) {
|
||||
in_array = true;
|
||||
} else if (symbol === ']' && !quoted) {
|
||||
in_array = false;
|
||||
} else if (symbol === split_symbol && !quoted && !in_array) {
|
||||
params.push(param);
|
||||
param = '';
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user