Skip to content

Commit

Permalink
fix(core,jdbc): PurgeLog with levels in postgres
Browse files Browse the repository at this point in the history
Fixes #4604
  • Loading branch information
loicmathieu authored and brian-mulier-p committed Aug 8, 2024
1 parent 0e46055 commit ae15cef
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ public class LogEntry implements DeletedInterface, TenantInterface {
@Builder.Default
boolean deleted = false;

public static List<String> findLevelsByMin(Level minLevel) {
public static List<Level> findLevelsByMin(Level minLevel) {
if (minLevel == null) {
return Arrays.stream(Level.values()).map(Enum::name).toList();
return Arrays.asList(Level.values());
}

return Arrays.stream(Level.values())
.filter(level -> level.toInt() >= minLevel.toInt())
.map(Enum::name)
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ void delete() {

logRepository.save(log1);

logRepository.deleteByQuery(null, "io.kestra.unittest", "flowId", null, null, ZonedDateTime.now().plusMinutes(1));
logRepository.deleteByQuery(null, "io.kestra.unittest", "flowId", List.of(Level.TRACE, Level.DEBUG, Level.INFO), null, ZonedDateTime.now().plusMinutes(1));

find = logRepository.findByExecutionId(null, log1.getExecutionId(), null, Pageable.from(1, 50));
assertThat(find.size(), is(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.event.Level;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


Expand All @@ -27,10 +28,9 @@ protected Condition findCondition(String query) {
}

@Override
protected Condition minLevel(Level minLevel) {
protected Condition levelsCondition(List<Level> levels) {
return DSL.condition("level in (" +
LogEntry
.findLevelsByMin(minLevel)
levels
.stream()
.map(s -> "'" + s + "'::log_level")
.collect(Collectors.joining(", ")) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public int deleteByQuery(String tenantId, String namespace, String flowId, List<
}

if (logLevels != null) {
delete = delete.and(field("level").in(logLevels));
delete = delete.and(levelsCondition(logLevels));
}

return delete.execute();
Expand Down Expand Up @@ -493,7 +493,11 @@ private List<LogEntry> query(String tenantId, Condition condition, Level minLeve
});
}

protected Condition minLevel(Level minLevel) {
return field("level").in(LogEntry.findLevelsByMin(minLevel));
private Condition minLevel(Level minLevel) {
return levelsCondition(LogEntry.findLevelsByMin(minLevel));
}

protected Condition levelsCondition(List<Level> levels) {
return field("level").in(levels.stream().map(level -> level.name()).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public Flux<Event<LogEntry>> follow(
@Parameter(description = "The min log level filter") @Nullable @QueryValue Level minLevel
) {
AtomicReference<Runnable> cancel = new AtomicReference<>();
List<String> levels = LogEntry.findLevelsByMin(minLevel);
List<String> levels = LogEntry.findLevelsByMin(minLevel).stream().map(level -> level.name()).toList();

return Flux
.<Event<LogEntry>>create(emitter -> {
Expand Down

0 comments on commit ae15cef

Please sign in to comment.