Search in sources :

Example 6 with StartsAndRuntimeList

use of com.infiniteautomation.mango.statistics.StartsAndRuntimeList in project ma-modules-public by infiniteautomation.

the class PointValueTimeCsvWriter method writeAllStatistics.

@Override
public void writeAllStatistics(StatisticsGenerator statisticsGenerator, DataPointVO vo, boolean rendered) throws IOException {
    if (info.isMultiplePointsPerArray()) {
        if (statisticsGenerator instanceof ValueChangeCounter) {
            // We only need the timestamp here for image links
            ValueChangeCounter stats = (ValueChangeCounter) statisticsGenerator;
            writeDataValue(vo.getXid() + DOT + RollupEnum.START.name(), vo, stats.getStartValue(), stats.getPeriodStartTime(), rendered);
            writeDataValue(vo.getXid() + DOT + RollupEnum.FIRST.name(), vo, stats.getFirstValue(), stats.getFirstTime(), rendered);
            writeDataValue(vo.getXid() + DOT + RollupEnum.LAST.name(), vo, stats.getLastValue(), stats.getLastTime(), rendered);
            writeIntegerField(vo.getXid() + DOT + RollupEnum.COUNT.name(), stats.getCount());
        } else if (statisticsGenerator instanceof StartsAndRuntimeList) {
            StartsAndRuntimeList stats = (StartsAndRuntimeList) statisticsGenerator;
            writeDataValue(vo.getXid() + DOT + RollupEnum.START.name(), vo, stats.getStartValue(), stats.getPeriodStartTime(), rendered);
            writeDataValue(vo.getXid() + DOT + RollupEnum.FIRST.name(), vo, stats.getFirstValue(), stats.getFirstTime(), rendered);
            writeDataValue(vo.getXid() + DOT + RollupEnum.LAST.name(), vo, stats.getLastValue(), stats.getLastTime(), rendered);
            writeIntegerField(vo.getXid() + DOT + RollupEnum.COUNT.name(), stats.getCount());
        } else if (statisticsGenerator instanceof AnalogStatistics) {
            AnalogStatistics stats = (AnalogStatistics) statisticsGenerator;
            writeAccumulator(vo.getXid() + DOT + RollupEnum.ACCUMULATOR.name(), vo, stats, rendered);
            writeAnalogStatistic(vo.getXid() + DOT + RollupEnum.AVERAGE.name(), vo, stats.getAverage(), rendered);
            writeAnalogStatistic(vo.getXid() + DOT + RollupEnum.DELTA.name(), vo, stats.getDelta(), rendered);
            writeAnalogStatistic(vo.getXid() + DOT + RollupEnum.MINIMUM.name(), vo, stats.getMinimumValue(), rendered);
            writeAnalogStatistic(vo.getXid() + DOT + RollupEnum.MAXIMUM.name(), vo, stats.getMaximumValue(), rendered);
            writeAnalogStatistic(vo.getXid() + DOT + RollupEnum.SUM.name(), vo, stats.getSum(), rendered);
            writeAnalogStatistic(vo.getXid() + DOT + RollupEnum.START.name(), vo, stats.getStartValue(), rendered);
            writeAnalogStatistic(vo.getXid() + DOT + RollupEnum.FIRST.name(), vo, stats.getFirstValue(), rendered);
            writeAnalogStatistic(vo.getXid() + DOT + RollupEnum.LAST.name(), vo, stats.getLastValue(), rendered);
            writeIntegral(vo.getXid() + DOT + RollupEnum.INTEGRAL.name(), vo, stats.getIntegral(), rendered);
            writeIntegerField(vo.getXid() + DOT + RollupEnum.COUNT.name(), stats.getCount());
        }
    } else {
        if (statisticsGenerator instanceof ValueChangeCounter) {
            // We only need the timestamp here for image links
            ValueChangeCounter stats = (ValueChangeCounter) statisticsGenerator;
            writeDataValue(RollupEnum.START.name(), vo, stats.getStartValue(), stats.getPeriodStartTime(), rendered);
            writeDataValue(RollupEnum.FIRST.name(), vo, stats.getFirstValue(), stats.getFirstTime(), rendered);
            writeDataValue(RollupEnum.LAST.name(), vo, stats.getLastValue(), stats.getLastTime(), rendered);
            writeIntegerField(RollupEnum.COUNT.name(), stats.getCount());
        } else if (statisticsGenerator instanceof StartsAndRuntimeList) {
            StartsAndRuntimeList stats = (StartsAndRuntimeList) statisticsGenerator;
            writeDataValue(RollupEnum.START.name(), vo, stats.getStartValue(), stats.getPeriodStartTime(), rendered);
            writeDataValue(RollupEnum.FIRST.name(), vo, stats.getFirstValue(), stats.getFirstTime(), rendered);
            writeDataValue(RollupEnum.LAST.name(), vo, stats.getLastValue(), stats.getLastTime(), rendered);
            writeIntegerField(RollupEnum.COUNT.name(), stats.getCount());
        } else if (statisticsGenerator instanceof AnalogStatistics) {
            AnalogStatistics stats = (AnalogStatistics) statisticsGenerator;
            writeAccumulator(RollupEnum.ACCUMULATOR.name(), vo, stats, rendered);
            writeAnalogStatistic(RollupEnum.AVERAGE.name(), vo, stats.getAverage(), rendered);
            writeAnalogStatistic(RollupEnum.DELTA.name(), vo, stats.getDelta(), rendered);
            writeAnalogStatistic(RollupEnum.MINIMUM.name(), vo, stats.getMinimumValue(), rendered);
            writeAnalogStatistic(RollupEnum.MAXIMUM.name(), vo, stats.getMaximumValue(), rendered);
            writeAnalogStatistic(RollupEnum.SUM.name(), vo, stats.getSum(), rendered);
            writeAnalogStatistic(RollupEnum.START.name(), vo, stats.getStartValue(), rendered);
            writeAnalogStatistic(RollupEnum.FIRST.name(), vo, stats.getFirstValue(), rendered);
            writeAnalogStatistic(RollupEnum.LAST.name(), vo, stats.getLastValue(), rendered);
            writeIntegral(RollupEnum.INTEGRAL.name(), vo, stats.getIntegral(), rendered);
            writeIntegerField(RollupEnum.COUNT.name(), stats.getCount());
        }
    }
}
Also used : ValueChangeCounter(com.infiniteautomation.mango.statistics.ValueChangeCounter) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics)

