Search in sources :

Example 1 with ExpandTimePeriodAdjuster

use of com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster in project ma-modules-public by infiniteautomation.

the class ZonedDateTimeRangeQueryInfo method setupDates.

/**
 * Round off the period for rollups
 */
protected void setupDates() {
    // Round off the period if we are using periodic rollup
    if (this.timePeriod != null) {
        TruncateTimePeriodAdjuster adj = new TruncateTimePeriodAdjuster(TimePeriodType.convertFrom(this.timePeriod.getType()), this.timePeriod.getPeriods());
        from = from.with(adj);
        ExpandTimePeriodAdjuster expander = new ExpandTimePeriodAdjuster(from, TimePeriodType.convertFrom(this.timePeriod.getType()), this.timePeriod.getPeriods());
        to = to.with(expander);
    }
}
Also used : ExpandTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster) TruncateTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.TruncateTimePeriodAdjuster)

Example 2 with ExpandTimePeriodAdjuster

use of com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster in project ma-modules-public by infiniteautomation.

the class PointValueRestController method getRollupPointValuesAsSingleArray.

@ApiOperation(value = "Rollup values for multiple data points, return in time ascending order", notes = "From time inclusive, To time exclusive. Return in single array.", response = PointValueTimeModel.class, responseContainer = "Array")
@RequestMapping(method = RequestMethod.GET, value = "/single-array/time-period/{xids}/{rollup}")
public Stream<StreamingMultiPointModel> getRollupPointValuesAsSingleArray(@ApiParam(value = "Point xids", required = true, allowMultiple = true) @PathVariable String[] xids, @ApiParam(value = "Rollup type") @PathVariable(value = "rollup") RollupEnum rollup, @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 Period Type") @RequestParam(value = "timePeriodType", required = false) TimePeriodType timePeriodType, @ApiParam(value = "Time Periods") @RequestParam(value = "timePeriods", required = false) Integer timePeriods, @ApiParam(value = "Time zone") @RequestParam(value = "timezone", required = false) String timezone, @ApiParam(value = "Limit, limits returned array size") @RequestParam(value = "limit", required = false) Integer limit, @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 = "Truncate the from time and expand to time based on the time period settings") @RequestParam(value = "truncate", required = false, defaultValue = "false") boolean truncate, @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(rollup).withFields(fields).withDateTimeFormat(dateTimeFormat).withTimezone(timezone, from, to).withLocale(locale).withTimestampSource(TimestampSource.STATISTIC);
    var defaultMapper = mapperBuilder.build(DefaultStreamMapper::new);
    var aggregateMapper = mapperBuilder.build(AggregateValueMapper::new);
    from = from.withZoneSameInstant(defaultMapper.getZoneId());
    to = to.withZoneSameInstant(defaultMapper.getZoneId());
    TemporalAmount rollupPeriod;
    if (timePeriodType == null || timePeriods == null) {
        rollupPeriod = Duration.between(from, to);
    } else {
        if (truncate) {
            from = from.with(new TruncateTimePeriodAdjuster(timePeriodType.getChronoUnit(), timePeriods));
            to = to.with(new ExpandTimePeriodAdjuster(from, timePeriodType.getChronoUnit(), timePeriods));
        }
        rollupPeriod = timePeriodType.toTemporalAmount(timePeriods);
    }
    // limit is a total limit, however may as well limit per point
    var streamGenerator = rollupStream(from, to, limit, rollup, rollupPeriod, defaultMapper, aggregateMapper);
    var streams = points.stream().map(streamGenerator).collect(Collectors.toUnmodifiableList());
    // merge the streams and group by timestamp
    var mergedStream = MergingIterator.mergeStreams(streams, Comparator.comparingLong(StreamingPointValueTimeModel::getExactTimestamp));
    var grouped = TimestampGrouper.groupByTimestamp(mergedStream, defaultMapper::formatTime);
    return limit == null ? grouped : grouped.limit(limit);
}
Also used : DefaultStreamMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.DefaultStreamMapper) ExpandTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster) AggregateValueMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.AggregateValueMapper) TemporalAmount(java.time.temporal.TemporalAmount) TruncateTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.TruncateTimePeriodAdjuster) StreamMapperBuilder(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.StreamMapperBuilder) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ExpandTimePeriodAdjuster

use of com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster in project ma-modules-public by infiniteautomation.

the class PointValueRestController method getRollupPointValuesAsMultipleArrays.

