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

converting to single query for getting component meta information #395

Merged
merged 1 commit into from
Oct 25, 2023
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 @@ -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