Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion openaev-api/src/main/java/io/openaev/config/CachingConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@

import com.github.benmanes.caffeine.cache.Caffeine;
import java.time.Duration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableCaching
@Slf4j
public class CachingConfig {

@Bean
public CacheManager cacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager("license");
CaffeineCacheManager cacheManager = new CaffeineCacheManager("license", "lowRetentionCache");

cacheManager.setCaffeine(
Caffeine.newBuilder().expireAfterWrite(Duration.ofDays(1)).maximumSize(100));

return cacheManager;
}

/** Emptying the cache every second to avoid old data on the admin users being persisted */
@CacheEvict(value = "lowRetentionCache", allEntries = true)
@Scheduled(fixedRateString = "3000")
public void emptyAdminUsersCache() {
log.info("emptying low retention cache");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static io.openaev.database.model.Filters.isEmptyFilterGroup;
import static io.openaev.helper.StreamHelper.fromIterable;
import static io.openaev.utils.FilterUtilsJpa.computeFilterGroupJpa;
import static io.openaev.utils.FilterUtilsRuntime.computeFilterGroupRuntime;
import static java.time.Instant.now;

import io.openaev.database.model.*;
Expand All @@ -16,7 +15,6 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -149,25 +147,13 @@ private List<AssetGroup> computeDynamicAssets(@NotNull final List<AssetGroup> as
return assetGroups;
}

List<Asset> assets = this.assetService.assets();
assetGroups.forEach(
assetGroup -> {
if (!isEmptyFilterGroup(assetGroup.getDynamicFilter())) {
Predicate<Object> filters = computeFilterGroupRuntime(assetGroup.getDynamicFilter());

List<Asset> filteredAssets =
assets.stream()
.filter(
asset ->
"Endpoint"
.equals(
asset.getType())) // Filters for dynamic assets are applicable
// only to endpoints
.filter(filters)
.toList();

assetGroup.setDynamicAssets(filteredAssets);
}
Filters.FilterGroup filterGroup = assetGroup.getDynamicFilter();
filterGroup
.getFilters()
.add(Filters.Filter.getNewDefaultEqualFilter("asset_type", List.of("Endpoint")));
assetGroup.setDynamicAssets(this.assetService.assetsFromFilterGroups(filterGroup));
});
return assetGroups;
}
Expand Down
11 changes: 11 additions & 0 deletions openaev-api/src/main/java/io/openaev/service/AssetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import static io.openaev.helper.StreamHelper.fromIterable;

import io.openaev.database.model.Asset;
import io.openaev.database.model.Filters;
import io.openaev.database.model.SecurityPlatform;
import io.openaev.database.repository.AssetRepository;
import io.openaev.database.repository.SecurityPlatformRepository;
import io.openaev.utils.FilterUtilsJpa;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
Expand All @@ -27,6 +30,14 @@ public List<Asset> assets(@NotBlank final List<String> assetIds) {
return fromIterable(this.assetRepository.findAllById(assetIds));
}

@Cacheable("lowRetentionCache")
public List<Asset> assetsFromFilterGroups(@NotNull Filters.FilterGroup filterGroup) {
if (filterGroup.getFilters() == null || filterGroup.getFilters().isEmpty()) {
return List.of();
}
return this.assetRepository.findAll(FilterUtilsJpa.computeFilterGroupJpa(filterGroup));
}

public List<Asset> assets() {
return fromIterable(this.assetRepository.findAll());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Asset implements Base {
@Column(name = "asset_type", insertable = false, updatable = false)
@JsonProperty("asset_type")
@Setter(NONE)
@Queryable(filterable = true)
private String type;

@Queryable(searchable = true, sortable = true, filterable = true)
Expand Down
Loading