-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend/frontend] handle endpoint filtering
- Loading branch information
1 parent
c377ffa
commit 36cb5bc
Showing
23 changed files
with
526 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
openbas-api/src/main/java/io/openbas/rest/asset/endpoint/EndpointCriteriaBuilderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package io.openbas.rest.asset.endpoint; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.openbas.database.model.Endpoint; | ||
import io.openbas.rest.asset.endpoint.form.EndpointOutput; | ||
import io.openbas.utils.pagination.SearchPaginationInput; | ||
import jakarta.annotation.Resource; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.persistence.Tuple; | ||
import jakarta.persistence.TypedQuery; | ||
import jakarta.persistence.criteria.*; | ||
import lombok.RequiredArgsConstructor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
import static io.openbas.database.criteria.GenericCriteria.countQuery; | ||
import static io.openbas.rest.asset.endpoint.EndpointQueryHelper.execution; | ||
import static io.openbas.rest.asset.endpoint.EndpointQueryHelper.select; | ||
import static io.openbas.utils.pagination.PaginationUtils.buildPaginationCriteriaBuilder; | ||
import static io.openbas.utils.pagination.SortUtilsCriteriaBuilder.toSortCriteriaBuilder; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class EndpointCriteriaBuilderService { | ||
|
||
@Resource | ||
protected ObjectMapper mapper; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
public Page<EndpointOutput> endpointPagination( | ||
@NotNull SearchPaginationInput searchPaginationInput) { | ||
return buildPaginationCriteriaBuilder( | ||
this::paginate, | ||
searchPaginationInput, | ||
Endpoint.class | ||
); | ||
} | ||
|
||
public List<EndpointOutput> find(Specification<Endpoint> specification) { | ||
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder(); | ||
|
||
CriteriaQuery<Tuple> cq = cb.createTupleQuery(); | ||
Root<Endpoint> root = cq.from(Endpoint.class); | ||
select(cb, cq, root); | ||
|
||
if (specification != null) { | ||
Predicate predicate = specification.toPredicate(root, cq, cb); | ||
if (predicate != null) { | ||
cq.where(predicate); | ||
} | ||
} | ||
|
||
TypedQuery<Tuple> query = entityManager.createQuery(cq); | ||
return execution(query, this.mapper); | ||
} | ||
|
||
// -- PRIVATE -- | ||
|
||
private Page<EndpointOutput> paginate( | ||
Specification<Endpoint> specification, | ||
Specification<Endpoint> specificationCount, | ||
Pageable pageable) { | ||
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder(); | ||
|
||
CriteriaQuery<Tuple> cq = cb.createTupleQuery(); | ||
Root<Endpoint> endpointRoot = cq.from(Endpoint.class); | ||
select(cb, cq, endpointRoot); | ||
|
||
// -- Specification -- | ||
if (specification != null) { | ||
Predicate predicate = specification.toPredicate(endpointRoot, cq, cb); | ||
if (predicate != null) { | ||
cq.where(predicate); | ||
} | ||
} | ||
|
||
// -- Sorting -- | ||
List<Order> orders = toSortCriteriaBuilder(cb, endpointRoot, pageable.getSort()); | ||
cq.orderBy(orders); | ||
|
||
// Type Query | ||
TypedQuery<Tuple> query = entityManager.createQuery(cq); | ||
|
||
// -- Pagination -- | ||
query.setFirstResult((int) pageable.getOffset()); | ||
query.setMaxResults(pageable.getPageSize()); | ||
|
||
// -- EXECUTION -- | ||
List<EndpointOutput> endpoints = execution(query, this.mapper); | ||
|
||
// -- Count Query -- | ||
Long total = countQuery(cb, this.entityManager, Endpoint.class, specificationCount); | ||
|
||
return new PageImpl<>(endpoints, pageable, total); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
openbas-api/src/main/java/io/openbas/rest/asset/endpoint/EndpointQueryHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.openbas.rest.asset.endpoint; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.openbas.database.model.*; | ||
import io.openbas.rest.asset.endpoint.form.EndpointOutput; | ||
import jakarta.persistence.Tuple; | ||
import jakarta.persistence.TypedQuery; | ||
import jakarta.persistence.criteria.*; | ||
|
||
import java.time.Instant; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static io.openbas.database.model.Asset.ACTIVE_THRESHOLD; | ||
import static io.openbas.utils.JpaUtils.createJoinArrayAggOnId; | ||
import static io.openbas.utils.JpaUtils.createLeftJoin; | ||
import static java.time.Instant.now; | ||
|
||
public class EndpointQueryHelper { | ||
|
||
private EndpointQueryHelper() {} | ||
|
||
// -- SELECT -- | ||
|
||
public static void select(CriteriaBuilder cb, CriteriaQuery<Tuple> cq, Root<Endpoint> endpointRoot) { | ||
// Array aggregations | ||
Join<Endpoint, Executor> endpointExecutorJoin = createLeftJoin(endpointRoot, "executor"); | ||
Expression<String[]> tagIdsExpression = createJoinArrayAggOnId(cb, endpointRoot, "tags"); | ||
|
||
// Multiselect | ||
cq.multiselect( | ||
endpointRoot.get("id").alias("asset_id"), | ||
endpointRoot.get("name").alias("asset_name"), | ||
endpointExecutorJoin.get("id").alias("asset_executor"), | ||
endpointRoot.get("lastSeen").alias("asset_last_seen"), | ||
endpointRoot.get("platform").alias("endpoint_platform"), | ||
endpointRoot.get("arch").alias("endpoint_arch"), | ||
tagIdsExpression.alias("asset_tags") | ||
).distinct(true); | ||
|
||
// Group by | ||
cq.groupBy(Collections.singletonList( | ||
endpointRoot.get("id") | ||
)); | ||
} | ||
|
||
// -- EXECUTION -- | ||
|
||
public static List<EndpointOutput> execution(TypedQuery<Tuple> query, ObjectMapper mapper) { | ||
return query.getResultList() | ||
.stream() | ||
.map(tuple -> (EndpointOutput) EndpointOutput.builder() | ||
.id(tuple.get("asset_id", String.class)) | ||
.name(tuple.get("asset_name", String.class)) | ||
.executor(tuple.get("asset_executor", String.class)) | ||
.active(isActive(tuple.get("asset_last_seen", Instant.class))) | ||
.tags(Arrays.stream(tuple.get("asset_tags", String[].class)).collect(Collectors.toSet())) | ||
.platform(tuple.get("endpoint_platform", Endpoint.PLATFORM_TYPE.class)) | ||
.arch(tuple.get("endpoint_arch", Endpoint.PLATFORM_ARCH.class)) | ||
.build()) | ||
.toList(); | ||
} | ||
|
||
private static boolean isActive(Instant lastSeen) { | ||
return Optional.ofNullable(lastSeen) | ||
.map(last -> (now().toEpochMilli() - last.toEpochMilli()) < ACTIVE_THRESHOLD).orElse(false); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
openbas-api/src/main/java/io/openbas/rest/asset/endpoint/form/EndpointOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.openbas.rest.asset.endpoint.form; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.openbas.database.model.Endpoint; | ||
import io.openbas.rest.asset.form.AssetOutput; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
@EqualsAndHashCode(callSuper = true) | ||
@Data | ||
@SuperBuilder | ||
public class EndpointOutput extends AssetOutput { | ||
@NotNull | ||
@JsonProperty("endpoint_platform") | ||
private Endpoint.PLATFORM_TYPE platform; | ||
|
||
@NotNull | ||
@JsonProperty("endpoint_arch") | ||
private Endpoint.PLATFORM_ARCH arch; | ||
} |
29 changes: 29 additions & 0 deletions
29
openbas-api/src/main/java/io/openbas/rest/asset/form/AssetOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.openbas.rest.asset.form; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Data; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
import java.util.Set; | ||
|
||
@SuperBuilder | ||
@Data | ||
public abstract class AssetOutput { | ||
@NotBlank | ||
@JsonProperty("asset_id") | ||
private String id; | ||
|
||
@NotBlank | ||
@JsonProperty("asset_name") | ||
private String name; | ||
|
||
@JsonProperty("asset_executor") | ||
private String executor; | ||
|
||
@JsonProperty("asset_tags") | ||
private Set<String> tags; | ||
|
||
@JsonProperty("asset_active") | ||
private boolean active; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.