Search in sources :

Example 1 with Sample

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

the class PrimaryData method next.

@Override
public Row<Measurement> next() {
    if (!hasNext())
        throw new NoSuchElementException();
    Timestamp intervalCeiling = m_timestamps.next();
    Row<Measurement> output = new Row<>(intervalCeiling, m_resource);
    for (Datasource ds : m_resultDescriptor.getDatasources().values()) {
        Accumulation accumulation = getOrCreateAccumulation(ds.getSource());
        accumulation.reset();
        int lastSampleIdx = 0;
        if (m_lastSampleIndex.containsKey(ds.getSource())) {
            lastSampleIdx = m_lastSampleIndex.get(ds.getSource());
        }
        Sample last = null;
        for (int sampleIdx = lastSampleIdx; sampleIdx < m_samples.size(); sampleIdx++) {
            Row<Sample> row = m_samples.get(sampleIdx);
            Sample current;
            current = row.getElement(ds.getSource());
            // Skip the row if it does not contain a sample for the current datasource
            if (current == null) {
                continue;
            }
            if (last == null) {
                last = current;
                lastSampleIdx = sampleIdx;
                continue;
            }
            // Accumulate nothing when samples are beyond this interval
            if (intervalCeiling.lt(last.getTimestamp())) {
                break;
            }
            Timestamp lowerBound = last.getTimestamp();
            if (lastIntervalCeiling != null && lastIntervalCeiling.gt(lowerBound)) {
                lowerBound = lastIntervalCeiling;
            }
            Timestamp upperBound = current.getTimestamp();
            if (intervalCeiling.lt(upperBound)) {
                upperBound = intervalCeiling;
            }
            if (lowerBound.gt(upperBound)) {
                lowerBound = upperBound;
            }
            Duration elapsedWithinInterval = upperBound.minus(lowerBound);
            Duration elapsedBetweenSamples = current.getTimestamp().minus(last.getTimestamp());
            m_lastSampleIndex.put(ds.getSource(), lastSampleIdx);
            accumulation.accumulateValue(elapsedWithinInterval, elapsedBetweenSamples, ds.getHeartbeat(), current.getValue()).accumlateAttrs(current.getAttributes());
            last = current;
            lastSampleIdx = sampleIdx;
        }
        // Add sample with accumulated value to output row
        output.addElement(new Measurement(output.getTimestamp(), output.getResource(), ds.getSource(), accumulation.getAverage(), accumulation.getAttributes()));
    }
    lastIntervalCeiling = intervalCeiling;
    return output;
}
Also used : Measurement(org.opennms.newts.api.Measurement) Datasource(org.opennms.newts.api.query.Datasource) Sample(org.opennms.newts.api.Sample) Duration(org.opennms.newts.api.Duration) Row(org.opennms.newts.api.Results.Row) Timestamp(org.opennms.newts.api.Timestamp) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with Sample

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

the class Rate method getRate.

private Sample getRate(Sample sample) {
    ValueType<?> value = NAN;
    Sample previous = m_prevSamples.get(sample.getName());
    if (previous != null) {
        long elapsed = sample.getTimestamp().asSeconds() - previous.getTimestamp().asSeconds();
        value = new Gauge(sample.getValue().delta(previous.getValue()).doubleValue() / elapsed);
    }
    return new Sample(sample.getTimestamp(), sample.getResource(), sample.getName(), GAUGE, value, sample.getAttributes());
}
Also used : Sample(org.opennms.newts.api.Sample) Gauge(org.opennms.newts.api.Gauge)

Example 3 with Sample

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

the class RateTest method testMissing.

