Search in sources :

Example 11 with QueryCancelledException

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

the class ValueChangeCounterQuantizerTest 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);
    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());
            // 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
            // Test Changes
            Assert.assertEquals(10, 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 12 with QueryCancelledException

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

the class MultiPointStatisticsStream method processRow.

@Override
protected void processRow(IdPointValueTime value, boolean firstBookend, boolean lastBookend, boolean cached) throws QueryCancelledException {
    try {
        final DataPointVO vo = voMap.get(value.getSeriesId());
        if (info.isUseCache() != PointValueTimeCacheControl.NONE && !cached)
            if (!processValueThroughCache(value, firstBookend, lastBookend))
                return;
        StatisticsGenerator generator = statsMap.compute(value.getSeriesId(), (k, v) -> {
            if (v == null) {
                switch(vo.getPointLocator().getDataType()) {
                    case BINARY:
                    case MULTISTATE:
                        v = new StartsAndRuntimeList(info.getFromMillis(), info.getToMillis(), value);
                        break;
                    case ALPHANUMERIC:
                        v = new ValueChangeCounter(info.getFromMillis(), info.getToMillis(), value);
                        break;
                    case NUMERIC:
                        v = new AnalogStatistics(info.getFromMillis(), info.getToMillis(), value);
                        break;
                    default:
                        throw new ShouldNeverHappenException("Invalid Data Type: " + voMap.get(value.getSeriesId()).getPointLocator().getDataType());
                }
            }
            if (!lastBookend && !firstBookend) {
                v.addValueTime(value);
            }
            return v;
        });
        if (lastBookend) {
            generator.done();
            this.writer.writeStartObject(vo.getXid());
            DataPointStatisticsGenerator gen = new DataPointStatisticsGenerator(vo, generator);
            // Pre-process the fields
            boolean rendered = false;
            Set<PointValueField> fields = new HashSet<>();
            for (PointValueField field : this.writer.getInfo().getFields()) {
                if (field == PointValueField.RENDERED) {
                    rendered = true;
                } else if (field == PointValueField.ANNOTATION) {
                    continue;
                } else {
                    fields.add(field);
                }
            }
            // Remove the Value field we will write it after
            fields.remove(PointValueField.VALUE);
            for (PointValueField field : fields) {
                field.writeValue(gen, info, Common.getTranslations(), false, writer);
            }
            this.writer.writeAllStatistics(generator, vo, rendered, fields.contains(PointValueField.RAW));
            this.writer.writeEndObject();
        }
    } catch (IOException e) {
        throw new QueryCancelledException(e);
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointStatisticsGenerator(com.infiniteautomation.mango.rest.latest.model.pointValue.quantize.DataPointStatisticsGenerator) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) IOException(java.io.IOException) StatisticsGenerator(com.serotonin.m2m2.view.stats.StatisticsGenerator) DataPointStatisticsGenerator(com.infiniteautomation.mango.rest.latest.model.pointValue.quantize.DataPointStatisticsGenerator) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) ValueChangeCounter(com.infiniteautomation.mango.statistics.ValueChangeCounter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) PointValueField(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField) HashSet(java.util.HashSet)

Example 13 with QueryCancelledException

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

the class PointValueTimeStreamJsonSerializer method serialize.

@Override
public void serialize(PointValueTimeStream<T, INFO> value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    PointValueTimeWriter writer = new PointValueTimeJsonWriter(value.getQueryInfo(), jgen);
    value.setContentType(StreamContentType.JSON);
    try {
        value.start(writer);
        value.streamData(writer);
        value.finish(writer);
        jgen.flush();
    } catch (QueryCancelledException e) {
        LOG.info("Query cancelled.", e);
    }
}
Also used : PointValueTimeWriter(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeWriter) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) PointValueTimeJsonWriter(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeJsonWriter)

Example 14 with QueryCancelledException

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

the class MultiDataPointStatisticsQuantizerStream method firstValue.

@Override
public void firstValue(IdPointValueTime value, boolean bookend) {
    try {
        DataPointStatisticsQuantizer<?> quantizer = this.quantizerMap.get(value.getSeriesId());
        if (!info.isSingleArray())
            writer.writeStartArray(quantizer.vo.getXid());
        updateQuantizers(value);
        quantizer.firstValue(value, bookend);
    } catch (IOException e) {
        throw new QueryCancelledException(e);
    }
}
Also used : IOException(java.io.IOException) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException)

Example 15 with QueryCancelledException

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

the class MultiDataPointDefaultRollupStatisticsQuantizerStream method firstValue.

@Override
public void firstValue(IdPointValueTime value, boolean bookend) {
    try {
        if (!useSimplify) {
            super.firstValue(value, bookend);
            return;
        }
        DataPointStatisticsQuantizer<?> quantizer = this.quantizerMap.get(value.getSeriesId());
        updateQuantizers(value);
        quantizer.firstValue(value, bookend);
    } catch (IOException e) {
        throw new QueryCancelledException(e);
    }
}
Also used : IOException(java.io.IOException) 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