Search in sources :

Example 6 with AnalogStatistics

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());
}
Also used : PointValueFacade(com.serotonin.m2m2.rt.dataImage.PointValueFacade) StartsAndRuntimeList(com.serotonin.m2m2.view.stats.StartsAndRuntimeList) ValueChangeCounter(com.serotonin.m2m2.view.stats.ValueChangeCounter) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnalogStatistics(com.serotonin.m2m2.view.stats.AnalogStatistics)

Example 7 with AnalogStatistics

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);
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 8 with AnalogStatistics

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);
}
Also used : PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) Test(org.junit.Test)

Example 9 with AnalogStatistics

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);
        }
    }
}
Also used : FixedRateTrigger(com.serotonin.timer.FixedRateTrigger) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) TimeoutTask(com.serotonin.m2m2.util.timeout.TimeoutTask)

Example 10 with AnalogStatistics

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();
}
Also used : ValueChangeCounter(com.serotonin.m2m2.view.stats.ValueChangeCounter) AnalogStatistics(com.serotonin.m2m2.view.stats.AnalogStatistics)

Aggregations

AnalogStatistics (com.infiniteautomation.mango.statistics.AnalogStatistics)10 IOException (java.io.IOException)9 Test (org.junit.Test)9 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)8 NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)7 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)7 ZonedDateTime (java.time.ZonedDateTime)7 MutableInt (org.apache.commons.lang3.mutable.MutableInt)7 ArrayList (java.util.ArrayList)6 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)4 AnalogStatistics (com.serotonin.m2m2.view.stats.AnalogStatistics)4 ValueChangeCounter (com.serotonin.m2m2.view.stats.ValueChangeCounter)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 StatisticsGenerator (com.serotonin.m2m2.view.stats.StatisticsGenerator)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 StartsAndRuntime (com.infiniteautomation.mango.statistics.StartsAndRuntime)1 StartsAndRuntimeList (com.infiniteautomation.mango.statistics.StartsAndRuntimeList)1 PointValueFacade (com.serotonin.m2m2.rt.dataImage.PointValueFacade)1 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)1 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)1