|
27 | 27 | import io.mixeway.utils.Status;
|
28 | 28 | import lombok.RequiredArgsConstructor;
|
29 | 29 | import lombok.extern.log4j.Log4j2;
|
| 30 | +import org.springframework.data.domain.PageRequest; |
30 | 31 | import org.springframework.http.HttpStatus;
|
31 | 32 | import org.springframework.http.ResponseEntity;
|
32 | 33 | import org.springframework.security.core.parameters.P;
|
|
35 | 36 |
|
36 | 37 | import java.security.Principal;
|
37 | 38 | import java.util.*;
|
| 39 | +import java.util.concurrent.CompletableFuture; |
| 40 | +import java.util.concurrent.ExecutionException; |
38 | 41 | import java.util.stream.Collectors;
|
39 | 42 |
|
40 | 43 | @Service
|
@@ -146,19 +149,42 @@ private List<VulnResponse> setVulnsForVulnName(String search) {
|
146 | 149 | public ResponseEntity<DashboardTopStatistics> getRootStatistics(Principal principal) {
|
147 | 150 | DashboardTopStatistics dashboardTopStatistics = new DashboardTopStatistics();
|
148 | 151 |
|
149 |
| - List<ProjectVulnerability> projectVulnerabilities = vulnTemplate.projectVulnerabilityRepository.getLatestVulnerabilitiesForProjects(permissionFactory.getProjectForPrincipal(principal)); |
| 152 | + // Fetch projects for the principal |
| 153 | + List<Project> projects = permissionFactory.getProjectForPrincipal(principal); |
150 | 154 |
|
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 | + } |
160 | 185 | }
|
161 | 186 |
|
| 187 | + |
162 | 188 | /**
|
163 | 189 | * Merging two projects. Move all resources in source project to destination project and delete source.
|
164 | 190 | * 1. Move all codeprojects
|
|
0 commit comments