Search in sources :

Example 1 with ValueChangeCounterQuantizer

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

the class ValueChangeCounterQuantizerTest 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);
    ValueChangeCounterQuantizer quantizer = new ValueChangeCounterQuantizer(bc, new StatisticsGeneratorQuantizerCallback<ValueChangeCounter>() {

        @Override
        public void quantizedStatistics(ValueChangeCounter statisticsGenerator) throws IOException {
            counter.increment();
            ValueChangeCounter stats = (ValueChangeCounter) statisticsGenerator;
            // Test periodStart
            Assert.assertEquals(time.toInstant().toEpochMilli(), stats.getPeriodStartTime());
            // Test periodEnd
            Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
            if (counter.getValue() == 1) {
                // Test Changes
                Assert.assertEquals(1, stats.getChanges());
            } else {
                // No data in other periods
                // Test Changes
                Assert.assertEquals(0, stats.getChanges());
            }
            // Move to next period time
            time = (ZonedDateTime) adjuster.adjustInto(time);
        }
    });
    quantizer.firstValue(new IdPointValueTime(1, new MultistateValue(1), time.toInstant().toEpochMilli()), 0, false);
    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) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ValueChangeCounter(com.infiniteautomation.mango.statistics.ValueChangeCounter) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 2 with ValueChangeCounterQuantizer

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

the class ValueChangeCounterQuantizerTest 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);
    ValueChangeCounterQuantizer quantizer = new ValueChangeCounterQuantizer(bc, new StatisticsGeneratorQuantizerCallback<ValueChangeCounter>() {

        @Override
        public void quantizedStatistics(ValueChangeCounter statisticsGenerator) throws IOException {
            counter.increment();
            ValueChangeCounter stats = (ValueChangeCounter) 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
            // Test Changes
            Assert.assertEquals(10, stats.getChanges());
            // 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) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ValueChangeCounter(com.infiniteautomation.mango.statistics.ValueChangeCounter) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 3 with ValueChangeCounterQuantizer

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

the class ValueChangeCounterQuantizerTest 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);
    ValueChangeCounterQuantizer quantizer = new ValueChangeCounterQuantizer(bc, new StatisticsGeneratorQuantizerCallback<ValueChangeCounter>() {

        @Override
        public void quantizedStatistics(ValueChangeCounter statisticsGenerator) throws IOException {
            counter.increment();
            ValueChangeCounter stats = (ValueChangeCounter) 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 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, 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) ZonedDateTime(java.time.ZonedDateTime) MutableInt(org.apache.commons.lang3.mutable.MutableInt) ValueChangeCounter(com.infiniteautomation.mango.statistics.ValueChangeCounter) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) Test(org.junit.Test)

Example 4 with ValueChangeCounterQuantizer

use of com.serotonin.m2m2.view.quantize2.ValueChangeCounterQuantizer in project ma-modules-public by infiniteautomation.

the class AbstractPointValueRollupCalculator method createQuantizer.

/**
 * Create the proper quantizer for csv generation
 * @param vo
 * @param startValue
 * @param bc
 * @param writer
 * @return
 */
protected AbstractDataQuantizer createQuantizer(DataPointVO vo, DataValue startValue, BucketCalculator bc, CSVPojoWriter<T> writer, boolean writeXidColumn, boolean writeHeaders) {
    if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC) {
        return new AnalogStatisticsQuantizer(bc, startValue, new NumericPointValueStatisticsQuantizerCsvCallback(writer.getWriter(), vo, this.useRendered, this.unitConversion, this.rollup, writeXidColumn, writeHeaders, this.limit, this.dateTimeFormat, this.timezone));
    } else {
        if (!rollup.nonNumericSupport()) {
            LOG.warn("Invalid non-numeric rollup type: " + rollup);
            // Default to first
            rollup = RollupEnum.FIRST;
        }
        return new ValueChangeCounterQuantizer(bc, startValue, new NonNumericPointValueStatisticsQuantizerCsvCallback(writer.getWriter(), vo, useRendered, unitConversion, this.rollup, writeXidColumn, writeHeaders, this.limit, this.dateTimeFormat, this.timezone));
    }
}
Also used : NonNumericPointValueStatisticsQuantizerCsvCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NonNumericPointValueStatisticsQuantizerCsvCallback) NumericPointValueStatisticsQuantizerCsvCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NumericPointValueStatisticsQuantizerCsvCallback) NonNumericPointValueStatisticsQuantizerCsvCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NonNumericPointValueStatisticsQuantizerCsvCallback) ValueChangeCounterQuantizer(com.serotonin.m2m2.view.quantize2.ValueChangeCounterQuantizer) AnalogStatisticsQuantizer(com.serotonin.m2m2.view.quantize2.AnalogStatisticsQuantizer)

