Search in sources :

Example 1 with TimeObject

use of de.fraunhofer.iosb.ilt.sta.model.TimeObject in project SensorThingsProcessor by FraunhoferIOSB.

the class ProcessorBatchAggregate method calculateAggregates.

private void calculateAggregates(BlockingQueue<CalculationOrder> queue, AggregateCombo combo) throws ServiceFailureException, ProcessException {
    Observation lastAggObs = combo.getLastForTarget();
    Instant calcIntervalStart;
    if (lastAggObs == null) {
        Observation firstSourceObs = combo.getFirstForSource();
        if (firstSourceObs == null) {
            LOGGER.debug("No source observations at all for {}.", combo);
            return;
        }
        Instant firstSourceStart = Utils.getPhenTimeStart(firstSourceObs);
        ZonedDateTime atZone = firstSourceStart.atZone(combo.getZoneId());
        ZonedDateTime firstIntStart = combo.level.toIntervalStart(atZone);
        if (atZone.isEqual(firstIntStart)) {
            calcIntervalStart = firstIntStart.toInstant();
        } else {
            calcIntervalStart = firstIntStart.plus(combo.level.duration).toInstant();
        }
    } else {
        TimeObject lastAggPhenTime = lastAggObs.getPhenomenonTime();
        calcIntervalStart = lastAggPhenTime.getAsInterval().getEnd();
    }
    Observation lastSourceObs = combo.getLastForSource();
    if (lastSourceObs == null) {
        LOGGER.debug("No source observations at all for {}.", combo);
        return;
    }
    Instant lastSourcePhenTime = Utils.getPhenTimeEnd(lastSourceObs);
    boolean more = true;
    while (more) {
        Instant calcIntervalEnd = calcIntervalStart.plus(combo.level.duration);
        if (lastSourcePhenTime.isBefore(calcIntervalEnd)) {
            LOGGER.debug("Nothing (more) to do for {}.", combo);
            return;
        }
        createOrderForDirectExecution(queue, combo, Interval.of(calcIntervalStart, calcIntervalEnd));
        calcIntervalStart = calcIntervalEnd;
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Instant(java.time.Instant) Observation(de.fraunhofer.iosb.ilt.sta.model.Observation) TimeObject(de.fraunhofer.iosb.ilt.sta.model.TimeObject)

Example 2 with TimeObject

use of de.fraunhofer.iosb.ilt.sta.model.TimeObject 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 3 with TimeObject

use of de.fraunhofer.iosb.ilt.sta.model.TimeObject in project SensorThingsProcessor by FraunhoferIOSB.

the class ValidatorAfter method isValid.

@Override
public boolean isValid(Observation obs) throws ProcessException {
    TimeObject phenomenonTime = obs.getPhenomenonTime();
    Instant obsInstant;
    if (phenomenonTime.isInterval()) {
        obsInstant = phenomenonTime.getAsInterval().getStart();
    } else {
        obsInstant = phenomenonTime.getAsDateTime().toInstant();
    }
    return refTime.isBefore(obsInstant);
}
Also used : Instant(java.time.Instant) TimeObject(de.fraunhofer.iosb.ilt.sta.model.TimeObject)

Example 4 with TimeObject

use of de.fraunhofer.iosb.ilt.sta.model.TimeObject in project SensorThingsProcessor by FraunhoferIOSB.

the class ValidatorBefore method isValid.

@Override
public boolean isValid(Observation obs) throws ProcessException {
    TimeObject phenomenonTime = obs.getPhenomenonTime();
    Instant obsInstant;
    if (phenomenonTime.isInterval()) {
        obsInstant = phenomenonTime.getAsInterval().getStart();
    } else {
        obsInstant = phenomenonTime.getAsDateTime().toInstant();
    }
    return refTime.isAfter(obsInstant);
}
Also used : Instant(java.time.Instant) TimeObject(de.fraunhofer.iosb.ilt.sta.model.TimeObject)

Example 5 with TimeObject

use of de.fraunhofer.iosb.ilt.sta.model.TimeObject in project FROST-Manager by FraunhoferIOSB.

the class ControllerCleaner method cleanObsForDatastream.

private void cleanObsForDatastream(Datastream ds) throws ServiceFailureException {
    List<Observation> toDelete = new ArrayList<>();
    EntityList<Observation> list = ds.observations().query().orderBy("phenomenonTime asc,id asc").select("id,phenomenonTime").top(100000).list();
    Iterator<Observation> it = list.fullIterator();
    long count = 0;
    long toDel = 0;
    TimeObject last = null;
    while (it.hasNext()) {
        Observation next = it.next();
        count++;
        logStatusCleaner.setObservations(count);
        TimeObject cur = next.getPhenomenonTime();
        if (last == null) {
            last = cur;
        } else {
            if (last.equals(cur)) {
                toDelete.add(next);
                logStatusCleaner.setToDelete(++toDel);
            }
            last = cur;
        }
    }
    if (toDelete.isEmpty()) {
        return;
    }
    LOGGER.info("Deleting {} obs for Datastream {}", toDelete.size(), ds);
    for (Observation obs : toDelete) {
        logStatusCleaner.setToDelete(--toDel);
        service.delete(obs);
    }
}
Also used : Observation(de.fraunhofer.iosb.ilt.sta.model.Observation) ArrayList(java.util.ArrayList) TimeObject(de.fraunhofer.iosb.ilt.sta.model.TimeObject)

Aggregations

TimeObject (de.fraunhofer.iosb.ilt.sta.model.TimeObject)9 Observation (de.fraunhofer.iosb.ilt.sta.model.Observation)6 Instant (java.time.Instant)6 ServiceFailureException (de.fraunhofer.iosb.ilt.sta.ServiceFailureException)2 Datastream (de.fraunhofer.iosb.ilt.sta.model.Datastream)2 Id (de.fraunhofer.iosb.ilt.sta.model.Id)2 ArrayList (java.util.ArrayList)2 MultiDatastream (de.fraunhofer.iosb.ilt.sta.model.MultiDatastream)1 ProcessException (de.fraunhofer.iosb.ilt.stp.ProcessException)1 BigDecimal (java.math.BigDecimal)1 ZonedDateTime (java.time.ZonedDateTime)1 List (java.util.List)1 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)1 Interval (org.threeten.extra.Interval)1