Skip to content

Commit

Permalink
extended search of the triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
sterlp committed Jan 6, 2025
1 parent 9bdf937 commit eee23b9
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ public AddTriggerRequest<T> newUniqueTrigger(T state) {
return new TaskTriggerBuilder<>(this).state(state).build();
}

@SuppressWarnings("rawtypes")
public static TaskId<?> of(String taskId) {
public static TaskId<Serializable> of(String taskId) {
if (taskId == null || taskId.isBlank()) return null;
return new TaskId(taskId);
return new TaskId<>(taskId);
}

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Serializable>(taskName.trim());
return of(StringUtils.trimToNull(id), taskId);
}

public static TriggerKey of(@Nullable String id, TaskId<? extends Serializable> taskId) {
return new TriggerKey(id == null ? UUID.randomUUID().toString() : id, taskId.name());
}

public TaskId<Serializable> toTaskId() {
if (taskName == null) return null;
return new TaskId<>(taskName);
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +77,15 @@ public long countTriggers(TriggerKey key) {
return triggerHistoryDetailRepository.countByKey(key);
}

public Page<TriggerHistoryLastStateEntity> findTriggerState(TaskId<?> taskId, Pageable page) {
return triggerHistoryDetailRepository.findAll(page);
public Page<TriggerHistoryLastStateEntity> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,9 +34,11 @@ public List<Trigger> listInstances(@PathVariable("instanceId") long instanceId)

@GetMapping("history")
public PagedModel<Trigger> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends HasTriggerData> extends JpaRepository<T, Long> {

@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<T> findAll(@Param("id") String id,
@Param("taskName") String taskName, Pageable page);

@Query("""
SELECT e FROM #{#entityName} e
WHERE e.data.key.taskName = :taskName
""")
Page<TriggerEntity> findAll(
@Param("taskName") String taskName, Pageable page);
Page<T> findAll(@Param("taskName") String taskName, Pageable page);

@Query("""
SELECT COUNT(e.data.key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ public Optional<TriggerEntity> get(TriggerKey triggerKey) {
return readTrigger.get(triggerKey);
}

@Transactional(readOnly = true , timeout = 10)
public Page<TriggerEntity> findAllTriggers(TriggerKey key, Pageable page) {
return this.readTrigger.listTriggers(key, page);
}

@Transactional(readOnly = true , timeout = 10)
public Page<TriggerEntity> findAllTriggers(TaskId<?> task, Pageable page) {
return this.editTrigger.listTriggers(task, page);
return this.readTrigger.listTriggers(task, page);
}

public void deleteAll() {
Expand Down Expand Up @@ -137,7 +142,7 @@ public Optional<TriggerEntity> cancel(TriggerKey key) {
* @return the amount of stored tasks
*/
@Transactional(timeout = 5, readOnly = true)
public long countTriggers(@Nullable TaskId<String> taskId) {
public long countTriggers(@Nullable TaskId<?> taskId) {
if (taskId == null || taskId.name() == null) {
return 0L;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ public long count() {

@GetMapping("triggers")
public PagedModel<Trigger> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,11 +34,6 @@ public class EditTriggerComponent {
private final StateSerializer stateSerializer = new StateSerializer();
private final TriggerRepository triggerRepository;

public Page<TriggerEntity> listTriggers(TaskId<?> task, Pageable page) {
if (task == null) return triggerRepository.findAll(page);
return triggerRepository.findAll(task.name(), page);
}

public Optional<TriggerEntity> completeTaskWithSuccess(TriggerKey key, Serializable state) {
return this.completeTaskWithStatus(key, state, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -46,4 +50,15 @@ public boolean hasPendingTriggers() {
public List<TriggerEntity> findTriggersLastPingAfter(OffsetDateTime dateTime) {
return triggerRepository.findTriggersLastPingAfter(dateTime);
}

public Page<TriggerEntity> 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<TriggerEntity> listTriggers(TaskId<? extends Serializable> task, Pageable page) {
if (task == null) return triggerRepository.findAll(page);
return triggerRepository.findAll(task.name(), page);
}
}

0 comments on commit eee23b9

Please sign in to comment.