Search in sources :

Example 1 with BucketCalculator

use of com.infiniteautomation.mango.quantize.BucketCalculator in project ma-modules-public by infiniteautomation.

the class MultiDataPointStatisticsQuantizerStream method finish.

@Override
public void finish(PointValueTimeWriter writer) throws QueryCancelledException, IOException {
    if (info.isSingleArray() && voMap.size() > 1) {
        // Fast forward to end to fill any gaps at the end and stream out data in time
        BucketCalculator bc;
        if (this.info.getTimePeriod() == null) {
            bc = new BucketsBucketCalculator(ZonedDateTime.ofInstant(Instant.ofEpochMilli(lastFullPeriodToMillis), info.getZoneId()), info.getTo(), 1);
        } else {
            bc = new TimePeriodBucketCalculator(ZonedDateTime.ofInstant(Instant.ofEpochMilli(lastFullPeriodToMillis), info.getZoneId()), info.getTo(), TimePeriodType.convertFrom(this.info.getTimePeriod().getType()), this.info.getTimePeriod().getPeriods());
        }
        Instant currentPeriodTo = bc.getStartTime().toInstant();
        Instant end = bc.getEndTime().toInstant();
        while (currentPeriodTo.isBefore(end)) {
            long nextTo = currentPeriodTo.toEpochMilli();
            for (DataPointStatisticsQuantizer<?> quant : this.quantizerMap.values()) {
                quant.fastForward(nextTo);
            }
            currentPeriodTo = bc.getNextPeriodTo().toInstant();
        }
        for (DataPointStatisticsQuantizer<?> quant : this.quantizerMap.values()) {
            if (!quant.isDone())
                quant.done();
        }
        // TODO This is likely not necessary
        Iterator<Long> it = this.periodStats.keySet().iterator();
        while (it.hasNext()) {
            List<DataPointValueTime> entries = this.periodStats.get(it.next());
            writePeriodStats(entries);
            it.remove();
        }
    } else {
        // The last data point may not have been done() as well as any with 0 data
        if (currentDataPointId != -1) {
            DataPointStatisticsQuantizer<?> quant = this.quantizerMap.get(currentDataPointId);
            if (!quant.isDone()) {
                quant.done();
            }
        }
        // For any with 0 data TODO Check for is open?
        for (DataPointStatisticsQuantizer<?> q : this.quantizerMap.values()) {
            if (!q.isDone()) {
                q.done();
            }
        }
    }
    super.finish(writer);
}
Also used : Instant(java.time.Instant) DataPointValueTime(com.infiniteautomation.mango.rest.latest.model.pointValue.DataPointValueTime) BucketsBucketCalculator(com.infiniteautomation.mango.quantize.BucketsBucketCalculator) TimePeriodBucketCalculator(com.infiniteautomation.mango.quantize.TimePeriodBucketCalculator) BucketsBucketCalculator(com.infiniteautomation.mango.quantize.BucketsBucketCalculator) BucketCalculator(com.infiniteautomation.mango.quantize.BucketCalculator) TimePeriodBucketCalculator(com.infiniteautomation.mango.quantize.TimePeriodBucketCalculator)

Example 2 with BucketCalculator

use of com.infiniteautomation.mango.quantize.BucketCalculator in project ma-core-public by infiniteautomation.

the class StatisticsAggregatorTest method emptyStream.

@Test
public void emptyStream() {
    BucketCalculator bucketCalc = new TemporalAmountBucketCalculator(from, to, aggregatePeriod);
    List<NumericAggregate> aggregates = StatisticsAggregator.aggregate(Stream.empty(), new AnalogStatisticsQuantizer(bucketCalc)).collect(Collectors.toList());
    Assert.assertEquals(expectedAggregateValues, aggregates.size());
    for (var aggregate : aggregates) {
        assertEquals(0L, aggregate.getCount());
        assertEquals(Double.NaN, aggregate.getArithmeticMean(), 0.0D);
        assertEquals(Double.NaN, aggregate.getAverage(), 0.0D);
    }
}
Also used : TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) BucketCalculator(com.infiniteautomation.mango.quantize.BucketCalculator) TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) AnalogStatisticsQuantizer(com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer) NumericAggregate(com.serotonin.m2m2.db.dao.pointvalue.NumericAggregate) Test(org.junit.Test)

Example 3 with BucketCalculator

use of com.infiniteautomation.mango.quantize.BucketCalculator in project ma-core-public by infiniteautomation.

the class StatisticsAggregatorTest method aggregateWithInitialValue.

@Test
public void aggregateWithInitialValue() {
    PointValueTime initialValue = new PointValueTime(1.0D, from.minusHours(1L).toInstant().toEpochMilli());
    PointValueGenerator generator = new ConstantPointValueGenerator(from.toInstant(), to.toInstant(), pollPeriod, new NumericValue(0.0D));
    var stream = generator.apply(new DataPointVO()).map(BatchPointValue::getValue);
    BucketCalculator bucketCalc = new TemporalAmountBucketCalculator(from, to, aggregatePeriod);
    List<NumericAggregate> aggregates = StatisticsAggregator.aggregate(Stream.concat(Stream.of(initialValue), stream), new AnalogStatisticsQuantizer(bucketCalc)).collect(Collectors.toList());
    Assert.assertEquals(expectedAggregateValues, aggregates.size());
    for (var aggregate : aggregates) {
        assertEquals(180L, aggregate.getCount());
        assertEquals(0.0D, aggregate.getArithmeticMean(), 0.0D);
        assertEquals(0.0D, aggregate.getArithmeticMean(), 0.0D);
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) BatchPointValue(com.serotonin.m2m2.db.dao.BatchPointValue) TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ConstantPointValueGenerator(com.infiniteautomation.mango.pointvalue.generator.ConstantPointValueGenerator) PointValueGenerator(com.infiniteautomation.mango.pointvalue.generator.PointValueGenerator) ConstantPointValueGenerator(com.infiniteautomation.mango.pointvalue.generator.ConstantPointValueGenerator) BucketCalculator(com.infiniteautomation.mango.quantize.BucketCalculator) TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) AnalogStatisticsQuantizer(com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer) NumericAggregate(com.serotonin.m2m2.db.dao.pointvalue.NumericAggregate) Test(org.junit.Test)