Example 7 with StartsAndRuntimeList

use of com.infiniteautomation.mango.statistics.StartsAndRuntimeList in project ma-core-public by infiniteautomation.

the class DataPointRT method scheduleTimeoutImpl.

public void scheduleTimeoutImpl(long fireTime) {
    synchronized (intervalLoggingLock) {
        DataValue value;
        if (vo.getLoggingType() == DataPointVO.LoggingTypes.INTERVAL) {
            if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.INSTANT)
                value = PointValueTime.getValue(pointValue);
            else if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.MAXIMUM || vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.MINIMUM) {
                value = PointValueTime.getValue(intervalValue);
                intervalValue = pointValue;
            } else if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.AVERAGE) {
                // If we don't have enough averaging values then we will bail and wait for more
                if (vo.isOverrideIntervalLoggingSamples() && (averagingValues.size() != vo.getIntervalLoggingSampleWindowSize()))
                    return;
                if (vo.getPointLocator().getDataTypeId() == DataTypes.MULTISTATE) {
                    StartsAndRuntimeList stats = new StartsAndRuntimeList(intervalStartTime, fireTime, intervalValue, averagingValues);
                    double maxProportion = -1;
                    Object valueAtMax = null;
                    for (StartsAndRuntime sar : stats.getData()) {
                        if (sar.getProportion() > maxProportion) {
                            maxProportion = sar.getProportion();
                            valueAtMax = sar.getValue();
                        }
                    }
                    if (valueAtMax != null)
                        value = new MultistateValue(DataValue.objectToValue(valueAtMax).getIntegerValue());
                    else
                        value = null;
                } else {
                    AnalogStatistics stats = new AnalogStatistics(intervalStartTime, fireTime, intervalValue, averagingValues);
                    if (stats.getAverage() == null || (stats.getAverage() == Double.NaN && stats.getCount() == 0))
                        value = null;
                    else if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
                        value = new NumericValue(stats.getAverage());
                    else if (vo.getPointLocator().getDataTypeId() == DataTypes.BINARY)
                        value = new BinaryValue(stats.getAverage() >= 0.5);
                    else
                        throw new ShouldNeverHappenException("Unsupported average interval logging data type.");
                }
                // Compute the center point of our average data, starting by finding where our period started
                long sampleWindowStartTime;
                if (vo.isOverrideIntervalLoggingSamples())
                    sampleWindowStartTime = averagingValues.get(0).getTime();
                else
                    sampleWindowStartTime = intervalStartTime;
                intervalStartTime = fireTime;
                // Fix to simulate center tapped filter (un-shift the average)
                fireTime = sampleWindowStartTime + (fireTime - sampleWindowStartTime) / 2L;
                intervalValue = pointValue;
                if (!vo.isOverrideIntervalLoggingSamples())
                    averagingValues.clear();
            } else
                throw new ShouldNeverHappenException("Unknown interval logging type: " + vo.getIntervalLoggingType());
        } else if (vo.getLoggingType() == DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL) {
            // Okay, no changes rescheduled the timer. Get a value, reschedule
            if (pointValue != null) {
                value = pointValue.getValue();
                if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
                    toleranceOrigin = pointValue.getDoubleValue();
            } else
                value = null;
            rescheduleChangeInterval(Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod()));
        } else
            value = null;
        if (value != null) {
            PointValueTime newValue = new PointValueTime(value, fireTime);
            valueCache.logPointValueAsync(newValue, null);
            // Fire logged Events
            fireEvents(null, newValue, null, false, false, true, false, false);
        }
    }
}
Also used : StartsAndRuntime(com.infiniteautomation.mango.statistics.StartsAndRuntime) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue)

