org.kairosdb.core.groupby.BinGroupBy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kairosdb Show documentation
Show all versions of kairosdb Show documentation
KairosDB is a fast distributed scalable time series abstraction on top of Cassandra.
/*
* Copyright 2016 KairosDB Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kairosdb.core.groupby;
import org.json.JSONException;
import org.json.JSONWriter;
import org.kairosdb.core.DataPoint;
import org.kairosdb.core.annotation.FeatureComponent;
import org.kairosdb.core.annotation.FeatureProperty;
import org.kairosdb.core.annotation.ValidationProperty;
import org.kairosdb.core.formatter.FormatterException;
import org.kairosdb.plugin.GroupBy;
import java.io.StringWriter;
import java.util.Map;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Groups data points by bin values. Data points are a range of values specified by bins.
* Bins array needs to be in ascending order.
*/
@FeatureComponent(
name = "bin",
description = "Groups data points by bins or buckets."
)
public class BinGroupBy implements GroupBy {
@FeatureProperty(
label = "Bin Values",
description = "List of bin values. For example, if the list of bins is 10, 20, 30, then values less than 10 are placed in the first group, values between 10-19 into the second group, and so forth.",
validations = {
@ValidationProperty(
expression = "value.length > 0",
message = "The list can't be empty."
),
@ValidationProperty(
expression = "value.every((n) => !isNaN(parseFloat(n)) && isFinite(n))",
message = "The list must contain only numbers."
)
}
)
private double[] bins;
public BinGroupBy() {
}
public BinGroupBy(final double[] bins) {
checkArgument(bins.length > 0);
this.bins = bins;
}
@Override
public int getGroupId(final DataPoint dataPoint, final Map tags) {
double dataValue = 0;
if (dataPoint.isLong())
dataValue = dataPoint.getLongValue();
else if (dataPoint.isDouble())
dataValue = dataPoint.getDoubleValue();
else
return -1;
if (dataValue < bins[0])
return 0;
for (int i = 0; i < bins.length - 1; i++) {
if (dataValue >= bins[i] && dataValue < bins[i + 1])
return i + 1;
}
return bins.length;
}
@Override
public GroupByResult getGroupByResult(final int id) {
return new GroupByResult() {
@Override
public String toJson() throws FormatterException {
final StringWriter stringWriter = new StringWriter();
try {
final JSONWriter writer = new JSONWriter(stringWriter);
writer.object();
writer.key("name").value("bin");
writer.key("bins").value(bins);
writer.key("group").object();
writer.key("bin_number").value(id);
writer.endObject();
writer.endObject();
} catch (final JSONException e) {
throw new FormatterException(e);
}
return stringWriter.toString();
}
};
}
@Override
public void setStartDate(final long startDate) {
}
public void setBins(final double[] bins) {
this.bins = bins;
}
}