Skip to content

Commit cd67b0b

Browse files
fix: delete only expired V2 schedules from dataProcessingContext (#477)
* fix: delete only expired V2 schedules from dataProcessingContext * update delete logic to delete v1 and V2 schedules * delete unused import and method * remove only matching expired V1 schedules
1 parent 6877cc1 commit cd67b0b

5 files changed

Lines changed: 183 additions & 100 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package fr.insee.genesis.domain.model.context.schedule;
2+
3+
import java.util.List;
4+
5+
public record DeletedExpiredSchedules(
6+
List<KraftwerkExecutionSchedule> v1Schedules,
7+
List<KraftwerkExecutionScheduleV2> v2Schedules
8+
) {
9+
public boolean isEmpty() {
10+
return v1Schedules.isEmpty() && v2Schedules.isEmpty();
11+
}
12+
}

src/main/java/fr/insee/genesis/domain/ports/spi/DataProcessingContextPersistancePort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package fr.insee.genesis.domain.ports.spi;
22

33
import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
4-
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionSchedule;
4+
import fr.insee.genesis.domain.model.context.schedule.DeletedExpiredSchedules;
55
import fr.insee.genesis.infrastructure.document.context.DataProcessingContextDocument;
66

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

2222
long count();
2323

24-
List<KraftwerkExecutionSchedule> removeExpiredSchedules(DataProcessingContextModel dataProcessingContextModel) throws IOException;
24+
DeletedExpiredSchedules removeExpiredSchedules(DataProcessingContextModel dataProcessingContextModel) throws IOException;
2525

2626
List<DataProcessingContextDocument> findAllByReview(boolean withReview);
2727
}