Example 4 with BucketCalculator

use of com.infiniteautomation.mango.quantize.BucketCalculator in project ma-core-public by infiniteautomation.

the class StatisticsAggregatorTest method initialValueOnly.

@Test
public void initialValueOnly() {
    PointValueTime initialValue = new PointValueTime(1.0D, from.minusHours(1L).toInstant().toEpochMilli());
    BucketCalculator bucketCalc = new TemporalAmountBucketCalculator(from, to, aggregatePeriod);
    List<NumericAggregate> aggregates = StatisticsAggregator.aggregate(Stream.of(initialValue), new AnalogStatisticsQuantizer(bucketCalc)).collect(Collectors.toList());
    Assert.assertEquals(expectedAggregateValues, aggregates.size());
    for (var aggregate : aggregates) {
        assertEquals(0L, aggregate.getCount());
        assertEquals(Double.NaN, aggregate.getArithmeticMean(), 0.0D);
        assertEquals(1.0D, aggregate.getAverage(), 0.0D);
    }
}
Also used : TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) BucketCalculator(com.infiniteautomation.mango.quantize.BucketCalculator) TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) AnalogStatisticsQuantizer(com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer) NumericAggregate(com.serotonin.m2m2.db.dao.pointvalue.NumericAggregate) Test(org.junit.Test)

Example 5 with BucketCalculator

use of com.infiniteautomation.mango.quantize.BucketCalculator in project ma-core-public by infiniteautomation.

the class StatisticsAggregatorTest method aggregate.

@Test
public void aggregate() {
    PointValueGenerator generator = new ConstantPointValueGenerator(from.toInstant(), to.toInstant(), pollPeriod, new NumericValue(0.0D));
    var stream = generator.apply(new DataPointVO()).map(BatchPointValue::getValue);
    BucketCalculator bucketCalc = new TemporalAmountBucketCalculator(from, to, aggregatePeriod);
    List<NumericAggregate> aggregates = StatisticsAggregator.aggregate(stream, new AnalogStatisticsQuantizer(bucketCalc)).collect(Collectors.toList());
    Assert.assertEquals(expectedAggregateValues, aggregates.size());
    for (var aggregate : aggregates) {
        assertEquals(180L, aggregate.getCount());
        assertEquals(0.0D, aggregate.getArithmeticMean(), 0.0D);
        assertEquals(0.0D, aggregate.getArithmeticMean(), 0.0D);
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) BatchPointValue(com.serotonin.m2m2.db.dao.BatchPointValue) TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) ConstantPointValueGenerator(com.infiniteautomation.mango.pointvalue.generator.ConstantPointValueGenerator) PointValueGenerator(com.infiniteautomation.mango.pointvalue.generator.PointValueGenerator) ConstantPointValueGenerator(com.infiniteautomation.mango.pointvalue.generator.ConstantPointValueGenerator) BucketCalculator(com.infiniteautomation.mango.quantize.BucketCalculator) TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) AnalogStatisticsQuantizer(com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer) NumericAggregate(com.serotonin.m2m2.db.dao.pointvalue.NumericAggregate) Test(org.junit.Test)

Aggregations

BucketCalculator (com.infiniteautomation.mango.quantize.BucketCalculator)6 AnalogStatisticsQuantizer (com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer)5 TemporalAmountBucketCalculator (com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator)5 NumericAggregate (com.serotonin.m2m2.db.dao.pointvalue.NumericAggregate)4 Test (org.junit.Test)4 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 ConstantPointValueGenerator (com.infiniteautomation.mango.pointvalue.generator.ConstantPointValueGenerator)2 PointValueGenerator (com.infiniteautomation.mango.pointvalue.generator.PointValueGenerator)2 BatchPointValue (com.serotonin.m2m2.db.dao.BatchPointValue)2 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)2 StatisticsAggregator (com.infiniteautomation.mango.db.iterators.StatisticsAggregator)1 AbstractPointValueTimeQuantizer (com.infiniteautomation.mango.quantize.AbstractPointValueTimeQuantizer)1 BucketsBucketCalculator (com.infiniteautomation.mango.quantize.BucketsBucketCalculator)1 StartsAndRuntimeListQuantizer (com.infiniteautomation.mango.quantize.StartsAndRuntimeListQuantizer)1 TimePeriodBucketCalculator (com.infiniteautomation.mango.quantize.TimePeriodBucketCalculator)1 ValueChangeCounterQuantizer (com.infiniteautomation.mango.quantize.ValueChangeCounterQuantizer)1 DataPointValueTime (com.infiniteautomation.mango.rest.latest.model.pointValue.DataPointValueTime)1 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 DefaultSeriesValueTime (com.serotonin.m2m2.view.stats.DefaultSeriesValueTime)1