use of com.serotonin.m2m2.view.stats.AnalogStatistics 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.AnalogStatistics in project ma-core-public by infiniteautomation.
the class AnalogStatisticsTest method testIntegral.
@Test
public void testIntegral() {
long periodStart = 0;
// 30 Days in ms
long periodEnd = 30l * 24l * 60l * 60l * 1000l;
AnalogStatistics s;
List<PointValueTime> values;
Double pointStartValue;
long pointChangeTime;
double pointChangeValue;
Double expectedIntegral;
// No values no start and end values either
s = new AnalogStatistics(periodStart, periodEnd, (Double) null, new ArrayList<IValueTime>());
assertEquals(new Double(0), s.getIntegral(), 0.001D);
// Start with 0 but no other values
s = new AnalogStatistics(periodStart, periodEnd, (Double) 0d, new ArrayList<IValueTime>());
assertEquals(new Double(0), s.getIntegral(), 0.001D);
// Start with 0, one change on day 25 at midnight and no end value
values = new ArrayList<PointValueTime>();
pointChangeTime = 25l * 24l * 60l * 60l * 1000l;
pointChangeValue = 200d;
values.add(new PointValueTime(pointChangeValue, pointChangeTime));
s = new AnalogStatistics(periodStart, periodEnd, (Double) 0d, values);
expectedIntegral = (pointChangeValue * (new Double(periodEnd) - new Double(pointChangeTime))) / 1000D;
assertEquals(expectedIntegral, s.getIntegral(), 0.001D);
// Start with 0, one change on day 25 at midnight and have end value of 0
values = new ArrayList<PointValueTime>();
pointChangeTime = 25l * 24l * 60l * 60l * 1000l;
pointChangeValue = 200d;
pointStartValue = 100d;
values.add(new PointValueTime(pointChangeValue, pointChangeTime));
s = new AnalogStatistics(periodStart, periodEnd, (Double) pointStartValue, values);
expectedIntegral = ((new Double(pointChangeTime) - new Double(periodStart)) * new Double(pointStartValue)) / 1000D;
expectedIntegral += (pointChangeValue * (new Double(periodEnd) - new Double(pointChangeTime))) / 1000D;
assertEquals(expectedIntegral, s.getIntegral(), 0.001D);
}
use of com.serotonin.m2m2.view.stats.AnalogStatistics in project ma-core-public by infiniteautomation.
the class AnalogStatisticsTest method testIntegralNoEndValue.
// Random tests
@Test
public void testIntegralNoEndValue() {
long periodStart = 0;
// 30 Days in ms
long periodEnd = 30l * 24l * 60l * 60l * 1000l;
AnalogStatistics s;
List<PointValueTime> values;
long pointChangeTime;
double pointChangeValue;
Double pointStartValue = 0D;
// Expected Values
Double expectedIntegral;
// Test a series of values
values = new ArrayList<PointValueTime>();
pointStartValue = 100d;
pointChangeTime = 25l * 24l * 60l * 60l * 1000l;
pointChangeValue = 200d;
long dayInMs = 24l * 60l * 60l * 1000l;
long time = dayInMs;
while (time < pointChangeTime) {
values.add(new PointValueTime(pointStartValue, time));
time += dayInMs;
}
values.add(new PointValueTime(pointChangeValue, pointChangeTime));
s = new AnalogStatistics(periodStart, periodEnd, (Double) pointStartValue, values);
expectedIntegral = ((new Double(pointChangeTime) - new Double(periodStart)) * new Double(pointStartValue)) / 1000D;
expectedIntegral += (pointChangeValue * (new Double(periodEnd) - new Double(pointChangeTime))) / 1000D;
assertEquals(expectedIntegral, s.getIntegral(), 0.001D);
}
use of com.serotonin.m2m2.view.stats.AnalogStatistics in project ma-core-public by infiniteautomation.
the class DataPointRT method initializeIntervalLogging.
//
// / Interval logging
//
/**
*/
public void initializeIntervalLogging(long nextPollTime, boolean quantize) {
if (vo.getLoggingType() != DataPointVO.LoggingTypes.INTERVAL && vo.getLoggingType() != DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL)
return;
synchronized (intervalLoggingLock) {
long loggingPeriodMillis = Common.getMillis(vo.getIntervalLoggingPeriodType(), vo.getIntervalLoggingPeriod());
long delay = loggingPeriodMillis;
if (quantize) {
// Quantize the start.
// Compute delay only if we are offset from the next poll time
long nextPollOffset = (nextPollTime % loggingPeriodMillis);
if (nextPollOffset != 0)
delay = loggingPeriodMillis - nextPollOffset;
LOG.debug("First interval log should be at: " + (nextPollTime + delay));
}
if (vo.getLoggingType() == DataPointVO.LoggingTypes.INTERVAL) {
intervalValue = pointValue;
if (vo.getIntervalLoggingType() == DataPointVO.IntervalLoggingTypes.AVERAGE) {
intervalStartTime = timer == null ? Common.timer.currentTimeMillis() : timer.currentTimeMillis();
if (averagingValues.size() > 0) {
Double nullValue = null;
AnalogStatistics stats = new AnalogStatistics(intervalStartTime - loggingPeriodMillis, intervalStartTime, nullValue, averagingValues);
PointValueTime newValue = new PointValueTime(stats.getAverage(), intervalStartTime);
valueCache.logPointValueAsync(newValue, null);
// Fire logged Events
fireEvents(null, newValue, null, false, false, true, false, false);
averagingValues.clear();
}
}
// Are we using a custom timer?
if (this.timer == null)
intervalLoggingTask = new TimeoutTask(new FixedRateTrigger(delay, loggingPeriodMillis), createIntervalLoggingTimeoutClient());
else
intervalLoggingTask = new TimeoutTask(new FixedRateTrigger(delay, loggingPeriodMillis), createIntervalLoggingTimeoutClient(), this.timer);
} else if (vo.getLoggingType() == DataPointVO.LoggingTypes.ON_CHANGE_INTERVAL) {
rescheduleChangeInterval(delay);
}
}
}
use of com.serotonin.m2m2.view.stats.AnalogStatistics 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();
}
Aggregations