RED-8702: Explore document databases to store entityLog
* removed observed * added migration
This commit is contained in:
parent
268e3cbf58
commit
e7e9d88238
@ -0,0 +1,80 @@
|
||||
package com.iqser.red.service.persistence.management.v1.processor.migration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.DossierEntity;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.FileEntity;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.service.FileManagementStorageService;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.DossierRepository;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository.FileRepository;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.utils.StorageIdUtils;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.analysislog.entitylog.EntityLog;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;
|
||||
import com.iqser.red.storage.commons.exception.StorageException;
|
||||
import com.iqser.red.storage.commons.exception.StorageObjectDoesNotExist;
|
||||
import com.iqser.red.storage.commons.service.StorageService;
|
||||
import com.knecon.fforesight.tenantcommons.TenantContext;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class StorageToMongoCopyService {
|
||||
|
||||
private final FileManagementStorageService fileManagementStorageService;
|
||||
private final StorageService storageService;
|
||||
|
||||
private final DossierRepository dossierRepository;
|
||||
private final FileRepository fileRepository;
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public void copy() {
|
||||
|
||||
List<DossierEntity> dossierEntities = dossierRepository.findAll();
|
||||
List<DossierFile> files = new ArrayList<>();
|
||||
dossierEntities.forEach(dossierEntity -> {
|
||||
List<FileEntity> byDossierId = fileRepository.findByDossierId(dossierEntity.getId());
|
||||
byDossierId.forEach(file -> files.add(new DossierFile(dossierEntity.getId(), file.getId())));
|
||||
});
|
||||
|
||||
for (DossierFile dossierFile : files) {
|
||||
log.info("Reading dossier {} file {} from storage", dossierFile.dossierId, dossierFile.fileId);
|
||||
Optional<EntityLog> entityLogFromStorage = getEntityLogFromStorageForMigration(dossierFile.dossierId, dossierFile.fileId);
|
||||
if (entityLogFromStorage.isPresent()) {
|
||||
log.info("File found, now saving in mongodb");
|
||||
fileManagementStorageService.saveEntityLog(dossierFile.dossierId, dossierFile.fileId, entityLogFromStorage.get());
|
||||
log.info("Deleting old file from storage");
|
||||
fileManagementStorageService.deleteObject(dossierFile.dossierId, dossierFile.fileId, FileType.ENTITY_LOG);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Optional<EntityLog> getEntityLogFromStorageForMigration(String dossierId, String fileId) {
|
||||
|
||||
try {
|
||||
return Optional.ofNullable(storageService.readJSONObject(TenantContext.getTenantId(),
|
||||
StorageIdUtils.getStorageId(dossierId, fileId, FileType.ENTITY_LOG),
|
||||
EntityLog.class));
|
||||
} catch (StorageObjectDoesNotExist e) {
|
||||
log.debug("EntityLog does not exist");
|
||||
} catch (StorageException e) {
|
||||
log.debug(e.getMessage());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
public record DossierFile(String dossierId, String fileId) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package com.iqser.red.service.persistence.management.v1.processor.migration.migrations;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.iqser.red.service.persistence.management.v1.processor.migration.Migration;
|
||||
import com.iqser.red.service.persistence.management.v1.processor.migration.StorageToMongoCopyService;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Setter
|
||||
@Service
|
||||
public class StorageToMongoMigration17 extends Migration {
|
||||
|
||||
private final StorageToMongoCopyService storageToMongoCopyService;
|
||||
|
||||
private static final String NAME = "Migration for entity log storage from s3 to mongodb";
|
||||
private static final long VERSION = 17;
|
||||
|
||||
|
||||
public StorageToMongoMigration17(StorageToMongoCopyService storageToMongoCopyService) {
|
||||
|
||||
super(NAME, VERSION);
|
||||
this.storageToMongoCopyService = storageToMongoCopyService;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void migrate() {
|
||||
|
||||
log.info("Migration: Copying all files for all dossiers to mongodb");
|
||||
storageToMongoCopyService.copy();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -25,7 +25,6 @@ import com.iqser.red.storage.commons.exception.StorageObjectDoesNotExist;
|
||||
import com.iqser.red.storage.commons.service.StorageService;
|
||||
import com.knecon.fforesight.tenantcommons.TenantContext;
|
||||
|
||||
import io.micrometer.observation.annotation.Observed;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -101,7 +100,6 @@ public class FileManagementStorageService {
|
||||
}
|
||||
|
||||
|
||||
@Observed(name = "FileManagementStorageService", contextualName = "get-entity-log")
|
||||
public EntityLog getEntityLog(String dossierId, String fileId) {
|
||||
|
||||
return entityLogMongoService.findEntityLogByDossierIdAndFileId(dossierId, fileId)
|
||||
@ -109,14 +107,14 @@ public class FileManagementStorageService {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
@Observed(name = "FileManagementStorageService", contextualName = "save-entity-log")
|
||||
public void saveEntityLog(String dossierId, String fileId, EntityLog entityLog) {
|
||||
|
||||
entityLogMongoService.saveEntityLog(dossierId, fileId, entityLog);
|
||||
}
|
||||
|
||||
@Observed(name = "FileManagementStorageService", contextualName = "entity-log-exists")
|
||||
|
||||
public boolean entityLogExists(String dossierId, String fileId) {
|
||||
|
||||
return entityLogMongoService.entityLogDocumentExists(dossierId, fileId);
|
||||
@ -174,7 +172,7 @@ public class FileManagementStorageService {
|
||||
storageService.deleteObject(TenantContext.getTenantId(), StorageIdUtils.getStorageId(dossierId, fileId, fileType));
|
||||
}
|
||||
|
||||
@Observed(name = "FileManagementStorageService", contextualName = "delete-entity-log")
|
||||
|
||||
public void deleteEntityLog(String dossierId, String fileId) {
|
||||
|
||||
entityLogMongoService.deleteEntityLog(dossierId, fileId);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user