Search in sources :

Example 16 with Timestamp

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);
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Timestamp(org.opennms.newts.api.Timestamp) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) Persister(org.opennms.netmgt.collection.api.Persister) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) Test(org.junit.Test)

Example 17 with Timestamp

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));
}
Also used : Context(org.opennms.newts.api.Context) Timestamp(org.opennms.newts.api.Timestamp) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 18 with Timestamp

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());
        }
    };
}
Also used : Sample(org.opennms.newts.api.Sample) Func1(rx.functions.Func1) Timestamp(org.opennms.newts.api.Timestamp)

Example 19 with Timestamp

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);
}
Also used : Future(java.util.concurrent.Future) Duration(org.opennms.newts.api.Duration) IntervalGenerator(org.opennms.newts.aggregate.IntervalGenerator) Timestamp(org.opennms.newts.api.Timestamp) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 20 with Timestamp

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();
    }
}
Also used : Measurement(org.opennms.newts.api.Measurement) Timer(com.codahale.metrics.Timer) Duration(org.opennms.newts.api.Duration) ResultProcessor(org.opennms.newts.aggregate.ResultProcessor) 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