Search in sources :

Example 6 with PointValueField

use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.

the class PointValueTimeStreamCsvMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    try {
        PointValueTimeStream<?, ?> stream = (PointValueTimeStream<?, ?>) object;
        stream.setContentType(StreamContentType.CSV);
        JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
        // Set the schema
        CsvSchema.Builder builder = CsvSchema.builder();
        builder.setUseHeader(true);
        // Setup our rendering parameters
        LatestQueryInfo info = stream.getQueryInfo();
        if (stream instanceof MultiPointTimeRangeDatabaseStream || stream instanceof MultiPointLatestDatabaseStream) {
            if (info.isSingleArray()) {
                if (info.isMultiplePointsPerArray()) {
                    Map<Integer, DataPointVO> voMap = stream.getVoMap();
                    Iterator<Integer> it = voMap.keySet().iterator();
                    boolean firstTimestamp = true;
                    while (it.hasNext()) {
                        String xid = voMap.get(it.next()).getXid();
                        for (PointValueField field : info.getFields()) {
                            if (field == PointValueField.TIMESTAMP) {
                                if (firstTimestamp)
                                    field.createColumn(builder, xid);
                                firstTimestamp = false;
                            } else
                                field.createColumn(builder, xid);
                        }
                    }
                } else {
                    for (PointValueField field : info.getFields()) field.createColumn(builder, null);
                }
            } else {
                for (PointValueField field : info.getFields()) field.createColumn(builder, null);
            }
        } else if (stream instanceof MultiDataPointStatisticsQuantizerStream || stream instanceof MultiDataPointDefaultRollupStatisticsQuantizerStream) {
            if (stream.getQueryInfo().isSingleArray()) {
                if (stream.getQueryInfo().isMultiplePointsPerArray()) {
                    Map<Integer, DataPointVO> voMap = stream.getVoMap();
                    Iterator<Integer> it = voMap.keySet().iterator();
                    boolean firstTimestamp = true;
                    while (it.hasNext()) {
                        String xid = voMap.get(it.next()).getXid();
                        for (PointValueField field : info.getFields()) {
                            if (field == PointValueField.TIMESTAMP) {
                                if (firstTimestamp)
                                    field.createColumn(builder, xid);
                                firstTimestamp = false;
                            } else if (field == PointValueField.VALUE) {
                                if (info.getRollup() == RollupEnum.ALL) {
                                    for (RollupEnum rollup : getAllRollups()) {
                                        builder.addColumn(xid + PointValueTimeWriter.DOT + rollup.name(), ColumnType.NUMBER_OR_STRING);
                                    }
                                } else {
                                    field.createColumn(builder, xid);
                                }
                            } else {
                                field.createColumn(builder, xid);
                            }
                        }
                    }
                } else {
                    // Single array
                    if (info.getRollup() == RollupEnum.ALL) {
                        for (RollupEnum rollup : getAllRollups()) {
                            builder.addColumn(rollup.name(), ColumnType.NUMBER_OR_STRING);
                        }
                        for (PointValueField field : info.getFields()) {
                            if (field == PointValueField.VALUE)
                                continue;
                            field.createColumn(builder, null);
                        }
                    } else {
                        for (PointValueField field : info.getFields()) field.createColumn(builder, null);
                    }
                }
            } else {
                if (info.getRollup() == RollupEnum.ALL) {
                    for (RollupEnum rollup : getAllRollups()) {
                        builder.addColumn(rollup.name(), ColumnType.NUMBER_OR_STRING);
                    }
                    for (PointValueField field : info.getFields()) {
                        if (field == PointValueField.VALUE)
                            continue;
                        field.createColumn(builder, null);
                    }
                } else {
                    for (PointValueField field : info.getFields()) field.createColumn(builder, null);
                }
            }
        }
        generator.setSchema(builder.build());
        PointValueTimeWriter writer = new PointValueTimeCsvWriter(stream.getQueryInfo(), stream.getVoMap().size(), generator);
        try {
            stream.start(writer);
            stream.streamData(writer);
            stream.finish(writer);
            generator.flush();
        } catch (QueryCancelledException e) {
            throw new HttpMessageNotWritableException("Query Cancelled");
        }
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : PointValueTimeStream(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeStream) MultiDataPointStatisticsQuantizerStream(com.infiniteautomation.mango.rest.latest.model.pointValue.quantize.MultiDataPointStatisticsQuantizerStream) MultiDataPointDefaultRollupStatisticsQuantizerStream(com.infiniteautomation.mango.rest.latest.model.pointValue.quantize.MultiDataPointDefaultRollupStatisticsQuantizerStream) LatestQueryInfo(com.infiniteautomation.mango.rest.latest.model.pointValue.query.LatestQueryInfo) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) PointValueTimeCsvWriter(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeCsvWriter) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) MultiPointTimeRangeDatabaseStream(com.infiniteautomation.mango.rest.latest.model.pointValue.query.MultiPointTimeRangeDatabaseStream) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) Iterator(java.util.Iterator) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) RollupEnum(com.infiniteautomation.mango.rest.latest.model.pointValue.RollupEnum) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MultiPointLatestDatabaseStream(com.infiniteautomation.mango.rest.latest.model.pointValue.query.MultiPointLatestDatabaseStream) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) PointValueTimeWriter(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeWriter) QueryCancelledException(com.infiniteautomation.mango.db.query.QueryCancelledException) Map(java.util.Map) PointValueField(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField)

