Search in sources :

Example 61 with QueryCancelledException

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

the class StartsAndRuntimeListQuantizerTest method testNoData.

@Test
public void testNoData() throws QueryCancelledException {
    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);
    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(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());
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.done();
    Assert.assertEquals(Integer.valueOf(31), counter.getValue());
}
Also used : 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 62 with QueryCancelledException

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

the class StartsAndRuntimeListQuantizerTest 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);
    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
            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, 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) Map(java.util.Map) Test(org.junit.Test)

Example 63 with QueryCancelledException

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

the class NumericPointValueDaoTestHelper method testLatestExceptionInCallback.

/* Latest Multiple w/ callback Test Methods */
public void testLatestExceptionInCallback() {
    MutableInt count = new MutableInt();
    MutableInt mutableIndex = new MutableInt();
    MutableLong timestamp = new MutableLong(endTs);
    try {
        this.dao.getPointValuesCombined(vos, null, endTs, null, TimeOrder.DESCENDING, new Consumer<>() {

            int seriesIdCounter = data.get(vo1.getSeriesId()).size() - 1;

            // Start before last 3 samples (extra)
            int seriesId2Counter = data.get(vo2.getSeriesId()).size() - 4;

            @Override
            public void accept(IdPointValueTime value) {
                mutableIndex.increment();
                count.increment();
                if (value.getTime() > timestamp.getValue())
                    Assert.fail("Timestamp out of order.");
                timestamp.setValue(value.getTime());
                if (value.getSeriesId() == vo2.getSeriesId()) {
                    // Check value
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesId2Counter).getDoubleValue(), value.getDoubleValue(), 0.001);
                    // Check time
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesId2Counter).getTime(), value.getTime());
                    seriesId2Counter--;
                } else {
                    // Check value
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesIdCounter).getDoubleValue(), value.getDoubleValue(), 0.001);
                    // Check time
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesIdCounter).getTime(), value.getTime());
                    seriesIdCounter--;
                }
                if (count.getValue() == 20)
                    throw new QueryCancelledException(new Exception("Exception Test"));
            }
        });
    } catch (QueryCancelledException e) {
    // noop
    }
    // Total is all samples + the extra 3 at the beginning of series2
    Assert.assertEquals(Integer.valueOf(20), count.getValue());
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) MutableInt(org.apache.commons.lang3.mutable.MutableInt) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException)

Example 64 with QueryCancelledException

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

the class NumericPointValueDaoTestHelper method testBookendExceptionInRowCallback.

public void testBookendExceptionInRowCallback() {
    MutableInt count = new MutableInt();
    MutableInt mutableIndex = new MutableInt();
    MutableLong timestamp = new MutableLong(startTs - 1);
    try {
        // Skip first 3
        // Check value
        // Check time
        // Check value is null as no data exists before the startTs for series 1
        // Check time
        // Check value
        // Check time
        // Check value
        // Check time
        this.dao.wideBookendQueryCombined(vos, startTs - 1, endTs, null, new WideCallback<IdPointValueTime>() {

            int seriesIdCounter = 0;

            // Skip first 3
            int seriesId2Counter = 3;

            @Override
            public void firstValue(IdPointValueTime value, boolean bookend) {
                mutableIndex.increment();
                if (value.getTime() < timestamp.getValue())
                    Assert.fail("Timestamp out of order.");
                timestamp.setValue(value.getTime());
                if (value.getSeriesId() == vo2.getSeriesId()) {
                    // Check value
                    Assert.assertEquals(data.get(value.getSeriesId()).get(2).getDoubleValue(), value.getDoubleValue(), 0.001);
                // Check time
                } else {
                    // Check value is null as no data exists before the startTs for series 1
                    Assert.assertNull(value.getValue());
                // Check time
                }
                Assert.assertEquals(startTs - 1, value.getTime());
                Assert.assertTrue(bookend);
            }

            @Override
            public void accept(IdPointValueTime value) {
                mutableIndex.increment();
                count.increment();
                if (value.getTime() < timestamp.getValue())
                    Assert.fail("Timestamp out of order.");
                timestamp.setValue(value.getTime());
                if (value.getSeriesId() == vo2.getSeriesId()) {
                    // Check value
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesId2Counter).getDoubleValue(), value.getDoubleValue(), 0.001);
                    // Check time
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesId2Counter).getTime(), value.getTime());
                    seriesId2Counter++;
                } else {
                    // Check value
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesIdCounter).getDoubleValue(), value.getDoubleValue(), 0.001);
                    // Check time
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesIdCounter).getTime(), value.getTime());
                    seriesIdCounter++;
                }
                if (count.getValue() == 20)
                    throw new QueryCancelledException(new Exception("Exception Test"));
            }

            @Override
            public void lastValue(IdPointValueTime value, boolean bookend) {
                Assert.fail("Query cancelled, should not get last value");
            }
        });
    } catch (QueryCancelledException e) {
    // noop
    }
    Assert.assertEquals(Integer.valueOf(20), count.getValue());
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) MutableInt(org.apache.commons.lang3.mutable.MutableInt) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException)

Example 65 with QueryCancelledException

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

the class NumericPointValueDaoTestHelper method testBetweenExceptionInCallback.

/* Values Between Tests */
public void testBetweenExceptionInCallback() {
    MutableInt count = new MutableInt();
    MutableInt mutableIndex = new MutableInt();
    MutableLong timestamp = new MutableLong(startTs);
    try {
        this.dao.getPointValuesCombined(vos, startTs, endTs, null, TimeOrder.ASCENDING, new Consumer<>() {

            int seriesIdCounter = 0;

            // Skip first 3
            int seriesId2Counter = 3;

            @Override
            public void accept(IdPointValueTime value) {
                mutableIndex.increment();
                count.increment();
                if (value.getTime() < timestamp.getValue())
                    Assert.fail("Timestamp out of order.");
                timestamp.setValue(value.getTime());
                if (value.getSeriesId() == vo2.getSeriesId()) {
                    // Check value
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesId2Counter).getDoubleValue(), value.getDoubleValue(), 0.001);
                    // Check time
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesId2Counter).getTime(), value.getTime());
                    seriesId2Counter++;
                } else {
                    // Check value
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesIdCounter).getDoubleValue(), value.getDoubleValue(), 0.001);
                    // Check time
                    Assert.assertEquals(data.get(value.getSeriesId()).get(seriesIdCounter).getTime(), value.getTime());
                    seriesIdCounter++;
                }
                if (count.getValue() == 20)
                    throw new QueryCancelledException(new Exception("Exception Test"));
            }
        });
    } catch (QueryCancelledException e) {
    // noop
    }
    Assert.assertEquals(Integer.valueOf(20), count.getValue());
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) MutableInt(org.apache.commons.lang3.mutable.MutableInt) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException)

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