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

feat(rest): Count of attachments used in different projects. #2895

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -128,6 +128,8 @@
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.eclipse.sw360.datahandler.thrift.users.RequestedAction;
import org.eclipse.sw360.datahandler.thrift.attachments.LicenseInfoUsage;
import org.eclipse.sw360.datahandler.thrift.attachments.SourcePackageUsage;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -136,20 +138,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InvalidPropertiesFormatException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -1895,6 +1885,24 @@ public ResponseEntity<?> saveAttachmentUsages(
}
}

public Map<String, Integer> countMap(Collection<AttachmentType> attachmentTypes, UsageData filter, Project project, User sw360User, String id) throws TException {
boolean projectWithSubProjects = !project.getLinkedProjects().isEmpty();
List<ProjectLink> mappedProjectLinks =
(!SW360Constants.ENABLE_FLEXIBLE_PROJECT_RELEASE_RELATIONSHIP)
? projectService.createLinkedProjects(project,
projectService.filterAndSortAttachments(attachmentTypes), true, sw360User)
: projectService.createLinkedProjectsWithAllReleases(project,
projectService.filterAndSortAttachments(attachmentTypes), true, sw360User);

if (!projectWithSubProjects) {
mappedProjectLinks = mappedProjectLinks.stream()
.filter(projectLink -> projectLink.getId().equals(id)).collect(Collectors.toList());
}

Map<String, Integer> countMap = projectService.storeAttachmentUsageCount(mappedProjectLinks, filter);
return countMap;
}

@Operation(
description = "Get all attachmentUsages of the projects.",
tags = {"Projects"}
Expand Down Expand Up @@ -1962,12 +1970,27 @@ public ResponseEntity attachmentUsages(
}
}

List<Map<String, Object>> releaseObjMap = getReleaseObjectMapper(releaseList);
Collection<AttachmentType> attachmentTypes;
UsageData type;
List<Map<String, Object>> releaseObjMap = new ArrayList<>();
if ("withCliAttachment".equalsIgnoreCase(filter)) {
attachmentTypes = SW360Constants.LICENSE_INFO_ATTACHMENT_TYPES;
type = UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet()));
Map<String, Integer> count = countMap(attachmentTypes, type, sw360Project, sw360User, id);
releaseObjMap = getReleaseObjectMapper(releaseList, count);
} else if ("withSourceAttachment".equalsIgnoreCase(filter)) {
attachmentTypes = SW360Constants.SOURCE_CODE_ATTACHMENT_TYPES;
type = UsageData.sourcePackage(new SourcePackageUsage());
Map<String, Integer> count = countMap(attachmentTypes, type, sw360Project, sw360User, id);
releaseObjMap = getReleaseObjectMapper(releaseList, count);
} else {
releaseObjMap = getReleaseObjectMapper(releaseList, null);
}
HalResource userHalResource = attachmentUsageReleases(sw360Project, releaseObjMap, listOfAttachmentUsages);
return new ResponseEntity<>(userHalResource, HttpStatus.OK);
}

