Search in sources :

Example 11 with NextTimePeriodAdjuster

use of com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster 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 12 with NextTimePeriodAdjuster

use of com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster in project ma-core-public by infiniteautomation.

the class AnalogStatisticsQuantizerTest method testNoData.

@Test
public void testNoData() throws IOException {
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    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
            Assert.assertEquals(Double.NaN, stats.getMinimumValue(), 0.0001);
            Assert.assertEquals(null, stats.getMinimumTime());
            // Test Maximum
            Assert.assertEquals(Double.NaN, stats.getMaximumValue(), 0.0001);
            Assert.assertEquals(null, stats.getMaximumTime());
            // Test Average
            Assert.assertEquals(Double.NaN, stats.getAverage(), 0.0001);
            // Test Integral
            Assert.assertEquals(Double.NaN, 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(null, stats.getStartValue());
            // Test count
            Assert.assertEquals(0, stats.getCount());
            // Test delta
            Assert.assertEquals(Double.NaN, stats.getDelta(), 0.0001);
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.done();
    Assert.assertEquals(new Integer(31), counter.getValue());
}
Also used : ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) IOException(java.io.IOException) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 13 with NextTimePeriodAdjuster

use of com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster 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 14 with NextTimePeriodAdjuster

use of com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster 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 15 with NextTimePeriodAdjuster

use of com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster 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

NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)24 IOException (java.io.IOException)24 ZonedDateTime (java.time.ZonedDateTime)24 MutableInt (org.apache.commons.lang3.mutable.MutableInt)24 Test (org.junit.Test)24 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)21 ArrayList (java.util.ArrayList)15 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)14 AnalogStatistics (com.infiniteautomation.mango.statistics.AnalogStatistics)8 StartsAndRuntimeList (com.infiniteautomation.mango.statistics.StartsAndRuntimeList)8 ValueChangeCounter (com.infiniteautomation.mango.statistics.ValueChangeCounter)8 StartsAndRuntime (com.infiniteautomation.mango.statistics.StartsAndRuntime)7 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)7 Map (java.util.Map)1