use of org.opennms.newts.api.Timestamp in project opennms by OpenNMS.
the class NewtsPersisterIT method canPersist.
@Test
public void canPersist() throws InterruptedException {
ServiceParameters params = new ServiceParameters(Collections.emptyMap());
RrdRepository repo = new RrdRepository();
// Only the last element of the path matters here
repo.setRrdBaseDir(Paths.get("a", "path", "that", "ends", "with", "snmp").toFile());
Persister persister = m_persisterFactory.createPersister(params, repo);
int nodeId = 1;
CollectionAgent agent = mock(CollectionAgent.class);
when(agent.getStorageResourcePath()).thenReturn(ResourcePath.get(Integer.toString(nodeId)));
NodeLevelResource nodeLevelResource = new NodeLevelResource(nodeId);
// Build a collection set with a single sample
Timestamp now = Timestamp.now();
CollectionSet collectionSet = new CollectionSetBuilder(agent).withNumericAttribute(nodeLevelResource, "metrics", "metric", 900, AttributeType.GAUGE).withTimestamp(now.asDate()).build();
// Persist
collectionSet.visit(persister);
// Wait for the sample(s) to be flushed
Thread.sleep(5 * 1000);
// Fetch the (persisted) sample
Resource resource = new Resource("snmp:1:metrics");
Timestamp end = Timestamp.now();
Results<Sample> samples = m_sampleRepository.select(Context.DEFAULT_CONTEXT, resource, Optional.of(now), Optional.of(end));
assertEquals(1, samples.getRows().size());
Row<Sample> row = samples.getRows().iterator().next();
assertEquals(900, row.getElement("metric").getValue().doubleValue(), 0.00001);
}
use of org.opennms.newts.api.Timestamp in project newts by OpenNMS.
the class SamplesResource method getSamples.
@GET
@Timed
@Path("/{resource}")
public Collection<Collection<SampleDTO>> getSamples(@PathParam("resource") Resource resource, @QueryParam("start") Optional<TimestampParam> start, @QueryParam("end") Optional<TimestampParam> end, @QueryParam("context") Optional<String> contextId) {
Optional<Timestamp> lower = Transform.toTimestamp(start);
Optional<Timestamp> upper = Transform.toTimestamp(end);
Context context = contextId.isPresent() ? new Context(contextId.get()) : Context.DEFAULT_CONTEXT;
return Transform.sampleDTOs(m_sampleRepository.select(context, resource, lower, upper));
}
use of org.opennms.newts.api.Timestamp in project newts by OpenNMS.
the class ImportRunner method adjustTime.
private Func1<? super Sample, ? extends Sample> adjustTime() {
return new Func1<Sample, Sample>() {
@Override
public Sample call(Sample s) {
Timestamp oldTs = s.getTimestamp();
Timestamp newTs = Timestamp.fromEpochMillis(m_timeoffset + Math.round(oldTs.asMillis() / m_timescaleFactor));
return new Sample(newTs, s.getResource(), s.getName(), s.getType(), s.getValue());
}
};
}
use of org.opennms.newts.api.Timestamp in project newts by OpenNMS.
the class CassandraSampleRepository method cassandraSelect.
private Iterator<com.datastax.driver.core.Row> cassandraSelect(Context context, Resource resource, Timestamp start, Timestamp end) {
List<Future<ResultSet>> futures = Lists.newArrayList();
Duration resourceShard = m_contextConfigurations.getResourceShard(context);
Timestamp lower = start.stepFloor(resourceShard);
Timestamp upper = end.stepFloor(resourceShard);
for (Timestamp partition : new IntervalGenerator(lower, upper, resourceShard)) {
BoundStatement bindStatement = m_selectStatement.bind();
bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
bindStatement.setTimestamp("start", start.asDate());
bindStatement.setTimestamp("end", end.asDate());
// Use the context specific consistency level
bindStatement.setConsistencyLevel(m_contextConfigurations.getReadConsistency(context));
futures.add(m_session.executeAsync(bindStatement));
}
return new ConcurrentResultWrapper(futures);
}
use of org.opennms.newts.api.Timestamp in project newts by OpenNMS.
the class CassandraSampleRepository method select.
@Override
public Results<Measurement> select(Context context, Resource resource, Optional<Timestamp> start, Optional<Timestamp> end, ResultDescriptor descriptor, Optional<Duration> resolution, SampleSelectCallback callback) {
Timer.Context timer = m_measurementSelectTimer.time();
validateSelect(start, end);
Timestamp upper = end.isPresent() ? end.get() : Timestamp.now();
Timestamp lower = start.isPresent() ? start.get() : upper.minus(Duration.seconds(86400));
Duration step;
if (resolution.isPresent()) {
step = resolution.get();
} else {
// Determine the ideal step size, splitting the interval evenly into N slices
long stepMillis = upper.minus(lower).asMillis() / TARGET_NUMBER_OF_STEPS;
// But every step must be a multiple of the interval
long intervalMillis = descriptor.getInterval().asMillis();
// If the interval is greater than the target step, use the 2 * interval as the step
if (intervalMillis >= stepMillis) {
step = descriptor.getInterval().times(2);
} else {
// Otherwise, round stepMillkeyis up to the closest multiple of intervalMillis
long remainderMillis = stepMillis % intervalMillis;
if (remainderMillis != 0) {
stepMillis = stepMillis + intervalMillis - remainderMillis;
}
step = Duration.millis(stepMillis);
}
}
LOG.debug("Querying database for resource {}, from {} to {}", resource, lower.minus(step), upper);
DriverAdapter driverAdapter = new DriverAdapter(cassandraSelect(context, resource, lower.minus(step), upper), descriptor.getSourceNames());
Results<Measurement> results;
callback.beforeProcess();
try {
results = new ResultProcessor(resource, lower, upper, descriptor, step).process(driverAdapter);
} finally {
callback.afterProcess();
}
LOG.debug("{} results returned from database", driverAdapter.getResultCount());
m_samplesSelected.mark(driverAdapter.getResultCount());
try {
return results;
} finally {
timer.stop();
}
}
Aggregations