Skip to content

Commit

Permalink
fixes in asset api
Browse files Browse the repository at this point in the history
  • Loading branch information
siewer committed Jun 7, 2024
1 parent 7d386ec commit fc658e1
Showing 1 changed file with 43 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,57 @@ public class GetAssetDashboardService {
private final VulnTemplate vulnTemplate;

public AssetDashboardModel buildDashboardModelForCodeProject(CodeProject codeProject) {
if (codeProject == null) {
throw new IllegalArgumentException("codeProject cannot be null");
}
if (vulnTemplate == null || vulnTemplate.projectVulnerabilityRepository == null) {
throw new IllegalArgumentException("vulnTemplate or vulnTemplate.projectVulnerabilityRepository cannot be null");
}

List<ProjectVulnerability> allProjectVulnerabilities = vulnTemplate.projectVulnerabilityRepository
.findByCodeProject(codeProject)
.stream()
.filter(pv -> pv.getCodeProjectBranch().getName().equals(codeProject.getBranch()))
.filter(pv -> pv != null && pv.getCodeProjectBranch() != null && pv.getCodeProjectBranch().getName().equals(codeProject.getBranch()))
.collect(Collectors.toList());

List<ProjectVulnerability> projectVulnerabilities = allProjectVulnerabilities.stream()
.filter(pv -> !pv.getStatus().getName().equals(vulnTemplate.STATUS_REMOVED.getName()))
.filter(pv -> pv != null && pv.getStatus() != null && !pv.getStatus().getName().equals(vulnTemplate.STATUS_REMOVED.getName()))
.filter(pv -> pv.getGrade() != 0)
.collect(Collectors.toList());

List<ProjectVulnerability> solvedVulnerabilities = allProjectVulnerabilities.stream()
.filter(pv -> pv.getStatus().getName().equals(vulnTemplate.STATUS_REMOVED.getName()))
.filter(pv -> pv != null && pv.getStatus() != null && pv.getStatus().getName().equals(vulnTemplate.STATUS_REMOVED.getName()))
.collect(Collectors.toList());

List<ProjectVulnerability> reviewedVulnerabilities = allProjectVulnerabilities.stream()
.filter(pv -> !pv.getStatus().getName().equals(vulnTemplate.STATUS_REMOVED.getName()))
.filter(pv -> pv != null && pv.getStatus() != null && !pv.getStatus().getName().equals(vulnTemplate.STATUS_REMOVED.getName()))
.filter(pv -> pv.getGrade() == 0 || pv.getGrade() == 1)
.collect(Collectors.toList());

List<ProjectVulnerability> notReviewedVulnerabilities = allProjectVulnerabilities.stream()
.filter(pv -> !pv.getStatus().getName().equals(vulnTemplate.STATUS_REMOVED.getName()))
.filter(pv -> pv != null && pv.getStatus() != null && !pv.getStatus().getName().equals(vulnTemplate.STATUS_REMOVED.getName()))
.filter(pv -> pv.getGrade() == -1)
.collect(Collectors.toList());

int allCrit = (int) projectVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_CRITICAL)).count();
int allHigh = (int) projectVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_HIGH)).count();
int allMedium = (int) projectVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_MEDIUM)).count();
int allLow = (int) projectVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_LOW)).count();

int allCrit = (int)projectVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_CRITICAL)).count();
int allHigh = (int)projectVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_HIGH)).count();
int allMedium = (int)projectVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_MEDIUM)).count();
int allLow = (int)projectVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_LOW)).count();

int solvedCrit = (int)solvedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_CRITICAL)).count();
int solvedHigh = (int)solvedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_HIGH)).count();
int solvedMedium = (int)solvedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_MEDIUM)).count();
int solvedLow = (int)solvedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_LOW)).count();
int solvedCrit = (int) solvedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_CRITICAL)).count();
int solvedHigh = (int) solvedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_HIGH)).count();
int solvedMedium = (int) solvedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_MEDIUM)).count();
int solvedLow = (int) solvedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_LOW)).count();

int reviewedCrit = (int)reviewedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_CRITICAL)).count();
int reviewedHigh = (int)reviewedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_HIGH)).count();
int reviewedMedium = (int)reviewedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_MEDIUM)).count();
int reviewedLow = (int)reviewedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_LOW)).count();
int reviewedCrit = (int) reviewedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_CRITICAL)).count();
int reviewedHigh = (int) reviewedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_HIGH)).count();
int reviewedMedium = (int) reviewedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_MEDIUM)).count();
int reviewedLow = (int) reviewedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_LOW)).count();

