Search in sources :

Example 6 with RollupEnum

use of com.serotonin.m2m2.web.mvc.rest.v1.model.time.RollupEnum 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 ResponseEntity<PointValueTimeStream<PointValueTimeModel, ZonedDateTimeRangeQueryInfo>> getRollupPointValuesAsSingleArray(HttpServletRequest request, @ApiParam(value = "Point xids", required = true, allowMultiple = true) @PathVariable String[] xids, @ApiParam(value = "Rollup type", required = false, allowMultiple = false) @PathVariable(value = "rollup") RollupEnum rollup, @ApiParam(value = "From time", required = false, allowMultiple = false) @RequestParam(value = "from", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime from, @ApiParam(value = "To time", required = false, allowMultiple = false) @RequestParam(value = "to", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime to, @ApiParam(value = "Time Period Type", required = false, allowMultiple = false) @RequestParam(value = "timePeriodType", required = false) TimePeriodType timePeriodType, @ApiParam(value = "Time Periods", required = false, allowMultiple = false) @RequestParam(value = "timePeriods", required = false) Integer timePeriods, @ApiParam(value = "Time zone", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone, @ApiParam(value = "Limit", required = false, allowMultiple = false) @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", required = false, allowMultiple = false) @RequestParam(value = "dateTimeFormat", required = false) String dateTimeFormat, @ApiParam(value = "Truncate the from time and expand to time based on the time period settings", required = false, allowMultiple = false) @RequestParam(value = "truncate", required = false, defaultValue = "false") boolean truncate, @ApiParam(value = "Fields to be included in the returned data, default is TIMESTAMP,VALUE", required = false, allowMultiple = false) @RequestParam(required = false) PointValueField[] fields, @AuthenticationPrincipal User user) {
    TimePeriod timePeriod = null;
    if ((timePeriodType != null) && (timePeriods != null)) {
        timePeriod = new TimePeriod(timePeriods, timePeriodType);
    }
    ZonedDateTimeRangeQueryInfo info = new ZonedDateTimeRangeQueryInfo(from, to, dateTimeFormat, timezone, rollup, timePeriod, limit, true, true, true, PointValueTimeCacheControl.NONE, null, null, truncate, fields);
    return generateStream(user, info, xids);
}
Also used : TimePeriod(com.serotonin.m2m2.web.mvc.rest.v1.model.time.TimePeriod) ZonedDateTimeRangeQueryInfo(com.infiniteautomation.mango.rest.v2.model.pointValue.query.ZonedDateTimeRangeQueryInfo) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with RollupEnum

use of com.serotonin.m2m2.web.mvc.rest.v1.model.time.RollupEnum in project ma-modules-public by infiniteautomation.

the class PointValueRestController method buildMap.

/**
 * Build and validate the map of Requested Data Points
 * @param user
 * @param xids
 * @return
 */
protected Map<Integer, DataPointVO> buildMap(User user, String[] xids, RollupEnum rollup) {
    // Build the map, check permissions
    Map<Integer, DataPointVO> voMap = new HashMap<Integer, DataPointVO>();
    for (String xid : xids) {
        DataPointVO vo = DataPointDao.instance.getByXid(xid);
        if (vo == null) {
            throw new NotFoundRestException();
        } else {
            if (!Permissions.hasDataPointReadPermission(user, vo))
                throw new AccessDeniedException();
        }
        // TODO Add support for NONE Default Rollup
        if (rollup == RollupEnum.POINT_DEFAULT && vo.getRollup() == RollupEnum.NONE.getId())
            throw new BadRequestException(new TranslatableMessage("common.default", "Default point rollup of NONE is not yet supported for point with xid: " + xid));
        ;
        // Validate the rollup
        switch(vo.getPointLocator().getDataTypeId()) {
            case DataTypes.ALPHANUMERIC:
            case DataTypes.BINARY:
            case DataTypes.IMAGE:
            case DataTypes.MULTISTATE:
                if (rollup.nonNumericSupport() == false)
                    throw new BadRequestException(new TranslatableMessage("rest.validate.rollup.incompatible", rollup.toString(), xid));
                break;
            case DataTypes.NUMERIC:
                break;
        }
        voMap.put(vo.getId(), vo);
    }
    // Do we have any points
    if (voMap.isEmpty())
        throw new NotFoundRestException();
    return voMap;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) HashMap(java.util.HashMap) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 8 with RollupEnum

use of com.serotonin.m2m2.web.mvc.rest.v1.model.time.RollupEnum 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 ResponseEntity<PointValueTimeStream<PointValueTimeModel, ZonedDateTimeRangeQueryInfo>> getRollupPointValues(HttpServletRequest request, @ApiParam(value = "Point xid", required = true, allowMultiple = false) @PathVariable String xid, @ApiParam(value = "Rollup type", required = false, allowMultiple = false) @PathVariable(value = "rollup") RollupEnum rollup, @ApiParam(value = "Date Time format pattern for timestamps as strings, if not included epoch milli number is used", required = false, allowMultiple = false) @RequestParam(value = "dateTimeFormat", required = false) String dateTimeFormat, @ApiParam(value = "From time", required = false, allowMultiple = false) @RequestParam(value = "from", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime from, @ApiParam(value = "To time", required = false, allowMultiple = false) @RequestParam(value = "to", required = false) @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime to, @ApiParam(value = "Time zone", required = false, allowMultiple = false) @RequestParam(value = "timezone", required = false) String timezone, @ApiParam(value = "Limit", required = false, allowMultiple = false) @RequestParam(value = "limit", required = false) Integer limit, @ApiParam(value = "Time Period Type", required = false, allowMultiple = false) @RequestParam(value = "timePeriodType", required = false) TimePeriodType timePeriodType, @ApiParam(value = "Time Periods", required = false, allowMultiple = false) @RequestParam(value = "timePeriods", required = false) Integer timePeriods, @ApiParam(value = "Truncate the from time and expand to time based on the time period settings", required = false, allowMultiple = false) @RequestParam(value = "truncate", required = false, defaultValue = "false") boolean truncate, @ApiParam(value = "Fields to be included in the returned data, default is TIMESTAMP,VALUE", required = false, allowMultiple = false) @RequestParam(required = false) PointValueField[] fields, @AuthenticationPrincipal User user) {
    TimePeriod timePeriod = null;
    if ((timePeriodType != null) && (timePeriods != null)) {
        timePeriod = new TimePeriod(timePeriods, timePeriodType);
    }
    ZonedDateTimeRangeQueryInfo info = new ZonedDateTimeRangeQueryInfo(from, to, dateTimeFormat, timezone, rollup, timePeriod, limit, true, false, true, PointValueTimeCacheControl.NONE, null, null, truncate, fields);
    return generateStream(user, info, new String[] { xid });
}
Also used : TimePeriod(com.serotonin.m2m2.web.mvc.rest.v1.model.time.TimePeriod) ZonedDateTimeRangeQueryInfo(com.infiniteautomation.mango.rest.v2.model.pointValue.query.ZonedDateTimeRangeQueryInfo) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with RollupEnum

use of com.serotonin.m2m2.web.mvc.rest.v1.model.time.RollupEnum in project ma-modules-public by infiniteautomation.

the class PointValueFunctionalTests method testGetAllAdmin.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetAllAdmin() {
    List<User> users = new ArrayList<>();
    User adminUser = UserTestData.adminUser();
    users.add(adminUser);
    users.add(UserTestData.newAdminUser());
    users.add(UserTestData.standardUser());
    // Setup our Mock DS
    DataSourceVO ds = DataSourceTestData.mockDataSource();
    // Configure our REST get settings
    String xid = ds.getXid();
    // Date to = new Date();
    // Date from = new Date(to.getTime() - 1000 * 60 * 60);
    // RollupEnum rollup = null;
    // TimePeriodType timePeriodType = null;
    // Integer timePeriods = null;
    // This will ensure that the getUsers() method returns
    // the mock list of users
    when(userDao.getUsers()).thenReturn(users);
    try {
        MvcResult result = this.mockMvc.perform(get("/v1/pointValues" + xid + ".json").sessionAttr("sessionUser", adminUser).param("from", "2014-08-10T00:00:00.000-10:00").param("to", "2014-08-10T00:00:00.000-10:00").accept(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk()).andReturn();
        List<PointValueTimeModel> models = this.objectMapper.readValue(result.getResponse().getContentAsString(), objectMapper.getTypeFactory().constructCollectionType(List.class, PointValueTimeModel.class));
        // Check the size
        assertEquals(users.size(), models.size());
    } catch (Exception e) {
        fail(e.getMessage());
    }
// Check the data
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) User(com.serotonin.m2m2.vo.User) PointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 10 with RollupEnum

use of com.serotonin.m2m2.web.mvc.rest.v1.model.time.RollupEnum in project ma-modules-public by infiniteautomation.

the class PointValueRestController method pointValuesForMultiplePointsAsSingleArray.

private ResponseEntity<QueryArrayStream<PointValueTimeModel>> pointValuesForMultiplePointsAsSingleArray(HttpServletRequest request, String[] xids, boolean useRendered, boolean unitConversion, DateTime from, DateTime to, RollupEnum rollup, TimePeriodType timePeriodType, Integer timePeriods, String timezone, Integer limit, String dateTimeFormat) {
    RestProcessResult<QueryArrayStream<PointValueTimeModel>> result = new RestProcessResult<QueryArrayStream<PointValueTimeModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (dateTimeFormat != null) {
            try {
                DateTimeFormatter.ofPattern(dateTimeFormat);
            } catch (IllegalArgumentException e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalid", "dateTimeFormat");
                throw new ValidationFailedRestException(vr);
            }
        }
        Map<Integer, DataPointVO> pointIdMap = new HashMap<Integer, DataPointVO>(xids.length);
        DataPointVO vo;
        for (String xid : xids) {
            vo = DataPointDao.instance.getByXid(xid);
            if (vo != null) {
                if (Permissions.hasDataPointReadPermission(user, vo))
                    pointIdMap.put(vo.getId(), vo);
                else {
                    // Abort, invalid permissions
                    result.addRestMessage(getUnauthorizedMessage());
                    return result.createResponseEntity();
                }
            }
        }
        if (timezone != null) {
            try {
                ZoneId.of(timezone);
            } catch (Exception e) {
                RestValidationResult vr = new RestValidationResult();
                vr.addError("validate.invalidValue", "timezone");
                throw new ValidationFailedRestException(vr);
            }
        }
        // Do we have any valid points?
        if (pointIdMap.size() == 0) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        try {
            long current = Common.timer.currentTimeMillis();
            if (from == null)
                from = new DateTime(current);
            if (to == null)
                to = new DateTime(current);
            // better not to for RESTfulness
            if (timezone != null) {
                DateTimeZone zone = DateTimeZone.forID(timezone);
                from = from.withZone(zone);
                to = to.withZone(zone);
            }
            // Are we using rollup
            if ((rollup != null) && (rollup != RollupEnum.NONE)) {
                if (rollup == RollupEnum.FFT) {
                // Special Rollup for FFT's with no time rollup action
                // TODO Need a way to return frequency or period values
                // IdPointValueFftCalculator calc = new
                // IdPointValueFftCalculator(pointIdMap, from.getTime(), to.getTime(),
                // true);
                // return result.createResponseEntity(calc);
                } else {
                    TimePeriod timePeriod = null;
                    if ((timePeriodType != null) && (timePeriods != null)) {
                        timePeriod = new TimePeriod(timePeriods, timePeriodType);
                    }
                    IdPointValueRollupCalculator calc = new IdPointValueRollupCalculator(pointIdMap, useRendered, unitConversion, rollup, timePeriod, from, to, limit, dateTimeFormat, timezone);
                    return result.createResponseEntity(calc);
                }
                return result.createResponseEntity();
            } else {
                IdPointValueTimeDatabaseStream pvtDatabaseStream = new IdPointValueTimeDatabaseStream(pointIdMap, useRendered, unitConversion, from.getMillis(), to.getMillis(), this.dao, limit, dateTimeFormat, timezone);
                return result.createResponseEntity(pvtDatabaseStream);
            }
        } catch (PermissionException e) {
            LOG.error(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    } else {
        return result.createResponseEntity();
    }
}
Also used : RestValidationResult(com.infiniteautomation.mango.rest.v2.model.RestValidationResult) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) User(com.serotonin.m2m2.vo.User) XidPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.XidPointValueTimeModel) RecentPointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel) PointValueTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) HashMap(java.util.HashMap) TimePeriod(com.serotonin.m2m2.web.mvc.rest.v1.model.time.TimePeriod) QueryArrayStream(com.serotonin.m2m2.web.mvc.rest.v1.model.QueryArrayStream) IdPointValueRollupCalculator(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.IdPointValueRollupCalculator) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) RTException(com.serotonin.m2m2.rt.RTException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) IdPointValueTimeDatabaseStream(com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.IdPointValueTimeDatabaseStream)

Aggregations

DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 TimePeriod (com.serotonin.m2m2.web.mvc.rest.v1.model.time.TimePeriod)6 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)5 User (com.serotonin.m2m2.vo.User)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)4 RestValidationResult (com.infiniteautomation.mango.rest.v2.model.RestValidationResult)4 RTException (com.serotonin.m2m2.rt.RTException)4 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)4 RestValidationFailedException (com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException)4 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)4 DateTime (org.joda.time.DateTime)4 DateTimeZone (org.joda.time.DateTimeZone)4 ZonedDateTimeRangeQueryInfo (com.infiniteautomation.mango.rest.v2.model.pointValue.query.ZonedDateTimeRangeQueryInfo)3 PointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.PointValueTimeModel)3 QueryArrayStream (com.serotonin.m2m2.web.mvc.rest.v1.model.QueryArrayStream)2 IdPointValueRollupCalculator (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.IdPointValueRollupCalculator)2 IdPointValueTimeDatabaseStream (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.IdPointValueTimeDatabaseStream)2 RecentPointValueTimeModel (com.serotonin.m2m2.web.mvc.rest.v1.model.pointValue.RecentPointValueTimeModel)2