Search in sources :

Example 6 with ResultDescriptor

use of org.opennms.newts.api.query.ResultDescriptor in project newts by OpenNMS.

the class InsertSelectMeasurementsITCase method testWithCounter.

@Test
public void testWithCounter() {
    Iterator<Row<Sample>> testSamples = new SampleRowsBuilder(new Resource("localhost"), MetricType.COUNTER).row(900000000).element("mCounter", // Thu Jul  9 11:00:00 CDT 1998
    0).row(900000300).element("mCounter", 300).row(900000600).element("mCounter", 600).row(900000900).element("mCounter", 900).row(900001200).element("mCounter", 1200).row(900001500).element("mCounter", 1500).row(900001800).element("mCounter", 1800, mapFor("a", "1")).row(900002100).element("mCounter", 2100).row(900002400).element("mCounter", 2400, mapFor("b", "2")).row(900002700).element("mCounter", 2700).row(900003000).element("mCounter", 3000).row(900003300).element("mCounter", 3300).row(900003600).element("mCounter", 3600).row(900003900).element("mCounter", 3900).row(900004200).element("mCounter", 4200).row(900004500).element("mCounter", 4500).row(900004800).element("mCounter", 4800).row(900005100).element("mCounter", 5100, mapFor("c", "3")).row(900005400).element("mCounter", 5400).row(900005700).element("mCounter", 5700, mapFor("d", "4")).row(900006000).element("mCounter", 6000).row(900006300).element("mCounter", 6300).row(900006600).element("mCounter", 6600).row(900006900).element("mCounter", 6900).row(900007200).element("mCounter", // Thu Jul  9 13:00:00 CDT 1998
    7200).build();
    ResultDescriptor rDescriptor = new ResultDescriptor(Duration.seconds(300)).datasource("mCounter-avg", "mCounter", Duration.seconds(600), AVERAGE).export("mCounter-avg");
    Iterator<Row<Measurement>> expected = new MeasurementRowsBuilder(new Resource("localhost")).row(900003600).element("mCounter-avg", 1).row(900007200).element("mCounter-avg", 1).build();
    writeSamples(testSamples);
    Results<Measurement> results = getRepository().select(Context.DEFAULT_CONTEXT, new Resource("localhost"), Optional.of(Timestamp.fromEpochSeconds(900003600)), Optional.of(Timestamp.fromEpochSeconds(900007200)), rDescriptor, Optional.of(Duration.minutes(60)));
    // Validate results
    assertRowsEqual(expected, results.iterator());
    // Validate merged attributes
    Iterator<Row<Measurement>> rows = results.iterator();
    assertAttributes(rows.next().getElement("mCounter-avg"), mapFor("a", "1", "b", "2"));
    assertAttributes(rows.next().getElement("mCounter-avg"), mapFor("c", "3", "d", "4"));
}
Also used : Measurement(org.opennms.newts.api.Measurement) MeasurementRowsBuilder(org.opennms.newts.persistence.cassandra.Utils.MeasurementRowsBuilder) Resource(org.opennms.newts.api.Resource) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) Row(org.opennms.newts.api.Results.Row) SampleRowsBuilder(org.opennms.newts.persistence.cassandra.Utils.SampleRowsBuilder) Test(org.junit.Test)

Example 7 with ResultDescriptor

use of org.opennms.newts.api.query.ResultDescriptor in project opennms by OpenNMS.

the class NewtsConverterIT method execute.

