Skip to content

Commit

Permalink
converting to single query for getting component meta information
Browse files Browse the repository at this point in the history
Signed-off-by: mehab <[email protected]>
  • Loading branch information
mehab committed Oct 25, 2023
1 parent b362975 commit 4a1292d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public PaginatedResult getComponents(final Project project, final boolean includ
if (RepositoryType.UNSUPPORTED != type) {
final RepositoryMetaComponent repoMetaComponent = getRepositoryMetaComponent(type, purl.getNamespace(), purl.getName());
component.setRepositoryMeta(repoMetaComponent);
component.setComponentMetaInformation(getMetaInformation(purl, component.getUuid()));
component.setComponentMetaInformation(getMetaInformation(component.getUuid()));
}
}
}
Expand Down Expand Up @@ -331,7 +331,7 @@ public PaginatedResult getComponents(ComponentIdentity identity, Project project
if (RepositoryType.UNSUPPORTED != type) {
final RepositoryMetaComponent repoMetaComponent = getRepositoryMetaComponent(type, purl.getNamespace(), purl.getName());
component.setRepositoryMeta(repoMetaComponent);
component.setComponentMetaInformation(getMetaInformation(purl, component.getUuid()));
component.setComponentMetaInformation(getMetaInformation(component.getUuid()));
}
}
}
Expand Down
46 changes: 33 additions & 13 deletions src/main/java/org/dependencytrack/persistence/QueryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.dependencytrack.persistence;

import alpine.common.logging.Logger;
import alpine.common.util.BooleanUtil;
import alpine.model.ApiKey;
import alpine.model.ConfigProperty;
Expand All @@ -27,6 +28,7 @@
import alpine.persistence.AlpineQueryManager;
import alpine.persistence.PaginatedResult;
import alpine.resources.AlpineRequest;
import alpine.server.util.DbUtil;
import com.github.packageurl.PackageURL;
import com.google.common.collect.Lists;
import io.github.resilience4j.retry.Retry;
Expand Down Expand Up @@ -125,6 +127,8 @@
public class QueryManager extends AlpineQueryManager {

private AlpineRequest request;

private static final Logger LOGGER = Logger.getLogger(QueryManager.class);
private BomQueryManager bomQueryManager;
private ComponentQueryManager componentQueryManager;
private FindingsQueryManager findingsQueryManager;
Expand Down Expand Up @@ -1843,19 +1847,35 @@ public IntegrityAnalysis getIntegrityAnalysisByComponentUuid(UUID uuid) {
return getIntegrityAnalysisQueryManager().getIntegrityAnalysisByComponentUuid(uuid);
}

public ComponentMetaInformation getMetaInformation(PackageURL purl, UUID uuid) {
Date publishedAt = null;
Date lastFetched = null;
IntegrityMatchStatus integrityMatchStatus = null;
final IntegrityMetaComponent integrityMetaComponent = getIntegrityMetaComponent(purl.toString());
final IntegrityAnalysis integrityAnalysis = getIntegrityAnalysisByComponentUuid(uuid);
if (integrityMetaComponent != null) {
publishedAt = integrityMetaComponent.getPublishedAt();
lastFetched = integrityMetaComponent.getLastFetch();
}
if (integrityAnalysis != null) {
integrityMatchStatus = integrityAnalysis.getIntegrityCheckStatus();
public ComponentMetaInformation getMetaInformation(UUID uuid) {

Connection connection = null;
PreparedStatement preparedStatement = null;
String queryString = """
SELECT "C"."ID", "C"."PURL", "IMC"."LAST_FETCH", "IMC"."PUBLISHED_AT", "IA"."INTEGRITY_CHECK_STATUS" FROM "COMPONENT" "C"
JOIN "INTEGRITY_META_COMPONENT" "IMC" ON "C"."PURL" ="IMC"."PURL" JOIN "INTEGRITY_ANALYSIS" "IA" ON "IA"."ID" ="C"."ID" WHERE "C"."UUID" = ?
""";
try {
connection = (Connection) pm.getDataStoreConnection();

preparedStatement = connection.prepareStatement(queryString);
preparedStatement.setString(1, uuid.toString());
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
var publishedDate = Date.from(resultSet.getTimestamp("PUBLISHED_AT").toInstant());
Date lastFetch = Date.from(resultSet.getTimestamp("LAST_FETCH").toInstant());
return new ComponentMetaInformation(publishedDate,
IntegrityMatchStatus.valueOf(resultSet.getString("INTEGRITY_CHECK_STATUS")),
lastFetch);

}
} catch (Exception ex) {
LOGGER.error("error occurred while fetch component published date and integrity information", ex);
throw new RuntimeException(ex);
} finally {
DbUtil.close(preparedStatement);
DbUtil.close(connection);
}
return new ComponentMetaInformation(publishedAt, integrityMatchStatus, lastFetched);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public Response getComponentByUuid(
detachedComponent.setRepositoryMeta(repoMetaComponent);
}
if (includeIntegrityMetaData) {
detachedComponent.setComponentMetaInformation(qm.getMetaInformation(component.getPurl(), component.getUuid()));
detachedComponent.setComponentMetaInformation(qm.getMetaInformation(component.getUuid()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testGetMetaInformation() {
integrityMetaComponent.setStatus(FetchStatus.PROCESSED);
qm.createIntegrityMetaComponent(integrityMetaComponent);
component = qm.createComponent(component, false);
ComponentMetaInformation componentMetaInformation = qm.getMetaInformation(component.getPurl(), component.getUuid());
ComponentMetaInformation componentMetaInformation = qm.getMetaInformation(component.getUuid());
Assert.assertEquals(HASH_MATCH_PASSED, componentMetaInformation.integrityMatchStatus());
Assert.assertEquals(integrityMetaComponent.getPublishedAt(), componentMetaInformation.publishedDate());
Assert.assertEquals(integrityMetaComponent.getLastFetch(), componentMetaInformation.lastFetched());
Expand Down

0 comments on commit 4a1292d

Please sign in to comment.