diff --git a/clinical-domain-agent/src/main/java/care/smith/fts/cda/DefaultTransferProcessRunner.java b/clinical-domain-agent/src/main/java/care/smith/fts/cda/DefaultTransferProcessRunner.java index 35838fa5..c3d64035 100644 --- a/clinical-domain-agent/src/main/java/care/smith/fts/cda/DefaultTransferProcessRunner.java +++ b/clinical-domain-agent/src/main/java/care/smith/fts/cda/DefaultTransferProcessRunner.java @@ -1,6 +1,7 @@ package care.smith.fts.cda; import static care.smith.fts.util.JsonLogFormatter.asJson; +import static care.smith.fts.util.NanoIdUtils.nanoId; import static java.util.stream.Stream.concat; import care.smith.fts.api.ConsentedPatient; @@ -14,7 +15,6 @@ import java.util.List; import java.util.Map; import java.util.Queue; -import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -39,9 +39,8 @@ public DefaultTransferProcessRunner( @Override public String start(TransferProcessDefinition process, List pids) { - var processId = UUID.randomUUID().toString(); - log.info("Run process with processId: {}", processId); - log.info("Project configuration: {}", asJson(om, process.rawConfig())); + var processId = nanoId(6); + log.info("[Process {}] Created, config: {}", processId, asJson(om, process.rawConfig())); var transferProcessInstance = new TransferProcessInstance(process, processId, pids); startOrQueue(processId, transferProcessInstance); @@ -56,6 +55,7 @@ private synchronized void startOrQueue( transferProcessInstance.execute(); instances.put(processId, transferProcessInstance); } else { + log.info("[Process {}] Queued", processId); queued.add(transferProcessInstance); } } @@ -126,9 +126,10 @@ public void execute() { .transform(this::selectData) .transform(this::deidentify) .transform(this::sendBundles) - .doOnComplete(this::updateStatus) + .doOnComplete(this::onComplete) .doOnComplete(DefaultTransferProcessRunner.this::onComplete) .subscribe(); + log.info("[Process {}] Started", processId()); } private Flux selectCohort(List pids) { @@ -159,8 +160,9 @@ private Flux sendBundles(Flux deidentification) { .onErrorContinue((e, r) -> status.updateAndGet(TransferProcessStatus::incSkippedBundles)); } - private void updateStatus() { - status.updateAndGet(s -> s.phase() != Phase.FATAL ? checkCompletion(s) : s); + private void onComplete() { + var status = this.status.updateAndGet(s -> s.phase() != Phase.FATAL ? checkCompletion(s) : s); + log.info("[Process {}] Finished with: {}", processId(), status.phase()); } private TransferProcessStatus checkCompletion(TransferProcessStatus s) { diff --git a/util/pom.xml b/util/pom.xml index 6c3d4b7b..d3e553ed 100644 --- a/util/pom.xml +++ b/util/pom.xml @@ -35,6 +35,12 @@ jakarta.validation-api + + com.aventrix.jnanoid + jnanoid + 2.0.0 + + org.junit.jupiter junit-jupiter diff --git a/util/src/main/java/care/smith/fts/util/NanoIdUtils.java b/util/src/main/java/care/smith/fts/util/NanoIdUtils.java new file mode 100644 index 00000000..92365db4 --- /dev/null +++ b/util/src/main/java/care/smith/fts/util/NanoIdUtils.java @@ -0,0 +1,12 @@ +package care.smith.fts.util; + +import static com.aventrix.jnanoid.jnanoid.NanoIdUtils.DEFAULT_ALPHABET; +import static com.aventrix.jnanoid.jnanoid.NanoIdUtils.DEFAULT_NUMBER_GENERATOR; +import static com.aventrix.jnanoid.jnanoid.NanoIdUtils.randomNanoId; + +public interface NanoIdUtils { + + static String nanoId(int size) { + return randomNanoId(DEFAULT_NUMBER_GENERATOR, DEFAULT_ALPHABET, size); + } +} diff --git a/util/src/test/java/care/smith/fts/util/NanoIdUtilsTest.java b/util/src/test/java/care/smith/fts/util/NanoIdUtilsTest.java new file mode 100644 index 00000000..ebc9b97e --- /dev/null +++ b/util/src/test/java/care/smith/fts/util/NanoIdUtilsTest.java @@ -0,0 +1,34 @@ +package care.smith.fts.util; + +import static care.smith.fts.util.NanoIdUtils.nanoId; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +import java.util.stream.IntStream; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class NanoIdUtilsTest { + private static Stream significantSizes() { + return IntStream.rangeClosed(1, 21).mapToObj(Arguments::of); + } + + @ParameterizedTest(name = "size of {0}") + @MethodSource("significantSizes") + public void idHasSpecifiedSize(int size) { + assertThat(nanoId(size)).hasSize(size); + } + + @Test + public void zeroSizeNotAllowed() { + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> nanoId(0)); + } + + @Test + public void negativeSizeNotAllowed() { + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> nanoId(-1)); + } +}