private List<Map<String, Object>> getReleaseObjectMapper(List<EntityModel<Release>> releaseList) {
private List<Map<String, Object>> getReleaseObjectMapper(List<EntityModel<Release>> releaseList, Map<String, Integer> count) {
ObjectMapper oMapper = new ObjectMapper();
List<Map<String, Object>> modifiedList = new ArrayList<>();
for (EntityModel<Release> rel : releaseList) {
Expand All @@ -1983,6 +2006,7 @@ private List<Map<String, Object>> getReleaseObjectMapper(List<EntityModel<Releas
final ImmutableSet<String> fieldsToKeep = ImmutableSet.of("name", "version", "componentType", "clearingState", ATTACHMENTS);
Map<String, Object> valueToKeep = new LinkedHashMap<>();
Link releaseLink = null;
Integer attachmentCount = 0;
if (relMap != null) {
for (Map.Entry<String, Object> entry : relMap.entrySet()) {
if (entry != null && entry.getKey().equals("id")) {
Expand All @@ -2006,6 +2030,16 @@ private List<Map<String, Object>> getReleaseObjectMapper(List<EntityModel<Releas
map.put("checkedTeam", att.get("checkedTeam"));
map.put("checkedComment", att.get("checkedComment"));
map.put("checkedOn", att.get("checkedOn"));
if (count != null) {
for (Map.Entry<String, Integer> entryMap : count.entrySet()) {
String key = entryMap.getKey();
if (key.contains((CharSequence) att.get("attachmentContentId"))) {
attachmentCount = entryMap.getValue();
break;
}
}
map.put("attachmentUsageCount",attachmentCount);
}
attList.add(map);
}
valueToKeep.put(entry.getKey(), attList);
Expand Down Expand Up @@ -2078,7 +2112,7 @@ public List<Release> filterReleases(User sw360User, String filter, Set<String> r
final Release sw360Release = releaseService.getReleaseForUserById(relId, sw360User);
releaseService.setComponentDependentFieldsInRelease(sw360Release, sw360User);
List<Attachment> cliAttachments = sw360Release.getAttachments().stream()
.filter(attachment -> attachment.getAttachmentType() == AttachmentType.COMPONENT_LICENSE_INFO_XML)
.filter(attachment -> attachment.getAttachmentType() == AttachmentType.COMPONENT_LICENSE_INFO_XML || attachment.getAttachmentType() == AttachmentType.COMPONENT_LICENSE_INFO_COMBINED)
.collect(Collectors.toList());
Set<Attachment> cliAttachmentsSet = new HashSet<>(cliAttachments);
sw360Release.setAttachments(cliAttachmentsSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,48 @@ public Function<ProjectLink, ProjectLink> filterAndSortAttachments(Collection<At
.collect(Collectors.toList())));
}

public Map<String, Integer> storeAttachmentUsageCount(List<ProjectLink> mappedProjectLinks, UsageData filter) throws TException {
try {
ThriftClients thriftClients = new ThriftClients();
AttachmentService.Iface attachmentClient = thriftClients.makeAttachmentClient();
Map<Source, Set<String>> containedAttachments = extractContainedAttachments(mappedProjectLinks);
Map<Map<Source, String>, Integer> attachmentUsages = attachmentClient.getAttachmentUsageCount(containedAttachments,
filter);
Map<String, Integer> countMap = attachmentUsages.entrySet().stream().collect(Collectors.toMap(entry -> {
Entry<Source, String> key = entry.getKey().entrySet().iterator().next();
return key.getKey().getFieldValue() + "_" + key.getValue();
}, Entry::getValue));
return countMap;
} catch (TException e) {
log.error(e.getMessage());
return Collections.emptyMap();
}
}

/**
* Walks through a list of project links and extracts all release attachments
* with their owner. The returned map is a mapping from a release to its
* attachment content ids.
*
* @param projectLinks
* list of project links to walk through
*
* @return map of releases and their attachment content ids
*/
public static Map<Source, Set<String>> extractContainedAttachments(Collection<ProjectLink> projectLinks) {
Map<Source, Set<String>> attachments = Maps.newHashMap();

for (ProjectLink projectLink : projectLinks) {
for (ReleaseLink releaseLink : projectLink.linkedReleases) {
Set<String> attachmentIds = attachments.getOrDefault(Source.releaseId(releaseLink.getId()), Sets.newHashSet());
attachmentIds.addAll(releaseLink.getAttachments().stream().map(a -> a.getAttachmentContentId()).collect(Collectors.toList()));
attachments.put(Source.releaseId(releaseLink.getId()), attachmentIds);
}
}

return attachments;
}

public Function<ProjectLink, ProjectLink> filterAndSortAllAttachments(Collection<AttachmentType> attachmentTypes) {
Predicate<Attachment> filter = att -> attachmentTypes.contains(att.getAttachmentType());
return createProjectLinkMapper(rl -> {
Expand Down Expand Up @@ -1000,6 +1042,12 @@ public List<ProjectLink> createLinkedProjects(Project project, Function<ProjectL
return linkedProjects.stream().map(projectLinkMapper).collect(Collectors.toList());
}

protected List<ProjectLink> createLinkedProjectsWithAllReleases(Project project,
Function<ProjectLink, ProjectLink> projectLinkMapper, boolean deep, User user) {
final Collection<ProjectLink> linkedProjects = SW360Utils.getLinkedProjectsWithAllReleasesAsFlatList(project, deep, new ThriftClients(), log, user);
return linkedProjects.stream().map(projectLinkMapper).collect(Collectors.toList());
}

public Set<Release> getReleasesFromProjectIds(List<String> projectIds, boolean transitive, final User sw360User,
Sw360ReleaseService releaseService) {
final List<Callable<List<Release>>> callableTasksToGetReleases = new ArrayList<Callable<List<Release>>>();
Expand Down