@Test
public void testMissing() {
    Results<Sample> input = new Results<>();
    Timestamp start = Timestamp.fromEpochMillis(1000);
    Duration step = Duration.seconds(1);
    // row_1
    input.addElement(new Sample(start, m_resource, m_metrics[0], COUNTER, new Counter(0)));
    input.addElement(new Sample(start, m_resource, m_metrics[1], COUNTER, new Counter(0)));
    // row_2
    input.addElement(new Sample(start.plus(step), m_resource, m_metrics[0], COUNTER, new Counter(100)));
    input.addElement(new Sample(start.plus(step), m_resource, m_metrics[1], COUNTER, new Counter(100)));
    // row_3 (sample for m_metrics[0] missing)
    input.addElement(new Sample(start.plus(step.times(2)), m_resource, m_metrics[1], COUNTER, new Counter(200)));
    // row_4
    input.addElement(new Sample(start.plus(step.times(3)), m_resource, m_metrics[0], COUNTER, new Counter(300)));
    input.addElement(new Sample(start.plus(step.times(3)), m_resource, m_metrics[1], COUNTER, new Counter(300)));
    Iterator<Results.Row<Sample>> output = new Rate(input.iterator(), getMetrics(2)).iterator();
    // result_1 is always null
    assertTrue(output.hasNext());
    assertEquals(new Gauge(Double.NaN), output.next().getElement(m_metrics[0]).getValue());
    // result_2, rate 100
    assertTrue(output.hasNext());
    assertEquals(100.0d, output.next().getElement(m_metrics[0]).getValue().doubleValue(), 0.0d);
    // result_3, missing because sample in row_3 is missing
    assertTrue(output.hasNext());
    assertNull(output.next().getElement(m_metrics[0]));
    // result_4, rate of 100 calculated between row_4 and row_2 
    assertTrue(output.hasNext());
    assertEquals(100.0d, output.next().getElement(m_metrics[0]).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) Duration(org.opennms.newts.api.Duration) Row(org.opennms.newts.api.Results.Row) Timestamp(org.opennms.newts.api.Timestamp) Gauge(org.opennms.newts.api.Gauge) Test(org.junit.Test)

Example 4 with Sample

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

the class CassandraIndexerITCase method testDelete.

@Test
public void testDelete() {
    ResourceMetadataCache cache = mock(ResourceMetadataCache.class);
    when(cache.get(any(Context.class), any(Resource.class))).thenReturn(Optional.<ResourceMetadata>absent());
    MetricRegistry registry = new MetricRegistry();
    ContextConfigurations contextConfigurations = new ContextConfigurations();
    CassandraSession session = newtsInstance.getCassandraSession();
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(false).build();
    Indexer indexer = new CassandraIndexer(session, 86400, cache, registry, options, new SimpleResourceIdSplitter(), contextConfigurations);
    CassandraSearcher searcher = new CassandraSearcher(session, registry, contextConfigurations);
    Map<String, String> base = map("meat", "people", "bread", "beer");
    List<Sample> samples = Lists.newArrayList();
    samples.add(sampleFor(new Resource("aaa", Optional.of(base)), "m0"));
    samples.add(sampleFor(new Resource("aab", Optional.of(map(base, "music", "metal", "beverage", "beer"))), "m0"));
    samples.add(sampleFor(new Resource("aac:aaa", Optional.of(map(base, "music", "country"))), "m0"));
    indexer.update(samples);
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(2));
    indexer.delete(Context.DEFAULT_CONTEXT, new Resource("aaa", Optional.of(base)));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(1));
    indexer.delete(Context.DEFAULT_CONTEXT, new Resource("aaa", Optional.of(base)));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(1));
    indexer.delete(Context.DEFAULT_CONTEXT, new Resource("aac:aaa", Optional.of(base)));
    assertThat(searcher.search(Context.DEFAULT_CONTEXT, QueryBuilder.matchAnyValue("aaa")).size(), equalTo(0));
}
Also used : Context(org.opennms.newts.api.Context) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) QueryBuilder(org.opennms.newts.api.search.QueryBuilder) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) Indexer(org.opennms.newts.api.search.Indexer) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) Test(org.junit.Test)

Example 5 with Sample

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

