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) { }function setAlias(alias, timeseries) {
timeseries.target = alias; timeseries.target = alias;
return 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) { }function setAliasByRegex(alias, timeseries) {
timeseries.target = extractText(timeseries.target, alias); timeseries.target = extractText(timeseries.target, alias);
return timeseries; return timeseries;
@@ -265,7 +276,8 @@ System.register(['lodash', './utils'], function (_export, _context) {
bottom: _.partial(limit, 'bottom'), bottom: _.partial(limit, 'bottom'),
timeShift: timeShift, timeShift: timeShift,
setAlias: setAlias, setAlias: setAlias,
setAliasByRegex: setAliasByRegex setAliasByRegex: setAliasByRegex,
replaceAlias: replaceAlias
}; };
aggregationFunctions = { aggregationFunctions = {
avg: AVERAGE, 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; var getHistoryPromise = void 0;
if (useTrends) { if (useTrends) {
(function () { var valueType = _this2.getTrendValueType(target);
var valueType = _this2.getTrendValueType(target); getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) {
getHistoryPromise = _this2.zabbix.getTrend(items, timeFrom, timeTo).then(function (history) { return responseHandler.handleTrends(history, items, valueType);
return responseHandler.handleTrends(history, items, valueType); });
});
})();
} else { } else {
// Use history // Use history
getHistoryPromise = _this2.zabbix.getHistory(items, timeFrom, timeTo).then(function (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 // Apply aggregations
if (aggregationFunctions.length) { if (aggregationFunctions.length) {
(function () { var dp = _.map(timeseries_data, 'datapoints');
var dp = _.map(timeseries_data, 'datapoints'); dp = sequence(aggregationFunctions)(dp);
dp = sequence(aggregationFunctions)(dp);
var aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name'); var aggFuncNames = _.map(metricFunctions.getCategories()['Aggregate'], 'name');
var lastAgg = _.findLast(target.functions, function (func) { var lastAgg = _.findLast(target.functions, function (func) {
return _.includes(aggFuncNames, func.def.name); return _.includes(aggFuncNames, func.def.name);
}); });
timeseries_data = [{ timeseries_data = [{
target: lastAgg.text, target: lastAgg.text,
datapoints: dp datapoints: dp
}]; }];
})();
} }
// Apply alias functions // Apply alias functions
@@ -419,12 +415,10 @@ System.register(['lodash', 'app/core/utils/datemath', './utils', './migrations',
return func.def.name === 'timeShift'; return func.def.name === 'timeShift';
}); });
if (timeShiftFunc) { if (timeShiftFunc) {
(function () { var shift = timeShiftFunc.params[0];
var shift = timeShiftFunc.params[0]; _.forEach(timeseries_data, function (series) {
_.forEach(timeseries_data, function (series) { series.datapoints = dataProcessor.unShiftTimeSeries(shift, series.datapoints);
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: [] defaultParams: []
}); });
addFuncDef({
name: 'replaceAlias',
category: 'Alias',
params: [{ name: 'regexp', type: 'string' }, { name: 'newAlias', type: 'string' }],
defaultParams: ['/(.*)/', '$1']
});
_.each(categories, function (funcList, catName) { _.each(categories, function (funcList, catName) {
categories[catName] = _.sortBy(funcList, 'name'); 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; 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) { function setAliasByRegex(alias, timeseries) {
timeseries.target = extractText(timeseries.target, alias); timeseries.target = extractText(timeseries.target, alias);
return timeseries; return timeseries;
@@ -315,7 +328,8 @@ var metricFunctions = {
bottom: _lodash2.default.partial(limit, 'bottom'), bottom: _lodash2.default.partial(limit, 'bottom'),
timeShift: timeShift, timeShift: timeShift,
setAlias: setAlias, setAlias: setAlias,
setAliasByRegex: setAliasByRegex setAliasByRegex: setAliasByRegex,
replaceAlias: replaceAlias
}; };
var aggregationFunctions = { var aggregationFunctions = {

View File

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

View File

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

View File

@@ -158,3 +158,35 @@ Examples:
``` ```
setAlias(Zabbix busy [a-zA-Z]+) 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; 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) { function setAliasByRegex(alias, timeseries) {
timeseries.target = extractText(timeseries.target, alias); timeseries.target = extractText(timeseries.target, alias);
return timeseries; return timeseries;
@@ -308,7 +321,8 @@ let metricFunctions = {
bottom: _.partial(limit, 'bottom'), bottom: _.partial(limit, 'bottom'),
timeShift: timeShift, timeShift: timeShift,
setAlias: setAlias, setAlias: setAlias,
setAliasByRegex: setAliasByRegex setAliasByRegex: setAliasByRegex,
replaceAlias: replaceAlias
}; };
let aggregationFunctions = { let aggregationFunctions = {

View File

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