Search in sources :

Example 51 with QueryCancelledException

use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-core-public by MangoAutomation.

the class StartsAndRuntimeListQuantizerTest method testStartValueNoPeriodValues.

@Test
public void testStartValueNoPeriodValues() throws QueryCancelledException {
    // 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 QueryCancelledException {
            counter.increment();
            StartsAndRuntimeList stats = 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()), true);
    quantizer.done();
    Assert.assertEquals(Integer.valueOf(31), counter.getValue());
}
Also used : IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) 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) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 52 with QueryCancelledException

use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-core-public by MangoAutomation.

the class StartsAndRuntimeListQuantizerTest method testStartValueOneValuePerPeriod.

@Test
public void testStartValueOneValuePerPeriod() throws QueryCancelledException {
    // 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 QueryCancelledException {
            counter.increment();
            StartsAndRuntimeList stats = 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(sampleTime.toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(1, stats.getLastValue().getIntegerValue());
            Assert.assertEquals(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()), true);
    for (int count = 0; count < data.size(); count++) quantizer.accept(data.get(count));
    quantizer.lastValue(data.get(data.size() - 1), true);
    quantizer.done();
    Assert.assertEquals(Integer.valueOf(31), counter.getValue());
}
Also used : ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) 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) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 53 with QueryCancelledException

use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-core-public by MangoAutomation.

the class StartsAndRuntimeListQuantizerTest method testNoStartValueManyValuesPerPeriod.

@Test
public void testNoStartValueManyValuesPerPeriod() throws QueryCancelledException {
    // 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 QueryCancelledException {
            counter.increment();
            StartsAndRuntimeList stats = 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(time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(10, stats.getLastValue().getIntegerValue());
            Assert.assertEquals(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, true);
    for (int count = 0; count < data.size(); count++) quantizer.accept(data.get(count));
    quantizer.lastValue(data.get(data.size() - 1), true);
    quantizer.done();
    Assert.assertEquals(Integer.valueOf(31), counter.getValue());
}
Also used : ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) 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) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 54 with QueryCancelledException

use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-core-public by MangoAutomation.

the class ValueChangeCounterQuantizerTest method testNoStartValueOneValuePerPeriod.

@Test
public void testNoStartValueOneValuePerPeriod() throws QueryCancelledException {
    // 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 QueryCancelledException {
            counter.increment();
            ValueChangeCounter stats = 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(sampleTime.toInstant().toEpochMilli(), (long) stats.getFirstTime());
            // Test last
            Assert.assertEquals(1, stats.getLastValue().getIntegerValue());
            Assert.assertEquals(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, true);
    for (int count = 0; count < data.size(); count++) quantizer.accept(data.get(count));
    quantizer.lastValue(data.get(data.size() - 1), true);
    quantizer.done();
    Assert.assertEquals(Integer.valueOf(31), counter.getValue());
}
Also used : ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) 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) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 55 with QueryCancelledException

use of com.infiniteautomation.mango.db.query.QueryCancelledException in project ma-core-public by MangoAutomation.

the class ValueChangeCounterQuantizerTest method testStartValueAtStartManyValuesPerPeriod.

@Test
public void testStartValueAtStartManyValuesPerPeriod() throws QueryCancelledException {
    // 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);
    ValueChangeCounterQuantizer quantizer = new ValueChangeCounterQuantizer(bc, new StatisticsGeneratorQuantizerCallback<ValueChangeCounter>() {

        @Override
        public void quantizedStatistics(ValueChangeCounter statisticsGenerator) throws QueryCancelledException {
            counter.increment();
            ValueChangeCounter stats = statisticsGenerator;
            // Test periodStart
            Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
            // Test periiodEnd
            Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
            if (counter.getValue() == 1) {
                // Test first
                Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
                Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getFirstTime());
            } else {
                // Test first
                Assert.assertEquals(1, stats.getFirstValue().getIntegerValue());
                Assert.assertEquals(time.plusHours(12).toInstant().toEpochMilli(), (long) stats.getFirstTime());
            }
            // Test last
            Assert.assertEquals(10, stats.getLastValue().getIntegerValue());
            Assert.assertEquals(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());
                // Test count
                Assert.assertEquals(11, stats.getCount());
                // Test changes
                Assert.assertEquals(9, stats.getChanges());
            } else {
                Assert.assertEquals(10, stats.getStartValue().getIntegerValue());
                // Test count
                Assert.assertEquals(10, stats.getCount());
                // Test changes
                Assert.assertEquals(10, stats.getChanges());
            }
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(new IdPointValueTime(1, new MultistateValue(1), time.toInstant().toEpochMilli()), false);
    for (int count = 0; count < data.size(); count++) quantizer.accept(data.get(count));
    quantizer.lastValue(data.get(data.size() - 1), true);
    quantizer.done();
    Assert.assertEquals(Integer.valueOf(31), counter.getValue());
}
Also used : ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) 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) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Aggregations

QueryCancelledException (com.infiniteautomation.mango.db.query.QueryCancelledException)65 MutableInt (org.apache.commons.lang3.mutable.MutableInt)56 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)50 NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)48 ZonedDateTime (java.time.ZonedDateTime)48 Test (org.junit.Test)48 ArrayList (java.util.ArrayList)30 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)28 AnalogStatistics (com.infiniteautomation.mango.statistics.AnalogStatistics)17 StartsAndRuntimeList (com.infiniteautomation.mango.statistics.StartsAndRuntimeList)17 ValueChangeCounter (com.infiniteautomation.mango.statistics.ValueChangeCounter)17 StartsAndRuntime (com.infiniteautomation.mango.statistics.StartsAndRuntime)14 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)14 MutableLong (org.apache.commons.lang3.mutable.MutableLong)8 IOException (java.io.IOException)7 Map (java.util.Map)3 PointValueField (com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField)2 PointValueTimeWriter (com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeWriter)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)1