@ApiOperation(value = "Rollup values for multiple data points, return in time ascending order", notes = "From time inclusive, To time exclusive.  Returns a map of xid to point value time arrays.", response = PointValueTimeModel.class, responseContainer = "Object")
@RequestMapping(method = RequestMethod.GET, value = "/multiple-arrays/time-period/{xids}/{rollup}")
public Map<String, Stream<StreamingPointValueTimeModel>> getRollupPointValuesAsMultipleArrays(@ApiParam(value = "Point xids", required = true, allowMultiple = true) @PathVariable String[] xids, @ApiParam(value = "Rollup type") @PathVariable(value = "rollup") RollupEnum rollup, @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 Period Type") @RequestParam(value = "timePeriodType", required = false) TimePeriodType timePeriodType, @ApiParam(value = "Time Periods") @RequestParam(value = "timePeriods", required = false) Integer timePeriods, @ApiParam(value = "Time zone") @RequestParam(value = "timezone", required = false) String timezone, @ApiParam(value = "Limit (per series)") @RequestParam(value = "limit", required = false) Integer limit, @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 = "Truncate the from time and expand to time based on the time period settings") @RequestParam(value = "truncate", required = false, defaultValue = "false") boolean truncate, @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(rollup).withFields(fields).withDateTimeFormat(dateTimeFormat).withTimezone(timezone, from, to).withLocale(locale);
    var defaultMapper = mapperBuilder.build(DefaultStreamMapper::new);
    var aggregateMapper = mapperBuilder.build(AggregateValueMapper::new);
    from = from.withZoneSameInstant(defaultMapper.getZoneId());
    to = to.withZoneSameInstant(defaultMapper.getZoneId());
    TemporalAmount rollupPeriod;
    if (timePeriodType == null || timePeriods == null) {
        rollupPeriod = Duration.between(from, to);
    } else {
        if (truncate) {
            from = from.with(new TruncateTimePeriodAdjuster(timePeriodType.getChronoUnit(), timePeriods));
            to = to.with(new ExpandTimePeriodAdjuster(from, timePeriodType.getChronoUnit(), timePeriods));
        }
        rollupPeriod = timePeriodType.toTemporalAmount(timePeriods);
    }
    var streamGenerator = rollupStream(from, to, limit, rollup, rollupPeriod, defaultMapper, aggregateMapper);
    return points.stream().collect(Collectors.toUnmodifiableMap(DataPointVO::getXid, streamGenerator));
}
Also used : DefaultStreamMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.DefaultStreamMapper) ExpandTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster) AggregateValueMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.AggregateValueMapper) TemporalAmount(java.time.temporal.TemporalAmount) TruncateTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.TruncateTimePeriodAdjuster) StreamMapperBuilder(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.StreamMapperBuilder) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ExpandTimePeriodAdjuster

use of com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster in project ma-modules-public by infiniteautomation.

the class PointValueRestController method getRollupPointValues.

@ApiOperation(value = "Rollup values For 1 Data Point, return in time ascending order", notes = "From time inclusive, To time exclusive.", response = PointValueTimeModel.class, responseContainer = "Array")
@RequestMapping(method = RequestMethod.GET, value = "/time-period/{xid}/{rollup}")
public Stream<StreamingPointValueTimeModel> getRollupPointValues(@ApiParam(value = "Point xid", required = true) @PathVariable String xid, @ApiParam(value = "Rollup type") @PathVariable(value = "rollup") RollupEnum rollup, @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 = "From time") @RequestParam(value = "from") @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime from, @ApiParam(value = "To time") @RequestParam(value = "to") @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime to, @ApiParam(value = "Time zone") @RequestParam(value = "timezone", required = false) String timezone, @ApiParam(value = "Limit") @RequestParam(value = "limit", required = false) Integer limit, @ApiParam(value = "Time Period Type") @RequestParam(value = "timePeriodType", required = false) TimePeriodType timePeriodType, @ApiParam(value = "Time Periods") @RequestParam(value = "timePeriods", required = false) Integer timePeriods, @ApiParam(value = "Truncate the from time and expand to time based on the time period settings") @RequestParam(value = "truncate", required = false, defaultValue = "false") boolean truncate, @ApiParam(value = "Fields to be included in the returned data, default is TIMESTAMP,VALUE") @RequestParam(required = false) PointValueField[] fields, Locale locale) {
    DataPointVO point = dataPointService.get(xid);
    var mapperBuilder = new StreamMapperBuilder().withDataPoint(point).withRollup(rollup).withFields(fields).withDateTimeFormat(dateTimeFormat).withTimezone(timezone, from, to).withLocale(locale);
    var defaultMapper = mapperBuilder.build(DefaultStreamMapper::new);
    var aggregateMapper = mapperBuilder.build(AggregateValueMapper::new);
    from = from.withZoneSameInstant(defaultMapper.getZoneId());
    to = to.withZoneSameInstant(defaultMapper.getZoneId());
    TemporalAmount rollupPeriod;
    if (timePeriodType == null || timePeriods == null) {
        rollupPeriod = Duration.between(from, to);
    } else {
        if (truncate) {
            from = from.with(new TruncateTimePeriodAdjuster(timePeriodType.getChronoUnit(), timePeriods));
            to = to.with(new ExpandTimePeriodAdjuster(from, timePeriodType.getChronoUnit(), timePeriods));
        }
        rollupPeriod = timePeriodType.toTemporalAmount(timePeriods);
    }
    return rollupStream(from, to, limit, rollup, rollupPeriod, defaultMapper, aggregateMapper).apply(point);
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DefaultStreamMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.DefaultStreamMapper) ExpandTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster) AggregateValueMapper(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.AggregateValueMapper) TemporalAmount(java.time.temporal.TemporalAmount) TruncateTimePeriodAdjuster(com.infiniteautomation.mango.util.datetime.TruncateTimePeriodAdjuster) StreamMapperBuilder(com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.StreamMapperBuilder) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ExpandTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.ExpandTimePeriodAdjuster)4 TruncateTimePeriodAdjuster (com.infiniteautomation.mango.util.datetime.TruncateTimePeriodAdjuster)4 AggregateValueMapper (com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.AggregateValueMapper)3 DefaultStreamMapper (com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.DefaultStreamMapper)3 StreamMapperBuilder (com.infiniteautomation.mango.rest.latest.streamingvalues.mapper.StreamMapperBuilder)3 ApiOperation (io.swagger.annotations.ApiOperation)3 TemporalAmount (java.time.temporal.TemporalAmount)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1