Skip to content

Commit

Permalink
Refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisszmundy committed Aug 26, 2024
1 parent 87c746f commit 1b1e020
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ void addSchedule(String surveyName,
void updateLastExecutionName(String surveyName, LocalDateTime newDate) throws NotFoundException;

long countSchedules();

List<KraftwerkExecutionSchedule> removeExpiredSchedules(StoredSurveySchedule surveySchedule);
}
56 changes: 33 additions & 23 deletions src/main/java/fr/insee/genesis/domain/service/ScheduleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,24 @@ public void deleteSchedule(String surveyName) throws NotFoundException {

public List<KraftwerkExecutionSchedule> deleteExpiredSchedules(String surveyName) throws NotFoundException {
List<StoredSurveySchedule> storedSurveySchedules = scheduleMongoDBRepository.findBySurveyName(surveyName);
if(storedSurveySchedules.isEmpty()){
if (storedSurveySchedules.isEmpty()) {
throw new NotFoundException();
}
List<KraftwerkExecutionSchedule> deletedKraftwerkExecutionSchedules = new ArrayList<>();
for(StoredSurveySchedule surveySchedule : storedSurveySchedules) {
for (KraftwerkExecutionSchedule kraftwerkExecutionScheduleToRemove :
surveySchedule.getKraftwerkExecutionScheduleList().stream().filter(
kraftwerkExecutionSchedule -> kraftwerkExecutionSchedule.getScheduleEndDate().isBefore(LocalDateTime.now())
).toList()) {
Query query = Query.query(Criteria.where("scheduleEndDate").is(kraftwerkExecutionScheduleToRemove.getScheduleEndDate()));
mongoTemplate.updateMulti(Query.query(Criteria.where("surveyName").is(surveyName)), new Update().pull(
"kraftwerkExecutionScheduleList", query),
Constants.MONGODB_SCHEDULE_COLLECTION_NAME);
log.info("Removed kraftwerk execution schedule on {} because it is expired since {}", surveyName,
kraftwerkExecutionScheduleToRemove.getScheduleEndDate());

deletedKraftwerkExecutionSchedules.add(kraftwerkExecutionScheduleToRemove);
}
for (StoredSurveySchedule surveySchedule : storedSurveySchedules) {
deletedKraftwerkExecutionSchedules.addAll(removeExpiredSchedules(surveySchedule));
//Delete schedule if empty kraftwerkExecutionScheduleList
scheduleMongoDBRepository.findBySurveyName(surveyName)
.stream()
.filter(storedSurveySchedule -> storedSurveySchedule.getKraftwerkExecutionScheduleList().isEmpty())
.forEach(storedSurveySchedule -> {
try {
deleteSchedule(surveyName);
} catch (NotFoundException e) {
log.error("Tried to delete schedule for {} but wasn't found !", surveyName);
}
});
}
//Delete schedule if empty kraftwerkExecutionScheduleList
storedSurveySchedules = scheduleMongoDBRepository.findBySurveyName(surveyName);
for(StoredSurveySchedule surveySchedule : storedSurveySchedules) {
if(surveySchedule.getKraftwerkExecutionScheduleList().isEmpty()){
deleteSchedule(surveyName);
}
}

return deletedKraftwerkExecutionSchedules;
}

Expand All @@ -129,4 +120,23 @@ public void updateLastExecutionName(String surveyName, LocalDateTime newDate) th
public long countSchedules() {
return scheduleMongoDBRepository.count();
}

@Override
public List<KraftwerkExecutionSchedule> removeExpiredSchedules(StoredSurveySchedule surveySchedule) {
List<KraftwerkExecutionSchedule> deletedKraftwerkExecutionSchedules = new ArrayList<>();
for (KraftwerkExecutionSchedule kraftwerkExecutionScheduleToRemove :
surveySchedule.getKraftwerkExecutionScheduleList().stream().filter(
kraftwerkExecutionSchedule -> kraftwerkExecutionSchedule.getScheduleEndDate().isBefore(LocalDateTime.now())
).toList()) {
deletedKraftwerkExecutionSchedules.add(kraftwerkExecutionScheduleToRemove);
Query query =
Query.query(Criteria.where("scheduleEndDate").is(kraftwerkExecutionScheduleToRemove.getScheduleEndDate()));
mongoTemplate.updateMulti(Query.query(Criteria.where("surveyName").is(surveySchedule.getSurveyName())), new Update().pull(
"kraftwerkExecutionScheduleList", query),
Constants.MONGODB_SCHEDULE_COLLECTION_NAME);
log.info("Removed kraftwerk execution schedule on {} because it is expired since {}", surveySchedule.getSurveyName(),
kraftwerkExecutionScheduleToRemove.getScheduleEndDate());
}
return deletedKraftwerkExecutionSchedules;
}
}
35 changes: 22 additions & 13 deletions src/test/java/fr/insee/genesis/stubs/ScheduleApiPortStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,16 @@ public void deleteSchedule(String surveyName) throws NotFoundException {
public List<KraftwerkExecutionSchedule> deleteExpiredSchedules(String surveyName) throws NotFoundException {
List<StoredSurveySchedule> mongoStubFiltered = mongoStub.stream().filter(scheduleDocument ->
scheduleDocument.getSurveyName().equals(surveyName)).toList();
if(mongoStubFiltered.isEmpty()){
throw new NotFoundException();
}
List<KraftwerkExecutionSchedule> deletedKraftwerkExecutionSchedules = new ArrayList<>();
if(!mongoStubFiltered.isEmpty()) {
for(StoredSurveySchedule surveySchedule : mongoStubFiltered){
deletedKraftwerkExecutionSchedules.addAll(surveySchedule.getKraftwerkExecutionScheduleList().stream().filter(
kraftwerkExecutionSchedule1 ->
kraftwerkExecutionSchedule1.getScheduleEndDate().isBefore(LocalDateTime.now())).toList());
surveySchedule.getKraftwerkExecutionScheduleList().removeIf(
kraftwerkExecutionSchedule1 ->
kraftwerkExecutionSchedule1.getScheduleEndDate().isBefore(LocalDateTime.now())
);
if(surveySchedule.getKraftwerkExecutionScheduleList().isEmpty()){
mongoStub.remove(surveySchedule);
}
for(StoredSurveySchedule surveySchedule : mongoStubFiltered){
deletedKraftwerkExecutionSchedules.addAll(removeExpiredSchedules(surveySchedule));
if(surveySchedule.getKraftwerkExecutionScheduleList().isEmpty()){
mongoStub.remove(surveySchedule);
}
}else throw new NotFoundException();
}
return deletedKraftwerkExecutionSchedules;
}

Expand All @@ -135,4 +130,18 @@ public void updateLastExecutionName(String surveyName, LocalDateTime newDate) th
public long countSchedules() {
return mongoStub.size();
}

@Override
public List<KraftwerkExecutionSchedule> removeExpiredSchedules(StoredSurveySchedule surveySchedule) {
List<KraftwerkExecutionSchedule> deletedKraftwerkExecutionSchedules = new ArrayList<>(
surveySchedule.getKraftwerkExecutionScheduleList().stream().filter(
kraftwerkExecutionSchedule1 ->
kraftwerkExecutionSchedule1.getScheduleEndDate().isBefore(LocalDateTime.now())).toList()
);
surveySchedule.getKraftwerkExecutionScheduleList().removeIf(
kraftwerkExecutionSchedule1 ->
kraftwerkExecutionSchedule1.getScheduleEndDate().isBefore(LocalDateTime.now())
);
return deletedKraftwerkExecutionSchedules;
}
}

0 comments on commit 1b1e020

Please sign in to comment.