private void execute(final boolean storeByGroup, final boolean storeByForeignSource, final boolean useRrdTool) throws Exception {
    final Path data = OPENNMS_HOME.resolve("share").resolve(useRrdTool ? "rrd" : "jrb").resolve(storeByGroup ? "sbg" : "sbm").resolve(storeByForeignSource ? "fs" : "id");
    assertTrue(Files.isDirectory(data));
    exit.expectSystemExitWithStatus(0);
    exit.checkAssertionAfterwards(() -> {
        assertThat(resourceStorageDao.exists(RESOURCE_PATH_SNMP, 0), is(true));
        assertThat(resourceStorageDao.getAttributes(RESOURCE_PATH_SNMP), hasItems(allOf(hasProperty("name", is("ifInOctets"))), allOf(hasProperty("name", is("ifSpeed")), hasProperty("value", is("1000")))));
        assertThat(resourceStorageDao.exists(RESOURCE_PATH_RESPONSE, 0), is(true));
        assertThat(resourceStorageDao.getAttributes(RESOURCE_PATH_RESPONSE), hasItems(allOf(hasProperty("name", is("icmp")))));
        final Results<Measurement> result = repository.select(Context.DEFAULT_CONTEXT, new Resource(NewtsUtils.toResourceId(ResourcePath.get(RESOURCE_PATH_SNMP, "mib2-interfaces"))), Optional.of(Timestamp.fromEpochSeconds(1414504800)), Optional.of(Timestamp.fromEpochSeconds(1417047045)), new ResultDescriptor(Duration.seconds(7200)).datasource("ifInOctets", StandardAggregationFunctions.AVERAGE).export("ifInOctets"), Optional.of(Duration.seconds(7200)));
        assertThat(result.getRows().size(), is(EXPECTED_DATA.length));
        int i = 0;
        for (Results.Row<Measurement> r : result) {
            final double deltaAbs = r.getElement("ifInOctets").getValue().doubleValue() - EXPECTED_DATA[i].value;
            final double deltaRel = deltaAbs / EXPECTED_DATA[i].value * 100.0;
            /* Use the following for debugging of non-matching entries...
                System.out.println(String.format(
                        "%4d: %-24s %11.2f %11.2f %11.2f (%6.2f) %36s|%-36s %f%%",
                        i,
                        r.getElement("ifInOctets").getTimestamp().asDate().toString(),
                        r.getElement("ifInOctets").getValue().doubleValue(),
                        EXPECTED_DATA[i].value,
                        deltaAbs,
                        deltaRel,
                        deltaAbs < -1.0 ? Strings.repeat("▒", (int) Math.abs(Math.log10(-deltaAbs) * 10.0)) : "",
                        deltaAbs >  1.0 ? Strings. repeat("▒", (int) Math.abs(Math.log10(deltaAbs) * 10.0)) : "",
                        Math.abs(deltaAbs / EXPECTED_DATA[i].value * 100.0)));
                 */
            assertThat(r.getTimestamp().asSeconds(), is(EXPECTED_DATA[i].timestamp));
            if (i != 270) {
                // We got some errors on the RRA boundaries - ignore them
                assertThat(r.getElement("ifInOctets").getValue().doubleValue(), is(anyOf(equalTo(EXPECTED_DATA[i].value), // Allow a relative error of 0.3%
                closeTo(EXPECTED_DATA[i].value, EXPECTED_DATA[i].value * 0.003))));
            }
            i++;
        }
    });
    NewtsConverter.main("-o", OPENNMS_HOME.toString(), "-r", data.toString(), "-t", Boolean.toString(useRrdTool), "-T", RRD_BINARY.toAbsolutePath().toString(), "-s", Boolean.toString(storeByGroup));
}
Also used : Path(java.nio.file.Path) ResourcePath(org.opennms.netmgt.model.ResourcePath) Measurement(org.opennms.newts.api.Measurement) Results(org.opennms.newts.api.Results) Resource(org.opennms.newts.api.Resource) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor)

Example 8 with ResultDescriptor

use of org.opennms.newts.api.query.ResultDescriptor in project opennms by OpenNMS.

the class NewtsFetchStrategy method getMeasurementsForResourceCallable.

private Callable<Collection<Row<Measurement>>> getMeasurementsForResourceCallable(final String newtsResourceId, final List<Source> listOfSources, final Optional<Timestamp> start, final Optional<Timestamp> end, final LateAggregationParams lag) {
    return new Callable<Collection<Row<Measurement>>>() {

        @Override
        public Collection<Row<Measurement>> call() throws Exception {
            ResultDescriptor resultDescriptor = new ResultDescriptor(lag.getInterval());
            for (Source source : listOfSources) {
                final String metricName = source.getAttribute();
                final String name = source.getLabel();
                final AggregationFunction fn = toAggregationFunction(source.getAggregation());
                resultDescriptor.datasource(name, metricName, lag.getHeartbeat(), fn);
                resultDescriptor.export(name);
            }
            LOG.debug("Querying Newts for resource id {} with result descriptor: {}", newtsResourceId, resultDescriptor);
            Results<Measurement> results = m_sampleRepository.select(m_context, new Resource(newtsResourceId), start, end, resultDescriptor, Optional.of(Duration.millis(lag.getStep())), limitConcurrentAggregationsCallback);
            Collection<Row<Measurement>> rows = results.getRows();
            LOG.debug("Found {} rows.", rows.size());
            return rows;
        }
    };
}
Also used : AggregationFunction(org.opennms.newts.api.query.AggregationFunction) Measurement(org.opennms.newts.api.Measurement) OnmsResource(org.opennms.netmgt.model.OnmsResource) Resource(org.opennms.newts.api.Resource) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) Row(org.opennms.newts.api.Results.Row) Callable(java.util.concurrent.Callable) Source(org.opennms.netmgt.measurements.model.Source)

Example 9 with ResultDescriptor

use of org.opennms.newts.api.query.ResultDescriptor in project newts by OpenNMS.

the class Transform method resultDescriptor.

/**
     * Convert a {@link ResultDescriptorDTO} to {@link ResultDescriptor}.
     *
     * @param rDescriptorDTO
     *            the DTO to transform
     * @return the corresponding descriptor
     */
static ResultDescriptor resultDescriptor(ResultDescriptorDTO rDescriptorDTO) {
    ResultDescriptor rDescriptor = new ResultDescriptor(rDescriptorDTO.getInterval());
    for (ResultDescriptorDTO.Datasource ds : rDescriptorDTO.getDatasources()) {
        if (ds.getHeartbeat() != null) {
            rDescriptor.datasource(ds.getLabel(), ds.getSource(), ds.getHeartbeat(), ds.getFunction());
        } else {
            rDescriptor.datasource(ds.getLabel(), ds.getSource(), ds.getFunction());
        }
    }
    for (ResultDescriptorDTO.Expression expr : rDescriptorDTO.getExpressions()) {
        rDescriptor.expression(expr.getLabel(), expr.getExpression());
    }
    rDescriptor.export(rDescriptorDTO.getExports());
    return rDescriptor;
}
Also used : ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor)

