Search in sources :

Example 1 with AggregateValue

use of com.serotonin.m2m2.db.dao.pointvalue.AggregateValue in project ma-modules-public by infiniteautomation.

the class AggregateValueMapper method apply.

@Override
public StreamingPointValueTimeModel apply(SeriesValueTime<? extends AggregateValue> value) {
    DataPointVO point = lookupPoint(value.getSeriesId());
    RollupEnum rollup = rollup(point);
    AggregateValue aggregate = value.getValue();
    StreamingPointValueTimeModel model = new StreamingPointValueTimeModel(point.getXid(), value.getTime());
    if (rollup == RollupEnum.ALL) {
        AllStatisticsModel allStatisticsModel = getAllRollup(point, aggregate);
        model.setAllStatistics(allStatisticsModel);
    } else {
        ValueTimeModel rollupValue = getRollupValue(point, aggregate, rollup);
        model.setValueModel(rollupValue);
    }
    if (fields.contains(PointValueField.CACHED)) {
        model.setCached(false);
    }
    if (fields.contains(PointValueField.BOOKEND)) {
        model.setBookend(false);
    }
    return copyPointPropertiesToModel(point, model);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) AggregateValue(com.serotonin.m2m2.db.dao.pointvalue.AggregateValue) AllStatisticsModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.AllStatisticsModel) MultistateAllStatisticsModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.MultistateAllStatisticsModel) StreamingPointValueTimeModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.StreamingPointValueTimeModel) StreamingPointValueTimeModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.StreamingPointValueTimeModel) ValueTimeModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.ValueTimeModel) RollupEnum(com.infiniteautomation.mango.rest.latest.model.pointValue.RollupEnum)

Example 2 with AggregateValue

use of com.serotonin.m2m2.db.dao.pointvalue.AggregateValue in project ma-modules-public by infiniteautomation.

the class AggregateValueMapper method getAllRollup.

private AllStatisticsModel getAllRollup(DataPointVO point, AggregateValue stats) {
    AllStatisticsModel all;
    if (stats instanceof NumericAggregate) {
        NumericAllModel model = new NumericAllModel();
        model.setAccumulator(getRollupValue(point, stats, RollupEnum.ACCUMULATOR));
        model.setAverage(getRollupValue(point, stats, RollupEnum.AVERAGE));
        model.setDelta(getRollupValue(point, stats, RollupEnum.DELTA));
        model.setIntegral(getRollupValue(point, stats, RollupEnum.INTEGRAL));
        model.setMaximum(getRollupValue(point, stats, RollupEnum.MAXIMUM));
        model.setMinimum(getRollupValue(point, stats, RollupEnum.MINIMUM));
        model.setSum(getRollupValue(point, stats, RollupEnum.SUM));
        model.setMaximumInPeriod(getRollupValue(point, stats, RollupEnum.MAXIMUM_IN_PERIOD));
        model.setMinimumInPeriod(getRollupValue(point, stats, RollupEnum.MINIMUM_IN_PERIOD));
        model.setArithmeticMean(getRollupValue(point, stats, RollupEnum.ARITHMETIC_MEAN));
        all = model;
    } else if (stats instanceof StartsAndRuntimeAggregate) {
        MultistateAllStatisticsModel model = new MultistateAllStatisticsModel();
        var startsStats = ((StartsAndRuntimeAggregate) stats);
        var startsModel = startsStats.getData().stream().map(start -> {
            String rendered = renderValue(point, start.getDataValue());
            return new StartsAndRuntimeModel(start.getDataValue(), rendered, start.getStarts(), start.getRuntime(), start.getProportion());
        }).collect(Collectors.toUnmodifiableList());
        model.setStartsAndRuntimes(startsModel);
        all = model;
    } else {
        all = new AllStatisticsModel();
    }
    if (fields.contains(PointValueField.TIMESTAMP)) {
        all.setTimestamp(formatTime(stats.getPeriodStartTime()));
    }
    all.setCount(stats.getCount());
    all.setFirst(getRollupValue(point, stats, RollupEnum.FIRST));
    all.setLast(getRollupValue(point, stats, RollupEnum.LAST));
    all.setStart(getRollupValue(point, stats, RollupEnum.START));
    return all;
}
Also used : AllStatisticsModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.AllStatisticsModel) MultistateAllStatisticsModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.MultistateAllStatisticsModel) MultistateAllStatisticsModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.MultistateAllStatisticsModel) StartsAndRuntimeAggregate(com.serotonin.m2m2.db.dao.pointvalue.StartsAndRuntimeAggregate) NumericAllModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.NumericAllModel) StartsAndRuntimeModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.MultistateAllStatisticsModel.StartsAndRuntimeModel) NumericAggregate(com.serotonin.m2m2.db.dao.pointvalue.NumericAggregate)

Example 3 with AggregateValue

use of com.serotonin.m2m2.db.dao.pointvalue.AggregateValue 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

AllStatisticsModel (com.infiniteautomation.mango.rest.latest.streamingvalues.model.AllStatisticsModel)2 MultistateAllStatisticsModel (com.infiniteautomation.mango.rest.latest.streamingvalues.model.MultistateAllStatisticsModel)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 StatisticsAggregator (com.infiniteautomation.mango.db.iterators.StatisticsAggregator)1 AbstractPointValueTimeQuantizer (com.infiniteautomation.mango.quantize.AbstractPointValueTimeQuantizer)1 AnalogStatisticsQuantizer (com.infiniteautomation.mango.quantize.AnalogStatisticsQuantizer)1 BucketCalculator (com.infiniteautomation.mango.quantize.BucketCalculator)1 StartsAndRuntimeListQuantizer (com.infiniteautomation.mango.quantize.StartsAndRuntimeListQuantizer)1 TemporalAmountBucketCalculator (com.infiniteautomation.mango.quantize.TemporalAmountBucketCalculator)1 ValueChangeCounterQuantizer (com.infiniteautomation.mango.quantize.ValueChangeCounterQuantizer)1 RollupEnum (com.infiniteautomation.mango.rest.latest.model.pointValue.RollupEnum)1 StartsAndRuntimeModel (com.infiniteautomation.mango.rest.latest.streamingvalues.model.MultistateAllStatisticsModel.StartsAndRuntimeModel)1 NumericAllModel (com.infiniteautomation.mango.rest.latest.streamingvalues.model.NumericAllModel)1 StreamingPointValueTimeModel (com.infiniteautomation.mango.rest.latest.streamingvalues.model.StreamingPointValueTimeModel)1 ValueTimeModel (com.infiniteautomation.mango.rest.latest.streamingvalues.model.ValueTimeModel)1 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 AggregateValue (com.serotonin.m2m2.db.dao.pointvalue.AggregateValue)1 NumericAggregate (com.serotonin.m2m2.db.dao.pointvalue.NumericAggregate)1 StartsAndRuntimeAggregate (com.serotonin.m2m2.db.dao.pointvalue.StartsAndRuntimeAggregate)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1