Skip to content

Commit

Permalink
Add difference of the first value and the last value for aggregators
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Larsen <[email protected]>
  • Loading branch information
xiayang authored and manolama committed May 21, 2018
1 parent 627620b commit 26355ac
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/core/Aggregators.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public enum Interpolation {
public static final Aggregator DEV = new StdDev(
Interpolation.LERP, "dev");

/** Aggregator that returns the difference of the first value and the
* last value in the data points */
public static final Aggregator DIFF = new Diff(
Interpolation.LERP, "diff");

/** Sums data points but will cause the SpanGroup to return a 0 if timestamps
* don't line up instead of interpolating. */
public static final Aggregator ZIMSUM = new Sum(
Expand Down Expand Up @@ -174,6 +179,7 @@ public enum Interpolation {
aggregators.put("median", MEDIAN);
aggregators.put("mult", MULTIPLY);
aggregators.put("dev", DEV);
aggregators.put("diff", DIFF);
aggregators.put("count", COUNT);
aggregators.put("zimsum", ZIMSUM);
aggregators.put("mimmin", MIMMIN);
Expand Down Expand Up @@ -526,6 +532,53 @@ public double runDouble(final Doubles values) {

}

/**
* Difference of the first value and the last value in multi values aggregator.
*/
private static final class Diff extends net.opentsdb.core.Aggregator {
public Diff(final Interpolation method, final String name) {
super(method, name);
}

@Override
public long runLong(final Longs values) {
long first_mean = values.nextLongValue();

if (!values.hasNextValue()) {
return 0;
}

long last_mean = 0;
do {
last_mean = values.nextLongValue();
} while (values.hasNextValue());

return last_mean - first_mean;
}

@Override
public double runDouble(final Doubles values) {
double first_mean = values.nextDoubleValue();
while (Double.isNaN(first_mean) && values.hasNextValue()) {
first_mean = values.nextDoubleValue();
}

if (Double.isNaN(first_mean)) {
return Double.NaN;
}
if (!values.hasNextValue()) {
return 0.;
}

double last_mean = 0.;
do {
last_mean = values.nextDoubleValue();
} while (values.hasNextValue());

return last_mean - first_mean;
}
}

private static final class Count extends Aggregator {
public Count(final Interpolation method, final String name) {
super(method, name);
Expand Down
2 changes: 1 addition & 1 deletion tools/check_tsd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import sys
import time
from optparse import OptionParser

AGGREGATORS = ('avg', 'count', 'dev',
AGGREGATORS = ('avg', 'count', 'dev', 'diff',
'ep50r3', 'ep50r7', 'ep75r3', 'ep75r7', 'ep90r3', 'ep90r7', 'ep95r3', 'ep95r7',
'ep99r3', 'ep99r7', 'ep999r3', 'ep999r7',
'mimmin', 'mimmax', 'min', 'max', 'none',
Expand Down

0 comments on commit 26355ac

Please sign in to comment.