Search in sources :

Example 11 with FilterDef

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);
    }
}
Also used : FilterDef(org.opennms.netmgt.measurements.model.FilterDef) Test(org.junit.Test)

Example 12 with FilterDef

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);
    }
}
Also used : FilterDef(org.opennms.netmgt.measurements.model.FilterDef) Test(org.junit.Test)

Example 13 with FilterDef

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);
    }
}
Also used : FilterDef(org.opennms.netmgt.measurements.model.FilterDef) Test(org.junit.Test)

Example 14 with FilterDef

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")));
}
Also used : FilterDef(org.opennms.netmgt.measurements.model.FilterDef) Test(org.junit.Test)

Example 15 with FilterDef

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);
            }
        }
    }
}
Also used : ValidationException(org.opennms.netmgt.measurements.api.exceptions.ValidationException) FilterDef(org.opennms.netmgt.measurements.model.FilterDef) HashMap(java.util.HashMap) Expression(org.opennms.netmgt.measurements.model.Expression) Source(org.opennms.netmgt.measurements.model.Source)

Aggregations

FilterDef (org.opennms.netmgt.measurements.model.FilterDef)15 Test (org.junit.Test)13 Source (org.opennms.netmgt.measurements.model.Source)2 HashMap (java.util.HashMap)1 FilterException (org.opennms.netmgt.measurements.api.exceptions.FilterException)1 ValidationException (org.opennms.netmgt.measurements.api.exceptions.ValidationException)1 Expression (org.opennms.netmgt.measurements.model.Expression)1 QueryRequest (org.opennms.netmgt.measurements.model.QueryRequest)1 QueryResponse (org.opennms.netmgt.measurements.model.QueryResponse)1