the class CassandraIndexerTest method insertStatementsAreDeduplicatedWhenIndexingManySamples.

@Test
public void insertStatementsAreDeduplicatedWhenIndexingManySamples() {
    CassandraSession session = mock(CassandraSession.class);
    ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class);
    when(session.executeAsync(statementCaptor.capture())).thenReturn(mock(ResultSetFuture.class));
    PreparedStatement statement = mock(PreparedStatement.class);
    BoundStatement boundStatement = mock(BoundStatement.class);
    when(session.prepare(any(RegularStatement.class))).thenReturn(statement);
    when(statement.bind()).thenReturn(boundStatement);
    when(boundStatement.setString(any(String.class), any(String.class))).thenReturn(boundStatement);
    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder().withHierarchicalIndexing(true).withMaxBatchSize(1).build();
    MetricRegistry registry = new MetricRegistry();
    GuavaResourceMetadataCache cache = new GuavaResourceMetadataCache(2048, registry);
    CassandraIndexer indexer = new CassandraIndexer(session, 0, cache, registry, options, new EscapableResourceIdSplitter(), new ContextConfigurations());
    Resource r = new Resource("snmp:1589:vmware5Cpu:2:vmware5Cpu");
    List<Sample> samples = Lists.newArrayList();
    samples.add(new Sample(Timestamp.now(), r, "CpuCostopSum", MetricType.GAUGE, new Gauge(0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuIdleSum", MetricType.GAUGE, new Gauge(19299.0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuMaxLdSum", MetricType.GAUGE, new Gauge(0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuOverlapSum", MetricType.GAUGE, new Gauge(5.0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuRdySum", MetricType.GAUGE, new Gauge(41.0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuRunSum", MetricType.GAUGE, new Gauge(619.0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuSpwaitSum", MetricType.GAUGE, new Gauge(0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuSystemSum", MetricType.GAUGE, new Gauge(0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuUsagemhzAvg", MetricType.GAUGE, new Gauge(32.0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuUsedSum", MetricType.GAUGE, new Gauge(299.0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuWaitSum", MetricType.GAUGE, new Gauge(19343)));
    // Index the collection of samples
    indexer.update(samples);
    // Verify the number of exectuteAsync calls
    verify(session, times(20)).executeAsync(any(Statement.class));
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) Sample(org.opennms.newts.api.Sample) MetricRegistry(com.codahale.metrics.MetricRegistry) Resource(org.opennms.newts.api.Resource) CassandraSession(org.opennms.newts.cassandra.CassandraSession) PreparedStatement(com.datastax.driver.core.PreparedStatement) RegularStatement(com.datastax.driver.core.RegularStatement) Gauge(org.opennms.newts.api.Gauge) ContextConfigurations(org.opennms.newts.cassandra.ContextConfigurations) BoundStatement(com.datastax.driver.core.BoundStatement) Test(org.junit.Test)

Aggregations

Sample (org.opennms.newts.api.Sample)37 Resource (org.opennms.newts.api.Resource)18 Test (org.junit.Test)14 Timestamp (org.opennms.newts.api.Timestamp)12 MetricRegistry (com.codahale.metrics.MetricRegistry)9 Gauge (org.opennms.newts.api.Gauge)6 ContextConfigurations (org.opennms.newts.cassandra.ContextConfigurations)6 Timer (com.codahale.metrics.Timer)5 List (java.util.List)5 Counter (org.opennms.newts.api.Counter)5 Row (org.opennms.newts.api.Results.Row)5 CassandraSession (org.opennms.newts.cassandra.CassandraSession)5 Map (java.util.Map)4 Context (org.opennms.newts.api.Context)4 Results (org.opennms.newts.api.Results)4 Meter (com.codahale.metrics.Meter)3 BoundStatement (com.datastax.driver.core.BoundStatement)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)3 RegularStatement (com.datastax.driver.core.RegularStatement)3 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)3