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;
}
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);
}
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));
}
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));
}
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();
}
}
Aggregations