Example 5 with ValueChangeCounterQuantizer

use of com.serotonin.m2m2.view.quantize2.ValueChangeCounterQuantizer in project ma-modules-public by infiniteautomation.

the class AbstractPointValueRollupCalculator method createQuantizer.

/**
 * Create the proper quantizer for json generation
 * @param startValue
 * @param bc
 * @param jgen
 * @return
 */
protected AbstractDataQuantizer createQuantizer(DataPointVO vo, DataValue startValue, BucketCalculator bc, JsonGenerator jgen) {
    if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC) {
        return new AnalogStatisticsQuantizer(bc, startValue, new NumericPointValueStatisticsQuantizerJsonCallback(jgen, vo, this.useRendered, this.unitConversion, this.rollup, this.limit, this.dateTimeFormat, this.timezone));
    } else {
        if (!rollup.nonNumericSupport()) {
            LOG.warn("Invalid non-numeric rollup type: " + rollup);
            // Default to first
            rollup = RollupEnum.FIRST;
        }
        return new ValueChangeCounterQuantizer(bc, startValue, new NonNumericPointValueStatisticsQuantizerJsonCallback(jgen, vo, useRendered, unitConversion, this.rollup, this.limit, this.dateTimeFormat, this.timezone));
    }
}
Also used : NumericPointValueStatisticsQuantizerJsonCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NumericPointValueStatisticsQuantizerJsonCallback) NonNumericPointValueStatisticsQuantizerJsonCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NonNumericPointValueStatisticsQuantizerJsonCallback) ValueChangeCounterQuantizer(com.serotonin.m2m2.view.quantize2.ValueChangeCounterQuantizer) NonNumericPointValueStatisticsQuantizerJsonCallback(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NonNumericPointValueStatisticsQuantizerJsonCallback) AnalogStatisticsQuantizer(com.serotonin.m2m2.view.quantize2.AnalogStatisticsQuantizer)

Aggregations

ValueChangeCounter (com.infiniteautomation.mango.statistics.ValueChangeCounter)7 NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)7 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)7 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)7 IOException (java.io.IOException)7 ZonedDateTime (java.time.ZonedDateTime)7 MutableInt (org.apache.commons.lang3.mutable.MutableInt)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)5 AnalogStatisticsQuantizer (com.serotonin.m2m2.view.quantize2.AnalogStatisticsQuantizer)2 ValueChangeCounterQuantizer (com.serotonin.m2m2.view.quantize2.ValueChangeCounterQuantizer)2 NonNumericPointValueStatisticsQuantizerCsvCallback (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NonNumericPointValueStatisticsQuantizerCsvCallback)1 NonNumericPointValueStatisticsQuantizerJsonCallback (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NonNumericPointValueStatisticsQuantizerJsonCallback)1 NumericPointValueStatisticsQuantizerCsvCallback (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NumericPointValueStatisticsQuantizerCsvCallback)1 NumericPointValueStatisticsQuantizerJsonCallback (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.statistics.NumericPointValueStatisticsQuantizerJsonCallback)1