-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented exclude and grep functions
- Loading branch information
1 parent
ec58cd3
commit 9fe800a
Showing
4 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/net/iponweb/disthene/reader/graphite/functions/ExcludeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/net/iponweb/disthene/reader/graphite/functions/GrepFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters