Search in sources :

Example 1 with TimePeriodBucketCalculator

use of com.serotonin.m2m2.view.quantize2.TimePeriodBucketCalculator in project ma-core-public by infiniteautomation.

the class AnalogStatisticsQuantizerTest 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<>();
    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);
            // Test Minimum
            Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
            // Test Maximum
            Assert.assertEquals(1.0, stats.getMaximumValue(), 0.0001);
            if (counter.getValue() == 1) {
                Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getMinimumTime());
                Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getMaximumTime());
            } else {
                // Period start if there was a start value
                Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMinimumTime());
                Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMaximumTime());
            }
            // Test Average
            Assert.assertEquals(1.0d, stats.getAverage(), 0.0001);
            // Test Integral
            if (counter.getValue() == 1) {
                double integral = 1.0d * 12 * 60 * 60;
                Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
            } else {
                // 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
            if (counter.getValue() == 1)
                Assert.assertEquals(null, stats.getStartValue());
            else
                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(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) 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 2 with TimePeriodBucketCalculator

use of com.serotonin.m2m2.view.quantize2.TimePeriodBucketCalculator in project ma-core-public by infiniteautomation.

the class AnalogStatisticsQuantizerTest method testStartValueAtPeriodStartNoPeriodValues.

@Test
public void testStartValueAtPeriodStartNoPeriodValues() 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);
    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 periodEnd
            Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
            ZonedDateTime sampleTime = time;
            if (counter.getValue() == 1) {
                // Test Minimum
                Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
                Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getMinimumTime());
                // Test Maximum
                Assert.assertEquals(1.0, stats.getMaximumValue(), 0.0001);
                Assert.assertEquals((long) sampleTime.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
                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);
            } else {
                // No data in other periods
                // 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.0, stats.getAverage(), 0.0001);
                // Test Integral
                double integral = 1.0 * 24 * 60 * 60;
                Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
                // Test sum
                Assert.assertEquals(0.0d, stats.getSum(), 0.0001);
                // 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.0, stats.getStartValue(), 0.0001);
                // Test count
                Assert.assertEquals(0, stats.getCount());
                // Test delta
                Assert.assertEquals(0.0d, stats.getDelta(), 0.0001);
            }
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(new IdPointValueTime(1, new NumericValue(1.0), time.toInstant().toEpochMilli()), 0, false);
    quantizer.done();
    Assert.assertEquals(new Integer(31), counter.getValue());
}
Also used : 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 3 with TimePeriodBucketCalculator

use of com.serotonin.m2m2.view.quantize2.TimePeriodBucketCalculator in project ma-core-public by infiniteautomation.

the class AnalogStatisticsQuantizerTest method testStartValueAtStartManyValuesPerPeriod.

@Test
public void testStartValueAtStartManyValuesPerPeriod() throws IOException {
    // Generate data at 12 noon for every day in the period
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    NextTimePeriodAdjuster hourlyAdjuster = new NextTimePeriodAdjuster(TimePeriods.HOURS, 1);
    time = ZonedDateTime.of(2017, 01, 01, 12, 00, 00, 0, zoneId);
    List<IdPointValueTime> data = new ArrayList<>();
    while (time.toInstant().isBefore(to.toInstant())) {
        // Insert 10 values per day
        double value = 1.0;
        ZonedDateTime daily = ZonedDateTime.ofInstant(time.toInstant(), zoneId);
        for (int i = 0; i < 10; i++) {
            data.add(new IdPointValueTime(1, new NumericValue(value), daily.toInstant().toEpochMilli()));
            daily = (ZonedDateTime) hourlyAdjuster.adjustInto(daily);
            value = value + 1.0d;
        }
        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());
            // Test Minimum
            if (counter.getValue() == 1) {
                Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
                Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMinimumTime());
                // Test Maximum
                Assert.assertEquals(10.0, stats.getMaximumValue(), 0.0001);
                Assert.assertEquals(time.plusHours(12).plusHours(9).toInstant().toEpochMilli(), (long) stats.getMaximumTime());
            } else {
                Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
                Assert.assertEquals(time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getMinimumTime());
                // Test Maximum
                Assert.assertEquals(10.0, stats.getMaximumValue(), 0.0001);
                Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMaximumTime());
            }
            // 1-9 for 1hr each, 10 for 12hrs at the start and 2hrs at the end
            if (counter.getValue() == 1) {
                double integral = 1d * 13 * 60d * 60d + 2d * 60 * 60 + 3d * 60 * 60 + 4d * 60 * 60 + 5d * 60 * 60 + 6d * 60 * 60 + 7d * 60 * 60 + 8d * 60 * 60 + 9d * 60 * 60;
                integral = integral + 10d * 3d * 60 * 60;
                double average = integral / (24d * 60d * 60d);
                Assert.assertEquals(average, stats.getAverage(), 0.0001);
                // Test Integral
                Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
            } else {
                double integral = 1d * 60d * 60d + 2d * 60 * 60 + 3d * 60 * 60 + 4d * 60 * 60 + 5d * 60 * 60 + 6d * 60 * 60 + 7d * 60 * 60 + 8d * 60 * 60 + 9d * 60 * 60;
                integral = integral + 10d * 15d * 60 * 60;
                double average = integral / (24d * 60d * 60d);
                Assert.assertEquals(average, stats.getAverage(), 0.0001);
                // Test Integral
                Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
            }
            // Test sum
            if (counter.getValue() == 1) {
                Assert.assertEquals(56d, stats.getSum(), 0.0001);
                // Test first
                Assert.assertEquals(1.0d, stats.getFirstValue(), 0.0001);
                Assert.assertEquals((long) time.toInstant().toEpochMilli(), (long) stats.getFirstTime());
            } else {
                Assert.assertEquals(55d, stats.getSum(), 0.0001);
                // Test first
                Assert.assertEquals(1.0d, stats.getFirstValue(), 0.0001);
                Assert.assertEquals((long) time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
            }
            // Test last
            Assert.assertEquals(10.0d, stats.getLastValue(), 0.0001);
            Assert.assertEquals((long) time.plusHours(12).plusHours(9).toInstant().toEpochMilli(), (long) stats.getLastTime());
            if (counter.getValue() == 1) {
                // Test start (the first start value will be null
                Assert.assertEquals(1.0, stats.getStartValue(), 0.0001);
                // Test count
                Assert.assertEquals(11, stats.getCount());
            } else {
                // Test start (the first start value will be null
                Assert.assertEquals(10.0, stats.getStartValue(), 0.0001);
                // Test count
                Assert.assertEquals(10, stats.getCount());
            }
            // Test delta
            if (counter.getValue() == 1) {
                // 1 to 10
                Assert.assertEquals(9.0, stats.getDelta(), 0.0001);
            } else {
                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.toInstant().toEpochMilli()), 0, false);
    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 4 with TimePeriodBucketCalculator

use of com.serotonin.m2m2.view.quantize2.TimePeriodBucketCalculator in project ma-core-public by infiniteautomation.

the class StartsAndRuntimeListQuantizerTest method testNoStartValueManyValuesPerPeriod.

@Test
public void testNoStartValueManyValuesPerPeriod() throws IOException {
    // Generate data at 12 noon for every day in the period
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    NextTimePeriodAdjuster hourlyAdjuster = new NextTimePeriodAdjuster(TimePeriods.HOURS, 1);
    time = ZonedDateTime.of(2017, 01, 01, 12, 00, 00, 0, zoneId);
    List<IdPointValueTime> data = new ArrayList<>();
    while (time.toInstant().isBefore(to.toInstant())) {
        // Insert 10 values per day
        int value = 1;
        ZonedDateTime daily = ZonedDateTime.ofInstant(time.toInstant(), zoneId);
        for (int i = 0; i < 10; i++) {
            data.add(new IdPointValueTime(1, new MultistateValue(value), daily.toInstant().toEpochMilli()));
            daily = (ZonedDateTime) hourlyAdjuster.adjustInto(daily);
            value = value + 1;
        }
        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());
            // Test first
            Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
            Assert.assertEquals((long) time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(10, stats.getLastValue().getIntegerValue());
            Assert.assertEquals((long) time.plusHours(12).plusHours(9).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(10, stats.getStartValue().getIntegerValue());
            // Test count
            Assert.assertEquals(10, stats.getCount());
            // Ensure data
            if (counter.getValue() == 1) {
                // 1 to 10, was 10 for last 3 hrs of day
                Assert.assertEquals(10, stats.getStartsAndRuntime().size());
                for (StartsAndRuntime value : stats.getData()) {
                    switch((Integer) value.getValue()) {
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                            Assert.assertEquals(1, value.getStarts());
                            Assert.assertEquals(100d * (1d / 12d), value.getPercentage(), 0.0001);
                            Assert.assertEquals(1d / 12d, value.getProportion(), 0.0001);
                            Assert.assertEquals(60 * 60 * 1000, value.getRuntime());
                            break;
                        case 10:
                            Assert.assertEquals(1, value.getStarts());
                            Assert.assertEquals(100d * (3d / 12d), value.getPercentage(), 0.0001);
                            Assert.assertEquals(3d / 12d, value.getProportion(), 0.0001);
                            Assert.assertEquals(3 * 60 * 60 * 1000, value.getRuntime());
                            break;
                    }
                }
            } else {
                // Start in state 10 for 12 hrs more
                Assert.assertEquals(10, stats.getStartsAndRuntime().size());
                for (StartsAndRuntime value : stats.getData()) {
                    switch((Integer) value.getValue()) {
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                            Assert.assertEquals(1, value.getStarts());
                            Assert.assertEquals(100d * (1d / 24d), value.getPercentage(), 0.0001);
                            Assert.assertEquals(1d / 24d, value.getProportion(), 0.0001);
                            Assert.assertEquals(60 * 60 * 1000, value.getRuntime());
                            break;
                        case 10:
                            Assert.assertEquals(1, value.getStarts());
                            Assert.assertEquals(100d * (15d / 24d), value.getPercentage(), 0.0001);
                            Assert.assertEquals(15d / 24d, value.getProportion(), 0.0001);
                            Assert.assertEquals(15 * 60 * 60 * 1000, value.getRuntime());
                            break;
                    }
                }
            }
            // 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) Test(org.junit.Test)

Example 5 with TimePeriodBucketCalculator

use of com.serotonin.m2m2.view.quantize2.TimePeriodBucketCalculator in project ma-core-public by infiniteautomation.

the class StartsAndRuntimeListQuantizerTest method testStartValueManyValuesPerPeriod.

@Test
public void testStartValueManyValuesPerPeriod() throws IOException {
    // Generate data at 12 noon for every day in the period
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    NextTimePeriodAdjuster hourlyAdjuster = new NextTimePeriodAdjuster(TimePeriods.HOURS, 1);
    time = ZonedDateTime.of(2017, 01, 01, 12, 00, 00, 0, zoneId);
    List<IdPointValueTime> data = new ArrayList<>();
    while (time.toInstant().isBefore(to.toInstant())) {
        // Insert 10 values per day
        int value = 1;
        ZonedDateTime daily = ZonedDateTime.ofInstant(time.toInstant(), zoneId);
        for (int i = 0; i < 10; i++) {
            data.add(new IdPointValueTime(1, new MultistateValue(value), daily.toInstant().toEpochMilli()));
            daily = (ZonedDateTime) hourlyAdjuster.adjustInto(daily);
            value = value + 1;
        }
        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());
            // Test first
            Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
            Assert.assertEquals((long) time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(10, stats.getLastValue().getIntegerValue());
            Assert.assertEquals((long) time.plusHours(12).plusHours(9).toInstant().toEpochMilli(), (long) stats.getLastTime());
            // Test start (the first start value will be null
            if (counter.getValue() == 1)
                Assert.assertEquals(1, stats.getStartValue().getIntegerValue());
            else
                Assert.assertEquals(10, stats.getStartValue().getIntegerValue());
            // Test count
            Assert.assertEquals(10, stats.getCount());
            // Ensure data
            if (counter.getValue() == 1) {
                // 1 to 10, was 10 for last 3 hrs of day
                Assert.assertEquals(10, stats.getStartsAndRuntime().size());
                for (StartsAndRuntime value : stats.getData()) {
                    switch((Integer) value.getValue()) {
                        case 1:
                            Assert.assertEquals(1, value.getStarts());
                            Assert.assertEquals(100d * (13d / 24d), value.getPercentage(), 0.0001);
                            Assert.assertEquals(13d / 24d, value.getProportion(), 0.0001);
                            Assert.assertEquals(13 * 60 * 60 * 1000, value.getRuntime());
                            break;
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                            Assert.assertEquals(1, value.getStarts());
                            Assert.assertEquals(100d * (1d / 24d), value.getPercentage(), 0.0001);
                            Assert.assertEquals(1d / 24d, value.getProportion(), 0.0001);
                            Assert.assertEquals(60 * 60 * 1000, value.getRuntime());
                            break;
                        case 10:
                            Assert.assertEquals(1, value.getStarts());
                            Assert.assertEquals(100d * (3d / 24d), value.getPercentage(), 0.0001);
                            Assert.assertEquals(3d / 24d, value.getProportion(), 0.0001);
                            Assert.assertEquals(3 * 60 * 60 * 1000, value.getRuntime());
                            break;
                    }
                }
            } else {
                // Start in state 10 for 12 hrs more
                Assert.assertEquals(10, stats.getStartsAndRuntime().size());
                for (StartsAndRuntime value : stats.getData()) {
                    switch((Integer) value.getValue()) {
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 9:
                            Assert.assertEquals(1, value.getStarts());
                            Assert.assertEquals(100d * (1d / 24d), value.getPercentage(), 0.0001);
                            Assert.assertEquals(1d / 24d, value.getProportion(), 0.0001);
                            Assert.assertEquals(60 * 60 * 1000, value.getRuntime());
                            break;
                        case 10:
                            Assert.assertEquals(1, value.getStarts());
                            Assert.assertEquals(100d * (15d / 24d), value.getPercentage(), 0.0001);
                            Assert.assertEquals(15d / 24d, value.getProportion(), 0.0001);
                            Assert.assertEquals(15 * 60 * 60 * 1000, value.getRuntime());
                            break;
                    }
                }
            }
            // 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)

Aggregations

NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)21 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)21 IOException (java.io.IOException)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 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)1 RestValidationResult (com.infiniteautomation.mango.rest.v2.model.RestValidationResult)1 RTException (com.serotonin.m2m2.rt.RTException)1 TimePeriodBucketCalculator (com.serotonin.m2m2.view.quantize2.TimePeriodBucketCalculator)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 User (com.serotonin.m2m2.vo.User)1