Skip to content

Commit

Permalink
[backend] Refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois committed Dec 26, 2024
1 parent fbcacea commit daf016c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
8 changes: 1 addition & 7 deletions openbas-api/src/main/java/io/openbas/executors/Executor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.openbas.executors;

import static io.openbas.database.model.InjectStatusExecution.traceInfo;
import static io.openbas.utils.InjectionUtils.isInInjectableRange;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.openbas.asset.QueueService;
Expand Down Expand Up @@ -155,11 +156,4 @@ public InjectStatus execute(ExecutableInject executableInject) {
+ injectorContract.getInjector().getType()));
}

// region utils
private boolean isInInjectableRange(Injection injection) {
Instant now = Instant.now();
Instant start = now.minus(Duration.parse("PT1H"));
Instant injectWhen = injection.getDate().orElseThrow();
return injectWhen.isAfter(start) && injectWhen.isBefore(now);
}
}
20 changes: 7 additions & 13 deletions openbas-api/src/main/java/io/openbas/executors/Injector.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.openbas.executors;

import static io.openbas.database.model.InjectStatusExecution.traceError;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.openbas.database.model.*;
Expand All @@ -11,15 +9,17 @@
import io.openbas.service.FileService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import static io.openbas.database.model.InjectStatusExecution.traceError;
import static io.openbas.utils.InjectionUtils.isInInjectableRange;

public abstract class Injector {

Expand Down Expand Up @@ -73,12 +73,6 @@ public Execution executeInjection(ExecutableInject executableInject) {
}

// region utils
private boolean isInInjectableRange(Injection injection) {
Instant now = Instant.now();
Instant start = now.minus(Duration.parse("PT1H"));
Instant injectWhen = injection.getDate().orElseThrow();
return injectWhen.isAfter(start) && injectWhen.isBefore(now);
}

public <T> T contentConvert(
@NotNull final ExecutableInject injection, @NotNull final Class<T> converter)
Expand Down
19 changes: 19 additions & 0 deletions openbas-api/src/main/java/io/openbas/utils/InjectionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.openbas.utils;

import io.openbas.database.model.Injection;

import java.time.Duration;
import java.time.Instant;

public class InjectionUtils {

private InjectionUtils() {}

public static boolean isInInjectableRange(Injection injection) {
Instant now = Instant.now();
Instant start = now.minus(Duration.parse("PT2M"));
Instant injectWhen = injection.getDate().orElseThrow();
return injectWhen.isAfter(start) && injectWhen.isBefore(now);
}

}

0 comments on commit daf016c

Please sign in to comment.