Search in sources :

Example 1 with Interval

use of org.threeten.extra.Interval in project FROST-Manager by FraunhoferIOSB.

the class AggregateCombo method calculateIntervalsForTime.

public List<Interval> calculateIntervalsForTime(TimeObject phenTime) {
    List<Interval> retval = new ArrayList<>();
    Instant phenTimeStart = Utils.getPhenTimeStart(phenTime);
    Instant phenTimeEnd = Utils.getPhenTimeEnd(phenTime);
    ZonedDateTime atZone = phenTimeStart.atZone(getZoneId());
    ZonedDateTime intStart = level.toIntervalStart(atZone);
    ZonedDateTime intEnd = intStart.plus(level.amount, level.unit);
    retval.add(Interval.of(intStart.toInstant(), intEnd.toInstant()));
    while (intEnd.toInstant().isBefore(phenTimeEnd)) {
        intStart = intEnd;
        intEnd = intStart.plus(level.amount, level.unit);
        retval.add(Interval.of(intStart.toInstant(), intEnd.toInstant()));
    }
    return retval;
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Instant(java.time.Instant) ArrayList(java.util.ArrayList) Interval(org.threeten.extra.Interval)

Example 2 with Interval

use of org.threeten.extra.Interval in project FROST-Manager by FraunhoferIOSB.

the class AggregateCombo method unsetCurrent.

/**
 * Unsets the current interval. If the given interval is the same as the
 * current interval, null is returned. If the given interval is not the same
 * as the current interval, the current interval is returned.
 *
 * @param other The interval to check against the current interval.
 * @return null if the given interval is the same as the current interval.
 */
public Interval unsetCurrent(Interval other) {
    if (currentInterval == null) {
        // There is no interval.
        return null;
    }
    if (currentInterval.equals(other)) {
        // The given interval is the same. Do nothing.
        currentInterval = null;
        return null;
    } else {
        // The interval is different. Recalculate the old interval.
        Interval old = currentInterval;
        currentInterval = null;
        return old;
    }
}
Also used : Interval(org.threeten.extra.Interval)

Example 3 with Interval

use of org.threeten.extra.Interval in project SensorThingsProcessor by FraunhoferIOSB.

the class Aggregator method calculateAggregateResultFromOriginalLists.

public List<BigDecimal> calculateAggregateResultFromOriginalLists(Interval interval, List<Observation> sourceObs) {
    List<BigDecimal> result;
    int scale = 0;
    DescriptiveStatistics stats = new DescriptiveStatistics();
    BigDecimal min = new BigDecimal(Double.MAX_VALUE);
    BigDecimal max = new BigDecimal(-Double.MAX_VALUE);
    for (Observation obs : sourceObs) {
        Object obsResultObj = obs.getResult();
        if (!(obsResultObj instanceof List)) {
            LOGGER.error("Expected list result, got {}", obsResultObj == null ? obsResultObj : obsResultObj.getClass().getName());
            continue;
        }
        List list = (List) obsResultObj;
        TimeObject phenomenonTime = obs.getPhenomenonTime();
        if (!phenomenonTime.isInterval()) {
            LOGGER.error("Expected phenTime to be an interval.");
            continue;
        }
        Interval phenInterval = phenomenonTime.getAsInterval();
        int itemCount = list.size();
        int firstItem = 0;
        int lastItem = itemCount - 1;
        double itemDistMillis = ((double) phenInterval.toDuration().toMillis()) / itemCount;
        if (phenInterval.getStart().isBefore(interval.getStart())) {
            long skipMillis = Duration.between(phenInterval.getStart(), interval.getStart()).toMillis();
            firstItem = (int) (skipMillis / itemDistMillis);
        }
        if (phenInterval.getEnd().isAfter(interval.getEnd())) {
            long skipMillis = Duration.between(interval.getEnd(), phenInterval.getEnd()).toMillis();
            int skipEnd = (int) (skipMillis / itemDistMillis);
            lastItem -= skipEnd;
        }
        for (int i = firstItem; i <= lastItem && i < itemCount; i++) {
            BigDecimal number = handleResult(list.get(i));
            if (number == null) {
                LOGGER.warn("Empty result in {}", obs);
                continue;
            }
            scale = Math.max(getScale(number), scale);
            stats.addValue(number.doubleValue());
            min = number.compareTo(min) < 0 ? number : min;
            max = number.compareTo(max) > 0 ? number : max;
        }
    }
    BigDecimal avg = new BigDecimal(stats.getMean());
    BigDecimal dev = new BigDecimal(stats.getStandardDeviation());
    result = new ArrayList<>(4);
    result.add(avg.setScale(Math.min(scale, avg.scale()), RoundingMode.HALF_UP));
    result.add(min);
    result.add(max);
    result.add(dev.setScale(Math.min(scale, dev.scale()), RoundingMode.HALF_UP));
    return result;
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) Observation(de.fraunhofer.iosb.ilt.sta.model.Observation) TimeObject(de.fraunhofer.iosb.ilt.sta.model.TimeObject) TimeObject(de.fraunhofer.iosb.ilt.sta.model.TimeObject) List(java.util.List) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) Interval(org.threeten.extra.Interval)

