Search in sources :

Example 6 with Timestamp

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

Example 7 with Timestamp

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

the class NewtsReporter method report.

@Override
@SuppressWarnings("rawtypes")
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) {
    Timestamp timestamp = Timestamp.fromEpochMillis(clock.getTime());
    List<Sample> samples = Lists.newArrayList();
    for (Map.Entry<String, Gauge> entry : gauges.entrySet()) {
        reportGauge(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Counter> entry : counters.entrySet()) {
        reportCounter(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
        reportHistogram(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Meter> entry : meters.entrySet()) {
        reportMeter(samples, timestamp, entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, Timer> entry : timers.entrySet()) {
        reportTimer(samples, timestamp, entry.getKey(), entry.getValue());
    }
    this.repository.insert(samples);
}
Also used : Histogram(com.codahale.metrics.Histogram) Meter(com.codahale.metrics.Meter) Sample(org.opennms.newts.api.Sample) Timestamp(org.opennms.newts.api.Timestamp) Gauge(com.codahale.metrics.Gauge) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 8 with Timestamp

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

Example 9 with Timestamp

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

the class SelectDispatcher method go.

@Override
void go() throws InterruptedException {
    createThreads();
    for (Timestamp t : new IntervalGenerator(m_config.getStart(), m_config.getEnd(), m_config.getSelectLength(), true)) {
        for (String resource : m_config.getResources()) {
            m_queryQueue.put(new Query(resource, t.minus(m_config.getSelectLength()), t, m_config.getResolution()));
        }
    }
    shutdown();
}
Also used : IntervalGenerator(org.opennms.newts.aggregate.IntervalGenerator) Timestamp(org.opennms.newts.api.Timestamp)

Example 10 with Timestamp

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

the class LineParser method parseLine.

List<Sample> parseLine(String line) throws ParseException {
    LOG.trace("Parsing {}", line);
    List<Sample> samples = Lists.newArrayList();
    Resource station = new Resource(stringAt(line, 0));
    String wban = stringAt(line, 7);
    String dateYMD = stringAt(line, 14);
    Date date = getDateFormat().parse(dateYMD);
    Timestamp ts = new Timestamp(date.getTime(), TimeUnit.MILLISECONDS);
    double meanTemp = doubleAt(line, 24);
    samples.add(new Sample(ts, station, "meanTemperature", GAUGE, LineParser.valueFor(meanTemp, 9999.9)));
    double dewpoint = doubleAt(line, 35);
    samples.add(new Sample(ts, station, "dewPoint", GAUGE, LineParser.valueFor(dewpoint, 9999.9)));
    double slp = doubleAt(line, 46);
    Gauge seaLevelPressure = LineParser.valueFor(slp, 9999.9);
    samples.add(new Sample(ts, station, "seaLevelPressure", GAUGE, seaLevelPressure));
    double stp = doubleAt(line, 57);
    Gauge stationPressure = LineParser.valueFor(stp, 9999.9);
    samples.add(new Sample(ts, station, "stationPressure", GAUGE, stationPressure));
    double vis = doubleAt(line, 68);
    Gauge visibility = LineParser.valueFor(vis, 999.9);
    samples.add(new Sample(ts, station, "visibility", GAUGE, visibility));
    double speed = doubleAt(line, 78);
    Gauge meanWindSpeed = new Gauge(speed);
    samples.add(new Sample(ts, station, "meanWindSpeed", GAUGE, meanWindSpeed));
    double maxSpeed = doubleAt(line, 88);
    Gauge maxWindSpeed = LineParser.valueFor(maxSpeed, 999.9);
    samples.add(new Sample(ts, station, "maxWindSpeed", GAUGE, maxWindSpeed));
    double maxGust = doubleAt(line, 95);
    Gauge maxWindGust = LineParser.valueFor(maxGust, 999.9);
    samples.add(new Sample(ts, station, "maxWindGust", GAUGE, maxWindGust));
    double maxTemp = doubleAt(line, 102);
    Gauge maxTemperature = LineParser.valueFor(maxTemp, 9999.9);
    samples.add(new Sample(ts, station, "maxTemperature", GAUGE, maxTemperature));
    double minTemp = doubleAt(line, 110);
    Gauge minTemperature = LineParser.valueFor(minTemp, 9999.9);
    samples.add(new Sample(ts, station, "minTemperature", GAUGE, minTemperature));
    LOG.trace("Station number {}, WBAN {}, date {}, Max Temp {}...", station, wban, dateYMD, maxTemperature);
    return samples;
}
Also used : Sample(org.opennms.newts.api.Sample) Resource(org.opennms.newts.api.Resource) Timestamp(org.opennms.newts.api.Timestamp) Date(java.util.Date) Gauge(org.opennms.newts.api.Gauge)

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