Search in sources :

Example 1 with ValueChangeCounter

use of com.serotonin.m2m2.view.stats.ValueChangeCounter in project ma-modules-public by infiniteautomation.

the class IdPointValueStatisticsQuantizerJsonCallback method closePeriod.

/*
     * (non-Javadoc)
     * 
     * @see
     * com.serotonin.m2m2.web.mvc.rest.v1.statistics.ParentStatisticsQuantizerCallback#closePeriod(
     * java.util.Map, long)
     */
@Override
public void closePeriod(Map<Integer, StatisticsGenerator> periodStatsMap, long periodStartTime) {
    if (limiter.limited())
        return;
    try {
        // Write out the period start time
        this.jgen.writeStartObject();
        writeTimestamp(periodStartTime);
        this.jgen.writeStringField(ROLLUP, this.rollup.name());
        Iterator<Integer> it = periodStatsMap.keySet().iterator();
        while (it.hasNext()) {
            Integer id = it.next();
            StatisticsGenerator stats = periodStatsMap.get(id);
            DataPointVO vo = this.voMap.get(id);
            if (stats instanceof ValueChangeCounter) {
                ValueChangeCounter statisticsGenerator = (ValueChangeCounter) stats;
                switch(rollup) {
                    case ALL:
                        this.jgen.writeObjectFieldStart(vo.getXid());
                        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
                            this.writeImageValue(statisticsGenerator.getFirstValue(), statisticsGenerator.getFirstTime(), periodStartTime, vo, RollupEnum.FIRST.name());
                        else
                            this.writeDataValue(periodStartTime, statisticsGenerator.getFirstValue(), vo, RollupEnum.FIRST.name());
                        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
                            this.writeImageValue(statisticsGenerator.getLastValue(), statisticsGenerator.getLastTime(), periodStartTime, vo, RollupEnum.LAST.name());
                        else
                            this.writeDataValue(periodStartTime, statisticsGenerator.getLastValue(), vo, RollupEnum.LAST.name());
                        this.jgen.writeNumberField(RollupEnum.COUNT.name(), statisticsGenerator.getCount());
                        this.jgen.writeEndObject();
                        break;
                    case START:
                        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
                            this.writeImageValue(statisticsGenerator.getStartValue(), periodStartTime, periodStartTime, vo, vo.getXid());
                        else
                            this.writeDataValue(periodStartTime, statisticsGenerator.getStartValue(), vo, vo.getXid());
                        break;
                    case FIRST:
                        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
                            this.writeImageValue(statisticsGenerator.getFirstValue(), statisticsGenerator.getFirstTime(), periodStartTime, vo, vo.getXid());
                        else
                            this.writeDataValue(periodStartTime, statisticsGenerator.getFirstValue(), vo, vo.getXid());
                        break;
                    case LAST:
                        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
                            this.writeImageValue(statisticsGenerator.getLastValue(), statisticsGenerator.getLastTime(), periodStartTime, vo, vo.getXid());
                        else
                            this.writeDataValue(periodStartTime, statisticsGenerator.getLastValue(), vo, vo.getXid());
                        break;
                    case COUNT:
                        this.jgen.writeNumberField(vo.getXid(), statisticsGenerator.getCount());
                        break;
                    default:
                        // Default to first
                        this.writeDataValue(periodStartTime, statisticsGenerator.getFirstValue(), vo, vo.getXid());
                }
            } else if (stats instanceof AnalogStatistics) {
                AnalogStatistics statisticsGenerator = (AnalogStatistics) stats;
                switch(rollup) {
                    case ALL:
                        this.jgen.writeObjectFieldStart(vo.getXid());
                        this.writeDouble(statisticsGenerator.getAverage(), vo, RollupEnum.AVERAGE.name());
                        this.writeDouble(statisticsGenerator.getDelta(), vo, RollupEnum.DELTA.name());
                        this.writeDouble(statisticsGenerator.getMinimumValue(), vo, RollupEnum.MINIMUM.name());
                        this.writeDouble(statisticsGenerator.getMaximumValue(), vo, RollupEnum.MAXIMUM.name());
                        Double acc = statisticsGenerator.getLastValue();
                        if (acc == null) {
                            acc = statisticsGenerator.getMaximumValue();
                        }
                        this.writeDouble(acc, vo, RollupEnum.ACCUMULATOR.name());
                        this.writeDouble(statisticsGenerator.getSum(), vo, RollupEnum.SUM.name());
                        this.writeDouble(statisticsGenerator.getFirstValue(), vo, RollupEnum.FIRST.name());
                        this.writeDouble(statisticsGenerator.getLastValue(), vo, RollupEnum.LAST.name());
                        this.writeIntegral(statisticsGenerator.getIntegral(), vo, RollupEnum.INTEGRAL.name());
                        this.jgen.writeNumberField(RollupEnum.COUNT.name(), statisticsGenerator.getCount());
                        this.jgen.writeEndObject();
                        break;
                    case AVERAGE:
                        this.writeDouble(statisticsGenerator.getAverage(), vo, vo.getXid());
                        break;
                    case DELTA:
                        this.writeDouble(statisticsGenerator.getDelta(), vo, vo.getXid());
                        break;
                    case MINIMUM:
                        this.writeDouble(statisticsGenerator.getMinimumValue(), vo, vo.getXid());
                        break;
                    case MAXIMUM:
                        this.writeDouble(statisticsGenerator.getMaximumValue(), vo, vo.getXid());
                        break;
                    // TODO This should be removed after discussion with Jared
                    case ACCUMULATOR:
                        Double accumulatorValue = statisticsGenerator.getLastValue();
                        if (accumulatorValue == null) {
                            accumulatorValue = statisticsGenerator.getMaximumValue();
                        }
                        this.writeDouble(accumulatorValue, vo, vo.getXid());
                        break;
                    case SUM:
                        this.writeDouble(statisticsGenerator.getSum(), vo, vo.getXid());
                        break;
                    case START:
                        this.writeDouble(statisticsGenerator.getStartValue(), vo, vo.getXid());
                        break;
                    case FIRST:
                        this.writeDouble(statisticsGenerator.getFirstValue(), vo, vo.getXid());
                        break;
                    case LAST:
                        this.writeDouble(statisticsGenerator.getLastValue(), vo, vo.getXid());
                        break;
                    case COUNT:
                        this.jgen.writeNumberField(vo.getXid(), statisticsGenerator.getCount());
                        break;
                    case INTEGRAL:
                        this.writeIntegral(statisticsGenerator.getIntegral(), vo, vo.getXid());
                        break;
                    default:
                        // Default to first
                        this.writeDouble(statisticsGenerator.getFirstValue(), vo, vo.getXid());
                }
            } else {
                throw new ShouldNeverHappenException("Unsuported statistics type: " + stats.getClass().getName());
            }
        }
        this.jgen.writeEndObject();
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) StatisticsGenerator(com.serotonin.m2m2.view.stats.StatisticsGenerator) ValueChangeCounter(com.serotonin.m2m2.view.stats.ValueChangeCounter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) AnalogStatistics(com.serotonin.m2m2.view.stats.AnalogStatistics) IOException(java.io.IOException)

Example 2 with ValueChangeCounter

use of com.serotonin.m2m2.view.stats.ValueChangeCounter in project ma-modules-public by infiniteautomation.

the class IdPointValueStatisticsQuantizerCsvCallback method closePeriod.

/* (non-Javadoc)
	 * @see com.serotonin.m2m2.web.mvc.rest.v1.statistics.ParentStatisticsQuantizerCallback#closePeriod(java.util.Map, long)
	 */
@Override
public void closePeriod(Map<Integer, StatisticsGenerator> periodStatsMap, long periodStartTime) {
    try {
        if (!wroteHeaders) {
            this.writer.writeNext(headers);
            this.wroteHeaders = true;
        }
        // Write out the period start time
        if (dateFormatter == null)
            this.rowData[1] = Long.toString(periodStartTime);
        else
            this.rowData[1] = dateFormatter.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(periodStartTime), TimeZone.getDefault().toZoneId()));
        Iterator<Integer> it = periodStatsMap.keySet().iterator();
        while (it.hasNext()) {
            Integer id = it.next();
            StatisticsGenerator stats = periodStatsMap.get(id);
            DataPointVO vo = this.voMap.get(id);
            if (stats instanceof ValueChangeCounter) {
                ValueChangeCounter statisticsGenerator = (ValueChangeCounter) stats;
                switch(rollup) {
                    case FIRST:
                        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
                            this.writeImageValue(statisticsGenerator.getFirstValue(), statisticsGenerator.getFirstTime(), periodStartTime, vo);
                        else
                            this.writeDataValue(periodStartTime, statisticsGenerator.getFirstValue(), vo);
                        break;
                    case LAST:
                        if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
                            this.writeImageValue(statisticsGenerator.getLastValue(), statisticsGenerator.getLastTime(), periodStartTime, vo);
                        else
                            this.writeDataValue(periodStartTime, statisticsGenerator.getLastValue(), vo);
                        break;
                    case COUNT:
                        this.rowData[this.columnMap.get(vo.getId())] = Integer.toString(statisticsGenerator.getCount());
                        break;
                    default:
                        // Default to first
                        this.writeDataValue(periodStartTime, statisticsGenerator.getFirstValue(), vo);
                }
            } else if (stats instanceof AnalogStatistics) {
                AnalogStatistics statisticsGenerator = (AnalogStatistics) stats;
                switch(rollup) {
                    case AVERAGE:
                        this.writeDouble(statisticsGenerator.getAverage(), vo);
                        break;
                    case DELTA:
                        this.writeDouble(statisticsGenerator.getDelta(), vo);
                        break;
                    case MINIMUM:
                        this.writeDouble(statisticsGenerator.getMinimumValue(), vo);
                        break;
                    case MAXIMUM:
                        this.writeDouble(statisticsGenerator.getMaximumValue(), vo);
                        break;
                    // TODO This should be removed after discussion with Jared
                    case ACCUMULATOR:
                        Double accumulatorValue = statisticsGenerator.getLastValue();
                        if (accumulatorValue == null) {
                            accumulatorValue = statisticsGenerator.getMaximumValue();
                        }
                        this.writeDouble(accumulatorValue, vo);
                        break;
                    case SUM:
                        this.writeDouble(statisticsGenerator.getSum(), vo);
                        break;
                    case START:
                        this.writeDouble(statisticsGenerator.getStartValue(), vo);
                        break;
                    case FIRST:
                        this.writeDouble(statisticsGenerator.getFirstValue(), vo);
                        break;
                    case LAST:
                        this.writeDouble(statisticsGenerator.getLastValue(), vo);
                        break;
                    case COUNT:
                        this.rowData[this.columnMap.get(vo.getId())] = Integer.toString(statisticsGenerator.getCount());
                        break;
                    case INTEGRAL:
                        this.writeIntegral(statisticsGenerator.getIntegral(), vo);
                        break;
                    default:
                        // Default to first
                        this.writeDouble(statisticsGenerator.getFirstValue(), vo);
                }
            } else {
                throw new ShouldNeverHappenException("Unsuported statistics type: " + stats.getClass().getName());
            }
        }
        this.writer.writeNext(this.rowData);
        // Clear out the row data
        for (int i = 1; i < this.rowData.length; i++) this.rowData[i] = null;
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) StatisticsGenerator(com.serotonin.m2m2.view.stats.StatisticsGenerator) ValueChangeCounter(com.serotonin.m2m2.view.stats.ValueChangeCounter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) AnalogStatistics(com.serotonin.m2m2.view.stats.AnalogStatistics) IOException(java.io.IOException)

