Search in sources :

Example 1 with Timestamp

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

use of org.opennms.newts.api.Timestamp 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 3 with Timestamp

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

the class IntervalGeneratorTest method test.

@Test
public void test() {
    List<Timestamp> timestamps = Lists.newArrayList(getTimestamps(150, 3500));
    assertEquals(13, timestamps.size());
    assertEquals(new Timestamp(0, TimeUnit.SECONDS), timestamps.get(0));
    assertEquals(new Timestamp(3300, TimeUnit.SECONDS), timestamps.get(11));
    assertEquals(new Timestamp(3600, TimeUnit.SECONDS), timestamps.get(12));
    timestamps = Lists.newArrayList(getTimestamps(0, 3600));
    assertEquals(13, timestamps.size());
    assertEquals(new Timestamp(0, TimeUnit.SECONDS), timestamps.get(0));
    assertEquals(new Timestamp(3600, TimeUnit.SECONDS), timestamps.get(12));
}
Also used : Timestamp(org.opennms.newts.api.Timestamp) Test(org.junit.Test)

Example 4 with Timestamp

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

the class IntervalGeneratorTest method testReversed.

@Test
public void testReversed() {
    List<Timestamp> timestamps = Lists.newArrayList(getTimestamps(150, 3500, true));
    assertEquals(13, timestamps.size());
    assertEquals(new Timestamp(0, TimeUnit.SECONDS), timestamps.get(12));
    assertEquals(new Timestamp(3300, TimeUnit.SECONDS), timestamps.get(1));
    assertEquals(new Timestamp(3600, TimeUnit.SECONDS), timestamps.get(0));
    timestamps = Lists.newArrayList(getTimestamps(0, 3600, true));
    assertEquals(13, timestamps.size());
    assertEquals(new Timestamp(0, TimeUnit.SECONDS), timestamps.get(12));
    assertEquals(new Timestamp(3600, TimeUnit.SECONDS), timestamps.get(0));
}
Also used : Timestamp(org.opennms.newts.api.Timestamp) Test(org.junit.Test)

Example 5 with Timestamp

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

the class CassandraSampleRepository method insert.

@Override
public void insert(Collection<Sample> samples, boolean calculateTimeToLive) {
    Timer.Context timer = m_insertTimer.time();
    Timestamp now = Timestamp.now();
    Batch batch = unloggedBatch();
    for (Sample m : samples) {
        int ttl = m_ttl;
        if (calculateTimeToLive) {
            ttl -= (int) (now.asSeconds() - m.getTimestamp().asSeconds());
            if (ttl <= 0) {
                LOG.debug("Skipping expired sample: {}", m);
                continue;
            }
        }
        Duration resourceShard = m_contextConfigurations.getResourceShard(m.getContext());
        Insert insert = insertInto(SchemaConstants.T_SAMPLES).value(SchemaConstants.F_CONTEXT, m.getContext().getId()).value(SchemaConstants.F_PARTITION, m.getTimestamp().stepFloor(resourceShard).asSeconds()).value(SchemaConstants.F_RESOURCE, m.getResource().getId()).value(SchemaConstants.F_COLLECTED, m.getTimestamp().asMillis()).value(SchemaConstants.F_METRIC_NAME, m.getName()).value(SchemaConstants.F_VALUE, ValueType.decompose(m.getValue()));
        // for any sample that has not specified them.
        if (m.getAttributes() != null) {
            insert.value(SchemaConstants.F_ATTRIBUTES, m.getAttributes());
        }
        // Use the context specific consistency level
        insert.setConsistencyLevel(m_contextConfigurations.getWriteConsistency(m.getContext()));
        batch.add(insert.using(ttl(ttl)));
    }
    try {
        m_session.execute(batch);
        if (m_processorService != null) {
            m_processorService.submit(samples);
        }
        m_samplesInserted.mark(samples.size());
    } finally {
        timer.stop();
    }
}
Also used : Timer(com.codahale.metrics.Timer) Batch(com.datastax.driver.core.querybuilder.Batch) QueryBuilder.unloggedBatch(com.datastax.driver.core.querybuilder.QueryBuilder.unloggedBatch) Sample(org.opennms.newts.api.Sample) Duration(org.opennms.newts.api.Duration) Insert(com.datastax.driver.core.querybuilder.Insert) Timestamp(org.opennms.newts.api.Timestamp)

Aggregations

Timestamp (org.opennms.newts.api.Timestamp)24 Sample (org.opennms.newts.api.Sample)12 Duration (org.opennms.newts.api.Duration)8 Resource (org.opennms.newts.api.Resource)8 Test (org.junit.Test)7 Row (org.opennms.newts.api.Results.Row)5 Timer (com.codahale.metrics.Timer)4 Results (org.opennms.newts.api.Results)4 Map (java.util.Map)3 Future (java.util.concurrent.Future)3 IntervalGenerator (org.opennms.newts.aggregate.IntervalGenerator)3 Context (org.opennms.newts.api.Context)3 Gauge (org.opennms.newts.api.Gauge)3 Measurement (org.opennms.newts.api.Measurement)3 Timed (com.codahale.metrics.annotation.Timed)2 BoundStatement (com.datastax.driver.core.BoundStatement)2 List (java.util.List)2 SortedMap (java.util.SortedMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 ResourcePath (org.opennms.netmgt.model.ResourcePath)2