Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.insee.genesis.domain.model.context.schedule;

import java.util.List;

public record DeletedExpiredSchedules(
List<KraftwerkExecutionSchedule> v1Schedules,
List<KraftwerkExecutionScheduleV2> v2Schedules
) {
public boolean isEmpty() {
return v1Schedules.isEmpty() && v2Schedules.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.insee.genesis.domain.ports.spi;

import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionSchedule;
import fr.insee.genesis.domain.model.context.schedule.DeletedExpiredSchedules;
import fr.insee.genesis.infrastructure.document.context.DataProcessingContextDocument;

import java.io.IOException;
Expand All @@ -21,7 +21,7 @@ public interface DataProcessingContextPersistancePort {

long count();

List<KraftwerkExecutionSchedule> removeExpiredSchedules(DataProcessingContextModel dataProcessingContextModel) throws IOException;
DeletedExpiredSchedules removeExpiredSchedules(DataProcessingContextModel dataProcessingContextModel) throws IOException;

List<DataProcessingContextDocument> findAllByReview(boolean withReview);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import fr.insee.genesis.controller.dto.KraftwerkExecutionScheduleInput;
import fr.insee.genesis.controller.dto.rawdata.ScheduleResponseDto;
import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionSchedule;
import fr.insee.genesis.domain.model.context.schedule.DeletedExpiredSchedules;
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionScheduleV2;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.domain.ports.api.DataProcessingContextApiPort;
Expand Down Expand Up @@ -266,33 +266,49 @@ public List<ScheduleResponseDto> getAllSchedulesV2() {
@Override
public void deleteExpiredSchedules(String logFolder) throws GenesisException {
List<DataProcessingContextModel> dataProcessingContextModels =
DataProcessingContextMapper.INSTANCE.listDocumentToListModel(dataProcessingContextPersistancePort.findAll());
for(DataProcessingContextModel context : dataProcessingContextModels){
DataProcessingContextMapper.INSTANCE.listDocumentToListModel(
dataProcessingContextPersistancePort.findAll()
);

for (DataProcessingContextModel context : dataProcessingContextModels) {
try {
List<KraftwerkExecutionSchedule> deletedKraftwerkExecutionSchedules = dataProcessingContextPersistancePort.removeExpiredSchedules(context);
//Save in JSON log
if(!deletedKraftwerkExecutionSchedules.isEmpty()) {
DeletedExpiredSchedules deletedSchedules =
dataProcessingContextPersistancePort.removeExpiredSchedules(context);

if (!deletedSchedules.isEmpty()) {
String scheduleName = context.getCollectionInstrumentId();
Path jsonLogPath = Path.of(logFolder, Constants.SCHEDULE_ARCHIVE_FOLDER_NAME,
scheduleName + ".json");
Path jsonLogPath = Path.of(
logFolder,
Constants.SCHEDULE_ARCHIVE_FOLDER_NAME,
scheduleName + ".json"
);

ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
objectMapper.registerModule(new JavaTimeModule());
String jsonToWrite = objectMapper.writeValueAsString(deletedKraftwerkExecutionSchedules);
if(Files.exists(jsonLogPath)){
//Remove last ] and append survey

String jsonToWrite = objectMapper.writeValueAsString(deletedSchedules);

if (Files.exists(jsonLogPath)) {
StringBuilder content = new StringBuilder(Files.readString(jsonLogPath));
content.setCharAt(content.length()-1, ',');
content.append(jsonToWrite, 1, jsonToWrite.length()-1);
content.setCharAt(content.length() - 1, ',');
content.append(jsonToWrite, 1, jsonToWrite.length() - 1);
content.append(']');
Files.write(jsonLogPath, content.toString().getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
}else {
Files.write(
jsonLogPath,
content.toString().getBytes(),
StandardOpenOption.TRUNCATE_EXISTING
);
} else {
Files.createDirectories(jsonLogPath.getParent());
Files.write(jsonLogPath, jsonToWrite.getBytes());
}
}
} catch (IOException _) {
String name = context.getCollectionInstrumentId();
throw new GenesisException(HttpStatus.INTERNAL_SERVER_ERROR,String.format("An error occured trying to delete expired schedules for %s",name));
String name = context.getCollectionInstrumentId();
throw new GenesisException(
HttpStatus.INTERNAL_SERVER_ERROR,
String.format("An error occured trying to delete expired schedules for %s", name)
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import fr.insee.genesis.Constants;
import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
import fr.insee.genesis.domain.model.context.schedule.DeletedExpiredSchedules;
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionSchedule;
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionScheduleV2;
import fr.insee.genesis.domain.ports.spi.DataProcessingContextPersistancePort;
import fr.insee.genesis.infrastructure.document.context.DataProcessingContextDocument;
import fr.insee.genesis.infrastructure.mappers.DataProcessingContextMapper;
Expand All @@ -16,10 +18,9 @@
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Qualifier("dataProcessingContextMongoAdapter")
Expand Down Expand Up @@ -75,23 +76,62 @@ public long count() {
}

@Override
public List<KraftwerkExecutionSchedule> removeExpiredSchedules(DataProcessingContextModel dataProcessingContextModel) throws IOException {
//TODO move non mongo related logic to service
List<KraftwerkExecutionSchedule> deletedKraftwerkExecutionSchedules = new ArrayList<>();
for (KraftwerkExecutionSchedule kraftwerkExecutionScheduleToRemove :
dataProcessingContextModel.getKraftwerkExecutionScheduleList().stream().filter(
kraftwerkExecutionSchedule -> kraftwerkExecutionSchedule.getScheduleEndDate().isBefore(LocalDateTime.now())
).toList()) {
deletedKraftwerkExecutionSchedules.add(kraftwerkExecutionScheduleToRemove);
Query query =
Query.query(Criteria.where("scheduleEndDate").is(kraftwerkExecutionScheduleToRemove.getScheduleEndDate()));
if (dataProcessingContextModel.getCollectionInstrumentId() != null){
mongoTemplate.updateMulti(Query.query(Criteria.where("collectionInstrumentId").is(dataProcessingContextModel.getCollectionInstrumentId())), new Update().pull(
"kraftwerkExecutionScheduleList", query),
Constants.MONGODB_SCHEDULE_COLLECTION_NAME);
}
public DeletedExpiredSchedules removeExpiredSchedules(DataProcessingContextModel context) {
LocalDateTime now = LocalDateTime.now();

List<KraftwerkExecutionSchedule> deletedV1 =
Optional.ofNullable(context.getKraftwerkExecutionScheduleList())
.orElse(List.of())
.stream()
.filter(schedule -> schedule.getScheduleEndDate() != null)
.filter(schedule -> schedule.getScheduleEndDate().isBefore(now))
.toList();

List<KraftwerkExecutionScheduleV2> deletedV2 =
Optional.ofNullable(context.getKraftwerkExecutionScheduleV2List())
.orElse(List.of())
.stream()
.filter(schedule -> schedule.getScheduleEndDate() != null)
.filter(schedule -> schedule.getScheduleEndDate().isBefore(now))
.toList();

Query query = Query.query(
Criteria.where("collectionInstrumentId").is(context.getCollectionInstrumentId())
);

for (KraftwerkExecutionSchedule scheduleToRemove : deletedV1) {
Update update = new Update().pull(
"kraftwerkExecutionScheduleList",
Query.query(
Criteria.where("scheduleEndDate")
.is(scheduleToRemove.getScheduleEndDate())
).getQueryObject()
);

mongoTemplate.updateMulti(
query,
update,
Constants.MONGODB_CONTEXT_COLLECTION_NAME
);
}
return deletedKraftwerkExecutionSchedules;

for (KraftwerkExecutionScheduleV2 scheduleToRemove : deletedV2) {
Update update = new Update().pull(
"kraftwerkExecutionScheduleV2List",
Query.query(
Criteria.where("scheduleUuid")
.is(scheduleToRemove.getScheduleUuid())
).getQueryObject()
);

mongoTemplate.updateMulti(
query,
update,
Constants.MONGODB_CONTEXT_COLLECTION_NAME
);
}

return new DeletedExpiredSchedules(deletedV1, deletedV2);
}

@Override
Expand Down
Loading
Loading