Example 7 with PointValueField

use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.

the class MultiPointStatisticsStreamTest method testSingleNumericPointBothChange.

@Test
public void testSingleNumericPointBothChange() throws IOException, QueryCancelledException {
    // Setup the data to run once daily for 30 days
    ZonedDateTime from = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
    ZonedDateTime to = ZonedDateTime.of(2017, 02, 01, 00, 00, 00, 0, zoneId);
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    int cacheSize = 10;
    MockDataSourceVO ds = createDataSource();
    DataPointVO dp = createDataPoint(ds.getId(), DataType.NUMERIC, cacheSize);
    DataPointWrapper<AnalogStatistics> point = new DataPointWrapper<AnalogStatistics>(ds, dp, new PointValueTime(1.0, 0), (value) -> {
        return new NumericValue(value.getDoubleValue() + 1.0);
    }, (info, w) -> {
        return new AnalogStatistics(info.getFromMillis(), info.getToMillis(), new PointValueTime(1.0, 0), w.values);
    }, (w, gen, root) -> {
        JsonNode stats = root.get(w.vo.getXid());
        if (stats == null)
            fail("Missing stats for point " + w.vo.getXid());
        JsonNode stat = stats.get(PointValueTimeWriter.START);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.START + " entry");
        PointValueTime value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getStartValue(), value.getDoubleValue(), 0.00001);
        assertEquals(gen.getPeriodStartTime(), value.getTime());
        stat = stats.get(PointValueTimeWriter.FIRST);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.FIRST + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getFirstValue(), value.getValue().getDoubleValue(), 0.00001);
        assertEquals((long) gen.getFirstTime(), value.getTime());
        stat = stats.get(PointValueTimeWriter.LAST);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.LAST + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getLastValue(), value.getValue().getDoubleValue(), 0.00001);
        assertEquals((long) gen.getLastTime(), value.getTime());
        stat = stats.get(PointValueTimeWriter.COUNT);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.COUNT + " entry");
        assertEquals(gen.getCount(), stat.asInt());
        stat = stats.get(PointValueTimeWriter.ACCUMULATOR);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.ACCUMULATOR + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        Double accumulatorValue = gen.getLastValue();
        if (accumulatorValue == null)
            accumulatorValue = gen.getMaximumValue();
        assertEquals(accumulatorValue, value.getDoubleValue(), 0.00001);
        stat = stats.get(PointValueTimeWriter.AVERAGE);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.AVERAGE + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getAverage(), value.getDoubleValue(), 0.00001);
        stat = stats.get(PointValueTimeWriter.DELTA);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.DELTA + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getDelta(), value.getDoubleValue(), 0.00001);
        stat = stats.get(PointValueTimeWriter.MINIMUM);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.MINIMUM + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getMinimumValue(), value.getValue().getDoubleValue(), 0.00001);
        assertEquals((long) gen.getMinimumTime(), value.getTime());
        stat = stats.get(PointValueTimeWriter.MAXIMUM);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.MAXIMUM + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getMaximumValue(), value.getValue().getDoubleValue(), 0.00001);
        assertEquals((long) gen.getMaximumTime(), value.getTime());
        stat = stats.get(PointValueTimeWriter.SUM);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.SUM + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getSum(), value.getDoubleValue(), 0.00001);
        stat = stats.get(PointValueTimeWriter.INTEGRAL);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.INTEGRAL + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getIntegral(), value.getDoubleValue(), 0.00001);
    });
    // Insert the data skipping first day so we get the initial value
    ZonedDateTime time = (ZonedDateTime) adjuster.adjustInto(from);
    timer.setStartTime(time.toInstant().toEpochMilli());
    while (time.toInstant().isBefore(to.toInstant())) {
        point.updatePointValue(new PointValueTime(point.getNextValue(), time.toInstant().toEpochMilli()));
        time = (ZonedDateTime) adjuster.adjustInto(time);
        timer.fastForwardTo(time.toInstant().toEpochMilli());
    }
    // Insert some values directly into the cache
    for (int i = 0; i < cacheSize; i++) {
        time = (ZonedDateTime) adjuster.adjustInto(time);
        timer.fastForwardTo(time.toInstant().toEpochMilli());
        point.saveOnlyToCache(new PointValueTime(point.getNextValue(), timer.currentTimeMillis()));
    }
    // Ensure we get all the data
    Instant now = Instant.ofEpochMilli(timer.currentTimeMillis() + 1);
    to = ZonedDateTime.ofInstant(now, zoneId);
    // Perform the query
    String dateTimeFormat = null;
    String timezone = zoneId.getId();
    PointValueTimeCacheControl cache = PointValueTimeCacheControl.BOTH;
    PointValueField[] fields = getFields();
    ZonedDateTimeStatisticsQueryInfo info = new ZonedDateTimeStatisticsQueryInfo(from, to, dateTimeFormat, timezone, cache, fields);
    test(info, point);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) Instant(java.time.Instant) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) JsonNode(com.fasterxml.jackson.databind.JsonNode) ZonedDateTime(java.time.ZonedDateTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) PointValueField(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField) Test(org.junit.Test)

