From 1600cd817a8855f5e469235edbba8c76a48c0a21 Mon Sep 17 00:00:00 2001 From: Duckflip Date: Fri, 6 Dec 2024 15:05:35 +0100 Subject: [PATCH] #2502 : mass nifti deletion update --- .../dataset/service/DatasetServiceImpl.java | 62 ++++++++++++++----- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/shanoir-ng-datasets/src/main/java/org/shanoir/ng/dataset/service/DatasetServiceImpl.java b/shanoir-ng-datasets/src/main/java/org/shanoir/ng/dataset/service/DatasetServiceImpl.java index 3d20e603a4..0e847f4a49 100644 --- a/shanoir-ng-datasets/src/main/java/org/shanoir/ng/dataset/service/DatasetServiceImpl.java +++ b/shanoir-ng-datasets/src/main/java/org/shanoir/ng/dataset/service/DatasetServiceImpl.java @@ -17,6 +17,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.transaction.Transactional; +import org.apache.commons.collections4.ListUtils; import org.apache.commons.io.FileUtils; import org.apache.solr.client.solrj.SolrServerException; import org.shanoir.ng.dataset.dto.VolumeByFormatDTO; @@ -117,6 +118,10 @@ public class DatasetServiceImpl implements DatasetService { @Autowired DatasetExpressionRepository datasetExpressionRepository; + @Autowired + @Lazy + private DatasetServiceImpl datasetServiceImpl; + private static final Logger LOG = LoggerFactory.getLogger(DatasetServiceImpl.class); @Override @@ -376,39 +381,66 @@ public List queryStatistics(String studyNameInRegExp, String studyName @Override @Async - @Transactional public void deleteNiftis(Long studyId) { - List datasets = this.findByStudyId(studyId); + List datasets = repository.findIdsByStudyId(studyId); + ShanoirEvent event = new ShanoirEvent(ShanoirEventType.DELETE_NIFTI_EVENT, studyId.toString(), KeycloakUtil.getTokenUserId(), "Preparing deletion of niftis", ShanoirEvent.IN_PROGRESS, 0, studyId); shanoirEventService.publishEvent(event); try { int total = datasets.size(); - float progress = 0; - for (Dataset dataset : datasets) { - progress = progress + 1f / total; - event.setProgress(progress); - event.setMessage("Deleting nifti for dataset: " + dataset.getId()); - shanoirEventService.publishEvent(event); - deleteNifti(dataset); + datasetServiceImpl.updateEvent(0f, event, null); + for (List partition : ListUtils.partition(datasets, 1000)){ + datasetServiceImpl.deletePartitionOfNiftis(partition, total, event); } + updateEvent(1f, event, studyId); + } catch (Exception e) { + datasetServiceImpl.updateEvent(-1f, event, studyId, e); + + } + } + + @Transactional + protected void deletePartitionOfNiftis(List partition, float total, ShanoirEvent event) { + + float progress = event.getProgress(); + for (Dataset dataset : repository.findAllById(partition)) { + progress = progress + 1f / total; + updateEvent(progress, event, dataset.getId()); + deleteNifti(dataset); + } + } + + protected void updateEvent(float progress, ShanoirEvent event, Long id) { + datasetServiceImpl.updateEvent(progress, event, id, null); + } + + @Transactional + protected void updateEvent(float progress, ShanoirEvent event, Long id, Exception e) { + event.setProgress(progress); + if (progress == 1f) { event.setProgress(1f); event.setStatus(ShanoirEvent.SUCCESS); - event.setMessage("Deleting nifti for study: " + studyId + ": Success."); - shanoirEventService.publishEvent(event); - } catch (Exception e) { + event.setMessage("Deleting nifti for study: " + id + ": Success."); + } else if (progress == -1f) { LOG.error("Could not properly delete niftis: ", e); event.setProgress(-1f); event.setStatus(ShanoirEvent.ERROR); - event.setMessage("Deleting nifti for study: " + studyId + ": Error. " + e.getMessage()); - shanoirEventService.publishEvent(event); + event.setMessage("Deleting nifti for study: " + id + ": Error. " + e.getMessage()); + } else if (Objects.isNull(id)) { + event.setProgress(progress); } + else if (Objects.isNull(id)) { + event.setProgress(progress); + event.setMessage("Deleting nifti for dataset: " + id); + } + shanoirEventService.publishEvent(event); } /** * Deletes nifti on file server * @param dataset */ - private void deleteNifti(Dataset dataset) { + public void deleteNifti(Dataset dataset) { Set expressionsToDelete = new HashSet<>(); for (Iterator iterex = dataset.getDatasetExpressions().iterator(); iterex.hasNext(); ) {