Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: batch processing was overriding previous results #121

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,23 @@
@Path("/purls")
public class PurlEndpoint {

@Inject
VulnerabilityService svc;
@Inject
VulnerabilityService svc;

@POST
public Uni<Map<String, List<OpenSourceVulnerability>>> find(PurlsRequest request) {
if(request == null || request.purls() == null || request.purls().isEmpty()) {
return Uni.createFrom().item(Collections.emptyMap());
}
return svc.findByPurls(request.purls());
@POST
public Uni<Map<String, List<OpenSourceVulnerability>>> find(PurlsRequest request) {
if (request == null || request.purls() == null || request.purls().isEmpty()) {
return Uni.createFrom().item(Collections.emptyMap());
}
return svc.findByPurls(request.purls());
}

@POST
@Path("/cves")
public Uni<Map<String, List<String>>> findCves(PurlsRequest request) {
if (request == null || request.purls() == null || request.purls().isEmpty()) {
return Uni.createFrom().item(Collections.emptyMap());
}
return svc.findCvesByPurls(request.purls());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ public interface VulnerabilityService {

Uni<Map<String, List<OpenSourceVulnerability>>> findByPurls(List<String> purls);

Uni<Map<String, List<String>>> findCvesByPurls(List<String> purls);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -75,12 +76,52 @@ public Uni<Map<String, OpenSourceVulnerability>> find(List<String> aliases) {
@Override
public Uni<Map<String, List<OpenSourceVulnerability>>> findByPurls(List<String> purls) {
return Multi.createFrom().items(new Partition(purls, MAX_OSV_BATCH).stream())
.onItem().transform(p -> processBatch(p)).onItem()
.transformToUniAndMerge(e -> e)
.toUni()
.onItem().transformToUniAndMerge(this::processBatch) // Merge the results from each batch
.collect().in(
() -> new HashMap<String, List<OpenSourceVulnerability>>(),
(Map<String, List<OpenSourceVulnerability>> acc, Map<String, List<OpenSourceVulnerability>> batchMap) -> {
batchMap.forEach((key, value) ->
acc.merge(key, value, (existing, newValues) -> {
existing.addAll(newValues);
return existing;
})
);
}
)
.onItem().ifNull().continueWith(Collections.emptyMap());
}

@Override
public Uni<Map<String, List<String>>> findCvesByPurls(List<String> purls) {
return Multi.createFrom().items(new Partition(purls, MAX_OSV_BATCH).stream())
.onItem().transformToUniAndMerge(this::processCveBatch) // Merge the results from each batch
.collect().in(
() -> new HashMap<String, List<String>>(),
(Map<String, List<String>> acc, Map<String, List<String>> batchMap) -> {
batchMap.forEach((key, value) ->
acc.merge(key, value, (existing, newValues) -> {
existing.addAll(newValues);
return existing;
})
);
}
)
.onItem().ifNull().continueWith(Collections.emptyMap());
}

private Uni<Map<String, List<String>>> processCveBatch(List<String> purls) {
var keys = Multi.createFrom().items(purls.stream());

var values = query(purls)
.onItem()
.transformToUniAndConcatenate(vulns -> find(vulns));

return Multi.createBy().combining().streams(keys, values).asTuple()
.collect().asMap(
t -> t.getItem1(),
t -> new ArrayList<>(t.getItem2().values().stream().map(OpenSourceVulnerability::getId).toList()));
}

private Uni<Map<String, List<OpenSourceVulnerability>>> processBatch(List<String> purls) {
var keys = Multi.createFrom().items(purls.stream());

Expand All @@ -89,13 +130,17 @@ private Uni<Map<String, List<OpenSourceVulnerability>>> processBatch(List<String
.transformToUniAndConcatenate(vulns -> find(vulns));

return Multi.createBy().combining().streams(keys, values).asTuple()
.collect().asMap(t -> t.getItem1(), t -> new ArrayList<>(t.getItem2().values()));
.collect().asMap(
t -> t.getItem1(),
t -> new ArrayList<>(t.getItem2().values()));
}

private Multi<List<String>> query(List<String> purls) {
List<QueryRequestItem> queries = purls.stream().map(purl -> new QueryRequestItem(new PackageRef(purl))).toList();
return osvApi.queryBatch(new QueryRequest(queries))
.onFailure().recoverWithItem(t -> new QueryResult(purls.stream().map(purl -> new QueryResultItem(Collections.emptyList())).toList()))
.onFailure()
.recoverWithItem(
t -> new QueryResult(purls.stream().map(purl -> new QueryResultItem(Collections.emptyList())).toList()))
.map(QueryResult::results)
.onItem().ifNull().continueWith(Collections.emptyList())
.onItem().transformToMulti(i -> Multi.createFrom().items(i.stream()))
Expand Down
Loading