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;
}
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;
}
}
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;
}
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);
}
}
}
}
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;
}
Aggregations