Example 8 with StartsAndRuntimeList

use of com.infiniteautomation.mango.statistics.StartsAndRuntimeList in project ma-core-public by infiniteautomation.

the class StartsAndRuntimeListQuantizerTest method testStartValueOneValuePerPeriod.

@Test
public void testStartValueOneValuePerPeriod() throws IOException {
    // Generate data at 12 noon for every day in the period
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    time = ZonedDateTime.of(2017, 01, 01, 12, 00, 00, 0, zoneId);
    List<IdPointValueTime> data = new ArrayList<>();
    int value = 1;
    while (time.toInstant().isBefore(to.toInstant())) {
        data.add(new IdPointValueTime(1, new MultistateValue(value), time.toInstant().toEpochMilli()));
        time = (ZonedDateTime) adjuster.adjustInto(time);
    }
    // Reset time to track periods
    time = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
    MutableInt counter = new MutableInt(0);
    BucketCalculator bc = new TimePeriodBucketCalculator(from, to, TimePeriods.DAYS, 1);
    StartsAndRuntimeListQuantizer quantizer = new StartsAndRuntimeListQuantizer(bc, new StatisticsGeneratorQuantizerCallback<StartsAndRuntimeList>() {

        @Override
        public void quantizedStatistics(StartsAndRuntimeList statisticsGenerator) throws IOException {
            counter.increment();
            StartsAndRuntimeList stats = (StartsAndRuntimeList) statisticsGenerator;
            // Test periodStart
            Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
            // Test periiodEnd
            Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
            ZonedDateTime sampleTime = time.plusHours(12);
            // Test first
            Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
            Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(1, stats.getLastValue().getIntegerValue());
            Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getLastTime());
            // Test start (the first start value will be null
            Assert.assertEquals(1, stats.getStartValue().getIntegerValue());
            // Test count
            Assert.assertEquals(1, stats.getCount());
            // Ensure data
            Assert.assertEquals(1, stats.getStartsAndRuntime().size());
            StartsAndRuntime one = stats.getStartsAndRuntime().get(1);
            Assert.assertEquals(1, one.getStarts());
            Assert.assertEquals(100.0d, one.getPercentage(), 0.0001);
            Assert.assertEquals(1.0d, one.getProportion(), 0.0001);
            Assert.assertEquals(24 * 60 * 60 * 1000, one.getRuntime());
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(new IdPointValueTime(1, new MultistateValue(1), time.minusHours(3).toInstant().toEpochMilli()), 0, true);
    for (int count = 0; count < data.size(); count++) quantizer.row(data.get(count), count + 1);
    quantizer.lastValue(data.get(data.size() - 1), data.size() + 1, true);
    quantizer.done();
    Assert.assertEquals(new Integer(31), counter.getValue());
}
Also used : ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) IOException(java.io.IOException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) StartsAndRuntime(com.infiniteautomation.mango.statistics.StartsAndRuntime) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 9 with StartsAndRuntimeList

use of com.infiniteautomation.mango.statistics.StartsAndRuntimeList in project ma-core-public by infiniteautomation.

the class StartsAndRuntimeListQuantizerTest method testNoStartValueOneValuePerPeriod.

@Test
public void testNoStartValueOneValuePerPeriod() throws IOException {
    // Generate data at 12 noon for every day in the period
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    time = ZonedDateTime.of(2017, 01, 01, 12, 00, 00, 0, zoneId);
    List<IdPointValueTime> data = new ArrayList<>();
    int value = 1;
    while (time.toInstant().isBefore(to.toInstant())) {
        data.add(new IdPointValueTime(1, new MultistateValue(value), time.toInstant().toEpochMilli()));
        time = (ZonedDateTime) adjuster.adjustInto(time);
    }
    // Reset time to track periods
    time = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
    MutableInt counter = new MutableInt(0);
    BucketCalculator bc = new TimePeriodBucketCalculator(from, to, TimePeriods.DAYS, 1);
    StartsAndRuntimeListQuantizer quantizer = new StartsAndRuntimeListQuantizer(bc, new StatisticsGeneratorQuantizerCallback<StartsAndRuntimeList>() {

        @Override
        public void quantizedStatistics(StartsAndRuntimeList statisticsGenerator) throws IOException {
            counter.increment();
            StartsAndRuntimeList stats = (StartsAndRuntimeList) statisticsGenerator;
            // Test periodStart
            Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
            // Test periiodEnd
            Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
            ZonedDateTime sampleTime = time.plusHours(12);
            // Test first
            Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
            Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(1, stats.getLastValue().getIntegerValue());
            Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getLastTime());
            // Test start (the first start value will be null
            if (counter.getValue() == 1)
                Assert.assertEquals(null, stats.getStartValue());
            else
                Assert.assertEquals(1, stats.getStartValue().getIntegerValue());
            // Test count
            Assert.assertEquals(1, stats.getCount());
            // Test StartsList
            Map<Object, StartsAndRuntime> map = stats.getStartsAndRuntime();
            StartsAndRuntime one = map.get(1);
            Assert.assertEquals(1, one.getStarts());
            Assert.assertEquals(100.0d, one.getPercentage(), 0.0001);
            Assert.assertEquals(1.0d, one.getProportion(), 0.0001);
            if (counter.getValue() == 1)
                Assert.assertEquals(12 * 60 * 60 * 1000, one.getRuntime());
            else
                Assert.assertEquals(24 * 60 * 60 * 1000, one.getRuntime());
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(null, 0, true);
    for (int count = 0; count < data.size(); count++) quantizer.row(data.get(count), count + 1);
    quantizer.lastValue(data.get(data.size() - 1), data.size() + 1, true);
    quantizer.done();
    Assert.assertEquals(new Integer(31), counter.getValue());
}
Also used : ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) IOException(java.io.IOException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) StartsAndRuntime(com.infiniteautomation.mango.statistics.StartsAndRuntime) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Map(java.util.Map) Test(org.junit.Test)

Example 10 with StartsAndRuntimeList

use of com.infiniteautomation.mango.statistics.StartsAndRuntimeList in project ma-core-public by infiniteautomation.

the class StartsAndRuntimeListQuantizerTest method testStartValueNoPeriodValues.

@Test
public void testStartValueNoPeriodValues() throws IOException {
    // Generate data at 12 noon for every day in the period
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    // Reset time to track periods
    time = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
    MutableInt counter = new MutableInt(0);
    BucketCalculator bc = new TimePeriodBucketCalculator(from, to, TimePeriods.DAYS, 1);
    StartsAndRuntimeListQuantizer quantizer = new StartsAndRuntimeListQuantizer(bc, new StatisticsGeneratorQuantizerCallback<StartsAndRuntimeList>() {

        @Override
        public void quantizedStatistics(StartsAndRuntimeList statisticsGenerator) throws IOException {
            counter.increment();
            StartsAndRuntimeList stats = (StartsAndRuntimeList) statisticsGenerator;
            // Test periodStart
            Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
            // Test periodEnd
            Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
            // Test first
            Assert.assertEquals(null, stats.getFirstValue());
            Assert.assertEquals(null, stats.getFirstTime());
            // Test last
            Assert.assertEquals(null, stats.getLastValue());
            Assert.assertEquals(null, stats.getLastTime());
            // Test start
            Assert.assertEquals(1, stats.getStartValue().getIntegerValue());
            // Test count
            Assert.assertEquals(0, stats.getCount());
            // Ensure data
            Assert.assertEquals(1, stats.getStartsAndRuntime().size());
            StartsAndRuntime one = stats.getStartsAndRuntime().get(1);
            Assert.assertEquals(0, one.getStarts());
            Assert.assertEquals(100.0d, one.getPercentage(), 0.0001);
            Assert.assertEquals(1.0d, one.getProportion(), 0.0001);
            Assert.assertEquals(24 * 60 * 60 * 1000, one.getRuntime());
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(new IdPointValueTime(1, new MultistateValue(1), time.minusHours(3).toInstant().toEpochMilli()), 0, true);
    quantizer.done();
    Assert.assertEquals(new Integer(31), counter.getValue());
}
Also used : IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) IOException(java.io.IOException) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) StartsAndRuntime(com.infiniteautomation.mango.statistics.StartsAndRuntime) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Aggregations

StartsAndRuntimeList (com.infiniteautomation.mango.statistics.StartsAndRuntimeList)12 StartsAndRuntime (com.infiniteautomation.mango.statistics.StartsAndRuntime)8 NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)8 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)8 IOException (java.io.IOException)8 ZonedDateTime (java.time.ZonedDateTime)8 MutableInt (org.apache.commons.lang3.mutable.MutableInt)8 Test (org.junit.Test)8 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)7 ArrayList (java.util.ArrayList)5 AnalogStatistics (com.infiniteautomation.mango.statistics.AnalogStatistics)3 ValueChangeCounter (com.infiniteautomation.mango.statistics.ValueChangeCounter)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)1 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)1 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)1 Map (java.util.Map)1