From eee23b97add0fa86547a4b14e0de6ed6a8cae74b Mon Sep 17 00:00:00 2001 From: Paul Sterl Date: Mon, 6 Jan 2025 17:36:15 +0100 Subject: [PATCH] extended search of the triggers --- .../spring/persistent_tasks/api/TaskId.java | 5 ++--- .../spring/persistent_tasks/api/TriggerKey.java | 16 +++++++++++++++- .../persistent_tasks/history/HistoryService.java | 13 ++++++++++--- .../history/api/TriggerHistoryResource.java | 6 +++++- .../shared/repository/TriggerDataRepository.java | 13 +++++++++---- .../persistent_tasks/trigger/TriggerService.java | 9 +++++++-- .../trigger/api/TriggerResource.java | 5 +++-- .../trigger/component/EditTriggerComponent.java | 10 +--------- .../trigger/component/ReadTriggerComponent.java | 15 +++++++++++++++ 9 files changed, 67 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/org/sterl/spring/persistent_tasks/api/TaskId.java b/core/src/main/java/org/sterl/spring/persistent_tasks/api/TaskId.java index 1c631af8..9ccd1505 100644 --- a/core/src/main/java/org/sterl/spring/persistent_tasks/api/TaskId.java +++ b/core/src/main/java/org/sterl/spring/persistent_tasks/api/TaskId.java @@ -24,10 +24,9 @@ public AddTriggerRequest newUniqueTrigger(T state) { return new TaskTriggerBuilder<>(this).state(state).build(); } - @SuppressWarnings("rawtypes") - public static TaskId of(String taskId) { + public static TaskId of(String taskId) { if (taskId == null || taskId.isBlank()) return null; - return new TaskId(taskId); + return new TaskId<>(taskId); } @RequiredArgsConstructor(access = AccessLevel.PRIVATE) diff --git a/core/src/main/java/org/sterl/spring/persistent_tasks/api/TriggerKey.java b/core/src/main/java/org/sterl/spring/persistent_tasks/api/TriggerKey.java index 881448cb..16dd3528 100644 --- a/core/src/main/java/org/sterl/spring/persistent_tasks/api/TriggerKey.java +++ b/core/src/main/java/org/sterl/spring/persistent_tasks/api/TriggerKey.java @@ -4,6 +4,9 @@ import java.time.OffsetDateTime; import java.util.UUID; +import org.apache.commons.lang3.StringUtils; +import org.springframework.lang.Nullable; + import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -24,11 +27,22 @@ public class TriggerKey implements Serializable { private String id; private String taskName; - public static TriggerKey of(String id, TaskId taskId) { + public static TriggerKey of(@Nullable String id, String taskName) { + if (StringUtils.trimToNull(id) == null + && StringUtils.trimToNull(taskName) == null) return null; + + var taskId = StringUtils.trimToNull(taskName) == null + ? null + : new TaskId(taskName.trim()); + return of(StringUtils.trimToNull(id), taskId); + } + + public static TriggerKey of(@Nullable String id, TaskId taskId) { return new TriggerKey(id == null ? UUID.randomUUID().toString() : id, taskId.name()); } public TaskId toTaskId() { + if (taskName == null) return null; return new TaskId<>(taskName); } /** diff --git a/core/src/main/java/org/sterl/spring/persistent_tasks/history/HistoryService.java b/core/src/main/java/org/sterl/spring/persistent_tasks/history/HistoryService.java index 9d97d6e1..5a7180bc 100644 --- a/core/src/main/java/org/sterl/spring/persistent_tasks/history/HistoryService.java +++ b/core/src/main/java/org/sterl/spring/persistent_tasks/history/HistoryService.java @@ -8,7 +8,6 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort.Direction; -import org.sterl.spring.persistent_tasks.api.TaskId; import org.sterl.spring.persistent_tasks.api.TriggerKey; import org.sterl.spring.persistent_tasks.history.model.TriggerHistoryDetailEntity; import org.sterl.spring.persistent_tasks.history.model.TriggerHistoryLastStateEntity; @@ -78,7 +77,15 @@ public long countTriggers(TriggerKey key) { return triggerHistoryDetailRepository.countByKey(key); } - public Page findTriggerState(TaskId taskId, Pageable page) { - return triggerHistoryDetailRepository.findAll(page); + public Page findTriggerState( + TriggerKey key, Pageable page) { + if (key == null) return triggerHistoryDetailRepository.findAll(page); + if (key.getId() == null && key.getTaskName() != null) { + return triggerHistoryDetailRepository.findAll(key.getTaskName(), page); + } + return triggerHistoryDetailRepository.findAll( + key.getId(), + key.getTaskName(), + page); } } diff --git a/core/src/main/java/org/sterl/spring/persistent_tasks/history/api/TriggerHistoryResource.java b/core/src/main/java/org/sterl/spring/persistent_tasks/history/api/TriggerHistoryResource.java index 85ec52e7..4c0343e3 100644 --- a/core/src/main/java/org/sterl/spring/persistent_tasks/history/api/TriggerHistoryResource.java +++ b/core/src/main/java/org/sterl/spring/persistent_tasks/history/api/TriggerHistoryResource.java @@ -9,8 +9,10 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.sterl.spring.persistent_tasks.api.Trigger; +import org.sterl.spring.persistent_tasks.api.TriggerKey; import org.sterl.spring.persistent_tasks.history.HistoryService; import org.sterl.spring.persistent_tasks.history.api.HistoryConverter.FromLastTriggerStateEntity; import org.sterl.spring.persistent_tasks.history.api.HistoryConverter.FromTriggerStateDetailEntity; @@ -32,9 +34,11 @@ public List listInstances(@PathVariable("instanceId") long instanceId) @GetMapping("history") public PagedModel list( + @RequestParam(name = "id", required = false) String taskId, + @RequestParam(name = "taskName", required = false) String name, @PageableDefault(size = 100, direction = Direction.DESC, sort = "id") Pageable pageable) { return FromLastTriggerStateEntity.INSTANCE.toPage( // - historyService.findTriggerState(null, pageable)); + historyService.findTriggerState(TriggerKey.of(taskId, name), pageable)); } } diff --git a/core/src/main/java/org/sterl/spring/persistent_tasks/shared/repository/TriggerDataRepository.java b/core/src/main/java/org/sterl/spring/persistent_tasks/shared/repository/TriggerDataRepository.java index b8004b23..93e83026 100644 --- a/core/src/main/java/org/sterl/spring/persistent_tasks/shared/repository/TriggerDataRepository.java +++ b/core/src/main/java/org/sterl/spring/persistent_tasks/shared/repository/TriggerDataRepository.java @@ -13,17 +13,22 @@ import org.sterl.spring.persistent_tasks.api.TriggerKey; import org.sterl.spring.persistent_tasks.shared.model.HasTriggerData; import org.sterl.spring.persistent_tasks.shared.model.TriggerStatus; -import org.sterl.spring.persistent_tasks.trigger.model.TriggerEntity; @NoRepositoryBean public interface TriggerDataRepository extends JpaRepository { - + @Query(""" + SELECT e FROM #{#entityName} e + WHERE (e.data.key.id LIKE :id% OR :id = NULL) + OR (e.data.key.taskName = :taskName OR :taskName = NULL) + """) + Page findAll(@Param("id") String id, + @Param("taskName") String taskName, Pageable page); + @Query(""" SELECT e FROM #{#entityName} e WHERE e.data.key.taskName = :taskName """) - Page findAll( - @Param("taskName") String taskName, Pageable page); + Page findAll(@Param("taskName") String taskName, Pageable page); @Query(""" SELECT COUNT(e.data.key) diff --git a/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/TriggerService.java b/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/TriggerService.java index 9e1f66fa..2269b9ca 100644 --- a/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/TriggerService.java +++ b/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/TriggerService.java @@ -93,9 +93,14 @@ public Optional get(TriggerKey triggerKey) { return readTrigger.get(triggerKey); } + @Transactional(readOnly = true , timeout = 10) + public Page findAllTriggers(TriggerKey key, Pageable page) { + return this.readTrigger.listTriggers(key, page); + } + @Transactional(readOnly = true , timeout = 10) public Page findAllTriggers(TaskId task, Pageable page) { - return this.editTrigger.listTriggers(task, page); + return this.readTrigger.listTriggers(task, page); } public void deleteAll() { @@ -137,7 +142,7 @@ public Optional cancel(TriggerKey key) { * @return the amount of stored tasks */ @Transactional(timeout = 5, readOnly = true) - public long countTriggers(@Nullable TaskId taskId) { + public long countTriggers(@Nullable TaskId taskId) { if (taskId == null || taskId.name() == null) { return 0L; } diff --git a/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/api/TriggerResource.java b/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/api/TriggerResource.java index 245164c7..807bcc8f 100644 --- a/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/api/TriggerResource.java +++ b/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/api/TriggerResource.java @@ -37,11 +37,12 @@ public long count() { @GetMapping("triggers") public PagedModel list( - @RequestParam(name = "taskId", required = false) String taskId, + @RequestParam(name = "id", required = false) String taskId, + @RequestParam(name = "taskName", required = false) String name, @PageableDefault(size = 100, direction = Direction.DESC, sort = "id") Pageable pageable) { return FromTriggerEntity.INSTANCE.toPage( - triggerService.findAllTriggers(TaskId.of(taskId), pageable)); + triggerService.findAllTriggers(TriggerKey.of(taskId, name), pageable)); } @PostMapping("triggers/{taskName}/{id}/run-at") diff --git a/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/component/EditTriggerComponent.java b/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/component/EditTriggerComponent.java index 8c62b233..b8053580 100644 --- a/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/component/EditTriggerComponent.java +++ b/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/component/EditTriggerComponent.java @@ -7,20 +7,17 @@ import java.util.Optional; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.lang.NonNull; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.sterl.spring.persistent_tasks.api.AddTriggerRequest; -import org.sterl.spring.persistent_tasks.api.TaskId; import org.sterl.spring.persistent_tasks.api.TriggerKey; import org.sterl.spring.persistent_tasks.shared.model.TriggerData; import org.sterl.spring.persistent_tasks.shared.model.TriggerStatus; import org.sterl.spring.persistent_tasks.trigger.event.TriggerAddedEvent; import org.sterl.spring.persistent_tasks.trigger.event.TriggerCanceledEvent; -import org.sterl.spring.persistent_tasks.trigger.event.TriggerSuccessEvent; import org.sterl.spring.persistent_tasks.trigger.event.TriggerFailedEvent; +import org.sterl.spring.persistent_tasks.trigger.event.TriggerSuccessEvent; import org.sterl.spring.persistent_tasks.trigger.model.TriggerEntity; import org.sterl.spring.persistent_tasks.trigger.repository.TriggerRepository; @@ -37,11 +34,6 @@ public class EditTriggerComponent { private final StateSerializer stateSerializer = new StateSerializer(); private final TriggerRepository triggerRepository; - public Page listTriggers(TaskId task, Pageable page) { - if (task == null) return triggerRepository.findAll(page); - return triggerRepository.findAll(task.name(), page); - } - public Optional completeTaskWithSuccess(TriggerKey key, Serializable state) { return this.completeTaskWithStatus(key, state, null); } diff --git a/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/component/ReadTriggerComponent.java b/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/component/ReadTriggerComponent.java index ec33091a..f289a048 100644 --- a/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/component/ReadTriggerComponent.java +++ b/core/src/main/java/org/sterl/spring/persistent_tasks/trigger/component/ReadTriggerComponent.java @@ -1,10 +1,14 @@ package org.sterl.spring.persistent_tasks.trigger.component; +import java.io.Serializable; import java.time.OffsetDateTime; import java.util.List; import java.util.Optional; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.lang.Nullable; +import org.sterl.spring.persistent_tasks.api.TaskId; import org.sterl.spring.persistent_tasks.api.TriggerKey; import org.sterl.spring.persistent_tasks.shared.model.TriggerStatus; import org.sterl.spring.persistent_tasks.shared.stereotype.TransactionalCompontant; @@ -46,4 +50,15 @@ public boolean hasPendingTriggers() { public List findTriggersLastPingAfter(OffsetDateTime dateTime) { return triggerRepository.findTriggersLastPingAfter(dateTime); } + + public Page listTriggers(TriggerKey key, Pageable page) { + if (key == null) return triggerRepository.findAll(page); + if (key.getId() == null) return listTriggers(key.toTaskId(), page); + return listTriggers(key.toTaskId(), page); + } + + public Page listTriggers(TaskId task, Pageable page) { + if (task == null) return triggerRepository.findAll(page); + return triggerRepository.findAll(task.name(), page); + } }