Skip to content

Commit

Permalink
* implemented changed function
Browse files Browse the repository at this point in the history
* fixed paths limit behavior
  • Loading branch information
EinsamHauer committed Oct 25, 2017
1 parent 45326c2 commit d404818
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
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.48</version>
<version>0.2.49</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.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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class FunctionRegistry {
registry.put("averageSeriesWithWildcards", AverageSeriesWithWildcardsFunction.class);
registry.put("avg", AverageSeriesFunction.class);
registry.put("cactiStyle", CactiStyleFunction.class);
registry.put("changed", ChangedFunction.class);
registry.put("color", ColorFunction.class);
registry.put("constantLine", ConstantLineFunction.class);
registry.put("countSeries", CountSeriesFunction.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
*/
public interface DistheneReaderHandler {

FullHttpResponse handle(HttpRequest request) throws ParameterParsingException, ExecutionException, InterruptedException, EvaluationException, LogarithmicScaleNotAllowed;
FullHttpResponse handle(HttpRequest request) throws ParameterParsingException, ExecutionException, InterruptedException, EvaluationException, LogarithmicScaleNotAllowed, TooMuchDataExpectedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.netty.handler.codec.http.*;
import net.iponweb.disthene.reader.exceptions.MissingParameterException;
import net.iponweb.disthene.reader.exceptions.ParameterParsingException;
import net.iponweb.disthene.reader.exceptions.TooMuchDataExpectedException;
import net.iponweb.disthene.reader.exceptions.UnsupportedMethodException;
import net.iponweb.disthene.reader.service.index.IndexService;
import net.iponweb.disthene.reader.service.stats.StatsService;
Expand All @@ -28,7 +29,7 @@ public PathsHandler(IndexService indexService, StatsService statsService) {
}

@Override
public FullHttpResponse handle(HttpRequest request) throws ParameterParsingException {
public FullHttpResponse handle(HttpRequest request) throws ParameterParsingException, TooMuchDataExpectedException {
PathsParameters parameters = parse(request);

statsService.incPathsRequests(parameters.getTenant());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Joiner;
import net.iponweb.disthene.reader.config.IndexConfiguration;
import net.iponweb.disthene.reader.exceptions.TooMuchDataExpectedException;
import net.iponweb.disthene.reader.utils.WildcardUtil;
import org.apache.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
Expand Down Expand Up @@ -84,7 +85,7 @@ public List<String> getPaths(String tenant, List<String> wildcards) {
return result;
}

public String getPathsAsJsonArray(String tenant, String wildcard) {
public String getPathsAsJsonArray(String tenant, String wildcard) throws TooMuchDataExpectedException {
String regEx = WildcardUtil.getPathsRegExFromWildcard(wildcard);

SearchResponse response = client.prepareSearch(indexConfiguration.getIndex())
Expand All @@ -97,7 +98,8 @@ public String getPathsAsJsonArray(String tenant, String wildcard) {

// if total hits exceeds maximum - abort right away returning empty array
if (response.getHits().totalHits() > indexConfiguration.getMaxPaths()) {
return "[]";
logger.debug("Total number of paths exceeds the limit: " + response.getHits().totalHits());
throw new TooMuchDataExpectedException("Total number of paths exceeds the limit: " + response.getHits().totalHits() + " (the limit is " + indexConfiguration.getMaxPaths() + ")");
}

List<String> paths = new ArrayList<>();
Expand Down

0 comments on commit d404818

Please sign in to comment.