Skip to content

Commit

Permalink
Update fetchGroup to include parent and collectionLogic
Browse files Browse the repository at this point in the history
Avoid repetitive queries for individual fields accessed during project metrics updates.

Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Dec 8, 2024
1 parent f55d48c commit 111900a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/dependencytrack/model/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,19 @@
}),
@FetchGroup(name = "METRICS_UPDATE", members = {
@Persistent(name = "id"),
@Persistent(name = "parent"),
@Persistent(name = "collectionLogic"),
@Persistent(name = "collectionTag"),
@Persistent(name = "lastInheritedRiskScore"),
@Persistent(name = "uuid")
}),
@FetchGroup(name = "PARENT", members = {
@Persistent(name = "parent")
}),
@FetchGroup(name = "PORTFOLIO_METRICS_UPDATE", members = {
@Persistent(name = "id"),
@Persistent(name = "lastInheritedRiskScore"),
@Persistent(name = "uuid")
})
})
@JsonInclude(JsonInclude.Include.NON_NULL)
Expand All @@ -122,7 +130,8 @@ public enum FetchGroup {
ALL,
METADATA,
METRICS_UPDATE,
PARENT
PARENT,
PORTFOLIO_METRICS_UPDATE
}

@PrimaryKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private List<Project> fetchNextActiveProjectsBatch(final PersistenceManager pm,

// NB: Set fetch group on PM level to avoid fields of the default fetch group from being loaded.
try (var ignoredPersistenceCustomization = new ScopedCustomization(pm)
.withFetchGroup(Project.FetchGroup.METRICS_UPDATE.name())) {
.withFetchGroup(Project.FetchGroup.PORTFOLIO_METRICS_UPDATE.name())) {
return List.copyOf(query.executeList());
} finally {
query.closeAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public void inform(final Event e) {
}
}

private void updateMetrics(final UUID uuid) throws Exception {
private void updateMetrics(final UUID uuid) {
final var counters = new Counters();

try (final QueryManager qm = new QueryManager()) {
final PersistenceManager pm = qm.getPersistenceManager();

final Project project = qm.getObjectByUuid(Project.class, uuid, List.of(Project.FetchGroup.METRICS_UPDATE.name()));
final Project project = fetchProject(pm, uuid);
if (project == null) {
throw new NoSuchElementException("Project " + uuid + " does not exist");
}
Expand Down Expand Up @@ -263,6 +263,20 @@ private void updateLatestVersionChildrenCollectionMetrics(final Project project,
}
}

private Project fetchProject(final PersistenceManager pm, final UUID uuid) {
final Query<Project> query = pm.newQuery(Project.class);
query.setFilter("uuid == :uuid");
query.setParameters(uuid);

// NB: Set fetch group on PM level to avoid fields of the default fetch group from being loaded.
try (var ignoredPersistenceCustomization = new ScopedCustomization(pm)
.withFetchGroup(Project.FetchGroup.METRICS_UPDATE.name())) {
return query.executeUnique();
} finally {
query.closeAll();
}
}

private List<Component> fetchNextComponentsPage(final PersistenceManager pm, final Project project, final Long lastId) {
final Query<Component> query = pm.newQuery(Component.class);
if (lastId == null) {
Expand Down

0 comments on commit 111900a

Please sign in to comment.