-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Old cases and events are deleted after a configurable time (def…
…ault is after 6 months) with all associated data. * Integrates scheduled jobs which deletes the old cases and events. * Sets the default property values in application.properties. * Extends the repositories for the necessary methods. * Fixes an error in IrisDateTimeProvider. The method is not allowed for `Instant`. * fixes and improves unittests * removes false JavaDoc @SInCE notes Integrates JavaFaker library for a simpler and random creation of test data. Refs iris-connect/iris-backlog#244 PR #384
- Loading branch information
1 parent
aff7f17
commit 3da22e4
Showing
14 changed files
with
411 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
iris-client-bff/src/main/java/iris/client_bff/cases/CaseDataSubmissionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
package iris.client_bff.cases; | ||
|
||
import iris.client_bff.cases.model.CaseDataSubmission; | ||
|
||
import iris.client_bff.cases.model.CaseDataSubmission.DataSubmissionIdentifier; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.util.Streamable; | ||
|
||
public interface CaseDataSubmissionRepository extends CrudRepository<CaseDataSubmission, DataSubmissionIdentifier> { | ||
public interface CaseDataSubmissionRepository extends JpaRepository<CaseDataSubmission, DataSubmissionIdentifier> { | ||
|
||
@Transactional | ||
Streamable<CaseDataSubmission> findAllByRequest(CaseDataRequest request); | ||
|
||
@Transactional | ||
void deleteAllByRequestIn(Iterable<? extends CaseDataRequest> requests); | ||
} |
76 changes: 76 additions & 0 deletions
76
iris-client-bff/src/main/java/iris/client_bff/cases/CaseDeleteJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package iris.client_bff.cases; | ||
|
||
import iris.client_bff.cases.CaseDataRequest.DataRequestIdentifier; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.time.LocalDate; | ||
import java.time.Period; | ||
import java.time.ZoneId; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.ConstructorBinding; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* This class collects all old cases and deletes this. | ||
* | ||
* @author Jens Kutzsche | ||
*/ | ||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
class CaseDeleteJob { | ||
|
||
private final @NonNull CaseDataRequestRepository caseRequests; | ||
private final @NonNull CaseDataSubmissionRepository caseSubmissions; | ||
private final @NonNull CaseDeletionProperties properties; | ||
|
||
@Transactional | ||
@Scheduled(cron = "${iris.client.case.delete-cron:-}") | ||
void deleteCaseRequests() { | ||
|
||
var refDate = LocalDate.now().minus(properties.getDeleteAfter()).atStartOfDay().atZone(ZoneId.systemDefault()) | ||
.toInstant(); | ||
|
||
var oldRequests = caseRequests.findByMetadataCreatedIsBefore(refDate).toList(); | ||
|
||
if (oldRequests.isEmpty()) { | ||
return; | ||
} | ||
|
||
log.debug("{} case data request(s) are deleted with period {} after their creation!", | ||
oldRequests, | ||
properties.getDeleteAfter(), | ||
oldRequests.get(0).getCreatedAt()); | ||
|
||
caseSubmissions.deleteAllByRequestIn(oldRequests); | ||
caseRequests.deleteAll(oldRequests); | ||
|
||
log.info("{} case data request(s) (IDs: {}) were deleted with period {} after their creation at {}!", | ||
oldRequests.size(), | ||
oldRequests.stream().map(CaseDataRequest::getId) | ||
.map(DataRequestIdentifier::toString) | ||
.collect(Collectors.joining(", ")), | ||
properties.getDeleteAfter(), | ||
oldRequests.get(0).getCreatedAt()); | ||
} | ||
|
||
@ConstructorBinding | ||
@RequiredArgsConstructor | ||
@ConfigurationProperties("iris.client.case") | ||
@Getter | ||
public static class CaseDeletionProperties { | ||
|
||
/** | ||
* Defines the {@link Period} after that a case will be deleted starting from the creation date. | ||
*/ | ||
private final Period deleteAfter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
iris-client-bff/src/main/java/iris/client_bff/events/EventDeleteJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package iris.client_bff.events; | ||
|
||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.time.LocalDate; | ||
import java.time.Period; | ||
import java.time.ZoneId; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.ConstructorBinding; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* This class collects all old events and deletes this. | ||
* | ||
* @author Jens Kutzsche | ||
*/ | ||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
class EventDeleteJob { | ||
|
||
private final @NonNull EventDataRequestRepository eventRequests; | ||
private final @NonNull EventDataSubmissionRepository eventSubmissions; | ||
private final @NonNull EventDeletionProperties properties; | ||
|
||
@Transactional | ||
@Scheduled(cron = "${iris.client.event.delete-cron:-}") | ||
void deleteEventRequests() { | ||
|
||
var refDate = LocalDate.now().minus(properties.getDeleteAfter()).atStartOfDay().atZone(ZoneId.systemDefault()) | ||
.toInstant(); | ||
|
||
var oldRequests = eventRequests.findByMetadataCreatedIsBefore(refDate).toList(); | ||
|
||
if (oldRequests.isEmpty()) { | ||
return; | ||
} | ||
|
||
log.debug("{} event data request(s) are deleted with period {} after their creation!", | ||
oldRequests, | ||
properties.getDeleteAfter(), | ||
oldRequests.get(0).getCreatedAt()); | ||
|
||
eventSubmissions.deleteAllByRequestIn(oldRequests); | ||
eventRequests.deleteAll(oldRequests); | ||
|
||
log.info("{} event data request(s) (IDs: {}) were deleted with period {} after their creation at {}!", | ||
oldRequests.size(), | ||
oldRequests.stream().map(EventDataRequest::getId) | ||
.map(EventDataRequest.DataRequestIdentifier::toString) | ||
.collect(Collectors.joining(", ")), | ||
properties.getDeleteAfter(), | ||
oldRequests.get(0).getCreatedAt()); | ||
} | ||
|
||
@ConstructorBinding | ||
@RequiredArgsConstructor | ||
@ConfigurationProperties("iris.client.event") | ||
@Getter | ||
public static class EventDeletionProperties { | ||
|
||
/** | ||
* Defines the {@link Period} after that a event will be deleted starting from the creation date. | ||
*/ | ||
private final Period deleteAfter; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
iris-client-bff/src/test/java/iris/client_bff/FakerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package iris.client_bff; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Locale; | ||
import java.util.Random; | ||
|
||
import org.apache.commons.lang3.RandomUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.github.javafaker.Faker; | ||
|
||
/** | ||
* @author Jens Kutzsche | ||
*/ | ||
@Configuration | ||
@Slf4j | ||
public class FakerConfig { | ||
|
||
@Value("${iris.test.faker.seed:#{null}}") | ||
Long preConfiguredSeed; | ||
|
||
static Faker faker; | ||
|
||
@Bean | ||
public Faker getFaker() { | ||
|
||
if (faker == null) { | ||
|
||
var seed = preConfiguredSeed == null ? RandomUtils.nextLong() : preConfiguredSeed; | ||
|
||
log.info( | ||
"Faker is created with SEED = {}; The seed can be set with property iris.test.faker.seed or env IRIS_TEST_FAKER_SEED.", | ||
seed); | ||
|
||
faker = new Faker(Locale.GERMANY, new Random(seed)); | ||
} | ||
|
||
return faker; | ||
} | ||
} |
Oops, something went wrong.