Commit ad4c0ab5 by Torkel Ödegaard

added more function definitions, added small hack to make randomWalk and…

added more function definitions, added small hack to make randomWalk and countLine that do not take any series work with the parser and target editor (the whole target editor and parsing & back to target rendering needs to be rewritten to handle functions that take multiple series)
parent 203b8df7
...@@ -17,6 +17,8 @@ function (angular, _, config, gfunc, Parser) { ...@@ -17,6 +17,8 @@ function (angular, _, config, gfunc, Parser) {
parseTarget(); parseTarget();
}; };
// The way parsing and the target editor works needs
// to be rewritten to handle functions that take multiple series
function parseTarget() { function parseTarget() {
$scope.functions = []; $scope.functions = [];
$scope.segments = []; $scope.segments = [];
...@@ -72,7 +74,13 @@ function (angular, _, config, gfunc, Parser) { ...@@ -72,7 +74,13 @@ function (angular, _, config, gfunc, Parser) {
throw { message: 'invalid number of parameters to method ' + func.def.name }; throw { message: 'invalid number of parameters to method ' + func.def.name };
} }
func.params[index - 1] = astNode.value; if (index === 0) {
func.params[index] = astNode.value;
}
else {
func.params[index - 1] = astNode.value;
}
break; break;
case 'metric': case 'metric':
...@@ -141,16 +149,7 @@ function (angular, _, config, gfunc, Parser) { ...@@ -141,16 +149,7 @@ function (angular, _, config, gfunc, Parser) {
} }
function wrapFunction(target, func) { function wrapFunction(target, func) {
var targetWrapped = func.def.name + '(' + target; return func.render(target);
_.each(func.params, function(param) {
if (_.isString(param)) {
targetWrapped += ",'" + param + "'";
}
else {
targetWrapped += "," + param;
}
});
return targetWrapped + ')';
} }
$scope.getAltSegments = function (index) { $scope.getAltSegments = function (index) {
......
...@@ -32,13 +32,6 @@ function (_) { ...@@ -32,13 +32,6 @@ function (_) {
}); });
addFuncDef({ addFuncDef({
name: "alias",
category: categories.Special,
params: [ { name: "alias", type: 'string' } ],
defaultParams: ['alias']
});
addFuncDef({
name: "holtWintersForecast", name: "holtWintersForecast",
category: categories.Calculate, category: categories.Calculate,
}); });
...@@ -70,6 +63,20 @@ function (_) { ...@@ -70,6 +63,20 @@ function (_) {
}); });
addFuncDef({ addFuncDef({
name: "alias",
category: categories.Special,
params: [ { name: "alias", type: 'string' } ],
defaultParams: ['alias']
});
addFuncDef({
name: "aliasSub",
category: categories.Special,
params: [ { name: "search", type: 'string' }, { name: "replace", type: 'string' } ],
defaultParams: ['', '']
});
addFuncDef({
name: "groupByNode", name: "groupByNode",
category: categories.Special, category: categories.Special,
params: [ params: [
...@@ -95,6 +102,30 @@ function (_) { ...@@ -95,6 +102,30 @@ function (_) {
}); });
addFuncDef({ addFuncDef({
name: 'aliasByMetric',
category: categories.Special,
});
addFuncDef({
name: 'randomWalk',
category: categories.Special,
params: [ { name: "name", type: "string", } ],
defaultParams: ['randomWalk']
});
addFuncDef({
name: 'countSeries',
category: categories.Special
});
addFuncDef({
name: 'constantLine',
category: categories.Special,
params: [ { name: "value", type: "int", } ],
defaultParams: [10]
});
addFuncDef({
name: 'scale', name: 'scale',
category: categories.Transform, category: categories.Transform,
params: [ { name: "factor", type: "int", } ], params: [ { name: "factor", type: "int", } ],
...@@ -125,6 +156,25 @@ function (_) { ...@@ -125,6 +156,25 @@ function (_) {
defaultParams: ['1h'] defaultParams: ['1h']
}); });
addFuncDef({
name: 'absolute',
category: categories.Transform,
});
addFuncDef({
name: 'averageAbove',
category: categories.Filter,
params: [ { name: "n", type: "int", } ],
defaultParams: [25]
});
addFuncDef({
name: 'averageBelow',
category: categories.Filter,
params: [ { name: "n", type: "int", } ],
defaultParams: [25]
});
_.each(categories, function(funcList, catName) { _.each(categories, function(funcList, catName) {
categories[catName] = _.sortBy(funcList, 'name'); categories[catName] = _.sortBy(funcList, 'name');
}); });
...@@ -135,6 +185,19 @@ function (_) { ...@@ -135,6 +185,19 @@ function (_) {
this.updateText(); this.updateText();
} }
FuncInstance.prototype.render = function (metricExp) {
var str = this.def.name + '(';
var parameters = _.map(this.params, function(value) {
return _.isString(value) ? "'" + value + "'" : value;
});
if (metricExp !== undefined) {
parameters.unshift(metricExp);
}
return str + parameters.join(',') + ')';
};
FuncInstance.prototype.updateText = function () { FuncInstance.prototype.updateText = function () {
if (this.params.length === 0) { if (this.params.length === 0) {
this.text = this.def.name + '()'; this.text = this.def.name + '()';
......
...@@ -2,7 +2,7 @@ define([ ...@@ -2,7 +2,7 @@ define([
'app/services/graphite/gfunc' 'app/services/graphite/gfunc'
], function(gfunc) { ], function(gfunc) {
describe('when creating func instance from func namae', function() { describe('when creating func instance from func names', function() {
it('should return func instance', function() { it('should return func instance', function() {
var func = gfunc.createFuncInstance('sumSeries'); var func = gfunc.createFuncInstance('sumSeries');
...@@ -34,11 +34,33 @@ define([ ...@@ -34,11 +34,33 @@ define([
}); });
describe('when rendering func instance', function() {
it('should handle single metric param', function() {
var func = gfunc.createFuncInstance('sumSeries');
expect(func.render('hello.metric')).to.equal("sumSeries(hello.metric)");
});
it('should handle metric param and int param and string param', function() {
var func = gfunc.createFuncInstance('groupByNode');
func.params[0] = 5;
func.params[1] = 'avg';
expect(func.render('hello.metric')).to.equal("groupByNode(hello.metric,5,'avg')");
});
it('should handle function with no metric param', function() {
var func = gfunc.createFuncInstance('randomWalk');
func.params[0] = 'test';
expect(func.render(undefined)).to.equal("randomWalk('test')");
});
});
describe('when requesting function categories', function() { describe('when requesting function categories', function() {
it('should return function categories', function() { it('should return function categories', function() {
var catIndex = gfunc.getCategories(); var catIndex = gfunc.getCategories();
expect(catIndex.Special.length).to.equal(3); expect(catIndex.Special.length).to.equal(7);
}); });
}); });
......
...@@ -20,6 +20,14 @@ define([ ...@@ -20,6 +20,14 @@ define([
expect(rootNode.params.length).to.be(1); expect(rootNode.params.length).to.be(1);
}); });
it('simple function with string arg', function() {
var parser = new Parser("randomWalk('test')");
var rootNode = parser.getAst();
expect(rootNode.type).to.be('function');
expect(rootNode.params.length).to.be(1);
expect(rootNode.params[0].type).to.be('string');
});
it('function with multiple args', function() { it('function with multiple args', function() {
var parser = new Parser("sum(test, 1, 'test')"); var parser = new Parser("sum(test, 1, 'test')");
var rootNode = parser.getAst(); var rootNode = parser.getAst();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment