implement replaceAlias() function, #287

This commit is contained in:
Alexander Zobnin
2017-04-17 13:25:02 +03:00
parent 0e318e719a
commit df1c3e7ef4
12 changed files with 139 additions and 55 deletions

View File

@@ -150,6 +150,17 @@ System.register(['lodash', './utils'], function (_export, _context) {
}function setAlias(alias, timeseries) {
timeseries.target = alias;
return timeseries;
}function replaceAlias(regexp, newAlias, timeseries) {
var pattern = void 0;
if (utils.isRegex(regexp)) {
pattern = utils.buildRegex(regexp);
} else {
pattern = regexp;
}
var alias = timeseries.target.replace(pattern, newAlias);
timeseries.target = alias;
return timeseries;
}function setAliasByRegex(alias, timeseries) {
timeseries.target = extractText(timeseries.target, alias);
return timeseries;
@@ -265,7 +276,8 @@ System.register(['lodash', './utils'], function (_export, _context) {
bottom: _.partial(limit, 'bottom'),
timeShift: timeShift,
setAlias: setAlias,
setAliasByRegex: setAliasByRegex
setAliasByRegex: setAliasByRegex,
replaceAlias: replaceAlias
};
aggregationFunctions = {
avg: AVERAGE,

File diff suppressed because one or more lines are too long

View File

@@ -334,12 +334,10 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
var getHistoryPromise = void 0;
if (useTrends) {
(function () {
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return responseHandler.handleTrends(history, items, valueType);
});
})();
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return responseHandler.handleTrends(history, items, valueType);
});
} else {
// Use history
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
@@ -386,20 +384,18 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
// Apply aggregations
if (aggregationFunctions.length) {
(function () {
var dp = _.map(timeseries_data, 'datapoints');
dp = sequence(aggregationFunctions)(dp);
var dp = _.map(timeseries_data, 'datapoints');
dp = sequence(aggregationFunctions)(dp);
var aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
var lastAgg = _.findLast(target.functions, function (func) {
return _.includes(aggFuncNames, func.def.name);
});
var aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
var lastAgg = _.findLast(target.functions, function (func) {
return _.includes(aggFuncNames, func.def.name);
});
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}];
})();
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}];
}
// Apply alias functions
@@ -419,12 +415,10 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
return func.def.name === 'timeShift';
});
if (timeShiftFunc) {
(function () {
var shift = timeShiftFunc.params[0];
_.forEach(timeseries_data, function (series) {
series.datapoints = dataProcessor.unShiftTimeSeries(shift, series.datapoints);
});
})();
var shift = timeShiftFunc.params[0];
_.forEach(timeseries_data, function (series) {
series.datapoints = dataProcessor.unShiftTimeSeries(shift, series.datapoints);
});
}
}
}, {

File diff suppressed because one or more lines are too long

View File

@@ -211,6 +211,13 @@ System.register(['lodash', 'jquery'], function (_export, _context) {
defaultParams: []
});
addFuncDef({
name: 'replaceAlias',
category: 'Alias',
params: [{ name: 'regexp', type: 'string' }, { name: 'newAlias', type: 'string' }],
defaultParams: ['/(.*)/', '$1']
});
_.each(categories, function (funcList, catName) {
categories[catName] = _.sortBy(funcList, 'name');
});

File diff suppressed because one or more lines are too long

View File

@@ -181,6 +181,19 @@ function setAlias(alias, timeseries) {
return timeseries;
}
function replaceAlias(regexp, newAlias, timeseries) {
var pattern = void 0;
if (utils.isRegex(regexp)) {
pattern = utils.buildRegex(regexp);
} else {
pattern = regexp;
}
var alias = timeseries.target.replace(pattern, newAlias);
timeseries.target = alias;
return timeseries;
}
function setAliasByRegex(alias, timeseries) {
timeseries.target = extractText(timeseries.target, alias);
return timeseries;
@@ -315,7 +328,8 @@ var metricFunctions = {
bottom: _lodash2.default.partial(limit, 'bottom'),
timeShift: timeShift,
setAlias: setAlias,
setAliasByRegex: setAliasByRegex
setAliasByRegex: setAliasByRegex,
replaceAlias: replaceAlias
};
var aggregationFunctions = {

View File

@@ -192,12 +192,10 @@ var ZabbixAPIDatasource = function () {
var getHistoryPromise = void 0;
if (useTrends) {
(function () {
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleTrends(history, items, valueType);
});
})();
var valueType = _this2.getTrendValueType(target);
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
return _responseHandler2.default.handleTrends(history, items, valueType);
});
} else {
// Use history
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (history) {
@@ -244,20 +242,18 @@ var ZabbixAPIDatasource = function () {
// Apply aggregations
if (aggregationFunctions.length) {
(function () {
var dp = _lodash2.default.map(timeseries_data, 'datapoints');
dp = sequence(aggregationFunctions)(dp);
var dp = _lodash2.default.map(timeseries_data, 'datapoints');
dp = sequence(aggregationFunctions)(dp);
var aggFuncNames = _lodash2.default.map(metricFunctions.getCategories()['Aggregate'], 'name');
var lastAgg = _lodash2.default.findLast(target.functions, function (func) {
return _lodash2.default.includes(aggFuncNames, func.def.name);
});
var aggFuncNames = _lodash2.default.map(metricFunctions.getCategories()['Aggregate'], 'name');
var lastAgg = _lodash2.default.findLast(target.functions, function (func) {
return _lodash2.default.includes(aggFuncNames, func.def.name);
});
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}];
})();
timeseries_data = [{
target: lastAgg.text,
datapoints: dp
}];
}
// Apply alias functions
@@ -277,12 +273,10 @@ var ZabbixAPIDatasource = function () {
return func.def.name === 'timeShift';
});
if (timeShiftFunc) {
(function () {
var shift = timeShiftFunc.params[0];
_lodash2.default.forEach(timeseries_data, function (series) {
series.datapoints = _dataProcessor2.default.unShiftTimeSeries(shift, series.datapoints);
});
})();
var shift = timeShiftFunc.params[0];
_lodash2.default.forEach(timeseries_data, function (series) {
series.datapoints = _dataProcessor2.default.unShiftTimeSeries(shift, series.datapoints);
});
}
}
}, {

View File

@@ -174,6 +174,13 @@ addFuncDef({
defaultParams: []
});
addFuncDef({
name: 'replaceAlias',
category: 'Alias',
params: [{ name: 'regexp', type: 'string' }, { name: 'newAlias', type: 'string' }],
defaultParams: ['/(.*)/', '$1']
});
_lodash2.default.each(categories, function (funcList, catName) {
categories[catName] = _lodash2.default.sortBy(funcList, 'name');
});

View File

@@ -158,3 +158,35 @@ Examples:
```
setAlias(Zabbix busy [a-zA-Z]+)
```
### replaceAlias
```
replaceAlias(pattern, newAlias)
```
Replace metric name using pattern. Pattern is regex or regular string. If regex is used, following special replacement patterns are supported:
|Pattern | Inserts|
----------|---------
|$$ | Inserts a "$". |
|$& | Inserts the matched substring. |
|$` | Inserts the portion of the string that precedes the matched substring. |
|$' | Inserts the portion of the string that follows the matched substring. |
|$n | Where n is a non-negative integer less than 100, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object. |
For more detais see [String.prototype.replace()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) function.
Examples:
```
CPU system time
replaceAlias(/CPU (.*) time/, $1) -> system
backend01: CPU system time
replaceAlias(/CPU (.*) time/, $1) -> backend01: system
backend01: CPU system time
replaceAlias(/.*CPU (.*) time/, $1) -> system
backend01: CPU system time
replaceAlias(/(.*): CPU (.*) time/, $1 - $2) -> backend01 - system
```

View File

@@ -168,6 +168,19 @@ function setAlias(alias, timeseries) {
return timeseries;
}
function replaceAlias(regexp, newAlias, timeseries) {
let pattern;
if (utils.isRegex(regexp)) {
pattern = utils.buildRegex(regexp);
} else {
pattern = regexp;
}
let alias = timeseries.target.replace(pattern, newAlias);
timeseries.target = alias;
return timeseries;
}
function setAliasByRegex(alias, timeseries) {
timeseries.target = extractText(timeseries.target, alias);
return timeseries;
@@ -308,7 +321,8 @@ let metricFunctions = {
bottom: _.partial(limit, 'bottom'),
timeShift: timeShift,
setAlias: setAlias,
setAliasByRegex: setAliasByRegex
setAliasByRegex: setAliasByRegex,
replaceAlias: replaceAlias
};
let aggregationFunctions = {

View File

@@ -187,6 +187,16 @@ addFuncDef({
defaultParams: []
});
addFuncDef({
name: 'replaceAlias',
category: 'Alias',
params: [
{ name: 'regexp', type: 'string' },
{ name: 'newAlias', type: 'string' }
],
defaultParams: ['/(.*)/', '$1']
});
_.each(categories, function(funcList, catName) {
categories[catName] = _.sortBy(funcList, 'name');
});