Skip to content

Commit

Permalink
#2502 : mass nifti deletion update
Browse files Browse the repository at this point in the history
  • Loading branch information
DuckflipXYZ committed Dec 6, 2024
1 parent 5b052a6 commit 1600cd8
Showing 1 changed file with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -376,39 +381,66 @@ public List<Object[]> queryStatistics(String studyNameInRegExp, String studyName

@Override
@Async
@Transactional
public void deleteNiftis(Long studyId) {
List<Dataset> datasets = this.findByStudyId(studyId);
List<Long> 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<Long> 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<Long> 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<DatasetExpression> expressionsToDelete = new HashSet<>();

for (Iterator<DatasetExpression> iterex = dataset.getDatasetExpressions().iterator(); iterex.hasNext(); ) {
Expand Down

0 comments on commit 1600cd8

Please sign in to comment.