Example 10 with ResultDescriptor

use of org.opennms.newts.api.query.ResultDescriptor in project newts by OpenNMS.

the class InsertSelectMeasurementsITCase method testWithAbsolute.

@Test
public void testWithAbsolute() {
    Iterator<Row<Sample>> testSamples = new SampleRowsBuilder(new Resource("localhost"), MetricType.ABSOLUTE).row(900000000).element("mAbsolute", // Thu Jul  9 11:00:00 CDT 1998
    300).row(900000300).element("mAbsolute", 300).row(900000600).element("mAbsolute", 300).row(900000900).element("mAbsolute", 300).row(900001200).element("mAbsolute", 300).row(900001500).element("mAbsolute", 300).row(900001800).element("mAbsolute", 300, mapFor("a", "1")).row(900002100).element("mAbsolute", 300).row(900002400).element("mAbsolute", 300, mapFor("b", "2")).row(900002700).element("mAbsolute", 300).row(900003000).element("mAbsolute", 300).row(900003300).element("mAbsolute", 300).row(900003600).element("mAbsolute", 300).row(900003900).element("mAbsolute", 300).row(900004200).element("mAbsolute", 300).row(900004500).element("mAbsolute", 300).row(900004800).element("mAbsolute", 300).row(900005100).element("mAbsolute", 300, mapFor("c", "3")).row(900005400).element("mAbsolute", 300).row(900005700).element("mAbsolute", 300, mapFor("d", "4")).row(900006000).element("mAbsolute", 300).row(900006300).element("mAbsolute", 300).row(900006600).element("mAbsolute", 300).row(900006900).element("mAbsolute", 300).row(900007200).element("mAbsolute", // Thu Jul  9 13:00:00 CDT 1998
    300).build();
    ResultDescriptor rDescriptor = new ResultDescriptor(Duration.seconds(300)).datasource("mAbsolute-avg", "mAbsolute", Duration.seconds(600), AVERAGE).export("mAbsolute-avg");
    Iterator<Row<Measurement>> expected = new MeasurementRowsBuilder(new Resource("localhost")).row(900003600).element("mAbsolute-avg", 1).row(900007200).element("mAbsolute-avg", 1).build();
    writeSamples(testSamples);
    Results<Measurement> results = getRepository().select(Context.DEFAULT_CONTEXT, new Resource("localhost"), Optional.of(Timestamp.fromEpochSeconds(900003600)), Optional.of(Timestamp.fromEpochSeconds(900007200)), rDescriptor, Optional.of(Duration.minutes(60)));
    // Validate results
    assertRowsEqual(expected, results.iterator());
    // Validate merged attributes
    Iterator<Row<Measurement>> rows = results.iterator();
    assertAttributes(rows.next().getElement("mAbsolute-avg"), mapFor("a", "1", "b", "2"));
    assertAttributes(rows.next().getElement("mAbsolute-avg"), mapFor("c", "3", "d", "4"));
}
Also used : Measurement(org.opennms.newts.api.Measurement) MeasurementRowsBuilder(org.opennms.newts.persistence.cassandra.Utils.MeasurementRowsBuilder) Resource(org.opennms.newts.api.Resource) ResultDescriptor(org.opennms.newts.api.query.ResultDescriptor) Row(org.opennms.newts.api.Results.Row) SampleRowsBuilder(org.opennms.newts.persistence.cassandra.Utils.SampleRowsBuilder) Test(org.junit.Test)

Aggregations

ResultDescriptor (org.opennms.newts.api.query.ResultDescriptor)33 Resource (org.opennms.newts.api.Resource)30 Row (org.opennms.newts.api.Results.Row)29 Test (org.junit.Test)28 MeasurementRowsBuilder (org.opennms.newts.aggregate.Utils.MeasurementRowsBuilder)20 SampleRowsBuilder (org.opennms.newts.aggregate.Utils.SampleRowsBuilder)17 Measurement (org.opennms.newts.api.Measurement)11 MeasurementRowsBuilder (org.opennms.newts.persistence.cassandra.Utils.MeasurementRowsBuilder)5 SampleRowsBuilder (org.opennms.newts.persistence.cassandra.Utils.SampleRowsBuilder)5 Timed (com.codahale.metrics.annotation.Timed)1 Path (java.nio.file.Path)1 Callable (java.util.concurrent.Callable)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Source (org.opennms.netmgt.measurements.model.Source)1 OnmsResource (org.opennms.netmgt.model.OnmsResource)1 ResourcePath (org.opennms.netmgt.model.ResourcePath)1 Context (org.opennms.newts.api.Context)1 Duration (org.opennms.newts.api.Duration)1 Results (org.opennms.newts.api.Results)1