Skip to content

Commit

Permalink
Added mimMax function
Browse files Browse the repository at this point in the history
  • Loading branch information
EinsamHauer committed Jul 5, 2022
1 parent 26baa81 commit 69e0541
Show file tree
Hide file tree
Showing 3 changed files with 61 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>2.0.7</version>
<version>2.0.8</version>
<name>disthene-reader</name>
<url>http://maven.apache.org</url>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.CollectionUtils;
import net.iponweb.disthene.reader.utils.TimeSeriesUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

public MinMaxFunction(String text) {
super(text, "minMax");
}

@Override
public List<TimeSeries> evaluate(TargetEvaluator evaluator) throws EvaluationException {
List<TimeSeries> processedArguments = new ArrayList<>(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 min = CollectionUtils.min(Arrays.asList(ts.getValues()));
Double max = CollectionUtils.max(Arrays.asList(ts.getValues()));
if (min != null && max != null) {
double range = max - min;
for (int i = 0; i < length; i++) {
if (ts.getValues()[i] != null ) {
ts.getValues()[i] = (ts.getValues()[i] - min) / range;
}
}
}
setResultingName(ts);
}

return processedArguments;
}

@Override
public void checkArguments() throws InvalidArgumentException {
if (arguments.size() != 1) throw new InvalidArgumentException("minMax: number of arguments is " + arguments.size() + ". Must be one.");
if (!(arguments.get(0) instanceof Target)) throw new InvalidArgumentException("minMax: argument is " + arguments.get(0).getClass().getName() + ". Must be series");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class FunctionRegistry {
registry.put("maximumBelow", MaximumBelowFunction.class);
registry.put("minimumAbove", MinimumAboveFunction.class);
registry.put("minimumBelow", MinimumBelowFunction.class);
registry.put("minMax", MinMaxFunction.class);
registry.put("maxSeries", MaxSeriesFunction.class);
registry.put("minSeries", MinSeriesFunction.class);
registry.put("mostDeviant", MostDeviantFunction.class);
Expand Down

0 comments on commit 69e0541

Please sign in to comment.