Example 3 with ValueChangeCounter

use of com.serotonin.m2m2.view.stats.ValueChangeCounter 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 4 with ValueChangeCounter

use of com.serotonin.m2m2.view.stats.ValueChangeCounter 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 5 with ValueChangeCounter

use of com.serotonin.m2m2.view.stats.ValueChangeCounter 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)

Aggregations

IOException (java.io.IOException)10 ValueChangeCounter (com.infiniteautomation.mango.statistics.ValueChangeCounter)8 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 ZonedDateTime (java.time.ZonedDateTime)7 ArrayList (java.util.ArrayList)7 MutableInt (org.apache.commons.lang3.mutable.MutableInt)7 Test (org.junit.Test)7 ValueChangeCounter (com.serotonin.m2m2.view.stats.ValueChangeCounter)5 AnalogStatistics (com.serotonin.m2m2.view.stats.AnalogStatistics)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 StatisticsGenerator (com.serotonin.m2m2.view.stats.StatisticsGenerator)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 InvalidArgumentException (com.serotonin.InvalidArgumentException)1 MessageFormatDirective (com.serotonin.m2m2.email.MessageFormatDirective)1 SubjectDirective (com.serotonin.m2m2.email.SubjectDirective)1 UsedImagesDirective (com.serotonin.m2m2.email.UsedImagesDirective)1 PointValueFacade (com.serotonin.m2m2.rt.dataImage.PointValueFacade)1