Search in sources :

Example 11 with StatisticsGenerator

use of com.serotonin.m2m2.view.stats.StatisticsGenerator in project ma-core-public by infiniteautomation.

the class ValueChangeCounterQuantizerTest 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);
    ValueChangeCounterQuantizer quantizer = new ValueChangeCounterQuantizer(bc, new StatisticsGeneratorQuantizerCallback<ValueChangeCounter>() {

        @Override
        public void quantizedStatistics(ValueChangeCounter statisticsGenerator) throws IOException {
            counter.increment();
            ValueChangeCounter stats = (ValueChangeCounter) 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 Changes
            if (counter.getValue() == 1)
                Assert.assertEquals(1, stats.getChanges());
            else
                Assert.assertEquals(0, stats.getChanges());
            // 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) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ValueChangeCounter(com.infiniteautomation.mango.statistics.ValueChangeCounter) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 12 with StatisticsGenerator

use of com.serotonin.m2m2.view.stats.StatisticsGenerator in project ma-modules-public by infiniteautomation.

the class PointValueTimeJsonWriter method writeAllStatistics.

/*
     * (non-Javadoc)
     * 
     * @see
     * com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeWriter#writeAllStatistics(
     * com.serotonin.m2m2.view.stats.ValueChangeCounter, com.serotonin.m2m2.vo.DataPointVO)
     */
@Override
public void writeAllStatistics(StatisticsGenerator statisticsGenerator, DataPointVO vo) throws IOException {
    this.jgen.writeStartObject();
    writeTimestamp(statisticsGenerator.getPeriodStartTime());
    if (statisticsGenerator instanceof ValueChangeCounter) {
        ValueChangeCounter stats = (ValueChangeCounter) statisticsGenerator;
        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
            this.writeImageValue(stats.getStartValue(), stats.getPeriodStartTime(), stats.getPeriodStartTime(), vo, RollupEnum.START.name());
        else
            this.writeDataValue(stats.getPeriodStartTime(), stats.getStartValue(), vo, RollupEnum.START.name());
        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
            this.writeImageValue(stats.getFirstValue(), stats.getFirstTime(), stats.getPeriodStartTime(), vo, RollupEnum.FIRST.name());
        else
            this.writeDataValue(stats.getPeriodStartTime(), stats.getFirstValue(), vo, RollupEnum.FIRST.name());
        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
            this.writeImageValue(stats.getLastValue(), stats.getLastTime(), stats.getPeriodStartTime(), vo, RollupEnum.LAST.name());
        else
            this.writeDataValue(stats.getPeriodStartTime(), stats.getLastValue(), vo, RollupEnum.LAST.name());
        this.jgen.writeNumberField(RollupEnum.COUNT.name(), stats.getCount());
    } else if (statisticsGenerator instanceof AnalogStatistics) {
        AnalogStatistics stats = (AnalogStatistics) statisticsGenerator;
        this.writeDouble(stats.getAverage(), vo, RollupEnum.AVERAGE.name());
        this.writeDouble(stats.getDelta(), vo, RollupEnum.DELTA.name());
        this.writeDouble(stats.getMinimumValue(), vo, RollupEnum.MINIMUM.name());
        this.writeDouble(stats.getMaximumValue(), vo, RollupEnum.MAXIMUM.name());
        Double acc = stats.getLastValue();
        if (acc == null) {
            acc = stats.getMaximumValue();
        }
        this.writeDouble(acc, vo, RollupEnum.ACCUMULATOR.name());
        this.writeDouble(stats.getSum(), vo, RollupEnum.SUM.name());
        this.writeDouble(stats.getStartValue(), vo, RollupEnum.START.name());
        this.writeDouble(stats.getFirstValue(), vo, RollupEnum.FIRST.name());
        this.writeDouble(stats.getLastValue(), vo, RollupEnum.LAST.name());
        this.writeIntegral(stats.getIntegral(), vo, RollupEnum.INTEGRAL.name());
        this.jgen.writeNumberField(RollupEnum.COUNT.name(), stats.getCount());
    }
    this.jgen.writeEndObject();
}
Also used : ValueChangeCounter(com.serotonin.m2m2.view.stats.ValueChangeCounter) AnalogStatistics(com.serotonin.m2m2.view.stats.AnalogStatistics)

Example 13 with StatisticsGenerator

use of com.serotonin.m2m2.view.stats.StatisticsGenerator in project ma-core-public by infiniteautomation.

the class AnalogStatisticsQuantizerTest method testStartValueOneValuePerPeriod.

// Test with Start Value and Values
@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<>();
    double value = 1.0;
    while (time.toInstant().isBefore(to.toInstant())) {
        data.add(new IdPointValueTime(1, new NumericValue(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);
    AnalogStatisticsQuantizer quantizer = new AnalogStatisticsQuantizer(bc, new StatisticsGeneratorQuantizerCallback<AnalogStatistics>() {

        @Override
        public void quantizedStatistics(AnalogStatistics statisticsGenerator) throws IOException {
            counter.increment();
            AnalogStatistics stats = (AnalogStatistics) 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);
            // Start Value was 3 hrs before 1st period start
            // Test Minimum
            Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
            Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMinimumTime());
            // Test Maximum
            Assert.assertEquals(1.0, stats.getMaximumValue(), 0.0001);
            Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMaximumTime());
            // Test Average
            Assert.assertEquals(1.0d, stats.getAverage(), 0.0001);
            // Test Integral
            // 24Hrs
            double integral = 1.0d * 24 * 60 * 60;
            Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
            // Test sum
            Assert.assertEquals(1.0d, stats.getSum(), 0.0001);
            // Test first
            Assert.assertEquals(1.0d, stats.getFirstValue(), 0.0001);
            Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(1.0d, stats.getLastValue(), 0.0001);
            Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getLastTime());
            // Test start (the first start value will be null
            Assert.assertEquals(1.0, stats.getStartValue(), 0.0001);
            // Test count
            Assert.assertEquals(1, stats.getCount());
            // Test delta
            Assert.assertEquals(0.0, stats.getDelta(), 0.0001);
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(new IdPointValueTime(1, new NumericValue(1.0), 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) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) IOException(java.io.IOException) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 14 with StatisticsGenerator

use of com.serotonin.m2m2.view.stats.StatisticsGenerator 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 15 with StatisticsGenerator

use of com.serotonin.m2m2.view.stats.StatisticsGenerator 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)

Aggregations

IOException (java.io.IOException)23 NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)21 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)21 ZonedDateTime (java.time.ZonedDateTime)21 MutableInt (org.apache.commons.lang3.mutable.MutableInt)21 Test (org.junit.Test)21 ArrayList (java.util.ArrayList)15 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)14 AnalogStatistics (com.infiniteautomation.mango.statistics.AnalogStatistics)7 StartsAndRuntime (com.infiniteautomation.mango.statistics.StartsAndRuntime)7 StartsAndRuntimeList (com.infiniteautomation.mango.statistics.StartsAndRuntimeList)7 ValueChangeCounter (com.infiniteautomation.mango.statistics.ValueChangeCounter)7 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)7 AnalogStatistics (com.serotonin.m2m2.view.stats.AnalogStatistics)3 ValueChangeCounter (com.serotonin.m2m2.view.stats.ValueChangeCounter)3 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 StatisticsGenerator (com.serotonin.m2m2.view.stats.StatisticsGenerator)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 Map (java.util.Map)1