Example 4 with Interval

use of org.threeten.extra.Interval in project SensorThingsProcessor by FraunhoferIOSB.

the class ProcessorBatchAggregate method createOrdersFor.

private void createOrdersFor(AggregateCombo combo, Observation obs, EntityType sourceType, Id sourceId) {
    List<Interval> intervals = combo.calculateIntervalsForTime(obs.getPhenomenonTime());
    int count = intervals.size();
    if (count > 1) {
        for (Interval interval : intervals) {
            LOGGER.debug("{} {}: Interval {} recalculating.", sourceType, sourceId, interval);
            CalculationOrder order = new CalculationOrder(combo, interval, Instant.now().plus(orderDelay));
            offerOrder(order);
        }
    } else {
        for (Interval interval : intervals) {
            Interval toCalculate;
            if (interval.getEnd().equals(Utils.getPhenTimeEnd(obs))) {
                // The observation is the last one for the interval.
                LOGGER.debug("{} {}: Interval {} recalculating, because end reached.", sourceType, sourceId, interval);
                CalculationOrder order = new CalculationOrder(combo, interval, Instant.now().plus(orderDelay));
                offerOrder(order);
                toCalculate = combo.unsetCurrent(interval);
            } else {
                toCalculate = combo.replaceIfNotCurrent(interval);
            }
            if (toCalculate != null) {
                LOGGER.debug("{} {}: Interval {} recalculating, because we now have {}.", sourceType, sourceId, toCalculate, interval);
                CalculationOrder order = new CalculationOrder(combo, toCalculate, Instant.now().plus(orderDelay));
                offerOrder(order);
            }
        }
    }
}
Also used : Interval(org.threeten.extra.Interval)

Example 5 with Interval

use of org.threeten.extra.Interval in project SensorThingsProcessor by FraunhoferIOSB.

the class AggregateCombo method calculateIntervalsForTime.

public List<Interval> calculateIntervalsForTime(TimeObject phenTime) {
    List<Interval> retval = new ArrayList<>();
    Instant phenTimeStart = Utils.getPhenTimeStart(phenTime);
    Instant phenTimeEnd = Utils.getPhenTimeEnd(phenTime);
    ZonedDateTime atZone = phenTimeStart.atZone(getZoneId());
    ZonedDateTime intStart = level.toIntervalStart(atZone);
    ZonedDateTime intEnd = intStart.plus(level.amount, level.unit);
    retval.add(Interval.of(intStart.toInstant(), intEnd.toInstant()));
    while (intEnd.toInstant().isBefore(phenTimeEnd)) {
        intStart = intEnd;
        intEnd = intStart.plus(level.amount, level.unit);
        retval.add(Interval.of(intStart.toInstant(), intEnd.toInstant()));
    }
    return retval;
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Instant(java.time.Instant) ArrayList(java.util.ArrayList) Interval(org.threeten.extra.Interval)

Aggregations

Interval (org.threeten.extra.Interval)26 Test (org.junit.Test)5 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 FeaturesCoreConfiguration (de.ii.ogcapi.features.core.domain.FeaturesCoreConfiguration)2 ImmutableFeaturesCoreConfiguration (de.ii.ogcapi.features.core.domain.ImmutableFeaturesCoreConfiguration)2 FeatureTypeConfigurationOgcApi (de.ii.ogcapi.foundation.domain.FeatureTypeConfigurationOgcApi)2 TemporalExtent (de.ii.ogcapi.foundation.domain.TemporalExtent)2 FeatureProvider2 (de.ii.xtraplatform.features.domain.FeatureProvider2)2 ZonedDateTime (java.time.ZonedDateTime)2 Calendar (java.util.Calendar)2 List (java.util.List)2 After (org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.After)2 Before (org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.Before)2 BeforeOrDuring (org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.BeforeOrDuring)2 During (org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.During)2 DuringOrAfter (org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.DuringOrAfter)2 TemporalBetween (org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.TemporalBetween)2 TemporalFieldValue (org.locationtech.geowave.core.geotime.store.query.filter.expression.temporal.TemporalFieldValue)2