Skip to content

Commit c7d456b

Browse files
committed
optimization
1 parent fe85ec3 commit c7d456b

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/main/java/io/mixeway/api/dashboard/service/DashboardService.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.mixeway.utils.Status;
2828
import lombok.RequiredArgsConstructor;
2929
import lombok.extern.log4j.Log4j2;
30+
import org.springframework.data.domain.PageRequest;
3031
import org.springframework.http.HttpStatus;
3132
import org.springframework.http.ResponseEntity;
3233
import org.springframework.security.core.parameters.P;
@@ -35,6 +36,8 @@
3536

3637
import java.security.Principal;
3738
import java.util.*;
39+
import java.util.concurrent.CompletableFuture;
40+
import java.util.concurrent.ExecutionException;
3841
import java.util.stream.Collectors;
3942

4043
@Service
@@ -146,19 +149,42 @@ private List<VulnResponse> setVulnsForVulnName(String search) {
146149
public ResponseEntity<DashboardTopStatistics> getRootStatistics(Principal principal) {
147150
DashboardTopStatistics dashboardTopStatistics = new DashboardTopStatistics();
148151

149-
List<ProjectVulnerability> projectVulnerabilities = vulnTemplate.projectVulnerabilityRepository.getLatestVulnerabilitiesForProjects(permissionFactory.getProjectForPrincipal(principal));
152+
// Fetch projects for the principal
153+
List<Project> projects = permissionFactory.getProjectForPrincipal(principal);
150154

151-
StatisticCard statisticCard = new StatisticCard(
152-
findProjectService.count(),
153-
getScanNumberService.getNumberOfScansRunning(),
154-
getScanNumberService.getNumberOfScansInQueue(),
155-
vulnTemplate.projectVulnerabilityRepository.countVulns()
156-
);
157-
dashboardTopStatistics.setStatisticCard(statisticCard);
158-
dashboardTopStatistics.setProjectVulnerabilityList(projectVulnerabilities.stream().limit(5).collect(Collectors.toList()));
159-
return new ResponseEntity<>(dashboardTopStatistics, HttpStatus.OK);
155+
// Fetch the top 5 latest vulnerabilities with pagination
156+
List<ProjectVulnerability> projectVulnerabilities = vulnTemplate.projectVulnerabilityRepository
157+
.getLatestVulnerabilitiesForProjects(projects, PageRequest.of(0, 5));
158+
159+
// Parallelize service calls
160+
CompletableFuture<Long> projectCountFuture = CompletableFuture.supplyAsync(() -> findProjectService.count());
161+
CompletableFuture<Long> scansRunningFuture = CompletableFuture.supplyAsync(() -> getScanNumberService.getNumberOfScansRunning());
162+
CompletableFuture<Long> scansInQueueFuture = CompletableFuture.supplyAsync(() -> getScanNumberService.getNumberOfScansInQueue());
163+
CompletableFuture<Long> vulnCountFuture = CompletableFuture.supplyAsync(() -> vulnTemplate.projectVulnerabilityRepository.countVulns());
164+
165+
try {
166+
// Wait for all futures to complete
167+
CompletableFuture.allOf(projectCountFuture, scansRunningFuture, scansInQueueFuture, vulnCountFuture).join();
168+
169+
StatisticCard statisticCard = new StatisticCard(
170+
projectCountFuture.get(),
171+
scansRunningFuture.get(),
172+
scansInQueueFuture.get(),
173+
vulnCountFuture.get()
174+
);
175+
176+
dashboardTopStatistics.setStatisticCard(statisticCard);
177+
dashboardTopStatistics.setProjectVulnerabilityList(projectVulnerabilities);
178+
179+
return new ResponseEntity<>(dashboardTopStatistics, HttpStatus.OK);
180+
} catch (InterruptedException | ExecutionException e) {
181+
// Handle exceptions appropriately
182+
Thread.currentThread().interrupt();
183+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
184+
}
160185
}
161186

187+
162188
/**
163189
* Merging two projects. Move all resources in source project to destination project and delete source.
164190
* 1. Move all codeprojects

src/main/java/io/mixeway/db/repository/ProjectVulnerabilityRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.mixeway.db.projection.CommonVulns;
55
import io.mixeway.db.projection.VulnBarChartProjection;
66
import io.mixeway.db.projection.VulnerableProjects;
7+
import org.springframework.data.domain.Pageable;
78
import org.springframework.data.jpa.repository.*;
89
import org.springframework.data.repository.query.Param;
910

@@ -216,5 +217,6 @@ int countRiskForCodeProject(@Param("codeProject_id")Long codeProject_id,@Param("
216217
long countVulns();
217218
// In ProjectVulnerabilityRepository
218219
List<ProjectVulnerability> findByProjectAndStatusIdNotAndGradeNot(Project project, Long statusId, int grade);
220+
List<ProjectVulnerability> getLatestVulnerabilitiesForProjects(List<Project> projects, Pageable pageable);
219221

220222
}

0 commit comments

Comments
 (0)