use of com.serotonin.m2m2.view.stats.StatisticsGenerator 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());
}
use of com.serotonin.m2m2.view.stats.StatisticsGenerator in project ma-modules-public by infiniteautomation.
the class PointValueTimeJsonWriter method writeAllStatistics.
/*
* (non-Javadoc)
*
* @see
* com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeWriter#writeAllStatistics(
* com.serotonin.m2m2.view.stats.ValueChangeCounter, com.serotonin.m2m2.vo.DataPointVO)
*/
@Override
public void writeAllStatistics(StatisticsGenerator statisticsGenerator, DataPointVO vo) throws IOException {
this.jgen.writeStartObject();
writeTimestamp(statisticsGenerator.getPeriodStartTime());
if (statisticsGenerator instanceof ValueChangeCounter) {
ValueChangeCounter stats = (ValueChangeCounter) statisticsGenerator;
if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
this.writeImageValue(stats.getStartValue(), stats.getPeriodStartTime(), stats.getPeriodStartTime(), vo, RollupEnum.START.name());
else
this.writeDataValue(stats.getPeriodStartTime(), stats.getStartValue(), vo, RollupEnum.START.name());
if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
this.writeImageValue(stats.getFirstValue(), stats.getFirstTime(), stats.getPeriodStartTime(), vo, RollupEnum.FIRST.name());
else
this.writeDataValue(stats.getPeriodStartTime(), stats.getFirstValue(), vo, RollupEnum.FIRST.name());
if (vo.getPointLocator().getDataTypeId() == DataTypes.IMAGE)
this.writeImageValue(stats.getLastValue(), stats.getLastTime(), stats.getPeriodStartTime(), vo, RollupEnum.LAST.name());
else
this.writeDataValue(stats.getPeriodStartTime(), stats.getLastValue(), vo, RollupEnum.LAST.name());
this.jgen.writeNumberField(RollupEnum.COUNT.name(), stats.getCount());
} else if (statisticsGenerator instanceof AnalogStatistics) {
AnalogStatistics stats = (AnalogStatistics) statisticsGenerator;
this.writeDouble(stats.getAverage(), vo, RollupEnum.AVERAGE.name());
this.writeDouble(stats.getDelta(), vo, RollupEnum.DELTA.name());
this.writeDouble(stats.getMinimumValue(), vo, RollupEnum.MINIMUM.name());
this.writeDouble(stats.getMaximumValue(), vo, RollupEnum.MAXIMUM.name());
Double acc = stats.getLastValue();
if (acc == null) {
acc = stats.getMaximumValue();
}
this.writeDouble(acc, vo, RollupEnum.ACCUMULATOR.name());
this.writeDouble(stats.getSum(), vo, RollupEnum.SUM.name());
this.writeDouble(stats.getStartValue(), vo, RollupEnum.START.name());
this.writeDouble(stats.getFirstValue(), vo, RollupEnum.FIRST.name());
this.writeDouble(stats.getLastValue(), vo, RollupEnum.LAST.name());
this.writeIntegral(stats.getIntegral(), vo, RollupEnum.INTEGRAL.name());
this.jgen.writeNumberField(RollupEnum.COUNT.name(), stats.getCount());
}
this.jgen.writeEndObject();
}
use of com.serotonin.m2m2.view.stats.StatisticsGenerator in project ma-core-public by infiniteautomation.
the class AnalogStatisticsQuantizerTest method testStartValueOneValuePerPeriod.
// Test with Start Value and Values
@Test
public void testStartValueOneValuePerPeriod() 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<>();
double value = 1.0;
while (time.toInstant().isBefore(to.toInstant())) {
data.add(new IdPointValueTime(1, new NumericValue(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);
AnalogStatisticsQuantizer quantizer = new AnalogStatisticsQuantizer(bc, new StatisticsGeneratorQuantizerCallback<AnalogStatistics>() {
@Override
public void quantizedStatistics(AnalogStatistics statisticsGenerator) throws IOException {
counter.increment();
AnalogStatistics stats = (AnalogStatistics) 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);
// Start Value was 3 hrs before 1st period start
// Test Minimum
Assert.assertEquals(1.0, stats.getMinimumValue(), 0.0001);
Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMinimumTime());
// Test Maximum
Assert.assertEquals(1.0, stats.getMaximumValue(), 0.0001);
Assert.assertEquals(time.toInstant().toEpochMilli(), (long) stats.getMaximumTime());
// Test Average
Assert.assertEquals(1.0d, stats.getAverage(), 0.0001);
// Test Integral
// 24Hrs
double integral = 1.0d * 24 * 60 * 60;
Assert.assertEquals(integral, stats.getIntegral(), 0.0001);
// Test sum
Assert.assertEquals(1.0d, stats.getSum(), 0.0001);
// Test first
Assert.assertEquals(1.0d, stats.getFirstValue(), 0.0001);
Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getFirstTime());
// Test last
Assert.assertEquals(1.0d, stats.getLastValue(), 0.0001);
Assert.assertEquals((long) sampleTime.toInstant().toEpochMilli(), (long) stats.getLastTime());
// Test start (the first start value will be null
Assert.assertEquals(1.0, stats.getStartValue(), 0.0001);
// Test count
Assert.assertEquals(1, stats.getCount());
// Test delta
Assert.assertEquals(0.0, stats.getDelta(), 0.0001);
// Move to next period time
time = (ZonedDateTime) adjuster.adjustInto(time);
}
});
quantizer.firstValue(new IdPointValueTime(1, new NumericValue(1.0), time.minusHours(3).toInstant().toEpochMilli()), 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());
}
use of com.serotonin.m2m2.view.stats.StatisticsGenerator in project ma-core-public by infiniteautomation.
the class StartsAndRuntimeListQuantizerTest method testStartValueOneValuePerPeriod.
@Test
public void testStartValueOneValuePerPeriod() 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);
StartsAndRuntimeListQuantizer quantizer = new StartsAndRuntimeListQuantizer(bc, new StatisticsGeneratorQuantizerCallback<StartsAndRuntimeList>() {
@Override
public void quantizedStatistics(StartsAndRuntimeList statisticsGenerator) throws IOException {
counter.increment();
StartsAndRuntimeList stats = (StartsAndRuntimeList) 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
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()), 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());
}
use of com.serotonin.m2m2.view.stats.StatisticsGenerator in project ma-core-public by infiniteautomation.
the class StartsAndRuntimeListQuantizerTest 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);
StartsAndRuntimeListQuantizer quantizer = new StartsAndRuntimeListQuantizer(bc, new StatisticsGeneratorQuantizerCallback<StartsAndRuntimeList>() {
@Override
public void quantizedStatistics(StartsAndRuntimeList statisticsGenerator) throws IOException {
counter.increment();
StartsAndRuntimeList stats = (StartsAndRuntimeList) 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 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, 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());
}
Aggregations