Fix item name expanding when key contains commas in quoted params
This commit is contained in:
60
dist/datasource-zabbix/specs/utils_specs.js
vendored
Normal file
60
dist/datasource-zabbix/specs/utils_specs.js
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
import _ from 'lodash';
|
||||
import * as utils from '../utils';
|
||||
|
||||
describe('Utils', () => {
|
||||
|
||||
describe('expandItemName()', () => {
|
||||
|
||||
it('should properly expand unquoted params', (done) => {
|
||||
let test_cases = [
|
||||
{
|
||||
name: `CPU $2 time`,
|
||||
key: `system.cpu.util[,user,avg1]`,
|
||||
expected: "CPU user time"
|
||||
},
|
||||
{
|
||||
name: `CPU $2 time - $3`,
|
||||
key: `system.cpu.util[,system,avg1]`,
|
||||
expected: "CPU system time - avg1"
|
||||
}
|
||||
];
|
||||
|
||||
_.each(test_cases, test_case => {
|
||||
let expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
|
||||
it('should properly expand quoted params with commas', (done) => {
|
||||
let test_cases = [
|
||||
{
|
||||
name: `CPU $2 time`,
|
||||
key: `system.cpu.util["type=user,value=avg",user]`,
|
||||
expected: "CPU user time"
|
||||
},
|
||||
{
|
||||
name: `CPU $1 time`,
|
||||
key: `system.cpu.util["type=user,value=avg","user"]`,
|
||||
expected: "CPU type=user,value=avg time"
|
||||
},
|
||||
{
|
||||
name: `CPU $1 time $3`,
|
||||
key: `system.cpu.util["type=user,value=avg",,"user"]`,
|
||||
expected: "CPU type=user,value=avg time user"
|
||||
},
|
||||
{
|
||||
name: `CPU $1 $2 $3`,
|
||||
key: `system.cpu.util["type=user,value=avg",time,"user"]`,
|
||||
expected: "CPU type=user,value=avg time user"
|
||||
}
|
||||
];
|
||||
|
||||
_.each(test_cases, test_case => {
|
||||
let expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
29
dist/datasource-zabbix/utils.js
vendored
29
dist/datasource-zabbix/utils.js
vendored
@@ -16,7 +16,8 @@ System.register(['lodash', 'moment'], function (_export, _context) {
|
||||
|
||||
// extract params from key:
|
||||
// "system.cpu.util[,system,avg1]" --> ["", "system", "avg1"]
|
||||
var key_params = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']')).split(',');
|
||||
var key_params_str = key.substring(key.indexOf('[') + 1, key.lastIndexOf(']'));
|
||||
var key_params = splitKeyParams(key_params_str);
|
||||
|
||||
// replace item parameters
|
||||
for (var i = key_params.length; i >= 1; i--) {
|
||||
@@ -25,10 +26,32 @@ System.register(['lodash', 'moment'], function (_export, _context) {
|
||||
return name;
|
||||
}
|
||||
|
||||
// Pattern for testing regex
|
||||
|
||||
_export('expandItemName', expandItemName);
|
||||
|
||||
function splitKeyParams(paramStr) {
|
||||
var params = [];
|
||||
var quoted = false;
|
||||
var split_symbol = ',';
|
||||
var param = '';
|
||||
|
||||
_.forEach(paramStr, function (symbol) {
|
||||
if (symbol === '"' && !quoted) {
|
||||
quoted = true;
|
||||
} else if (symbol === '"' && quoted) {
|
||||
quoted = false;
|
||||
} else if (symbol === split_symbol && !quoted) {
|
||||
params.push(param);
|
||||
param = '';
|
||||
} else {
|
||||
param += symbol;
|
||||
}
|
||||
});
|
||||
|
||||
params.push(param);
|
||||
return params;
|
||||
}
|
||||
|
||||
// Pattern for testing regex
|
||||
function isRegex(str) {
|
||||
return regexPattern.test(str);
|
||||
}
|
||||
|
||||
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
Reference in New Issue
Block a user