Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #384 from admin-ch/feature/VACCINECER-2315_Vorbere…
Browse files Browse the repository at this point in the history
…itung_Verkleinerung_der_Revokationstabelle

Improved reduction of revocation list.
  • Loading branch information
haraldloesing authored Oct 12, 2022
2 parents 881fdf2 + 4d7680b commit b5ac4f6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package ch.admin.bag.covidcertificate.service;

import ch.admin.bag.covidcertificate.api.request.SystemSource;
import ch.admin.bag.covidcertificate.domain.Revocation;
import ch.admin.bag.covidcertificate.domain.RevocationRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -15,29 +12,31 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static ch.admin.bag.covidcertificate.api.Constants.KPI_REVOCATION_LIST_REDUCTION_SYSTEM_KEY;
import static ch.admin.bag.covidcertificate.api.Constants.KPI_TYPE_REVOCATION_LIST_REDUCTION;
import static ch.admin.bag.covidcertificate.service.KpiDataService.CRON_ACCOUNT_CC_MANAGEMENT_SERVICE;

@Component
@RequiredArgsConstructor
@Slf4j
@ConditionalOnProperty(value = "CF_INSTANCE_INDEX", havingValue = "0")
public class RevocationListReductionScheduler {

private final RevocationRepository revocationRepository;
private final KpiDataService kpiDataService;
private final RevocationListReductionService revocationListReductionService;

@Value("${cc-management-service.update-deleted-marker.batch-size}")
private int batchSize;
@Value("${cc-management-service.update-deleted-marker.days-protected}")
private int daysProtected;

@Transactional
/**
* Method scheduled with Shedlock to detect revocations to be taken from the list of all revocations as the
* underlying covid certificates are no longer valid.
* To do so those revocations are marked as deleted. The covid certificate still is shown as invalid as the
* valid to date is in the past.
* Important is, that we don't touch revocations that got marked as fraud as those need to be 100% revoked
* and not only invalid.
*/
@Scheduled(cron = "${cc-management-service.update-deleted-marker.cron}")
public void updateDeletedMarker() {
@Transactional
public void detectRevocationsToBeMarkedAsDeleted() {
final var jobDateTime = LocalDateTime.now();
log.info("Starting reduction of list with revocations at {}",
jobDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
Expand All @@ -46,26 +45,13 @@ public void updateDeletedMarker() {
LocalTime deleteTime = LocalTime.now();
LocalDateTime deleteDate = LocalDateTime.of(deleteDay, deleteTime);
LocalDateTime latestValidDate = LocalDateTime.of(deleteDay.minusDays(daysProtected), LocalTime.MIDNIGHT);

List<Revocation> deletableUvcis = revocationRepository.findDeletableUvcis(latestValidDate, batchSize);
log.info("Identified {} revocations to be marked as deleted", deletableUvcis.size());
int markedCounter = 0;
for (Revocation revocation : deletableUvcis) {
revocation.setDeletedDateTime(deleteDate);
try {
revocationRepository.save(revocation);
markedCounter++;
kpiDataService.logRevocationListReductionKpiWithoutSecurityContext(
KPI_REVOCATION_LIST_REDUCTION_SYSTEM_KEY,
KPI_TYPE_REVOCATION_LIST_REDUCTION,
revocation.getUvci(),
SystemSource.RevocationListReduction,
CRON_ACCOUNT_CC_MANAGEMENT_SERVICE);
} catch (Exception ex) {
// keep rolling but log the issue
log.error("Exception updating revocations with deleted marker", ex);
for (int repeat = 0; repeat < 10; repeat++) {
log.info("Portion {} of {} each revocations to be marked as deleted", repeat, batchSize);
boolean toBeContinued = this.revocationListReductionService.updateDeletedMarker(
latestValidDate, deleteDate, batchSize);
if(toBeContinued == false) {
break;
}
}
log.info("Ending update of {} revocations marked as deleted", markedCounter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ch.admin.bag.covidcertificate.service;

import ch.admin.bag.covidcertificate.api.request.SystemSource;
import ch.admin.bag.covidcertificate.domain.Revocation;
import ch.admin.bag.covidcertificate.domain.RevocationRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import static ch.admin.bag.covidcertificate.api.Constants.KPI_REVOCATION_LIST_REDUCTION_SYSTEM_KEY;
import static ch.admin.bag.covidcertificate.api.Constants.KPI_TYPE_REVOCATION_LIST_REDUCTION;
import static ch.admin.bag.covidcertificate.service.KpiDataService.CRON_ACCOUNT_CC_MANAGEMENT_SERVICE;

@Service
@RequiredArgsConstructor
@Slf4j
public class RevocationListReductionService {

private final RevocationRepository revocationRepository;
private final KpiDataService kpiDataService;

@Transactional
public boolean updateDeletedMarker(LocalDateTime latestValidDate, LocalDateTime deleteDate, int batchSize) {

List<Revocation> deletableUvcis = revocationRepository.findDeletableUvcis(latestValidDate, batchSize);

if (CollectionUtils.isEmpty(deletableUvcis)) {
log.info("No revocations found to be marked as deleted");
return false;
}

log.info("Identified {} revocations to be marked as deleted", deletableUvcis.size());
int markedCounter = 0;
for (Revocation revocation : deletableUvcis) {
revocation.setDeletedDateTime(deleteDate);
try {
revocationRepository.save(revocation);
markedCounter++;
kpiDataService.logRevocationListReductionKpiWithoutSecurityContext(
KPI_REVOCATION_LIST_REDUCTION_SYSTEM_KEY,
KPI_TYPE_REVOCATION_LIST_REDUCTION,
revocation.getUvci(),
SystemSource.RevocationListReduction,
CRON_ACCOUNT_CC_MANAGEMENT_SERVICE);
} catch (Exception ex) {
// keep rolling but log the issue
log.error("Exception updating revocations with deleted marker", ex);
}
}
log.info("Ending update of {} revocations marked as deleted", markedCounter);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void testRevocationListAfterDeletion() {
// create creation KPI and prepare revocation
createCreationKpiDataAndPrepareRevocation();
// call deletion batch job on service level
this.revocationListReductionScheduler.updateDeletedMarker();
this.revocationListReductionScheduler.detectRevocationsToBeMarkedAsDeleted();
// get list of revoked UVCIs
List<String> listedUvcis = this.revocationService.getRevocations();
// check this list against prepared test data
Expand All @@ -83,8 +83,6 @@ void testRevocationListAfterDeletion() {

/**
* Method to create 3 vaccine KPI and 3 test KPI and prepare 4 of them for revocation.
*
* @return a list of 4 KPI to be revoked.
*/
private void createCreationKpiDataAndPrepareRevocation() {
String uvci = this.persistVaccineCreationKpi();
Expand Down

0 comments on commit b5ac4f6

Please sign in to comment.