From bde39eb0c5f008f2c4992dd96edaad994025ec94 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Tue, 26 Nov 2024 15:11:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=B7=A5=E4=BD=9C=E5=8F=B0):=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=A1=B9=E7=9B=AE=E6=A6=82=E8=A7=88=E5=92=8C?= =?UTF-8?q?=E6=88=91=E7=9A=84=E5=88=9B=E5=BB=BA=E6=95=B0=E6=8D=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/service/DashboardService.java | 174 ++++++++++-------- 1 file changed, 93 insertions(+), 81 deletions(-) diff --git a/backend/services/dashboard/src/main/java/io/metersphere/dashboard/service/DashboardService.java b/backend/services/dashboard/src/main/java/io/metersphere/dashboard/service/DashboardService.java index 33599de35f..c32aba5ef7 100644 --- a/backend/services/dashboard/src/main/java/io/metersphere/dashboard/service/DashboardService.java +++ b/backend/services/dashboard/src/main/java/io/metersphere/dashboard/service/DashboardService.java @@ -180,139 +180,151 @@ public class DashboardService { @NotNull private OverViewCountDTO getModuleCountMap(Map> permissionModuleProjectIdMap, List projects, Long toStartTime, Long toEndTime, String userId) { - Map map = new HashMap<>(); - List xaxis = new ArrayList<>(); - List nameArrayDTOList = new ArrayList<>(); + Map projectNameMap = projects.stream().collect(Collectors.toMap(Project::getId, Project::getName)); + + Map projectCaseCountMap; + Map projectReviewCountMap; + Map projectApiCountMap; + Map projectApiScenarioCountMap; + Map projectApiCaseCountMap; + Map projectPlanCountMap; + Map projectBugCountMap; //功能用例 - Map caseProjectCount = new HashMap<>(); Set caseProjectIds = permissionModuleProjectIdMap.get(PermissionConstants.FUNCTIONAL_CASE_READ); if (CollectionUtils.isNotEmpty(caseProjectIds)) { //有权限 List projectCaseCount = extFunctionalCaseMapper.projectCaseCount(caseProjectIds, toStartTime, toEndTime, userId); - int caseCount = projectCaseCount.stream().mapToInt(ProjectCountDTO::getCount).sum(); - map.put(FUNCTIONAL, caseCount); - xaxis.add(FUNCTIONAL); - caseProjectCount = projectCaseCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, t -> t)); + projectCaseCountMap = projectCaseCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, ProjectCountDTO::getCount)); + } else { + projectCaseCountMap = new HashMap<>(); } - //用例评审 - Map reviewProjectCount = new HashMap<>(); Set reviewProjectIds = permissionModuleProjectIdMap.get(PermissionConstants.CASE_REVIEW_READ); if (CollectionUtils.isNotEmpty(reviewProjectIds)) { List projectReviewCount = extCaseReviewMapper.projectReviewCount(reviewProjectIds, toStartTime, toEndTime, userId); - int reviewCount = projectReviewCount.stream().mapToInt(ProjectCountDTO::getCount).sum(); - map.put(CASE_REVIEW, reviewCount); - xaxis.add(CASE_REVIEW); - reviewProjectCount = projectReviewCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, t -> t)); + projectReviewCountMap = projectReviewCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, ProjectCountDTO::getCount)); + } else { + projectReviewCountMap = new HashMap<>(); } //接口 - Map apiProjectCount = new HashMap<>(); Set apiProjectIds = permissionModuleProjectIdMap.get(PermissionConstants.PROJECT_API_DEFINITION_READ); if (CollectionUtils.isNotEmpty(apiProjectIds)) { List projectApiCount = extApiDefinitionMapper.projectApiCount(apiProjectIds, toStartTime, toEndTime, userId); - int apiCount = projectApiCount.stream().mapToInt(ProjectCountDTO::getCount).sum(); - map.put(API, apiCount); - xaxis.add(API); - apiProjectCount = projectApiCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, t -> t)); - + projectApiCountMap = projectApiCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, ProjectCountDTO::getCount)); + } else { + projectApiCountMap = new HashMap<>(); } //接口用例 - Map apiCaseProjectCount = new HashMap<>(); Set apiCaseProjectIds = permissionModuleProjectIdMap.get(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ); if (CollectionUtils.isNotEmpty(apiCaseProjectIds)) { List projectApiCaseCount = extApiTestCaseMapper.projectApiCaseCount(apiCaseProjectIds, toStartTime, toEndTime, userId); - int apiCaseCount = projectApiCaseCount.stream().mapToInt(ProjectCountDTO::getCount).sum(); - map.put(API_CASE, apiCaseCount); - xaxis.add(API_CASE); - apiCaseProjectCount = projectApiCaseCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, t -> t)); - + projectApiCaseCountMap = projectApiCaseCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, ProjectCountDTO::getCount)); + } else { + projectApiCaseCountMap = new HashMap<>(); } //接口场景 - Map apiScenarioProjectCount = new HashMap<>(); Set scenarioProjectIds = permissionModuleProjectIdMap.get(PermissionConstants.PROJECT_API_SCENARIO_READ); if (CollectionUtils.isNotEmpty(scenarioProjectIds)) { List projectApiScenarioCount = extApiScenarioMapper.projectApiScenarioCount(scenarioProjectIds, toStartTime, toEndTime, userId); - int apiScenarioCount = projectApiScenarioCount.stream().mapToInt(ProjectCountDTO::getCount).sum(); - map.put(API_SCENARIO, apiScenarioCount); - xaxis.add(API_SCENARIO); - apiScenarioProjectCount = projectApiScenarioCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, t -> t)); + projectApiScenarioCountMap = projectApiScenarioCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, ProjectCountDTO::getCount)); + } else { + projectApiScenarioCountMap = new HashMap<>(); } //测试计划 - Map testPlanProjectCount = new HashMap<>(); Set planProjectIds = permissionModuleProjectIdMap.get(PermissionConstants.TEST_PLAN_READ); if (CollectionUtils.isNotEmpty(planProjectIds)) { List projectPlanCount = extTestPlanMapper.projectPlanCount(planProjectIds, toStartTime, toEndTime, userId); - int testPlanCount = projectPlanCount.stream().mapToInt(ProjectCountDTO::getCount).sum(); - map.put(TEST_PLAN, testPlanCount); - xaxis.add(TEST_PLAN); - testPlanProjectCount = projectPlanCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, t -> t)); - + projectPlanCountMap = projectPlanCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, ProjectCountDTO::getCount)); + } else { + projectPlanCountMap = new HashMap<>(); } //缺陷管理 - Map bugProjectCount = new HashMap<>(); Set bugProjectIds = permissionModuleProjectIdMap.get(PermissionConstants.PROJECT_BUG_READ); if (CollectionUtils.isNotEmpty(bugProjectIds)) { List projectBugCount = extBugMapper.projectBugCount(bugProjectIds, toStartTime, toEndTime, userId); - int bugCount = projectBugCount.stream().mapToInt(ProjectCountDTO::getCount).sum(); - map.put(BUG_COUNT, bugCount); - xaxis.add(BUG_COUNT); - bugProjectCount = projectBugCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, t -> t)); + projectBugCountMap = projectBugCount.stream().collect(Collectors.toMap(ProjectCountDTO::getProjectId, ProjectCountDTO::getCount)); + + }else { + projectBugCountMap = new HashMap<>(); } - for (Project project : projects) { - String projectId = project.getId(); - String projectName = project.getName(); - NameArrayDTO nameArrayDTO = new NameArrayDTO(); - nameArrayDTO.setId(projectId); - nameArrayDTO.setName(projectName); - List count = new ArrayList<>(); - ProjectCountDTO projectCountDTO = caseProjectCount.get(projectId); - if (projectCountDTO != null) { - count.add(projectCountDTO.getCount()); + List projectCaseCount = new ArrayList<>(); + List projectReviewCount = new ArrayList<>(); + List projectApiCount = new ArrayList<>(); + List projectApiCaseCount = new ArrayList<>(); + List projectApiScenarioCount = new ArrayList<>(); + List projectPlanCount = new ArrayList<>(); + List projectBugCount = new ArrayList<>(); + + projectNameMap.forEach((id, userName) -> { + if (projectCaseCountMap.get(id) != null) { + projectCaseCount.add(projectCaseCountMap.get(id)); } else { - count.add(0); + projectCaseCount.add(0); } - ProjectCountDTO reviewDTO = reviewProjectCount.get(projectId); - if (reviewDTO != null) { - count.add(reviewDTO.getCount()); + if (projectReviewCountMap.get(id) != null) { + projectReviewCount.add(projectReviewCountMap.get(id)); } else { - count.add(0); + projectReviewCount.add(0); } - ProjectCountDTO apiDTO = apiProjectCount.get(projectId); - if (apiDTO != null) { - count.add(apiDTO.getCount()); + if (projectApiCountMap.get(id) != null) { + projectApiCount.add(projectApiCountMap.get(id)); } else { - count.add(0); + projectApiCount.add(0); } - ProjectCountDTO apiCaseDTO = apiCaseProjectCount.get(projectId); - if (apiCaseDTO != null) { - count.add(apiCaseDTO.getCount()); + if (projectApiCaseCountMap.get(id) != null) { + projectApiCaseCount.add(projectApiCaseCountMap.get(id)); } else { - count.add(0); + projectApiCaseCount.add(0); } - ProjectCountDTO apiScenarioDTO = apiScenarioProjectCount.get(projectId); - if (apiScenarioDTO != null) { - count.add(apiScenarioDTO.getCount()); + if (projectApiScenarioCountMap.get(id) != null) { + projectApiScenarioCount.add(projectApiScenarioCountMap.get(id)); } else { - count.add(0); + projectApiScenarioCount.add(0); } - ProjectCountDTO testPlanDTO = testPlanProjectCount.get(projectId); - if (testPlanDTO != null) { - count.add(testPlanDTO.getCount()); + if (projectPlanCountMap.get(id) != null) { + projectPlanCount.add(projectPlanCountMap.get(id)); } else { - count.add(0); + projectPlanCount.add(0); } - ProjectCountDTO bugDTO = bugProjectCount.get(projectId); - if (bugDTO != null) { - count.add(bugDTO.getCount()); + if (projectBugCountMap.get(id) != null) { + projectBugCount.add(projectBugCountMap.get(id)); } else { - count.add(0); + projectBugCount.add(0); } - nameArrayDTO.setCount(count); - nameArrayDTOList.add(nameArrayDTO); - } + }); + List xaxis = new ArrayList<>(projectNameMap.values()); + + List nameArrayDTOList = new ArrayList<>(); + NameArrayDTO userCaseArray = new NameArrayDTO(); + userCaseArray.setCount(projectCaseCount); + nameArrayDTOList.add(userCaseArray); + + NameArrayDTO userReviewArray = new NameArrayDTO(); + userReviewArray.setCount(projectReviewCount); + nameArrayDTOList.add(userReviewArray); + + NameArrayDTO userApiArray = new NameArrayDTO(); + userApiArray.setCount(projectApiCount); + nameArrayDTOList.add(userApiArray); + + NameArrayDTO userApiCaseArray = new NameArrayDTO(); + userApiCaseArray.setCount(projectApiCaseCount); + nameArrayDTOList.add(userApiCaseArray); + + NameArrayDTO userApiScenarioArray = new NameArrayDTO(); + userApiScenarioArray.setCount(projectApiScenarioCount); + nameArrayDTOList.add(userApiScenarioArray); + + NameArrayDTO userPlanArray = new NameArrayDTO(); + userPlanArray.setCount(projectPlanCount); + nameArrayDTOList.add(userPlanArray); + + NameArrayDTO userBugArray = new NameArrayDTO(); + userBugArray.setCount(projectBugCount); + nameArrayDTOList.add(userBugArray); + OverViewCountDTO overViewCountDTO = new OverViewCountDTO(); - overViewCountDTO.setCaseCountMap(map); overViewCountDTO.setXAxis(xaxis); overViewCountDTO.setProjectCountList(nameArrayDTOList); if (CollectionUtils.isEmpty(xaxis)) {