Example 8 with PointValueField

use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.

the class MultiPointStatisticsStreamTest method testSingleNumericPointOnlyCacheChange.

@Test
public void testSingleNumericPointOnlyCacheChange() throws IOException, QueryCancelledException {
    // Setup the data to run once daily for 30 days
    ZonedDateTime from = ZonedDateTime.of(2017, 01, 01, 00, 00, 00, 0, zoneId);
    ZonedDateTime to = ZonedDateTime.of(2017, 02, 01, 00, 00, 00, 0, zoneId);
    NextTimePeriodAdjuster adjuster = new NextTimePeriodAdjuster(TimePeriods.DAYS, 1);
    int cacheSize = 10;
    MockDataSourceVO ds = createDataSource();
    DataPointVO dp = createDataPoint(ds.getId(), DataType.NUMERIC, cacheSize);
    DataPointWrapper<AnalogStatistics> point = new DataPointWrapper<AnalogStatistics>(ds, dp, new PointValueTime(1.0, 0), (value) -> {
        return new NumericValue(value.getDoubleValue() + 1.0);
    }, (info, w) -> {
        return new AnalogStatistics(info.getFromMillis(), info.getToMillis(), null, w.values);
    }, (w, gen, root) -> {
        JsonNode stats = root.get(w.vo.getXid());
        if (stats == null)
            fail("Missing stats for point " + w.vo.getXid());
        JsonNode stat = stats.get(PointValueTimeWriter.START);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.START + " entry");
        assertNull(gen.getStartValue());
        assertTrue(stat.isNull());
        stat = stats.get(PointValueTimeWriter.FIRST);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.FIRST + " entry");
        PointValueTime value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getFirstValue(), value.getValue().getDoubleValue(), 0.00001);
        assertEquals((long) gen.getFirstTime(), value.getTime());
        stat = stats.get(PointValueTimeWriter.LAST);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.LAST + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getLastValue(), value.getValue().getDoubleValue(), 0.00001);
        assertEquals((long) gen.getLastTime(), value.getTime());
        stat = stats.get(PointValueTimeWriter.COUNT);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.COUNT + " entry");
        assertEquals(gen.getCount(), stat.asInt());
        stat = stats.get(PointValueTimeWriter.ACCUMULATOR);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.ACCUMULATOR + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        Double accumulatorValue = gen.getLastValue();
        if (accumulatorValue == null)
            accumulatorValue = gen.getMaximumValue();
        assertEquals(accumulatorValue, value.getDoubleValue(), 0.00001);
        stat = stats.get(PointValueTimeWriter.AVERAGE);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.AVERAGE + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getAverage(), value.getDoubleValue(), 0.00001);
        stat = stats.get(PointValueTimeWriter.DELTA);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.DELTA + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getDelta(), value.getDoubleValue(), 0.00001);
        stat = stats.get(PointValueTimeWriter.MINIMUM);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.MINIMUM + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getMinimumValue(), value.getValue().getDoubleValue(), 0.00001);
        assertEquals((long) gen.getMinimumTime(), value.getTime());
        stat = stats.get(PointValueTimeWriter.MAXIMUM);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.MAXIMUM + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getMaximumValue(), value.getValue().getDoubleValue(), 0.00001);
        assertEquals((long) gen.getMaximumTime(), value.getTime());
        stat = stats.get(PointValueTimeWriter.SUM);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.SUM + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getSum(), value.getDoubleValue(), 0.00001);
        stat = stats.get(PointValueTimeWriter.INTEGRAL);
        if (stat == null)
            fail("Missing " + PointValueTimeWriter.INTEGRAL + " entry");
        value = getPointValueTime(w.vo.getPointLocator().getDataType(), stat);
        assertEquals(gen.getIntegral(), value.getDoubleValue(), 0.00001);
    });
    // Insert the data skipping first day so we get the initial value
    ZonedDateTime time = (ZonedDateTime) adjuster.adjustInto(from);
    timer.setStartTime(time.toInstant().toEpochMilli());
    while (time.toInstant().isBefore(to.toInstant())) {
        point.updatePointValue(new PointValueTime(point.getNextValue(), time.toInstant().toEpochMilli()));
        time = (ZonedDateTime) adjuster.adjustInto(time);
        timer.fastForwardTo(time.toInstant().toEpochMilli());
    }
    // Insert some values directly into the cache
    point.values.clear();
    for (int i = 0; i < cacheSize; i++) {
        time = (ZonedDateTime) adjuster.adjustInto(time);
        timer.fastForwardTo(time.toInstant().toEpochMilli());
        point.saveOnlyToCache(new PointValueTime(point.getNextValue(), timer.currentTimeMillis()));
    }
    // Ensure we get all the data
    Instant now = Instant.ofEpochMilli(timer.currentTimeMillis() + 1);
    to = ZonedDateTime.ofInstant(now, zoneId);
    // Perform the query
    String dateTimeFormat = null;
    String timezone = zoneId.getId();
    PointValueTimeCacheControl cache = PointValueTimeCacheControl.CACHE_ONLY;
    PointValueField[] fields = getFields();
    ZonedDateTimeStatisticsQueryInfo info = new ZonedDateTimeStatisticsQueryInfo(from, to, dateTimeFormat, timezone, cache, fields);
    test(info, point);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) Instant(java.time.Instant) AnalogStatistics(com.infiniteautomation.mango.statistics.AnalogStatistics) JsonNode(com.fasterxml.jackson.databind.JsonNode) ZonedDateTime(java.time.ZonedDateTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) NextTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster) PointValueField(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField) Test(org.junit.Test)

