Search in sources :

Example 6 with Results

use of org.opennms.newts.api.Results in project newts by OpenNMS.

the class ResultSerializationTest method testSamples.

@Test
public void testSamples() throws JsonProcessingException {
    // Use the optional attributes map at least once.
    Map<String, String> attributes = Maps.newHashMap();
    attributes.put("units", "bytes");
    Results<Sample> data = new Results<>();
    data.addElement(new Sample(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifInOctets", COUNTER, ValueType.compose(5000, COUNTER)));
    data.addElement(new Sample(Timestamp.fromEpochSeconds(900000000), new Resource("localhost"), "ifOutOctets", COUNTER, ValueType.compose(6000, COUNTER), attributes));
    data.addElement(new Sample(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifInOctets", COUNTER, ValueType.compose(6000, COUNTER)));
    data.addElement(new Sample(Timestamp.fromEpochSeconds(900000300), new Resource("localhost"), "ifOutOctets", COUNTER, ValueType.compose(7000, COUNTER)));
    String json = "[" + "  [" + "    {" + "      \"name\": \"ifOutOctets\"," + "      \"timestamp\":900000000000," + "      \"type\":\"COUNTER\"," + "      \"value\":6000," + "      \"attributes\":{\"units\":\"bytes\"}" + "    }," + "    {" + "      \"name\": \"ifInOctets\"," + "      \"timestamp\":900000000000," + "      \"type\":\"COUNTER\"," + "      \"value\":5000" + "    }" + "  ]," + "  [" + "    {" + "      \"name\": \"ifOutOctets\"," + "      \"timestamp\":900000300000," + "      \"type\":\"COUNTER\"," + "      \"value\":7000" + "    }," + "    {" + "      \"name\": \"ifInOctets\"," + "      \"timestamp\":900000300000," + "      \"type\":\"COUNTER\"," + "      \"value\":6000" + "    }" + "  ]" + "]";
    assertThat(new ObjectMapper().writeValueAsString(Transform.sampleDTOs(data)), is(normalize(json)));
}
Also used : Results(org.opennms.newts.api.Results) SearchResults(org.opennms.newts.api.search.SearchResults) Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 7 with Results

use of org.opennms.newts.api.Results in project newts by OpenNMS.

the class CassandraSampleRepository method select.

@Override
public Results<Sample> select(Context context, Resource resource, Optional<Timestamp> start, Optional<Timestamp> end) {
    Timer.Context timer = m_sampleSelectTimer.time();
    validateSelect(start, end);
    Timestamp upper = end.isPresent() ? end.get() : Timestamp.now();
    Timestamp lower = start.isPresent() ? start.get() : upper.minus(Duration.seconds(86400));
    LOG.debug("Querying database for resource {}, from {} to {}", resource, lower, upper);
    Results<Sample> samples = new Results<>();
    DriverAdapter driverAdapter = new DriverAdapter(cassandraSelect(context, resource, lower, upper));
    for (Row<Sample> row : driverAdapter) {
        samples.addRow(row);
    }
    LOG.debug("{} results returned from database", driverAdapter.getResultCount());
    m_samplesSelected.mark(driverAdapter.getResultCount());
    try {
        return samples;
    } finally {
        timer.stop();
    }
}
Also used : Timer(com.codahale.metrics.Timer) Results(org.opennms.newts.api.Results) Sample(org.opennms.newts.api.Sample) Timestamp(org.opennms.newts.api.Timestamp)

Example 8 with Results

use of org.opennms.newts.api.Results in project newts by OpenNMS.

the class RateTest method test.

@Test
public void test() {
    Results<Sample> input = new Results<>();
    int rows = 10, cols = 2, rate = 100;
    for (int i = 1; i <= rows; i++) {
        Timestamp t = Timestamp.fromEpochMillis(i * 1000);
        for (int j = 0; j < cols; j++) {
            input.addElement(new Sample(t, m_resource, m_metrics[j], COUNTER, new Counter((i + j) * rate)));
        }
    }
    Iterator<Results.Row<Sample>> output = new Rate(input.iterator(), getMetrics(cols)).iterator();
    for (int i = 1; i <= rows; i++) {
        assertTrue("Insufficient number of results", output.hasNext());
        Results.Row<Sample> row = output.next();
        assertEquals("Unexpected row timestamp", Timestamp.fromEpochMillis(i * 1000), row.getTimestamp());
        assertEquals("Unexpected row resource", m_resource, row.getResource());
        assertEquals("Unexpected number of columns", cols, row.getElements().size());
        for (int j = 0; j < cols; j++) {
            String name = m_metrics[j];
            assertNotNull("Missing sample" + name, row.getElement(name));
            assertEquals("Unexpected sample name", name, row.getElement(name).getName());
            assertEquals("Unexpected sample type", GAUGE, row.getElement(name).getType());
            // Samples in the first row are null, this is normal.
            if (i != 1) {
                assertEquals("Incorrect rate value", 100.0d, row.getElement(name).getValue().doubleValue(), 0.0d);
            }
        }
    }
}
Also used : Counter(org.opennms.newts.api.Counter) Results(org.opennms.newts.api.Results) Sample(org.opennms.newts.api.Sample) Rate(org.opennms.newts.aggregate.Rate) Row(org.opennms.newts.api.Results.Row) Timestamp(org.opennms.newts.api.Timestamp) Test(org.junit.Test)

Aggregations

Results (org.opennms.newts.api.Results)8 Resource (org.opennms.newts.api.Resource)5 Test (org.junit.Test)4 Measurement (org.opennms.newts.api.Measurement)4 Row (org.opennms.newts.api.Results.Row)4 Timestamp (org.opennms.newts.api.Timestamp)4 Sample (org.opennms.newts.api.Sample)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)2 Source (org.opennms.netmgt.measurements.model.Source)2 OnmsResource (org.opennms.netmgt.model.OnmsResource)2 ResourceId (org.opennms.netmgt.model.ResourceId)2 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)2 Rate (org.opennms.newts.aggregate.Rate)2 Duration (org.opennms.newts.api.Duration)2 ResultDescriptor (org.opennms.newts.api.query.ResultDescriptor)2 Timer (com.codahale.metrics.Timer)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 Strings (com.google.common.base.Strings)1