-
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.
Showing
6 changed files
with
69 additions
and
5 deletions.
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/ChangedFunction.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.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; | ||
|
||
/** | ||
* @author Andrei Ivanov | ||
*/ | ||
public class ChangedFunction extends DistheneFunction { | ||
|
||
public ChangedFunction(String text) { | ||
super(text, "offset"); | ||
} | ||
|
||
@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(); | ||
} | ||
|
||
int length = processedArguments.get(0).getValues().length; | ||
|
||
for (TimeSeries ts : processedArguments) { | ||
Double previous = null; | ||
for (int i = 0; i < length; i++) { | ||
if (previous == null) { | ||
previous = ts.getValues()[i]; | ||
ts.getValues()[i] = 0.; | ||
} else if (ts.getValues()[i] != null && !ts.getValues()[i].equals(previous)) { | ||
previous = ts.getValues()[i]; | ||
ts.getValues()[i] = 1.; | ||
} else { | ||
ts.getValues()[i] = 0.; | ||
} | ||
} | ||
ts.setName("changed(" + ts.getName() + ")"); | ||
} | ||
|
||
return processedArguments; | ||
} | ||
|
||
@Override | ||
public void checkArguments() throws InvalidArgumentException { | ||
if (arguments.size() > 1 || arguments.size() < 1) throw new InvalidArgumentException("offset: number of arguments is " + arguments.size() + ". Must be one."); | ||
if (!(arguments.get(0) instanceof Target)) throw new InvalidArgumentException("offset: argument is " + arguments.get(0).getClass().getName() + ". Must be series"); | ||
} | ||
} |
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
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
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
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