Search in sources :

Example 1 with AnalogStatisticsQuantizer

use of com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer 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 2 with AnalogStatisticsQuantizer

use of com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer 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 3 with AnalogStatisticsQuantizer

use of com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer 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 4 with AnalogStatisticsQuantizer

use of com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer 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)

Example 5 with AnalogStatisticsQuantizer

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

the class AggregateDao method aggregate.

/**
 * Aggregate a stream of raw point values into aggregate statistics. Mango statistics rely on knowing the initial
 * value of the point before the "from" time, you must include an initial start value in the stream (if one exists).
 * The timestamp of this start value should be less than the "from" time.
 *
 * @param point data point
 * @param from from time (inclusive)
 * @param to to time (exclusive)
 * @param pointValues stream of point values, must include a start value (at time < from) for accurate statistics
 * @param aggregationPeriod aggregation period (bucket/window size)
 * @return stream of aggregates
 */
default Stream<SeriesValueTime<AggregateValue>> aggregate(DataPointVO point, ZonedDateTime from, ZonedDateTime to, Stream<? extends PointValueTime> pointValues, TemporalAmount aggregationPeriod) {
    BucketCalculator bucketCalc = new TemporalAmountBucketCalculator(from, to, aggregationPeriod);
    AbstractPointValueTimeQuantizer<?> quantizer;
    switch(point.getPointLocator().getDataType()) {
        case BINARY:
        case MULTISTATE:
            quantizer = new StartsAndRuntimeListQuantizer(bucketCalc);
            break;
        case NUMERIC:
            quantizer = new AnalogStatisticsQuantizer(bucketCalc);
            break;
        case ALPHANUMERIC:
            quantizer = new ValueChangeCounterQuantizer(bucketCalc);
            break;
        default:
            throw new IllegalStateException("Unknown data type: " + point.getPointLocator().getDataType());
    }
    Stream<AggregateValue> aggregateStream = StatisticsAggregator.aggregate(pointValues, quantizer).filter(v -> v instanceof AggregateValue).map(v -> (AggregateValue) v);
    return aggregateStream.map(v -> new DefaultSeriesValueTime<>(point.getSeriesId(), v.getPeriodStartTime(), v));
}
Also used : IValueTime(com.serotonin.m2m2.view.stats.IValueTime) SeriesValueTime(com.serotonin.m2m2.view.stats.SeriesValueTime) MONTH_OF_YEAR(java.time.temporal.ChronoField.MONTH_OF_YEAR) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) ZonedDateTime(java.time.ZonedDateTime) StartsAndRuntimeListQuantizer(com.infiniteautomation.mango.quantize.StartsAndRuntimeListQuantizer) StatisticsAggregator(com.infiniteautomation.mango.db.iterators.StatisticsAggregator) AbstractPointValueTimeQuantizer(com.infiniteautomation.mango.quantize.AbstractPointValueTimeQuantizer) DAY_OF_MONTH(java.time.temporal.ChronoField.DAY_OF_MONTH) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ValueChangeCounterQuantizer(com.infiniteautomation.mango.quantize.ValueChangeCounterQuantizer) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) PeekingIterator(org.apache.commons.collections4.iterators.PeekingIterator) TemporalAmount(java.time.temporal.TemporalAmount) Optional(java.util.Optional) AnalogStatisticsQuantizer(com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer) DefaultSeriesValueTime(com.serotonin.m2m2.view.stats.DefaultSeriesValueTime) BucketCalculator(com.infiniteautomation.mango.quantize.BucketCalculator) TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) Nullable(org.checkerframework.checker.nullness.qual.Nullable) TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) StartsAndRuntimeListQuantizer(com.infiniteautomation.mango.quantize.StartsAndRuntimeListQuantizer) BucketCalculator(com.infiniteautomation.mango.quantize.BucketCalculator) TemporalAmountBucketCalculator(com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator) ValueChangeCounterQuantizer(com.infiniteautomation.mango.quantize.ValueChangeCounterQuantizer) AnalogStatisticsQuantizer(com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer)

Aggregations

AnalogStatisticsQuantizer (com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer)5 BucketCalculator (com.infiniteautomation.mango.quantize.BucketCalculator)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 StartsAndRuntimeListQuantizer (com.infiniteautomation.mango.quantize.StartsAndRuntimeListQuantizer)1 ValueChangeCounterQuantizer (com.infiniteautomation.mango.quantize.ValueChangeCounterQuantizer)1 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 DefaultSeriesValueTime (com.serotonin.m2m2.view.stats.DefaultSeriesValueTime)1 IValueTime (com.serotonin.m2m2.view.stats.IValueTime)1 SeriesValueTime (com.serotonin.m2m2.view.stats.SeriesValueTime)1 ZonedDateTime (java.time.ZonedDateTime)1