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);
}
Aggregations