Search in sources :

Example 1 with PurgePointValuesResponseModel

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

the class PointValueRestController method purgePointValues.

@ApiOperation(value = "Purge Point Values for one or many data points, or a single data source", notes = "User must have edit access to data source and its points, use created header to track progress/cancel")
@RequestMapping(method = RequestMethod.POST, value = "/purge")
public ResponseEntity<TemporaryResource<PurgePointValuesResponseModel, AbstractRestException>> purgePointValues(@RequestBody() PurgeDataPointValuesModel model, UriComponentsBuilder builder) {
    model.ensureValid();
    TemporaryResource<PurgePointValuesResponseModel, AbstractRestException> response = resourceManager.newTemporaryResource("DATA_POINT_PURGE", null, model.getExpiry(), model.getTimeout(), (resource) -> {
        PurgePointValuesResponseModel result = new PurgePointValuesResponseModel();
        Map<Integer, DataSourceVO> dataSourceMap = new HashMap<>();
        Map<String, DataPointVO> dataPointsMap = new HashMap<>();
        // Build the list of data point Xids
        List<String> xids = model.getXids();
        if (xids != null && !xids.isEmpty()) {
            for (String xid : xids) {
                DataPointVO vo = dataPointService.get(xid);
                dataPointsMap.put(xid, vo);
                if (vo != null) {
                    dataSourceMap.computeIfAbsent(vo.getDataSourceId(), (key) -> dataSourceService.get(vo.getDataSourceId()));
                }
            }
        } else {
            DataSourceVO ds = dataSourceService.get(model.getDataSourceXid());
            xids = new ArrayList<>();
            if (ds != null) {
                dataSourceMap.put(ds.getId(), ds);
                List<DataPointVO> points = dataPointService.getDataPoints(ds.getId());
                for (DataPointVO point : points) {
                    xids.add(point.getXid());
                    dataPointsMap.put(point.getXid(), point);
                }
            }
        }
        int maximum = xids.size();
        int position = 0;
        // Initial status
        resource.progressOrSuccess(result, position, maximum);
        for (String xid : xids) {
            try {
                // Get the point and its data source XID
                DataPointVO dp = dataPointsMap.get(xid);
                if (dp == null)
                    throw new NotFoundException();
                DataSourceVO ds = dataSourceMap.get(dp.getDataSourceId());
                if (ds == null)
                    throw new NotFoundException();
                // Do purge based on settings
                if (model.isPurgeAll())
                    Common.runtimeManager.purgeDataPointValues(dp);
                else if (model.isUseTimeRange())
                    Common.runtimeManager.purgeDataPointValuesBetween(dp, model.getTimeRange().getFrom().getTime(), model.getTimeRange().getTo().getTime());
                else {
                    long before = DateUtils.minus(Common.timer.currentTimeMillis(), TimePeriodType.convertFrom(model.getDuration().getType()), model.getDuration().getPeriods());
                    Common.runtimeManager.purgeDataPointValues(dp, before);
                }
                result.getSuccessfullyPurged().add(xid);
            } catch (NotFoundException e) {
                result.getNotFound().add(xid);
            } catch (PermissionException e) {
                result.getNoEditPermission().add(xid);
            }
            position++;
            resource.progressOrSuccess(result, position, maximum);
        }
        return null;
    });
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path("/point-values/purge/{id}").buildAndExpand(response.getId()).toUri());
    return new ResponseEntity<>(response, headers, HttpStatus.CREATED);
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) HttpHeaders(org.springframework.http.HttpHeaders) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) ResponseEntity(org.springframework.http.ResponseEntity) PurgePointValuesResponseModel(com.infiniteautomation.mango.rest.latest.model.pointValue.PurgePointValuesResponseModel) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

AbstractRestException (com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)1 PurgePointValuesResponseModel (com.infiniteautomation.mango.rest.latest.model.pointValue.PurgePointValuesResponseModel)1 NotFoundException (com.infiniteautomation.mango.util.exception.NotFoundException)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 ApiOperation (io.swagger.annotations.ApiOperation)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1