Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

package org.apache.dubbo.metrics.aggregate;

import com.tdunning.math.stats.Centroid;
import com.tdunning.math.stats.TDigest;

public abstract class DubboAbstractTDigest extends TDigest {
boolean recordAllData = false;

/**
* Same as {@link #weightedAverageSorted(double, double, double, double)} but flips
* the order of the variables if <code>x2</code> is greater than
* <code>x1</code>.
*/
static double weightedAverage(double x1, double w1, double x2, double w2) {
if (x1 <= x2) {
return weightedAverageSorted(x1, w1, x2, w2);
} else {
return weightedAverageSorted(x2, w2, x1, w1);
}
}

/**
* Compute the weighted average between <code>x1</code> with a weight of
* <code>w1</code> and <code>x2</code> with a weight of <code>w2</code>.
* This expects <code>x1</code> to be less than or equal to <code>x2</code>
* and is guaranteed to return a number in <code>[x1, x2]</code>. An
* explicit check is required since this isn't guaranteed with floating-point
* numbers.
*/
private static double weightedAverageSorted(double x1, double w1, double x2, double w2) {
assert x1 <= x2;
final double x = (x1 * w1 + x2 * w2) / (w1 + w2);
return Math.max(x1, Math.min(x, x2));
}

abstract void add(double x, int w, Centroid base);

/**
* Sets up so that all centroids will record all data assigned to them. For testing only, really.
*/
@Override
public TDigest recordAllData() {
recordAllData = true;
return this;
}

@Override
public boolean isRecording() {
return recordAllData;
}

/**
* Adds a sample to a histogram.
*
* @param x The value to add.
*/
@Override
public void add(double x) {
add(x, 1);
}

@Override
public void add(TDigest other) {
for (Centroid centroid : other.centroids()) {
add(centroid.mean(), centroid.count(), centroid);
}
}

}
Loading