int notReviewedCrit = (int)notReviewedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_CRITICAL)).count();
int notReviewedHigh = (int)notReviewedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_HIGH)).count();
int notReviewedMedium = (int)notReviewedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_MEDIUM)).count();
int notReviewedLow = (int)notReviewedVulnerabilities.stream().filter(pv -> pv.getSeverity().equals(Constants.VULN_CRITICALITY_LOW)).count();
int notReviewedCrit = (int) notReviewedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_CRITICAL)).count();
int notReviewedHigh = (int) notReviewedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_HIGH)).count();
int notReviewedMedium = (int) notReviewedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_MEDIUM)).count();
int notReviewedLow = (int) notReviewedVulnerabilities.stream().filter(pv -> pv != null && pv.getSeverity() != null && pv.getSeverity().equals(Constants.VULN_CRITICALITY_LOW)).count();

// Calculate average days for each severity level
long avgCrit = calculateAverageDays(allProjectVulnerabilities, "Critical");
Expand All @@ -79,9 +88,6 @@ public AssetDashboardModel buildDashboardModelForCodeProject(CodeProject codePro
int avgMediumPercent = calculatePercentage(avgMedium);
int avgLowPercent = calculatePercentage(avgLow);




AssetDashboardStatModel vulnStats = AssetDashboardStatModel.builder()
.crit(allCrit)
.high(allHigh)
Expand All @@ -94,30 +100,31 @@ public AssetDashboardModel buildDashboardModelForCodeProject(CodeProject codePro
.high(solvedHigh)
.medium(solvedMedium)
.low(solvedLow)
.critPercent((solvedCrit + allCrit) == 0 ? 0 : (int) Math.ceil((solvedCrit / (double)(solvedCrit + allCrit) * 100)) )
.highPercent((solvedHigh + allHigh) == 0 ? 0 : (int) Math.ceil((solvedHigh / (double)(solvedHigh + allHigh) * 100)) )
.mediumPercent((solvedMedium + allMedium) == 0 ? 0 : (int) Math.ceil((solvedMedium / (double)(solvedMedium + allMedium)) * 100) )
.lowPercent((solvedLow + allLow) == 0 ? 0 : (int) Math.ceil((solvedLow / (double)(solvedLow + allLow)) * 100))
.critPercent((solvedCrit + allCrit) == 0 ? 0 : (int) Math.ceil((solvedCrit / (double) (solvedCrit + allCrit) * 100)))
.highPercent((solvedHigh + allHigh) == 0 ? 0 : (int) Math.ceil((solvedHigh / (double) (solvedHigh + allHigh) * 100)))
.mediumPercent((solvedMedium + allMedium) == 0 ? 0 : (int) Math.ceil((solvedMedium / (double) (solvedMedium + allMedium)) * 100))
.lowPercent((solvedLow + allLow) == 0 ? 0 : (int) Math.ceil((solvedLow / (double) (solvedLow + allLow)) * 100))
.total(solvedVulnerabilities.size())
.build();

AssetDashboardStatModel reviewedIssues = AssetDashboardStatModel.builder()
.crit(reviewedCrit)
.high(reviewedHigh)
.medium(reviewedMedium)
.low(reviewedLow)
.critPercent((reviewedCrit + notReviewedCrit) == 0 ? 0 : (int) Math.ceil((reviewedCrit / (double)(reviewedCrit + notReviewedCrit) * 100)) )
.highPercent((reviewedHigh + notReviewedHigh) == 0 ? 0 : (int) Math.ceil((reviewedHigh / (double)(reviewedHigh + notReviewedHigh) * 100)) )
.mediumPercent((reviewedMedium + notReviewedMedium) == 0 ? 0 : (int) Math.ceil((reviewedMedium / (double)(reviewedMedium + notReviewedMedium)) * 100) )
.lowPercent((reviewedLow + notReviewedLow) == 0 ? 0 : (int) Math.ceil((reviewedLow / (double)(reviewedLow + notReviewedLow)) * 100))
.critPercent((reviewedCrit + notReviewedCrit) == 0 ? 0 : (int) Math.ceil((reviewedCrit / (double) (reviewedCrit + notReviewedCrit) * 100)))
.highPercent((reviewedHigh + notReviewedHigh) == 0 ? 0 : (int) Math.ceil((reviewedHigh / (double) (reviewedHigh + notReviewedHigh) * 100)))
.mediumPercent((reviewedMedium + notReviewedMedium) == 0 ? 0 : (int) Math.ceil((reviewedMedium / (double) (reviewedMedium + notReviewedMedium)) * 100))
.lowPercent((reviewedLow + notReviewedLow) == 0 ? 0 : (int) Math.ceil((reviewedLow / (double) (reviewedLow + notReviewedLow)) * 100))
.total(reviewedVulnerabilities.size())
.build();

AssetDashboardStatModel ttmIssues = AssetDashboardStatModel.builder()
.crit((int)avgCrit)
.crit((int) avgCrit)
.high((int) avgHigh)
.medium( (int) avgMedium)
.medium((int) avgMedium)
.low((int) avgLow)
.total((int)avgAll)
.total((int) avgAll)
.critPercent(avgCritPercent)
.lowPercent(avgLowPercent)
.highPercent(avgHighPercent)
Expand Down

0 comments on commit fc658e1

Please sign in to comment.