Skip to content

Commit

Permalink
implemented exclude and grep functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EinsamHauer committed May 4, 2017
1 parent ec58cd3 commit 9fe800a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>net.iponweb.disthene.reader</groupId>
<artifactId>disthene-reader</artifactId>
<packaging>jar</packaging>
<version>0.2.45</version>
<version>0.2.46</version>
<name>disthene-reader</name>
<url>http://maven.apache.org</url>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.iponweb.disthene.reader.graphite.functions;

import net.iponweb.disthene.reader.beans.TimeSeries;
import net.iponweb.disthene.reader.beans.TimeSeriesOption;
import net.iponweb.disthene.reader.exceptions.EvaluationException;
import net.iponweb.disthene.reader.exceptions.InvalidArgumentException;
import net.iponweb.disthene.reader.exceptions.TimeSeriesNotAlignedException;
import net.iponweb.disthene.reader.graph.ColorTable;
import net.iponweb.disthene.reader.graphite.Target;
import net.iponweb.disthene.reader.graphite.evaluation.TargetEvaluator;
import net.iponweb.disthene.reader.utils.TimeSeriesUtils;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Andrei Ivanov
*/
public class ExcludeFunction extends DistheneFunction {

public ExcludeFunction(String text) {
super(text, "exclude");
}

@Override
public List<TimeSeries> evaluate(TargetEvaluator evaluator) throws EvaluationException {
List<TimeSeries> processedArguments = new ArrayList<>();
processedArguments.addAll(evaluator.eval((Target) arguments.get(0)));

if (processedArguments.size() == 0) return new ArrayList<>();

if (!TimeSeriesUtils.checkAlignment(processedArguments)) {
throw new TimeSeriesNotAlignedException();
}

String regex = (String) arguments.get(1);
Pattern pattern = Pattern.compile(regex);

List<TimeSeries> result = new ArrayList<>();

for (TimeSeries ts : processedArguments) {
Matcher matcher = pattern.matcher(ts.getName());
if (!matcher.find()) {
result.add(ts);
}
}

return result;
}

@Override
public void checkArguments() throws InvalidArgumentException {
if (arguments.size() != 2) throw new InvalidArgumentException("exclude: number of arguments is " + arguments.size() + ". Must be two.");
if (!(arguments.get(0) instanceof Target)) throw new InvalidArgumentException("exclude: argument is " + arguments.get(0).getClass().getName() + ". Must be series");
if (!(arguments.get(1) instanceof String)) throw new InvalidArgumentException("exclude: argument is " + arguments.get(1).getClass().getName() + ". Must be a string");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package net.iponweb.disthene.reader.graphite.functions;

import net.iponweb.disthene.reader.beans.TimeSeries;
import net.iponweb.disthene.reader.exceptions.EvaluationException;
import net.iponweb.disthene.reader.exceptions.InvalidArgumentException;
import net.iponweb.disthene.reader.exceptions.TimeSeriesNotAlignedException;
import net.iponweb.disthene.reader.graphite.Target;
import net.iponweb.disthene.reader.graphite.evaluation.TargetEvaluator;
import net.iponweb.disthene.reader.utils.TimeSeriesUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Andrei Ivanov
*/
public class GrepFunction extends DistheneFunction {

public GrepFunction(String text) {
super(text, "grep");
}

@Override
public List<TimeSeries> evaluate(TargetEvaluator evaluator) throws EvaluationException {
List<TimeSeries> processedArguments = new ArrayList<>();
processedArguments.addAll(evaluator.eval((Target) arguments.get(0)));

if (processedArguments.size() == 0) return new ArrayList<>();

if (!TimeSeriesUtils.checkAlignment(processedArguments)) {
throw new TimeSeriesNotAlignedException();
}

String regex = (String) arguments.get(1);
Pattern pattern = Pattern.compile(regex);

List<TimeSeries> result = new ArrayList<>();

for (TimeSeries ts : processedArguments) {
Matcher matcher = pattern.matcher(ts.getName());
if (matcher.find()) {
result.add(ts);
}
}

return result;
}

@Override
public void checkArguments() throws InvalidArgumentException {
if (arguments.size() != 2) throw new InvalidArgumentException("grep: number of arguments is " + arguments.size() + ". Must be two.");
if (!(arguments.get(0) instanceof Target)) throw new InvalidArgumentException("grep: argument is " + arguments.get(0).getClass().getName() + ". Must be series");
if (!(arguments.get(1) instanceof String)) throw new InvalidArgumentException("grep: argument is " + arguments.get(1).getClass().getName() + ". Must be a string");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public class FunctionRegistry {
registry.put("diffSeries", DiffSeriesFunction.class);
registry.put("divideSeries", DivideSeriesFunction.class);
registry.put("drawAsInfinite", DrawAsInfiniteFunction.class);
registry.put("grep", GrepFunction.class);
registry.put("group", GroupFunction.class);
registry.put("exclude", ExcludeFunction.class);
registry.put("highestAverage", HighestAverageFunction.class);
registry.put("highestCurrent", HighestCurrentFunction.class);
registry.put("highestMax", HighestMaxFunction.class);
Expand Down

0 comments on commit 9fe800a

Please sign in to comment.