src/main/java/fr/insee/genesis/domain/service/context/DataProcessingContextService.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import fr.insee.genesis.controller.dto.KraftwerkExecutionScheduleInput;
77
import fr.insee.genesis.controller.dto.rawdata.ScheduleResponseDto;
88
import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
9-
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionSchedule;
9+
import fr.insee.genesis.domain.model.context.schedule.DeletedExpiredSchedules;
1010
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionScheduleV2;
1111
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
1212
import fr.insee.genesis.domain.ports.api.DataProcessingContextApiPort;
@@ -266,33 +266,49 @@ public List<ScheduleResponseDto> getAllSchedulesV2() {
266266
@Override
267267
public void deleteExpiredSchedules(String logFolder) throws GenesisException {
268268
List<DataProcessingContextModel> dataProcessingContextModels =
269-
DataProcessingContextMapper.INSTANCE.listDocumentToListModel(dataProcessingContextPersistancePort.findAll());
270-
for(DataProcessingContextModel context : dataProcessingContextModels){
269+
DataProcessingContextMapper.INSTANCE.listDocumentToListModel(
270+
dataProcessingContextPersistancePort.findAll()
271+
);
272+
273+
for (DataProcessingContextModel context : dataProcessingContextModels) {
271274
try {
272-
List<KraftwerkExecutionSchedule> deletedKraftwerkExecutionSchedules = dataProcessingContextPersistancePort.removeExpiredSchedules(context);
273-
//Save in JSON log
274-
if(!deletedKraftwerkExecutionSchedules.isEmpty()) {
275+
DeletedExpiredSchedules deletedSchedules =
276+
dataProcessingContextPersistancePort.removeExpiredSchedules(context);
277+
278+
if (!deletedSchedules.isEmpty()) {
275279
String scheduleName = context.getCollectionInstrumentId();
276-
Path jsonLogPath = Path.of(logFolder, Constants.SCHEDULE_ARCHIVE_FOLDER_NAME,
277-
scheduleName + ".json");
280+
Path jsonLogPath = Path.of(
281+
logFolder,
282+
Constants.SCHEDULE_ARCHIVE_FOLDER_NAME,
283+
scheduleName + ".json"
284+
);
285+
278286
ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
279287
objectMapper.registerModule(new JavaTimeModule());
280-
String jsonToWrite = objectMapper.writeValueAsString(deletedKraftwerkExecutionSchedules);
281-
if(Files.exists(jsonLogPath)){
282-
//Remove last ] and append survey
288+
289+
String jsonToWrite = objectMapper.writeValueAsString(deletedSchedules);
290+
291+
if (Files.exists(jsonLogPath)) {
283292
StringBuilder content = new StringBuilder(Files.readString(jsonLogPath));
284-
content.setCharAt(content.length()-1, ',');
285-
content.append(jsonToWrite, 1, jsonToWrite.length()-1);
293+
content.setCharAt(content.length() - 1, ',');
294+
content.append(jsonToWrite, 1, jsonToWrite.length() - 1);
286295
content.append(']');
287-
Files.write(jsonLogPath, content.toString().getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
288-
}else {
296+
Files.write(
297+
jsonLogPath,
298+
content.toString().getBytes(),
299+
StandardOpenOption.TRUNCATE_EXISTING
300+
);
301+
} else {
289302
Files.createDirectories(jsonLogPath.getParent());
290303
Files.write(jsonLogPath, jsonToWrite.getBytes());
291304
}
292305
}
293306
} catch (IOException _) {
294-
String name = context.getCollectionInstrumentId();
295-
throw new GenesisException(HttpStatus.INTERNAL_SERVER_ERROR,String.format("An error occured trying to delete expired schedules for %s",name));
307+
String name = context.getCollectionInstrumentId();
308+
throw new GenesisException(
309+
HttpStatus.INTERNAL_SERVER_ERROR,
310+
String.format("An error occured trying to delete expired schedules for %s", name)
311+
);
296312
}
297313
}
298314
}

src/main/java/fr/insee/genesis/infrastructure/adapter/DataProcessingContextMongoAdapter.java

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import fr.insee.genesis.Constants;
44
import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
5+
import fr.insee.genesis.domain.model.context.schedule.DeletedExpiredSchedules;
56
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionSchedule;
7+
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionScheduleV2;
68
import fr.insee.genesis.domain.ports.spi.DataProcessingContextPersistancePort;
79
import fr.insee.genesis.infrastructure.document.context.DataProcessingContextDocument;
810
import fr.insee.genesis.infrastructure.mappers.DataProcessingContextMapper;
@@ -16,10 +18,9 @@
1618
import org.springframework.data.mongodb.core.query.Update;
1719
import org.springframework.stereotype.Service;
1820

19-
import java.io.IOException;
2021
import java.time.LocalDateTime;
21-
import java.util.ArrayList;
2222
import java.util.List;
23+
import java.util.Optional;
2324

2425
@Service
2526
@Qualifier("dataProcessingContextMongoAdapter")
@@ -75,23 +76,66 @@ public long count() {
7576
}
7677

7778
@Override
78-
public List<KraftwerkExecutionSchedule> removeExpiredSchedules(DataProcessingContextModel dataProcessingContextModel) throws IOException {
79-
//TODO move non mongo related logic to service
80-
List<KraftwerkExecutionSchedule> deletedKraftwerkExecutionSchedules = new ArrayList<>();
81-
for (KraftwerkExecutionSchedule kraftwerkExecutionScheduleToRemove :
82-
dataProcessingContextModel.getKraftwerkExecutionScheduleList().stream().filter(
83-
kraftwerkExecutionSchedule -> kraftwerkExecutionSchedule.getScheduleEndDate().isBefore(LocalDateTime.now())
84-
).toList()) {
85-
deletedKraftwerkExecutionSchedules.add(kraftwerkExecutionScheduleToRemove);
86-
Query query =
87-
Query.query(Criteria.where("scheduleEndDate").is(kraftwerkExecutionScheduleToRemove.getScheduleEndDate()));
88-
if (dataProcessingContextModel.getCollectionInstrumentId() != null){
89-
mongoTemplate.updateMulti(Query.query(Criteria.where("collectionInstrumentId").is(dataProcessingContextModel.getCollectionInstrumentId())), new Update().pull(
90-
"kraftwerkExecutionScheduleList", query),
91-
Constants.MONGODB_SCHEDULE_COLLECTION_NAME);
92-
}
79+
public DeletedExpiredSchedules removeExpiredSchedules(DataProcessingContextModel context) {
80+
LocalDateTime now = LocalDateTime.now();
81+
82+
List<KraftwerkExecutionSchedule> deletedV1 =
83+
Optional.ofNullable(context.getKraftwerkExecutionScheduleList())
84+
.orElse(List.of())
85+
.stream()
86+
.filter(schedule -> schedule.getScheduleEndDate() != null)
87+
.filter(schedule -> schedule.getScheduleEndDate().isBefore(now))
88+
.toList();
89+
90+
List<KraftwerkExecutionScheduleV2> deletedV2 =
91+
Optional.ofNullable(context.getKraftwerkExecutionScheduleV2List())
92+
.orElse(List.of())
93+
.stream()
94+
.filter(schedule -> schedule.getScheduleEndDate() != null)
95+
.filter(schedule -> schedule.getScheduleEndDate().isBefore(now))
96+
.toList();
97+
98+
Query query = Query.query(
99+
Criteria.where("collectionInstrumentId").is(context.getCollectionInstrumentId())
100+
);
101+
102+
for (KraftwerkExecutionSchedule scheduleToRemove : deletedV1) {
103+
Update update = new Update().pull(
104+
"kraftwerkExecutionScheduleList",
105+
Query.query(
106+
Criteria.where("partitionId").is(scheduleToRemove.getPartitionId())
107+
.and("frequency").is(scheduleToRemove.getFrequency())
108+
.and("serviceToCall").is(scheduleToRemove.getServiceToCall())
109+
.and("scheduleBeginDate").is(scheduleToRemove.getScheduleBeginDate())
110+
.and("scheduleEndDate").is(scheduleToRemove.getScheduleEndDate())
111+
.and("trustParameters").is(scheduleToRemove.getTrustParameters())
112+
).getQueryObject()
113+
);
114+
115+
mongoTemplate.updateMulti(
116+
query,
117+
update,
118+
Constants.MONGODB_CONTEXT_COLLECTION_NAME
119+
);
93120
}
94-
return deletedKraftwerkExecutionSchedules;
121+
122+
for (KraftwerkExecutionScheduleV2 scheduleToRemove : deletedV2) {
123+
Update update = new Update().pull(
124+
"kraftwerkExecutionScheduleV2List",
125+
Query.query(
126+
Criteria.where("scheduleUuid")
127+
.is(scheduleToRemove.getScheduleUuid())
128+
).getQueryObject()
129+
);
130+
131+
mongoTemplate.updateMulti(
132+
query,
133+
update,
134+
Constants.MONGODB_CONTEXT_COLLECTION_NAME
135+
);
136+
}
137+
138+
return new DeletedExpiredSchedules(deletedV1, deletedV2);
95139
}
96140

97141
@Override

0 commit comments

Comments
 (0)