use of com.serotonin.m2m2.view.stats.StartsAndRuntimeList in project ma-core-public by infiniteautomation.
the class StartsAndRuntimeListQuantizerTest 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);
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());
// 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
if (counter.getValue() == 1) {
// 1 to 10, was 10 for last 3 hrs of day
Assert.assertEquals(10, stats.getStartsAndRuntime().size());
for (StartsAndRuntime value : stats.getData()) {
switch((Integer) value.getValue()) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
Assert.assertEquals(1, value.getStarts());
Assert.assertEquals(100d * (1d / 12d), value.getPercentage(), 0.0001);
Assert.assertEquals(1d / 12d, value.getProportion(), 0.0001);
Assert.assertEquals(60 * 60 * 1000, value.getRuntime());
break;
case 10:
Assert.assertEquals(1, value.getStarts());
Assert.assertEquals(100d * (3d / 12d), value.getPercentage(), 0.0001);
Assert.assertEquals(3d / 12d, value.getProportion(), 0.0001);
Assert.assertEquals(3 * 60 * 60 * 1000, value.getRuntime());
break;
}
}
} else {
// Start in state 10 for 12 hrs more
Assert.assertEquals(10, stats.getStartsAndRuntime().size());
for (StartsAndRuntime value : stats.getData()) {
switch((Integer) value.getValue()) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
Assert.assertEquals(1, value.getStarts());
Assert.assertEquals(100d * (1d / 24d), value.getPercentage(), 0.0001);
Assert.assertEquals(1d / 24d, value.getProportion(), 0.0001);
Assert.assertEquals(60 * 60 * 1000, value.getRuntime());
break;
case 10:
Assert.assertEquals(1, value.getStarts());
Assert.assertEquals(100d * (15d / 24d), value.getPercentage(), 0.0001);
Assert.assertEquals(15d / 24d, value.getProportion(), 0.0001);
Assert.assertEquals(15 * 60 * 60 * 1000, value.getRuntime());
break;
}
}
}
// 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.StartsAndRuntimeList in project ma-core-public by infiniteautomation.
the class StartsAndRuntimeListQuantizerTest method testStartValueManyValuesPerPeriod.
@Test
public void testStartValueManyValuesPerPeriod() 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);
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());
// 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(1, stats.getStartValue().getIntegerValue());
else
Assert.assertEquals(10, stats.getStartValue().getIntegerValue());
// Test count
Assert.assertEquals(10, stats.getCount());
// Ensure data
if (counter.getValue() == 1) {
// 1 to 10, was 10 for last 3 hrs of day
Assert.assertEquals(10, stats.getStartsAndRuntime().size());
for (StartsAndRuntime value : stats.getData()) {
switch((Integer) value.getValue()) {
case 1:
Assert.assertEquals(1, value.getStarts());
Assert.assertEquals(100d * (13d / 24d), value.getPercentage(), 0.0001);
Assert.assertEquals(13d / 24d, value.getProportion(), 0.0001);
Assert.assertEquals(13 * 60 * 60 * 1000, value.getRuntime());
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
Assert.assertEquals(1, value.getStarts());
Assert.assertEquals(100d * (1d / 24d), value.getPercentage(), 0.0001);
Assert.assertEquals(1d / 24d, value.getProportion(), 0.0001);
Assert.assertEquals(60 * 60 * 1000, value.getRuntime());
break;
case 10:
Assert.assertEquals(1, value.getStarts());
Assert.assertEquals(100d * (3d / 24d), value.getPercentage(), 0.0001);
Assert.assertEquals(3d / 24d, value.getProportion(), 0.0001);
Assert.assertEquals(3 * 60 * 60 * 1000, value.getRuntime());
break;
}
}
} else {
// Start in state 10 for 12 hrs more
Assert.assertEquals(10, stats.getStartsAndRuntime().size());
for (StartsAndRuntime value : stats.getData()) {
switch((Integer) value.getValue()) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
Assert.assertEquals(1, value.getStarts());
Assert.assertEquals(100d * (1d / 24d), value.getPercentage(), 0.0001);
Assert.assertEquals(1d / 24d, value.getProportion(), 0.0001);
Assert.assertEquals(60 * 60 * 1000, value.getRuntime());
break;
case 10:
Assert.assertEquals(1, value.getStarts());
Assert.assertEquals(100d * (15d / 24d), value.getPercentage(), 0.0001);
Assert.assertEquals(15d / 24d, value.getProportion(), 0.0001);
Assert.assertEquals(15 * 60 * 60 * 1000, value.getRuntime());
break;
}
}
}
// 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.StartsAndRuntimeList in project ma-core-public by infiniteautomation.
the class StartsAndRuntimeListQuantizerTest 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);
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 periodEnd
Assert.assertEquals(time.plusDays(1).toInstant().toEpochMilli(), stats.getPeriodEndTime());
ZonedDateTime sampleTime = time;
if (counter.getValue() == 1) {
// 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
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());
} else {
// No data in other periods
// 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(1, stats.getStartValue().getIntegerValue());
// Test count
Assert.assertEquals(0, stats.getCount());
// Ensure data
Assert.assertEquals(1, stats.getStartsAndRuntime().size());
StartsAndRuntime one = stats.getStartsAndRuntime().get(1);
Assert.assertEquals(0, 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.toInstant().toEpochMilli()), 0, false);
quantizer.done();
Assert.assertEquals(new Integer(31), counter.getValue());
}
use of com.serotonin.m2m2.view.stats.StartsAndRuntimeList in project ma-core-public by infiniteautomation.
the class StatisticsChartRenderer method addDataToModel.
@Override
public void addDataToModel(Map<String, Object> model, DataPointVO point) {
long startTime = getStartTime();
long endTime = startTime + getDuration();
PointValueFacade pointValueFacade = new PointValueFacade(point.getId());
List<PointValueTime> values = pointValueFacade.getPointValuesBetween(startTime, endTime);
PointValueTime startVT = null;
if (!values.isEmpty()) {
startVT = pointValueFacade.getPointValueBefore(startTime);
}
// Generate statistics on the values.
int dataTypeId = point.getPointLocator().getDataTypeId();
if (values.size() > 0) {
if (dataTypeId == DataTypes.BINARY || dataTypeId == DataTypes.MULTISTATE) {
// Runtime stats
StartsAndRuntimeList stats = new StartsAndRuntimeList(startTime, endTime, startVT, values);
model.put("start", startVT != null ? startTime : stats.getFirstTime());
model.put("end", endTime);
model.put("startsAndRuntimes", stats.getData());
} else if (dataTypeId == DataTypes.NUMERIC) {
AnalogStatistics stats = new AnalogStatistics(startTime, endTime, startVT, values);
model.put("start", startVT != null ? startTime : stats.getFirstTime());
model.put("end", endTime);
model.put("minimum", stats.getMinimumValue());
model.put("minTime", stats.getMinimumTime());
model.put("maximum", stats.getMaximumValue());
model.put("maxTime", stats.getMaximumTime());
model.put("average", stats.getAverage());
if (includeSum)
model.put("sum", stats.getSum());
model.put("count", stats.getCount());
model.put("noData", stats.getAverage() == null);
model.put("integral", stats.getIntegral());
} else if (dataTypeId == DataTypes.ALPHANUMERIC) {
ValueChangeCounter stats = new ValueChangeCounter(startTime, endTime, startVT, values);
model.put("changeCount", stats.getChanges());
}
}
model.put("logEntries", values.size());
}
use of com.serotonin.m2m2.view.stats.StartsAndRuntimeList in project ma-core-public by infiniteautomation.
the class DataPointRT method scheduleTimeoutImpl.
public void scheduleTimeoutImpl(long fireTime) {
synchronized (intervalLoggingLock) {
DataValue value;
if (vo.getLoggingType() == DataPointVO.LoggingTypes.INTERVAL) {
if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.INSTANT)
value = PointValueTime.getValue(pointValue);
else if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.MAXIMUM || vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.MINIMUM) {
value = PointValueTime.getValue(intervalValue);
intervalValue = pointValue;
} else if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.AVERAGE) {
// If we don't have enough averaging values then we will bail and wait for more
if (vo.isOverrideIntervalLoggingSamples() && (averagingValues.size() != vo.getIntervalLoggingSampleWindowSize()))
return;
if (vo.getPointLocator().getDataTypeId() == DataTypes.MULTISTATE) {
StartsAndRuntimeList stats = new StartsAndRuntimeList(intervalStartTime, fireTime, intervalValue, averagingValues);
double maxProportion = -1;
Object valueAtMax = null;
for (StartsAndRuntime sar : stats.getData()) {
if (sar.getProportion() > maxProportion) {
maxProportion = sar.getProportion();
valueAtMax = sar.getValue();
}
}
if (valueAtMax != null)
value = new MultistateValue(DataValue.objectToValue(valueAtMax).getIntegerValue());
else
value = null;
} else {
AnalogStatistics stats = new AnalogStatistics(intervalStartTime, fireTime, intervalValue, averagingValues);
if (stats.getAverage() == null || (stats.getAverage() == Double.NaN && stats.getCount() == 0))
value = null;
else if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
value = new NumericValue(stats.getAverage());
else if (vo.getPointLocator().getDataTypeId() == DataTypes.BINARY)
value = new BinaryValue(stats.getAverage() >= 0.5);
else
throw new ShouldNeverHappenException("Unsupported average interval logging data type.");
}
// Compute the center point of our average data, starting by finding where our period started
long sampleWindowStartTime;
if (vo.isOverrideIntervalLoggingSamples())
sampleWindowStartTime = averagingValues.get(0).getTime();
else
sampleWindowStartTime = intervalStartTime;
intervalStartTime = fireTime;
// Fix to simulate center tapped filter (un-shift the average)
fireTime = sampleWindowStartTime + (fireTime - sampleWindowStartTime) / 2L;
intervalValue = pointValue;
if (!vo.isOverrideIntervalLoggingSamples())
averagingValues.clear();
} else
throw new ShouldNeverHappenException("Unknown interval logging type: " + vo.getIntervalLoggingType());
} else if (vo.getLoggingType() == DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL) {
// Okay, no changes rescheduled the timer. Get a value, reschedule
if (pointValue != null) {
value = pointValue.getValue();
if (vo.getPointLocator().getDataTypeId() == DataTypes.NUMERIC)
toleranceOrigin = pointValue.getDoubleValue();
} else
value = null;
rescheduleChangeInterval(Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod()));
} else
value = null;
if (value != null) {
PointValueTime newValue = new PointValueTime(value, fireTime);
valueCache.logPointValueAsync(newValue, null);
// Fire logged Events
fireEvents(null, newValue, null, false, false, true, false, false);
}
}
}
Aggregations