use of org.opennms.newts.api.Duration 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.Duration 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.Duration 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();
}
}
use of org.opennms.newts.api.Duration in project newts by OpenNMS.
the class CassandraSampleRepository method delete.
@Override
public void delete(Context context, Resource resource) {
/**
* Check for ttl value > 0
*/
if (m_ttl > 0) {
/**
* Delete exactly from (now - ttl) till now
*/
final Timestamp start = Timestamp.now().minus(m_ttl, TimeUnit.SECONDS);
final Timestamp end = Timestamp.now();
final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
final List<Future<ResultSet>> futures = Lists.newArrayList();
for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
BoundStatement bindStatement = m_deleteStatement.bind();
bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
futures.add(m_session.executeAsync(bindStatement));
}
for (final Future<ResultSet> future : futures) {
try {
future.get();
} catch (final InterruptedException | ExecutionException e) {
throw Throwables.propagate(e);
}
}
} else {
// Choose (now - one year) till now...
Timestamp end = Timestamp.now();
Timestamp start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
// ... and check whether samples exist for this period of time.
while (cassandraSelect(context, resource, start, end).hasNext()) {
// Now delete the samples...
final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
final List<Future<ResultSet>> futures = Lists.newArrayList();
for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
BoundStatement bindStatement = m_deleteStatement.bind();
bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
futures.add(m_session.executeAsync(bindStatement));
}
for (final Future<ResultSet> future : futures) {
try {
future.get();
} catch (final InterruptedException | ExecutionException e) {
throw Throwables.propagate(e);
}
}
// ...set end to start and start to (end - one year)
end = start;
start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
// and start over again until no more samples are found
}
}
}
use of org.opennms.newts.api.Duration in project newts by OpenNMS.
the class MeasurementsResource method getMeasurements.
@POST
@Path("/{resource}")
@Timed
public Collection<Collection<MeasurementDTO>> getMeasurements(ResultDescriptorDTO descriptorDTO, @PathParam("resource") Resource resource, @QueryParam("start") Optional<TimestampParam> start, @QueryParam("end") Optional<TimestampParam> end, @QueryParam("resolution") Optional<DurationParam> resolution, @QueryParam("context") Optional<String> contextId) {
Optional<Timestamp> lower = Transform.toTimestamp(start);
Optional<Timestamp> upper = Transform.toTimestamp(end);
Optional<Duration> step = Transform.toDuration(resolution);
Context context = contextId.isPresent() ? new Context(contextId.get()) : Context.DEFAULT_CONTEXT;
LOG.debug("Retrieving measurements for resource {}, from {} to {} w/ resolution {} and w/ report {}", resource, lower, upper, step, descriptorDTO);
ResultDescriptor rDescriptor = Transform.resultDescriptor(descriptorDTO);
return Transform.measurementDTOs(m_repository.select(context, resource, lower, upper, rDescriptor, step));
}
Aggregations