Skip to content

Commit

Permalink
log deleted project info
Browse files Browse the repository at this point in the history
  • Loading branch information
sahibamittal committed Feb 19, 2025
1 parent 596b0c5 commit dff8d8f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/main/java/org/dependencytrack/persistence/jdbi/ProjectDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.annotation.Nullable;
import org.jdbi.v3.core.mapper.reflect.ColumnName;
import org.jdbi.v3.json.Json;
import org.jdbi.v3.sqlobject.config.RegisterConstructorMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.Define;
import org.jdbi.v3.sqlobject.customizer.DefineNamedBindings;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

Expand Down Expand Up @@ -228,7 +230,7 @@ record ConciseProjectMetricsRow(
""")
int deleteProject(@Bind final UUID projectUuid);

@SqlUpdate("""
@SqlQuery("""
WITH "CTE" AS (
SELECT "ID"
FROM "PROJECT"
Expand All @@ -239,10 +241,16 @@ record ConciseProjectMetricsRow(
DELETE
FROM "PROJECT"
WHERE "ID" IN (SELECT "ID" FROM "CTE")
RETURNING "NAME", "VERSION", "INACTIVE_SINCE"
""")
int deleteInactiveProjectsForRetentionDuration(@Bind final Instant retentionCutOff, @Bind final int batchSize);
@GetGeneratedKeys({"NAME", "VERSION", "INACTIVE_SINCE"})
@RegisterConstructorMapper(DeletedProject.class)
List<DeletedProject> deleteInactiveProjectsForRetentionDuration(@Bind final Instant retentionCutOff, @Bind final int batchSize);

@SqlUpdate("""
record DeletedProject(@ColumnName("NAME") String name, @ColumnName("VERSION") String version, @ColumnName("INACTIVE_SINCE") Instant inactiveSince) {
}

@SqlQuery("""
DELETE
FROM "PROJECT"
WHERE "PROJECT"."INACTIVE_SINCE" IS NOT NULL
Expand All @@ -255,8 +263,11 @@ record ConciseProjectMetricsRow(
ORDER BY "PROJECT"."INACTIVE_SINCE" DESC
LIMIT :versionCountThreshold
)
RETURNING "NAME", "VERSION", "INACTIVE_SINCE"
""")
int retainLastXInactiveProjects(@Bind final String projectName, @Bind final int versionCountThreshold);
@GetGeneratedKeys({"NAME", "VERSION", "INACTIVE_SINCE"})
@RegisterConstructorMapper(DeletedProject.class)
List<DeletedProject> retainLastXInactiveProjects(@Bind final String projectName, @Bind final int versionCountThreshold);

@SqlQuery("""
SELECT "PROJECT"."NAME"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ private Statistics informLocked() {
Instant retentionCutOff = Instant.now().minus(retentionDuration);
Integer numDeletedLastBatch = null;
while (numDeletedLastBatch == null || numDeletedLastBatch > 0) {
numDeletedLastBatch = withJdbiHandle(
final var deletedProjectsBatch = withJdbiHandle(
batchHandle -> {
final var projectDao = batchHandle.attach(ProjectDao.class);
return projectDao.deleteInactiveProjectsForRetentionDuration(retentionCutOff, batchSize);
});
numDeletedLastBatch = deletedProjectsBatch.size();
numDeletedTotal.addAndGet(numDeletedLastBatch);
deletedProjectsBatch.forEach(deletedProject ->
LOGGER.info("Inactive project deleted: [name:%s, version:%s, inactive since:%s]".formatted(deletedProject.name(), deletedProject.version(), deletedProject.inactiveSince())));
}
} else {
final int versionCountThreshold = withJdbiHandle(handle ->
Expand All @@ -105,7 +108,10 @@ private Statistics informLocked() {
final var projectDao = batchHandle.attach(ProjectDao.class);
List<String> projectBatch = projectDao.getDistinctProjects(versionCountThreshold, batchSize);
for (var projectName : projectBatch) {
numDeletedTotal.addAndGet(projectDao.retainLastXInactiveProjects(projectName, versionCountThreshold));
final var deletedProjects = projectDao.retainLastXInactiveProjects(projectName, versionCountThreshold);
numDeletedTotal.addAndGet(deletedProjects.size());
deletedProjects.forEach(deletedProject ->
LOGGER.info("Inactive project deleted: [name:%s, version:%s, inactive since:%s]".formatted(deletedProject.name(), deletedProject.version(), deletedProject.inactiveSince())));
}
return projectBatch.size();
});
Expand Down

0 comments on commit dff8d8f

Please sign in to comment.