Search in sources :

Example 1 with Sample

use of org.rrd4j.core.Sample in project openhab1-addons by openhab.

the class RRD4jService method store.

/**
     * @{inheritDoc}
     */
@Override
public synchronized void store(final Item item, final String alias) {
    final String name = alias == null ? item.getName() : alias;
    RrdDb db = getDB(name);
    if (db != null) {
        ConsolFun function = getConsolidationFunction(db);
        long now = System.currentTimeMillis() / 1000;
        if (function != ConsolFun.AVERAGE) {
            try {
                // happens right at this spot
                if (now - 1 > db.getLastUpdateTime()) {
                    // only do it if there is not already a value
                    double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
                    if (!Double.isNaN(lastValue)) {
                        Sample sample = db.createSample();
                        sample.setTime(now - 1);
                        sample.setValue(DATASOURCE_STATE, lastValue);
                        sample.update();
                        logger.debug("Stored '{}' with state '{}' in rrd4j database (again)", name, mapToState(lastValue, item.getName()));
                    }
                }
            } catch (IOException e) {
                logger.debug("Error storing last value (again): {}", e.getMessage());
            }
        }
        try {
            Sample sample = db.createSample();
            sample.setTime(now);
            DecimalType state = (DecimalType) item.getStateAs(DecimalType.class);
            if (state != null) {
                double value = state.toBigDecimal().doubleValue();
                if (db.getDatasource(DATASOURCE_STATE).getType() == DsType.COUNTER) {
                    // counter
                    // values
                    // must
                    // be
                    // adjusted
                    // by
                    // stepsize
                    value = value * db.getRrdDef().getStep();
                }
                sample.setValue(DATASOURCE_STATE, value);
                sample.update();
                logger.debug("Stored '{}' with state '{}' in rrd4j database", name, state);
            }
        } catch (IllegalArgumentException e) {
            if (e.getMessage().contains("at least one second step is required")) {
                // we try to store the value one second later
                Runnable task = new Runnable() {

                    @Override
                    public void run() {
                        store(item, name);
                    }
                };
                ScheduledFuture<?> job = scheduledJobs.get(name);
                if (job != null) {
                    job.cancel(true);
                    scheduledJobs.remove(name);
                }
                job = scheduler.schedule(task, 1, TimeUnit.SECONDS);
                scheduledJobs.put(name, job);
            } else {
                logger.warn("Could not persist '{}' to rrd4j database: {}", name, e.getMessage());
            }
        } catch (Exception e) {
            logger.warn("Could not persist '{}' to rrd4j database: {}", name, e.getMessage());
        }
        try {
            db.close();
        } catch (IOException e) {
            logger.debug("Error closing rrd4j database: {}", e.getMessage());
        }
    }
}
Also used : Sample(org.rrd4j.core.Sample) IOException(java.io.IOException) ScheduledFuture(java.util.concurrent.ScheduledFuture) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) IOException(java.io.IOException) ConsolFun(org.rrd4j.ConsolFun) DecimalType(org.openhab.core.library.types.DecimalType) RrdDb(org.rrd4j.core.RrdDb)

Example 2 with Sample

use of org.rrd4j.core.Sample in project ddf by codice.

the class MetricsEndpointTest method createRrdFile.

private void createRrdFile(int dateOffset, String metricName) throws Exception {
    // Create RRD file that Metrics Endpoint will detect
    rrdPath = TEST_DIR + metricName + ".rrd";
    int rrdStep = 60;
    RrdDef def = new RrdDef(rrdPath, rrdStep);
    long startTime = System.currentTimeMillis() / 1000 - dateOffset;
    def.setStartTime(startTime - rrdStep);
    def.addDatasource("data", DsType.COUNTER, 90, 0, Double.NaN);
    def.addArchive(ConsolFun.TOTAL, 0.5, 1, 5);
    rrdDb = RrdDbPool.getInstance().requestRrdDb(def);
    // Add enough samples to get one averaged sample stored into the RRD file
    long endTime = startTime;
    Sample sample = rrdDb.createSample();
    sample.setTime(endTime);
    sample.setValue("data", 100);
    sample.update();
    endTime += rrdStep;
    sample.setTime(endTime);
    sample.setValue("data", 200);
    sample.update();
    endTime += rrdStep;
    sample.setTime(endTime);
    sample.setValue("data", 100);
    sample.update();
    endTime += rrdStep;
    LOGGER.debug(rrdDb.dump());
    FetchRequest fetchRequest = rrdDb.createFetchRequest(ConsolFun.TOTAL, startTime, endTime);
    FetchData fetchData = fetchRequest.fetchData();
    LOGGER.debug(fetchData.dump());
    long[] timestamps = fetchData.getTimestamps();
    double[] values = fetchData.getValues(0);
    for (int i = 0; i < timestamps.length; i++) {
        LOGGER.debug("{}:  {}", getCalendarTime(timestamps[i]), values[i]);
    }
    rrdDb.close();
}
Also used : RrdDef(org.rrd4j.core.RrdDef) Sample(org.rrd4j.core.Sample) FetchRequest(org.rrd4j.core.FetchRequest) FetchData(org.rrd4j.core.FetchData)

Aggregations

Sample (org.rrd4j.core.Sample)2 IOException (java.io.IOException)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)1 DecimalType (org.openhab.core.library.types.DecimalType)1 ConsolFun (org.rrd4j.ConsolFun)1 FetchData (org.rrd4j.core.FetchData)1 FetchRequest (org.rrd4j.core.FetchRequest)1 RrdDb (org.rrd4j.core.RrdDb)1 RrdDef (org.rrd4j.core.RrdDef)1