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