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`,
|
name: `CPU $2 time - $3`,
|
||||||
key: `system.cpu.util[,system,avg1]`,
|
key: `system.cpu.util[,system,avg1]`,
|
||||||
expected: "CPU system time - 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();
|
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) {
|
function splitKeyParams(paramStr) {
|
||||||
var params = [];
|
var params = [];
|
||||||
var quoted = false;
|
var quoted = false;
|
||||||
|
var in_array = false;
|
||||||
var split_symbol = ',';
|
var split_symbol = ',';
|
||||||
var param = '';
|
var param = '';
|
||||||
|
|
||||||
_.forEach(paramStr, function (symbol) {
|
_.forEach(paramStr, function (symbol) {
|
||||||
if (symbol === '"' && !quoted) {
|
if (symbol === '"' && in_array) {
|
||||||
quoted = true;
|
param += symbol;
|
||||||
} else if (symbol === '"' && quoted) {
|
} else if (symbol === '"' && quoted) {
|
||||||
quoted = false;
|
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);
|
params.push(param);
|
||||||
param = '';
|
param = '';
|
||||||
} else {
|
} 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',
|
name: 'CPU $2 time - $3',
|
||||||
key: 'system.cpu.util[,system,avg1]',
|
key: 'system.cpu.util[,system,avg1]',
|
||||||
expected: "CPU system time - 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) {
|
_lodash2.default.each(test_cases, function (test_case) {
|
||||||
@@ -59,5 +63,27 @@ describe('Utils', function () {
|
|||||||
});
|
});
|
||||||
done();
|
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) {
|
function splitKeyParams(paramStr) {
|
||||||
var params = [];
|
var params = [];
|
||||||
var quoted = false;
|
var quoted = false;
|
||||||
|
var in_array = false;
|
||||||
var split_symbol = ',';
|
var split_symbol = ',';
|
||||||
var param = '';
|
var param = '';
|
||||||
|
|
||||||
_lodash2.default.forEach(paramStr, function (symbol) {
|
_lodash2.default.forEach(paramStr, function (symbol) {
|
||||||
if (symbol === '"' && !quoted) {
|
if (symbol === '"' && in_array) {
|
||||||
quoted = true;
|
param += symbol;
|
||||||
} else if (symbol === '"' && quoted) {
|
} else if (symbol === '"' && quoted) {
|
||||||
quoted = false;
|
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);
|
params.push(param);
|
||||||
param = '';
|
param = '';
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ describe('Utils', () => {
|
|||||||
name: `CPU $2 time - $3`,
|
name: `CPU $2 time - $3`,
|
||||||
key: `system.cpu.util[,system,avg1]`,
|
key: `system.cpu.util[,system,avg1]`,
|
||||||
expected: "CPU system time - 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();
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -25,15 +25,22 @@ export function expandItemName(name, key) {
|
|||||||
function splitKeyParams(paramStr) {
|
function splitKeyParams(paramStr) {
|
||||||
let params = [];
|
let params = [];
|
||||||
let quoted = false;
|
let quoted = false;
|
||||||
|
let in_array = false;
|
||||||
let split_symbol = ',';
|
let split_symbol = ',';
|
||||||
let param = '';
|
let param = '';
|
||||||
|
|
||||||
_.forEach(paramStr, symbol => {
|
_.forEach(paramStr, symbol => {
|
||||||
if (symbol === '"' && !quoted) {
|
if (symbol === '"' && in_array) {
|
||||||
quoted = true;
|
param += symbol;
|
||||||
} else if (symbol === '"' && quoted) {
|
} else if (symbol === '"' && quoted) {
|
||||||
quoted = false;
|
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);
|
params.push(param);
|
||||||
param = '';
|
param = '';
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user