use of org.opennms.netmgt.measurements.model.FilterDef in project opennms by OpenNMS.
the class DerivativeTest method canCaculatePercentile.
@Test
public void canCaculatePercentile() throws Exception {
FilterDef filterDef = new FilterDef("Derivative", "inputColumn", "X", "outputColumn", "Y");
RowSortedTable<Long, String, Double> table = TreeBasedTable.create();
// Add some values to the table
for (int k = 0; k < inputValues.length; k++) {
table.put(Long.valueOf(k), Filter.TIMESTAMP_COLUMN_NAME, (double) k * 1000);
table.put(Long.valueOf(k), "X", inputValues[k]);
}
// Apply the filter
getFilterEngine().filter(filterDef, table);
// Verify
Assert.assertEquals(inputValues.length, table.rowKeySet().size());
for (long k = 0; k < inputValues.length; k++) {
Assert.assertEquals((double) k * 1000, table.get(k, Filter.TIMESTAMP_COLUMN_NAME), 0.0001);
Assert.assertEquals(outputValues[(int) k], table.get(k, "Y"), 0.0001);
}
}
use of org.opennms.netmgt.measurements.model.FilterDef in project opennms by OpenNMS.
the class JEXLFilterTest method canDuplicateColumn.
@Test
public void canDuplicateColumn() throws Exception {
String jexlExpression = "for (k : table.rowKeySet()) {table.put(k, \"Z\", table.get(k, \"X\"));}";
FilterDef filterDef = new FilterDef("JEXL", "expression", jexlExpression);
RowSortedTable<Long, String, Double> table = TreeBasedTable.create();
// Add some values to the table
for (long k = 0; k < 90; k++) {
table.put(k, Filter.TIMESTAMP_COLUMN_NAME, (double) k * 1000);
table.put(k, "X", (double) k);
}
// Apply the filter
getFilterEngine().filter(filterDef, table);
// Verify
Assert.assertEquals(90, table.rowKeySet().size());
for (long k = 0; k < 90; k++) {
Assert.assertEquals((double) k * 1000, table.get(k, Filter.TIMESTAMP_COLUMN_NAME), 0.0001);
Assert.assertEquals((double) k, table.get(k, "Z"), 0.0001);
}
}
use of org.opennms.netmgt.measurements.model.FilterDef in project opennms by OpenNMS.
the class OutlierFilterIT method canInterpolateValues.
@Test
public void canInterpolateValues() throws Exception {
FilterDef filterDef = new FilterDef("Outlier", "inputColumn", "Y", "quantile", "0.99");
// Use non-constant values for the Y column
RowSortedTable<Long, String, Double> table = TreeBasedTable.create();
for (long i = 0; i < 100; i++) {
table.put(i, "X", (double) i);
table.put(i, "Y", 8 * Math.sin(i));
}
// Add an outlier near the middle of the series
table.put(42L, "Y", 9999.0d);
// Apply the filter
getFilterEngine().filter(filterDef, table);
// Verify
for (long i = 0; i < 100; i++) {
Assert.assertEquals((double) i, table.get(i, "X"), 0.0001);
double delta = 0.0001;
if (i == 42) {
// Use a larger delta on the same index as the outlier
delta = 6;
}
Assert.assertEquals(8 * Math.sin(i), table.get(i, "Y"), delta);
}
}
use of org.opennms.netmgt.measurements.model.FilterDef in project opennms by OpenNMS.
the class OutlierFilterIT method canFilterOutliers.
@Test
public void canFilterOutliers() throws Exception {
FilterDef filterDef = new FilterDef("Outlier", "inputColumn", "Y");
// Use constant values for the Y column
RowSortedTable<Long, String, Double> table = TreeBasedTable.create();
for (long i = 0; i < 100; i++) {
table.put(i, "X", (double) i);
table.put(i, "Y", 1.0d);
}
// Add an outlier to the first and last elements
table.put(0L, "Y", 9999.0d);
table.put(99L, "Y", 9999.0d);
// Add another one near the middle of the series
table.put(42L, "Y", 9999.0d);
// Apply the filter
getFilterEngine().filter(filterDef, table);
// The Y column should be constant - no outliers
for (long i = 1; i < 99; i++) {
Assert.assertEquals((double) i, table.get(i, "X"), 0.0001);
Assert.assertEquals(1.0d, table.get(i, "Y"), 0.0001);
}
// Outliers at the beginning and end of the series should be replaced with
// NaN, since these can't be properly interpolated
Assert.assertTrue(Double.isNaN(table.get(0L, "Y")));
Assert.assertTrue(Double.isNaN(table.get(99L, "Y")));
}
use of org.opennms.netmgt.measurements.model.FilterDef in project opennms by OpenNMS.
the class QueryRequestValidator method validate.
public void validate(QueryRequest request) throws ValidationException {
if (request.getEnd() < 0) {
throw new ValidationException("Query end must be >= 0: {}", request.getEnd());
}
if (request.getStep() <= 0) {
throw new ValidationException("Query step must be > 0: {}", request.getStep());
}
if ((request.getHeartbeat() == null && request.getInterval() != null) || (request.getHeartbeat() != null && request.getInterval() == null)) {
throw new ValidationException("If either the heartbeat or the interval are set, then both must be set.");
}
if (request.getHeartbeat() != null && request.getInterval() != null) {
if (request.getHeartbeat() <= 0) {
throw new ValidationException("Heartbeat must be positive: {}", request.getHeartbeat());
}
if (request.getInterval() <= 0) {
throw new ValidationException("Interval must be positive: {}", request.getInterval());
}
if (request.getStep() % request.getInterval() != 0) {
throw new ValidationException("Step must be a multiple of the interval. Step: {}, Interval: {}", request.getStep(), request.getInterval());
}
if (request.getHeartbeat() % request.getInterval() != 0) {
throw new ValidationException("Heartbeat must be a multiple of the interval. Interval: {} Heartbeat: {}", request.getInterval(), request.getHeartbeat());
}
}
final Map<String, String> labels = new HashMap<>();
for (final Source source : request.getSources()) {
if (source.getResourceId() == null || source.getAttribute() == null || source.getLabel() == null || source.getAggregation() == null) {
throw new ValidationException("Query source fields must be set: {}", source);
}
if (labels.containsKey(source.getLabel())) {
throw new ValidationException("Query source label '{}' conflict: source with that label is already defined.", source.getLabel());
} else {
labels.put(source.getLabel(), "source");
}
}
for (final Expression expression : request.getExpressions()) {
if (expression.getExpression() == null || expression.getLabel() == null) {
throw new ValidationException("Query expression fields must be set: {}", expression);
}
if (labels.containsKey(expression.getLabel())) {
final String type = labels.get(expression.getLabel());
throw new ValidationException("Query expression label '{}' conflict: {} with that label is already defined.", expression.getLabel(), type);
} else {
labels.put(expression.getLabel(), "expression");
}
}
List<FilterDef> filters = request.getFilters();
if (filters.size() > 0) {
for (FilterDef filter : filters) {
if (filter.getName() == null) {
throw new ValidationException("Filter name must be set: {}", filter);
}
}
}
}
Aggregations