Example 9 with PointValueField

use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.

the class PointValueRestController method getStatistics.

@ApiOperation(value = "GET statistics for data point(s) over the given time range", notes = "From time inclusive, To time exclusive. Returns map of xid to Statistics object", response = PointValueTimeModel.class, responseContainer = "Map")
@RequestMapping(method = RequestMethod.GET, value = "/statistics/{xids}")
public Map<String, StreamingPointValueTimeModel> getStatistics(@ApiParam(value = "Point xids", required = true, allowMultiple = true) @PathVariable String[] xids, @ApiParam(value = "From time") @RequestParam(value = "from", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime from, @ApiParam(value = "To time") @RequestParam(value = "to", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime to, @ApiParam(value = "Time zone") @RequestParam(value = "timezone", required = false) String timezone, @ApiParam(value = "Date Time format pattern for timestamps as strings, if not included epoch milli number is used") @RequestParam(value = "dateTimeFormat", required = false) String dateTimeFormat, @ApiParam(value = "Fields to be included in the returned data, default is TIMESTAMP,VALUE") @RequestParam(required = false) PointValueField[] fields, Locale locale) {
    var points = Arrays.stream(xids).distinct().map(dataPointService::get).collect(Collectors.toUnmodifiableSet());
    var mapperBuilder = new StreamMapperBuilder().withDataPoints(points).withRollup(RollupEnum.ALL).withFields(fields).withDateTimeFormat(dateTimeFormat).withTimezone(timezone, from, to).withLocale(locale).withTimestampSource(TimestampSource.STATISTIC);
    var aggregateMapper = mapperBuilder.build(AggregateValueMapper::new);
    var rollupPeriod = Duration.between(from, to);
    return points.stream().collect(Collectors.toUnmodifiableMap(DataPointVO::getXid, point -> {
        var aggregateDao = dao.getAggregateDao();
        try (var stream = aggregateDao.query(point, from, to, null, rollupPeriod)) {
            return stream.map(aggregateMapper).findAny().orElseThrow();
        }
    }));
}
Also used : Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) ZonedDateTime(java.time.ZonedDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) ApiParam(io.swagger.annotations.ApiParam) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) Duration(java.time.Duration) Map(java.util.Map) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) PointValueTimeModel(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueTimeModel) RuntimeManager(com.serotonin.m2m2.rt.RuntimeManager) XidTimeRangeQueryModel(com.infiniteautomation.mango.rest.latest.model.pointValue.query.XidTimeRangeQueryModel) RestController(org.springframework.web.bind.annotation.RestController) ZoneId(java.time.ZoneId) Stream(java.util.stream.Stream) DateUtils(com.serotonin.m2m2.util.DateUtils) TimestampSource(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.TimestampSource) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) DataSourceService(com.infiniteautomation.mango.spring.service.DataSourceService) StreamingMultiPointModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.StreamingMultiPointModel) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) DataType(com.serotonin.m2m2.DataType) PointValueTimeCacheControl(com.infiniteautomation.mango.rest.latest.model.pointValue.query.PointValueTimeCacheControl) MergingIterator(com.infiniteautomation.mango.db.iterators.MergingIterator) AggregateValueMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.AggregateValueMapper) TemporalAmount(java.time.temporal.TemporalAmount) DataPointService(com.infiniteautomation.mango.spring.service.DataPointService) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) Api(io.swagger.annotations.Api) TemporaryResourceWebSocketHandler(com.infiniteautomation.mango.rest.latest.temporaryResource.TemporaryResourceWebSocketHandler) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) PointValueField(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField) Common(com.serotonin.m2m2.Common) StreamingPointValueTimeModel(com.infiniteautomation.mango.rest.latest.streamingvalues.model.StreamingPointValueTimeModel) XidLatestQueryInfoModel(com.infiniteautomation.mango.rest.latest.model.pointValue.query.XidLatestQueryInfoModel) HttpStatus(org.springframework.http.HttpStatus) SystemSettingsDao(com.serotonin.m2m2.db.dao.SystemSettingsDao) RTException(com.serotonin.m2m2.rt.RTException) RollupEnum(com.infiniteautomation.mango.rest.latest.model.pointValue.RollupEnum) PointValueImportResult(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueImportResult) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) PathVariable(org.springframework.web.bind.annotation.PathVariable) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) PurgePointValuesResponseModel(com.infiniteautomation.mango.rest.latest.model.pointValue.PurgePointValuesResponseModel) LoggerFactory(org.slf4j.LoggerFactory) GenericRestException(com.infiniteautomation.mango.rest.latest.exception.GenericRestException) ExpandTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) ApiOperation(io.swagger.annotations.ApiOperation) StreamMapperBuilder(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.StreamMapperBuilder) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) Locale(java.util.Locale) SimplifyUtility(com.goebl.simplify.SimplifyUtility) ITime(com.serotonin.m2m2.view.stats.ITime) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) URI(java.net.URI) PointValueTimePointExtractor(com.infiniteautomation.mango.rest.pointextractor.PointValueTimePointExtractor) TemporaryResourceStatusUpdate(com.infiniteautomation.mango.rest.latest.temporaryResource.TemporaryResourceStatusUpdate) UnitConverter(javax.measure.converter.UnitConverter) TimePeriodType(com.infiniteautomation.mango.rest.latest.model.time.TimePeriodType) PurgeDataPointValuesModel(com.infiniteautomation.mango.rest.latest.model.pointValue.PurgeDataPointValuesModel) XidRollupTimeRangeQueryModel(com.infiniteautomation.mango.rest.latest.model.pointValue.query.XidRollupTimeRangeQueryModel) TimeZone(java.util.TimeZone) HttpHeaders(org.springframework.http.HttpHeaders) SetPointSource(com.serotonin.m2m2.rt.dataImage.SetPointSource) Collection(java.util.Collection) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ISO(org.springframework.format.annotation.DateTimeFormat.ISO) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) List(java.util.List) Environment(org.springframework.core.env.Environment) Entry(java.util.Map.Entry) Optional(java.util.Optional) TimeOrder(com.serotonin.m2m2.db.dao.pointvalue.TimeOrder) LegacyXidPointValueTimeModel(com.infiniteautomation.mango.rest.latest.model.pointValue.LegacyXidPointValueTimeModel) User(com.serotonin.m2m2.vo.User) TimestampGrouper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.TimestampGrouper) NotFoundRestException(com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) TemporaryResourceStatus(com.infiniteautomation.mango.rest.latest.temporaryResource.TemporaryResource.TemporaryResourceStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) Function(java.util.function.Function) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) DefaultStreamMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.DefaultStreamMapper) TruncateTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.TruncateTimePeriodAdjuster) FireEvents(com.serotonin.m2m2.rt.dataImage.DataPointRT.FireEvents) ServerErrorException(com.infiniteautomation.mango.rest.latest.exception.ServerErrorException) Nullable(org.checkerframework.checker.nullness.qual.Nullable) TemporaryResource(com.infiniteautomation.mango.rest.latest.temporaryResource.TemporaryResource) Logger(org.slf4j.Logger) MangoTaskTemporaryResourceManager(com.infiniteautomation.mango.rest.latest.temporaryResource.MangoTaskTemporaryResourceManager) LegacyPointValueTimeModel(com.infiniteautomation.mango.rest.latest.model.pointValue.LegacyPointValueTimeModel) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) ResponseEntity(org.springframework.http.ResponseEntity) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) Comparator(java.util.Comparator) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) AggregateValueMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.AggregateValueMapper) StreamMapperBuilder(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.StreamMapperBuilder) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with PointValueField

