Search in sources :

Example 31 with Sample

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

the class DriverAdapter method next.

@Override
public Results.Row<Sample> next() {
    if (!hasNext())
        throw new NoSuchElementException();
    Results.Row<Sample> nextNext = null;
    while (m_results.hasNext()) {
        Sample m = getNextSample();
        if (m.getTimestamp().gt(m_next.getTimestamp())) {
            nextNext = new Results.Row<>(m.getTimestamp(), m.getResource());
            addSample(nextNext, m);
            break;
        }
        addSample(m_next, m);
    }
    try {
        return m_next;
    } finally {
        m_next = nextNext;
    }
}
Also used : Results(org.opennms.newts.api.Results) Sample(org.opennms.newts.api.Sample) NoSuchElementException(java.util.NoSuchElementException)

Example 32 with Sample

use of org.opennms.newts.api.Sample 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)

Example 33 with Sample

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

the class CassandraIndexer method update.

@Override
public void update(Collection<Sample> samples) {
    Timer.Context ctx = m_updateTimer.time();
    Set<StatementGenerator> generators = Sets.newHashSet();
    Map<Context, Map<Resource, ResourceMetadata>> cacheQueue = Maps.newHashMap();
    for (Sample sample : samples) {
        maybeIndexResource(cacheQueue, generators, sample.getContext(), sample.getResource());
        maybeIndexResourceAttributes(cacheQueue, generators, sample.getContext(), sample.getResource());
        maybeAddMetricName(cacheQueue, generators, sample.getContext(), sample.getResource(), sample.getName());
    }
    try {
        if (!generators.isEmpty()) {
            synchronized (statementsInFlight) {
                generators.removeAll(statementsInFlight);
                statementsInFlight.addAll(generators);
            }
            m_inserts.mark(generators.size());
            // Asynchronously execute the statements
            List<ResultSetFuture> futures = Lists.newArrayList();
            for (Statement statementToExecute : toStatements(generators)) {
                futures.add(m_session.executeAsync(statementToExecute));
            }
            for (ResultSetFuture future : futures) {
                future.getUninterruptibly();
            }
        }
        // Order matters here; We want the cache updated only after a successful Cassandra write.
        for (Context context : cacheQueue.keySet()) {
            for (Map.Entry<Resource, ResourceMetadata> entry : cacheQueue.get(context).entrySet()) {
                m_cache.merge(context, entry.getKey(), entry.getValue());
            }
        }
    } finally {
        synchronized (statementsInFlight) {
            statementsInFlight.removeAll(generators);
        }
        ctx.stop();
    }
}
Also used : Context(org.opennms.newts.api.Context) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) Sample(org.opennms.newts.api.Sample) RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) Resource(org.opennms.newts.api.Resource) StatementGenerator(org.opennms.newts.cassandra.search.support.StatementGenerator) Timer(com.codahale.metrics.Timer) Map(java.util.Map)

Example 34 with Sample

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

the class Rate method next.

@Override
public Row<Sample> next() {
    if (!hasNext())
        throw new NoSuchElementException();
    Row<Sample> working = m_input.next();
    Row<Sample> result = new Row<>(working.getTimestamp(), working.getResource());
    for (String metricName : m_metrics) {
        Sample sample = working.getElement(metricName);
        if (sample == null) {
            continue;
        }
        // Use rate as result if one of counter types, else pass through as-is.
        result.addElement(COUNTERS.contains(sample.getType()) ? getRate(sample) : sample);
        m_prevSamples.put(sample.getName(), sample);
    }
    return result;
}
Also used : Sample(org.opennms.newts.api.Sample) Row(org.opennms.newts.api.Results.Row) NoSuchElementException(java.util.NoSuchElementException)

Example 35 with Sample

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

the class NewtsConverter method toSample.

protected static Sample toSample(AbstractDS ds, Resource resource, Timestamp timestamp, double value) {
    final String metric = ds.getName();
    final MetricType type = ds.isCounter() ? MetricType.COUNTER : MetricType.GAUGE;
    final ValueType<?> valueType = ds.isCounter() ? new Counter(UnsignedLong.valueOf(BigDecimal.valueOf(value).toBigInteger())) : new Gauge(value);
    return new Sample(timestamp, resource, metric, type, valueType);
}
Also used : Counter(org.opennms.newts.api.Counter) Sample(org.opennms.newts.api.Sample) MetricType(org.opennms.newts.api.MetricType) Gauge(org.opennms.newts.api.Gauge)

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