use of com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField in project ma-modules-public by infiniteautomation.

the class MultiPointStatisticsStreamTest method testSingleMultistatePointNoCacheNoChangeInitialValue.

/**
 * Start with a value of 1 at time 0
 * Then insert a value of 1 at midnight every day during Jan 2017
 */
@Test
public void testSingleMultistatePointNoCacheNoChangeInitialValue() {
    // Setup the data to run once daily for 30 days
    ZonedDateTime from = ZonedDateTime.of(2017, 1, 1, 0, 0, 0, 0, zoneId);
    ZonedDateTime to = ZonedDateTime.of(2017, 2, 1, 0, 0, 0, 0, zoneId);
    Duration adjuster = Duration.ofDays(1);
    MockDataSourceVO ds = createDataSource();
    DataPointVO dp = createDataPoint(ds.getId(), DataType.MULTISTATE, 1);
    DataPointWrapper<StartsAndRuntimeList> point = new DataPointWrapper<>(ds, dp, new PointValueTime(1, 0), // no change
    Function.identity(), (w) -> new StartsAndRuntimeList(from.toInstant().toEpochMilli(), to.toInstant().toEpochMilli(), w.initialValue, w.values), new StartsAndRuntimeListVerifier());
    // Insert the data skipping first day so we get the initial value
    ZonedDateTime time = from.plus(adjuster);
    timer.setStartTime(time.toInstant().toEpochMilli());
    while (time.toInstant().isBefore(to.toInstant())) {
        point.updatePointValue(new PointValueTime(point.getNextValue(), time.toInstant().toEpochMilli()));
        time = time.plus(adjuster);
        timer.fastForwardTo(time.toInstant().toEpochMilli());
    }
    // Perform the query
    String timezone = zoneId.getId();
    PointValueField[] fields = getFields();
    var result = getStatistics(List.of(dp), from, to, timezone, fields);
    test(result, point);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) Duration(java.time.Duration) ZonedDateTime(java.time.ZonedDateTime) StartsAndRuntimeList(com.infiniteautomation.mango.statistics.StartsAndRuntimeList) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) PointValueField(com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField) Test(org.junit.Test)

Aggregations

PointValueField (com.infiniteautomation.mango.rest.latest.model.pointValue.PointValueField)13 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)13 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)11 ZonedDateTime (java.time.ZonedDateTime)11 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)10 Test (org.junit.Test)10 AnalogStatistics (com.infiniteautomation.mango.statistics.AnalogStatistics)9 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)8 Duration (java.time.Duration)7 StartsAndRuntimeList (com.infiniteautomation.mango.statistics.StartsAndRuntimeList)5 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)5 Instant (java.time.Instant)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 NextTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.NextTimePeriodAdjuster)4 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)4 RollupEnum (com.infiniteautomation.mango.rest.latest.model.pointValue.RollupEnum)3 ValueChangeCounter (com.infiniteautomation.mango.statistics.ValueChangeCounter)3 Map (java.util.Map)3 AggregateValueMapper (com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.AggregateValueMapper)2 StreamMapperBuilder (com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.StreamMapperBuilder)2