diff --git "a/CI\346\265\213\350\257\225" "b/CI\346\265\213\350\257\225" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/IncResultDetailsOperation.java b/IncResultDetailsOperation.java new file mode 100644 index 0000000000000000000000000000000000000000..1353a7ba8e3dc108b0ebe6183e9a1e16ef7f12bd --- /dev/null +++ b/IncResultDetailsOperation.java @@ -0,0 +1,154 @@ +package com.huawei.operation; + +import com.huawei.entity.vo.codecheck.codecheckdetails.CodeCheckResultDetailsVo; +import com.huawei.entity.vo.ciinfo.codecheck.DefectVo; +import com.huawei.entity.vo.ciinfo.event.QueryIncDetailModel; +import com.huawei.enums.CodeCheckCollectionName; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.mongodb.core.FindAndReplaceOptions; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class IncResultDetailsOperation { + + @Autowired + @Qualifier("codeCheckMongoTemplate") + private MongoTemplate mongoTemplate; + + private static final String PROGRAM_SPECIFICATION = "coding standards"; + + + /** + * saveByUpsert + * + * @param defectVo defectVo + */ + public void saveByUpsert(DefectVo defectVo, int dayOfMonth) { + Criteria criteria = Criteria.where("uuid").is(defectVo.getUuid()) + .and("taskId").is(defectVo.getTaskId()) + .and("defectId").is(defectVo.getDefectId()); + Query query = Query.query(criteria); + FindAndReplaceOptions upsert = new FindAndReplaceOptions().upsert(); + // 确定当前代码检查的数据要拆分在哪个表 + StringBuffer sbTable = doJudgementForCurrentData(dayOfMonth); + mongoTemplate.findAndReplace(query, defectVo, upsert, String.valueOf(sbTable)); + } + + public void saveByUpsertInto(DefectVo defectVo) { + Criteria criteria = Criteria.where("uuid").is(defectVo.getUuid()) + .and("taskId").is(defectVo.getTaskId()) + .and("defectId").is(defectVo.getDefectId()); + Query query = Query.query(criteria); + FindAndReplaceOptions upsert = new FindAndReplaceOptions().upsert(); + mongoTemplate.findAndReplace(query, defectVo, upsert, CodeCheckCollectionName.TASK_INC_RESULT_DETAILS); + } + + /** + * 确定当前表需要拆分到哪个库中 + * + * @param dayOfMonth 今天是这个月的哪天 + * @return + */ + private StringBuffer doJudgementForCurrentData(int dayOfMonth) { + // 首先确定拆分10个表,从0-9 从第9天开始 删除第一天的数据 + int tableName = dayOfMonth % 10; + StringBuffer sb = new StringBuffer(); + sb.append(CodeCheckCollectionName.T_TASK_INC_RESULT_DETAILS).append(tableName); + return sb; + } + + + + /** + * 删除 + * + * @param taskId 任务id + * @param uuid uuid + */ + public void remove(String uuid, String taskId) { + Criteria criteria = Criteria.where("task_id").is(taskId).and("uuid").is(uuid); + // 有可能多个表都有当前uuid下的taskid 任务的数据 + for (int i = 0; i < 10; i++) { + String currentTable = CodeCheckCollectionName.T_TASK_INC_RESULT_DETAILS; + String tableFullName = currentTable + i; + mongoTemplate.remove(Query.query(criteria), tableFullName); + } + } + + + /** + * removeByUuids + * + * @param uuids uuids + * @return long + */ + public long removeByUuids(List uuids) { + Query query = Query.query(Criteria.where("uuid").in(uuids)); + return mongoTemplate.remove(query, CodeCheckCollectionName.T_TASK_INC_RESULT_DETAILS).getDeletedCount(); + } + + /** + * getResultListByUuid + * + * @param uuid uuid + * @param taskId taskId + * @param queryIncDetailModel queryIncDetailModel + * @return CodeCheckResultDetailsVo + */ + public CodeCheckResultDetailsVo getResultListByUuid(String uuid, String taskId, QueryIncDetailModel queryIncDetailModel) { + Criteria criteria = Criteria.where("uuid").is(uuid); + criteria.and("taskId").is(taskId); + if (StringUtils.isNotBlank(queryIncDetailModel.getDefectLevel())) { + criteria.and("defectLevel").is(queryIncDetailModel.getDefectLevel()); + } + if (StringUtils.isNotBlank(queryIncDetailModel.getDefectStatus())) { + criteria.and("defectStatus").is(queryIncDetailModel.getDefectStatus()); + } + if (StringUtils.isNotBlank(queryIncDetailModel.getFilePath())) { + criteria.and("filepath").is(queryIncDetailModel.getFilePath()); + } + if (StringUtils.isNotBlank(queryIncDetailModel.getFileName())) { + criteria.and("fileName").is(queryIncDetailModel.getFileName()); + } + if (StringUtils.isNotBlank(queryIncDetailModel.getRuleSystemTags())) { + //因为codecheck华为编程规范返回的时空串 + if (PROGRAM_SPECIFICATION.equals(queryIncDetailModel.getRuleSystemTags())) { + criteria.and("ruleSystemTags").is(""); + } else { + criteria.and("ruleSystemTags").is(queryIncDetailModel.getRuleSystemTags()); + } + } + if (StringUtils.isNotBlank(queryIncDetailModel.getDefectContent())) { + criteria.and("defectContent").is(queryIncDetailModel.getDefectContent()); + } + if (StringUtils.isNotBlank(queryIncDetailModel.getRuleName())) { + criteria.and("ruleName").is(queryIncDetailModel.getRuleName()); + } + Query query = Query.query(criteria); + query.skip((long) (queryIncDetailModel.getPageNum() - 1) * queryIncDetailModel.getPageSize()); + query.limit(queryIncDetailModel.getPageSize()); + // 总数量 + long count = 0; + for (int i = 0; i < 10; i++) { + count += mongoTemplate.count(Query.query(criteria), + CodeCheckCollectionName.T_TASK_INC_RESULT_DETAILS + i); + } + + List defectVos = new ArrayList<>(); + if (count > 0) { + for (int i = 0; i < 10; i++) { + defectVos.addAll(mongoTemplate.find(query, DefectVo.class, + CodeCheckCollectionName.T_TASK_INC_RESULT_DETAILS + i)); + } + } + return new CodeCheckResultDetailsVo(Long.valueOf(count).intValue(), defectVos); + } +} diff --git a/IncResultSummaryOperation.java b/IncResultSummaryOperation.java new file mode 100644 index 0000000000000000000000000000000000000000..d3f858672e2ba261ff37fd00c777cdaf6cf6e124 --- /dev/null +++ b/IncResultSummaryOperation.java @@ -0,0 +1,177 @@ +package com.huawei.operation; + +import com.huawei.ci.common.entity.CodeCheckResultSummary; +import com.huawei.entity.pojo.CodeCheckResultSummaryVo; +import com.huawei.enums.CodeCheckCollectionName; +import com.huawei.enums.CodeCheckStatus; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.mongodb.core.FindAndReplaceOptions; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * 单个增量任务检查详情操作类 + * + * @since 2022/11/8 + */ +@Component +public class IncResultSummaryOperation { + @Autowired + @Qualifier("codeCheckMongoTemplate") + private MongoTemplate mongoTemplate; + + /** + * updateCodeCheckStatus + * + * @param uuid uuid + * @param taskId taskId + * @param status status + */ + public void updateCodeCheckStatus(String uuid, String taskId, CodeCheckStatus status) { + Criteria criteria = Criteria.where("uuid").is(uuid).and("taskId").is(taskId); + Update codeCheckStatus = Update.update("codeCheckStatus", status.value()); + mongoTemplate.updateFirst(Query.query(criteria), codeCheckStatus, CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + } + + /** + * insertSummary + * + * @param summary summary + */ + public void insertSummary(CodeCheckResultSummary summary) { + mongoTemplate.save(summary, CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + } + + /** + * 增量模式代码检查概要 + * + * @param summary 对象信息 + */ + public void save(CodeCheckResultSummary summary) { + Criteria criteria = Criteria.where("uuid").is(summary.getUuid()).and("taskId").is(summary.getTaskId()); + Query query = Query.query(criteria); + FindAndReplaceOptions upsert = new FindAndReplaceOptions().upsert(); + mongoTemplate.findAndReplace(query, summary, upsert, CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + } + + /** + * 查询 + * + * @param taskId taskId + * @return {@link CodeCheckResultSummary} + */ + public CodeCheckResultSummary getByTaskId(String taskId) { + return mongoTemplate.findOne(Query.query(Criteria.where("taskId").is(taskId)), CodeCheckResultSummary.class, + CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + } + + /** + * getByUuid + * + * @param uuid uuid + * @return List + */ + public List getByUuid(String uuid) { + return mongoTemplate.find(Query.query(Criteria.where("uuid").is(uuid)), CodeCheckResultSummary.class, + CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + } + + /** + * getByUuidAndTaskId + * + * @param uuid uuid + * @param taskId taskId + * @return CodeCheckResultSummaryVo + */ + public CodeCheckResultSummaryVo getByUuidAndTaskId(String uuid, String taskId) { + Query query = Query.query(Criteria.where("uuid").is(uuid).and("taskId").is(taskId)); + return mongoTemplate.findOne(query, CodeCheckResultSummaryVo.class, + CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + } + + /** + * removeByUuids + * + * @param uuids uuids + * @return long + */ + public long removeByUuids(List uuids) { + Query query = Query.query(Criteria.where("uuid").in(uuids)); + return mongoTemplate.remove(query, CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY).getDeletedCount(); + } + + /** + * removeSummaryByDate + * + * @param date date + * @return long + */ + public long removeSummaryByDate(String date) { + Query query = Query.query(Criteria.where("date").lt(date)); + return mongoTemplate.remove(query, CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY).getDeletedCount(); + } + + /** + * updateIgnoreCount + * + * @param taskId taskId + * @param uuid uuid + * @param size size + */ + public void updateIgnoreCount(String taskId, String uuid, int size) { + Criteria criteria = Criteria.where("taskId").is(taskId).and("uuid").is(uuid); + Update ignoreCount = Update.update("ignoreCount", size); + mongoTemplate.updateFirst(Query.query(criteria), ignoreCount, CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + } + + /** + * updateIncResultSummary + * + * @param codeCheckResultSummary codeCheckResultSummary + */ + public void updateIncResultSummary(CodeCheckResultSummary codeCheckResultSummary) { + Query query = Query.query(Criteria.where("taskId").is(codeCheckResultSummary.getTaskId()).and("uuid").is(codeCheckResultSummary)); + mongoTemplate.findAndReplace(query, codeCheckResultSummary); + } + + /** + * getByUrlAndUUid + * + * @param prUrl prUrl + * @param uuid uuid + * @return CodeCheckResultSummaryVo + */ + public CodeCheckResultSummaryVo getByUrlAndUUid(String prUrl, String uuid) { + Criteria criteria = Criteria.where("uuid").is(uuid).and("mrUrl").is(prUrl); + Query query = Query.query(criteria); + CodeCheckResultSummaryVo currentOne = mongoTemplate.findOne(query, CodeCheckResultSummaryVo.class, CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + return currentOne; + } + + /** + * getAllSummaryVo + * + * @return List + */ + public List getAllSummaryVo() { + List all = mongoTemplate.findAll(CodeCheckResultSummaryVo.class, CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + return all; + } + + /** + * getSummaryByUuid + * + * @param uuid uuid + * @return List + */ + public List getSummaryByUuid(String uuid) { + return mongoTemplate.find(Query.query(Criteria.where("uuid").is(uuid)), CodeCheckResultSummaryVo.class, + CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY); + } +} diff --git a/PipelineOperation.java b/PipelineOperation.java new file mode 100644 index 0000000000000000000000000000000000000000..3327d6382e8877a470a6f79ef34b510c662b1777 --- /dev/null +++ b/PipelineOperation.java @@ -0,0 +1,100 @@ +package com.huawei.operation; + + + +import com.huawei.entity.vo.ciinfo.event.PipelineIdentity; +import com.huawei.entity.vo.ciinfo.event.PipelineQuery; +import com.huawei.entity.vo.ciinfo.event.PipelineVo; +import com.huawei.entity.vo.ciinfo.event.QueryStageModel; +import com.huawei.enums.CiCollectionName; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Component; + +import java.util.*; + +@Component +public class PipelineOperation { + @Autowired + @Qualifier("ciMongoTemplate") + private MongoTemplate mongoTemplate; + + /** + * 查询流水线列表 + * + * @param pipelineQuery 查询的条件 + * @return list + */ + public List getPipelines(PipelineQuery pipelineQuery) { + if (CollectionUtils.isEmpty(pipelineQuery.getPipelineIds())) { + return Collections.emptyList(); + } + Criteria criteria = Criteria.where("uuid").is(pipelineQuery.getUuid()) + .and("pipelineId").in(pipelineQuery.getPipelineIds()); + return mongoTemplate.find(Query.query(criteria), PipelineVo.class, CiCollectionName.PIPELINE); + } + + /** + * getPipelinesByComponent + * + * @param queryStageModel queryStageModel + * @return List + */ + public List getPipelinesByComponent(QueryStageModel queryStageModel) { + Criteria criteria = Criteria.where("namespace").is(queryStageModel.getProjectName()) + .and("manifest_branch").is(queryStageModel.getManifestBranch()) + .and("component").is(queryStageModel.getComponent()); + if (!StringUtils.isEmpty(queryStageModel.getStartTime())) { + criteria.and("timestamp").gte(queryStageModel.getStartTime()).lte(queryStageModel.getEndTime()); + } + return mongoTemplate.find(Query.query(criteria), PipelineVo.class, CiCollectionName.PIPELINE); + } + + /** + * 获取 + * + * @param queryStageModel queryStageModel + * @return List + */ + public List getUpgradeResultByDate(QueryStageModel queryStageModel) { + Criteria criteria = Criteria.where("upgradeResult").exists(true); + if (!StringUtils.isEmpty(queryStageModel.getStartTime())) { + criteria.and("timestamp").gte(queryStageModel.getStartTime()).lt(queryStageModel.getEndTime()); + } + return mongoTemplate.find(Query.query(criteria), PipelineVo.class, CiCollectionName.PIPELINE); + } + + /** + * 根据 uuid和pipelineId的组合查询 对应的流水线信息 + * + * @param allPipeline allPipeline + * @return List + */ + public List getPipeline(List allPipeline) { + Criteria[] criteriaList = allPipeline.stream().map(identity -> + Criteria.where("uuid").is(identity.getUuid()).and("pipelineId").is(identity.getPipelineId()) + .and("component").is(identity.getComponent()) + ).toArray(Criteria[]::new); + Criteria criteria = new Criteria(); + criteria.orOperator(criteriaList); + Query query = Query.query(criteria); + return mongoTemplate.find(query, PipelineVo.class, CiCollectionName.PIPELINE); + } + + /** + * 根据 uuid和pipelineId的组合查询 对应的流水线信息 + * + * + * @return List + */ + public List getPipelineById(String uuid,String ids) { + Criteria criteria = Criteria.where("uuid").is(uuid).and("pipelineId").is(ids); + List one = mongoTemplate.find(Query.query(criteria), PipelineVo.class, CiCollectionName.PIPELINE); + return one; + } +} diff --git a/ProjectInfoOperation.java b/ProjectInfoOperation.java new file mode 100644 index 0000000000000000000000000000000000000000..0c89826493d83570b709f800bfa7a0604321f443 --- /dev/null +++ b/ProjectInfoOperation.java @@ -0,0 +1,29 @@ +package com.huawei.operation; + +import com.huawei.entity.vo.codecheck.project.ProjectInfoVo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +public class ProjectInfoOperation { + @Autowired + @Qualifier("codeCheckMongoTemplate") + private MongoTemplate mongoTemplate; + + /** + * 查询所有项目信息 + * + * @return + */ + public List getAll() { + Criteria projectId = Criteria.where("projectId").exists(true).ne("").ne(null); + Query query = Query.query(projectId); + return mongoTemplate.find(query, ProjectInfoVo.class); + } +} diff --git a/ResultDetailsOperation.java b/ResultDetailsOperation.java new file mode 100644 index 0000000000000000000000000000000000000000..3ab04c6c07e3524918e2aa5c6e6f5c9a31d80d7a --- /dev/null +++ b/ResultDetailsOperation.java @@ -0,0 +1,449 @@ +package com.huawei.operation; + +import com.huawei.ci.common.codecheck.RestCodeCheckUtils; +import com.huawei.ci.common.contant.ProjectContant; +import com.huawei.entity.pojo.CodeCheckTaskIncVo; +import com.huawei.entity.pojo.QueryDefectDetailModel; +import com.huawei.entity.vo.ciinfo.codecheck.DefectVo; +import com.huawei.entity.vo.ciinfo.codecheck.Revision; +import com.huawei.entity.vo.codecheck.codecheckdetails.ReportVo; +import com.huawei.entity.vo.codecheck.eventModule.QueryDetailModel; +import com.huawei.entity.vo.codecheck.eventModule.ReportResultModel; +import com.huawei.entity.vo.codecheck.eventModule.RuleReportModel; +import com.huawei.entity.vo.codecheck.eventModule.ToolReportModel; +import com.huawei.enums.CodeCheckCollectionName; +import com.huawei.enums.CodeCheckConstants; +import com.mongodb.client.result.UpdateResult; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.domain.Sort; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.AggregationOperation; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.springframework.data.mongodb.core.aggregation.Aggregation.group; +import static org.springframework.data.mongodb.core.aggregation.Aggregation.match; +import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation; + +@Component +public class ResultDetailsOperation { + private static final Logger logger = LoggerFactory.getLogger(ResultDetailsOperation.class); + + private static final String FOSSSCAN = "fossscan"; + + private static final String PROGRAM_SPECIFICATION = "coding standards"; + + private static final String SENSITIVE_WORD = "wordstool"; + + private static final String OAT = "oat"; + + //每一批修改的问题数 + private static final int defectNum = 120; + + @Autowired + private RestCodeCheckUtils restCodeCheckUtils; + + @Autowired + @Qualifier("codeCheckMongoTemplate") + private MongoTemplate mongoTemplate; + + /** + * report 统计detail的方法 + * + * @param type type + * @param taskId taskId + * @param queryDetailModel queryDetailModel + * @param uuid uuid + * @return HashMap + */ + public HashMap getTaskIssuesReport(String type, String taskId, QueryDetailModel queryDetailModel, String uuid) { + boolean full = type.equalsIgnoreCase(CodeCheckConstants.HARMONY_FULL); + Criteria criteria = getTaskIssuesCommonCriteria(full, taskId, queryDetailModel, uuid); + if (StringUtils.isNotBlank(queryDetailModel.getDefectStatus())) { + criteria.and("defectStatus").is(queryDetailModel.getDefectStatus()); + } + if (StringUtils.isNotBlank(queryDetailModel.getFilePath())) { + criteria.and("filepath").regex(queryDetailModel.getFilePath()); + } + + if (StringUtils.isNotBlank(queryDetailModel.getFileName())) { + criteria.and(full ? "fileName" : "filepath").regex(queryDetailModel.getFileName()); + } + if (StringUtils.isNotBlank(queryDetailModel.getRule())) { + criteria.and("ruleName").regex(queryDetailModel.getRule()); + } + List operations = new ArrayList<>(); + operations.add(match(criteria)); + operations.add(group("defectLevel", "defectStatus", "fileName", "ruleSystemTags", "ruleName", "defectContent") + .first("defectLevel").as("defectLevel") + .first("defectStatus").as("defectStatus") + .first("fileName").as("fileName") + .first("ruleSystemTags").as("ruleSystemTags") + .first("ruleName").as("ruleName") + .first("defectContent").as("defectContent").count().as("total")); + List mappedResults = new ArrayList<>(); + if (full) { + mappedResults = mongoTemplate.aggregate(newAggregation(operations), + CodeCheckCollectionName.TASK_RESULT_DETAILS, ReportResultModel.class).getMappedResults(); + } else { + for (int i = 0; i < 10; ++i) { + mappedResults.addAll(mongoTemplate.aggregate(newAggregation(operations), + CodeCheckCollectionName.T_TASK_INC_RESULT_DETAILS + i, + ReportResultModel.class).getMappedResults()); + } + } + Map levelReport = new HashMap(); + Map statusReport = new HashMap(); + Map fileNameReport = new LinkedHashMap<>(); + Map contentReport = new LinkedHashMap<>(); + Map fossReport = new LinkedHashMap<>(); + List toolReportModels = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(mappedResults)) { + //根据等级分类统计 + levelReport = mappedResults.stream() + .collect(Collectors.groupingBy(ReportResultModel::getDefectLevel, Collectors.reducing(0, ReportResultModel::getTotal, Integer::sum))); + //根据状态分类统计 + statusReport = mappedResults.stream() + .collect(Collectors.groupingBy(ReportResultModel::getDefectStatus, Collectors.reducing(0, ReportResultModel::getTotal, Integer::sum))); + //根据工具统计 + mappedResults.stream() + .collect(Collectors.groupingBy(ReportResultModel::getRuleSystemTags, Collectors.reducing(0, ReportResultModel::getTotal, Integer::sum))) + .entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()) + .forEach(e -> { + ToolReportModel toolReportModel = new ToolReportModel(); + toolReportModel.setToolName(e.getKey()); + toolReportModel.setTotal(e.getValue()); + toolReportModels.add(toolReportModel); + }); + //根据规则名称分类统计排序 + if (CollectionUtils.isNotEmpty(toolReportModels)) { + for (ToolReportModel toolReportModel : toolReportModels) { + List ruleReportModels = new ArrayList<>(); + mappedResults.stream().filter(mappedResult -> mappedResult.getRuleSystemTags().equals(toolReportModel.getToolName())).collect(Collectors.groupingBy(ReportResultModel::getRuleName, Collectors.reducing(0, ReportResultModel::getTotal, Integer::sum))) + .entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()) + .forEach(e -> { + RuleReportModel ruleReportModel = new RuleReportModel(); + ruleReportModel.setRuleName(e.getKey()); + ruleReportModel.setTotal(e.getValue()); + ruleReportModels.add(ruleReportModel); + }); + toolReportModel.setToolName(StringUtils.isEmpty(toolReportModel.getToolName()) ? PROGRAM_SPECIFICATION : toolReportModel.getToolName()); + toolReportModel.setRuleReports(ruleReportModels); + } + } + List toolNameList = toolReportModels.stream().map(ToolReportModel::getToolName).distinct().collect(Collectors.toList()); + if (!toolNameList.contains(PROGRAM_SPECIFICATION)) { + ToolReportModel toolReportModel = new ToolReportModel(); + toolReportModel.setToolName(PROGRAM_SPECIFICATION); + toolReportModel.setTotal(0); + toolReportModels.add(toolReportModel); + } + if (!toolNameList.contains(SENSITIVE_WORD)) { + ToolReportModel toolReportModel = new ToolReportModel(); + toolReportModel.setToolName(SENSITIVE_WORD); + toolReportModel.setTotal(0); + toolReportModels.add(toolReportModel); + } + if (!toolNameList.contains(OAT)) { + ToolReportModel toolReportModel = new ToolReportModel(); + toolReportModel.setToolName(OAT); + toolReportModel.setTotal(0); + toolReportModels.add(toolReportModel); + } + //根据描述分类统计排序 + mappedResults.stream() + .collect(Collectors.groupingBy(ReportResultModel::getDefectContent, Collectors.reducing(0, ReportResultModel::getTotal, Integer::sum))) + .entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()).forEach(e -> contentReport.put(e.getKey(), e.getValue())); + //根据文件分类统计统计排序 + mappedResults.stream() + .collect(Collectors.groupingBy(ReportResultModel::getFileName, Collectors.reducing(0, ReportResultModel::getTotal, Integer::sum))) + .entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()).forEach(e -> fileNameReport.put(e.getKey(), e.getValue())); + } + + //fossscan问题加入 + Criteria criteriaFalse = Criteria.where("taskId").is(taskId).and("isConfirm").is(false); + Criteria criteriaTrue = Criteria.where("taskId").is(taskId).and("isConfirm").is(true); + if (full) { + criteriaFalse.and("date").is(queryDetailModel.getDate()); + criteriaTrue.and("date").is(queryDetailModel.getDate()); + } + { + criteriaFalse.and("uuid").is(uuid); + criteriaTrue.and("uuid").is(uuid); + } + Query fossQuery = new Query(criteriaFalse); + long fosscount = mongoTemplate.count(fossQuery, CodeCheckCollectionName.FOSSSCAN_FRAGMENT); + fossQuery = new Query(criteriaTrue); + long fosscountConfirm = mongoTemplate.count(fossQuery, CodeCheckCollectionName.FOSSSCAN_FRAGMENT); + fossReport.put("0", (int) fosscount); + fossReport.put("2", (int) fosscountConfirm); + + HashMap issueReport = new HashMap(); + issueReport.put("levelReport", levelReport); + issueReport.put("statusReport", statusReport); + issueReport.put("fileNameReport", fileNameReport); + issueReport.put("contentReport", contentReport); + issueReport.put("ruleSystemTagsReport", toolReportModels); + issueReport.put("fossReport", fossReport); + return issueReport; + } + + /** + * 获取概览页问题总数分类查询和详情页问题分类查询公共条件获取 + * + * @param isFull 全量或者增量标志 + * @param taskId 任务id + * @param queryDetailModel 查询条件参数 + * @param uuid 增量任务的uuid + * @return Criteria 条件 + */ + private Criteria getTaskIssuesCommonCriteria(boolean isFull, String taskId, QueryDetailModel queryDetailModel, + String uuid) { + Criteria criteria = Criteria.where("taskId").is(taskId).and("ruleSystemTags").ne(FOSSSCAN); + String date = ""; + if (!isFull) { + criteria.and("uuid").is(uuid); + } else if (StringUtils.isBlank(queryDetailModel.getDate())) { + //如果时间为空就去查询detail的最新时间 + Query query = new Query(); + query.fields().include("date"); + query.with(Sort.by(Sort.Order.desc("date"))); + query.addCriteria(Criteria.where("taskId").is(taskId)); + DefectVo defectVo = mongoTemplate.findOne(query, DefectVo.class, + CodeCheckCollectionName.TASK_RESULT_DETAILS); + if (defectVo != null) { + criteria.and("date").is(defectVo.getDate()); + date = defectVo.getDate(); + } + } else { + criteria.and("date").is(queryDetailModel.getDate()); + } + return criteria; + } + + /** + * 修改事件问题状态 + */ + public int updateEventDefect(String userId, String userName, QueryDetailModel queryDetailModel, String eventId) { + //统计修改总数 + int num = 0; + if (queryDetailModel.getDefectStatus() != null && queryDetailModel.getIds() != null) { + Query query = new Query(Criteria.where("uuid").is(eventId).and("_id").in(queryDetailModel.getIds())); + //获取原问题列表 + List defectVoList = new ArrayList<>(); + for (int i = 0; i < 10; ++i) { + defectVoList.addAll(mongoTemplate.find(query, DefectVo.class, + CodeCheckCollectionName.T_TASK_INC_RESULT_DETAILS + i)); + } + defectVoList = + defectVoList.stream().filter(defectVo -> !StringUtils.equals(defectVo.getDefectCheckerName().substring(0, 3), "OAT")).collect(Collectors.toList()); + if (defectVoList.size() > 0) { + Query taskQuery = new Query(Criteria.where("taskId").is(defectVoList.get(0).getTaskId()).and("uuid").is(eventId)); + CodeCheckTaskIncVo codeCheckTaskIncVo = mongoTemplate.findOne(taskQuery, CodeCheckTaskIncVo.class, CodeCheckCollectionName.TASK_INC); + if (codeCheckTaskIncVo != null && codeCheckTaskIncVo.getProcessing() != 2) { + return -1; + } + for (int start = 0; start < defectVoList.size(); start += defectNum) { + int end = defectVoList.size() - start <= defectNum ? defectVoList.size() - 1 : start + defectNum - 1; + StringBuilder ids = new StringBuilder(defectVoList.get(start).getDefectId() == null ? "" : defectVoList.get(start).getDefectId()); + for (int i = start + 1; i <= end; i++) { + ids.append(",").append(defectVoList.get(i).getDefectId() == null ? "" : defectVoList.get(i).getDefectId()); + } + if (defectVoList.size() > end && restCodeCheckUtils.updateDefectStatus(defectVoList.get(start).getTaskId(), ids.toString(), + Integer.parseInt(queryDetailModel.getDefectStatus()), ProjectContant.RequestConstant.REGION)) { + //修改库 + updateDefectsImpl(defectVoList.subList(start, end + 1), userId, userName, queryDetailModel.getDefectStatus(), 1); + num += end - start + 1; + } else { + return 0; + } + } + } else { + logger.warn("no defectIds"); + return 0; + } + } else { + logger.error("param is null"); + return 0; + } + return num; + } + + public void updateDefectsImpl(List defectVoList, String userId, String userName, String status, int inc) { + String detailsName; + String summaryName; + if (inc == 1) { + detailsName = CodeCheckCollectionName.TASK_INC_RESULT_DETAILS; + summaryName = CodeCheckCollectionName.TASK_INC_RESULT_SUMMARY; + } else { + detailsName = CodeCheckCollectionName.TASK_RESULT_DETAILS; + summaryName = CodeCheckCollectionName.TASK_RESULT_SUMMARY; + } + //遍历问题列表并统计未解决和已忽略问题数 + int def0 = 0, def2 = 0; + for (DefectVo defectVo : defectVoList) { + if (defectVo.getDefectStatus().equals("0")) { + def0++; + } + if (defectVo.getDefectStatus().equals("2")) { + def2++; + } + Revision revision = new Revision(); + revision.setNewStatus(Integer.parseInt(status)); + revision.setPreviousStatus(Integer.getInteger(defectVo.getDefectStatus())); + revision.setUserId(userId); + revision.setUserName(userName); + revision.setTimestamp(LocalDateTime.now()); + Query query = new Query(Criteria.where("defectId").is(defectVo.getDefectId())); + Update update = new Update(); + update.set("revision", revision); + update.set("defectStatus", status); + if (StringUtils.equals(detailsName, CodeCheckCollectionName.TASK_INC_RESULT_DETAILS)) { + for (int i = 0; i < 10; ++i) { + mongoTemplate.upsert(query, update, CodeCheckCollectionName.T_TASK_INC_RESULT_DETAILS + i); + } + } + mongoTemplate.upsert(query, update, detailsName); + } + //修改summry + Query query1; + if (inc == 1) { + query1 = new Query(Criteria.where("taskId").is(defectVoList.get(0).getTaskId()).and("uuid").is(defectVoList.get(0).getUuid())); + } else { + query1 = new Query(Criteria.where("taskId").is(defectVoList.get(0).getTaskId()).and("date").is(defectVoList.get(0).getDate())); + } + ReportVo reportVo = mongoTemplate.findOne(query1, ReportVo.class, summaryName); + if (reportVo != null) { + if (status.equals("0")) { + reportVo.setIssueCount(reportVo.getIssueCount() + defectVoList.size() - def0); + reportVo.setIgnoreCount(reportVo.getIgnoreCount() - def2); + } else if (status.equals("2")) { + reportVo.setIssueCount(reportVo.getIssueCount() - def0); + reportVo.setIgnoreCount(reportVo.getIgnoreCount() + defectVoList.size() - def2); + } + Update update1 = new Update(); + update1.set("issueCount", reportVo.getIssueCount()); + update1.set("new_count", reportVo.getIssueCount()); + update1.set("ignoreCount", reportVo.getIgnoreCount()); + mongoTemplate.updateFirst(query1, update1, summaryName); + } + } + + /** + * 每日代码检查问题查询条件 + * + * @Params: queryDefectDetailModel 查询参数 + * @return: List 问题详情列表 + */ + public List queryDefects(QueryDefectDetailModel queryDefectDetailModel) { + //根据是否传入uuid判断是全量的还是增量的问题详情 + Criteria criteria = Criteria.where("taskId").is(queryDefectDetailModel.getTaskId()); + String collectionName = CodeCheckCollectionName.TASK_RESULT_DETAILS; + if (StringUtils.isNotBlank(queryDefectDetailModel.getDate())) { + criteria.and("date").is(queryDefectDetailModel.getDate()); + } + if (StringUtils.isNotBlank(queryDefectDetailModel.getDefectLevel())) { + criteria.and("defectLevel").is(queryDefectDetailModel.getDefectLevel()); + } + if (StringUtils.isNotBlank(queryDefectDetailModel.getDefectStatus())) { + criteria.and("defectStatus").is(queryDefectDetailModel.getDefectStatus()); + } + if (StringUtils.isNotBlank(queryDefectDetailModel.getRuleSystemTags())) { + criteria.and("ruleSystemTags").is(queryDefectDetailModel.getRuleSystemTags()); + } + if (StringUtils.isNotBlank(queryDefectDetailModel.getRuleName())) { + criteria.and("ruleName").is(queryDefectDetailModel.getRuleName()); + } + List defectVos = new ArrayList<>(); + if (StringUtils.isNotBlank(queryDefectDetailModel.getUuid())) { + criteria.and("uuid").is(queryDefectDetailModel.getUuid()); + for (int i = 0; i < 10; ++i) { + defectVos.addAll(mongoTemplate.find(Query.query(criteria), DefectVo.class, + CodeCheckCollectionName.T_TASK_INC_RESULT_DETAILS + i)); + } + } else { + defectVos = mongoTemplate.find(Query.query(criteria), DefectVo.class, collectionName); + } + return defectVos; + } + + /** + * 根据问题id获取问题数据 + * + * @param defectId 问题id + * @param detailCollectionName 集合名称 + * @return DefectVo问题数据 + */ + public DefectVo getDefectById(String defectId, String detailCollectionName) { + Query query = Query.query(Criteria.where("_id").is(defectId)); + return mongoTemplate.findOne(query, DefectVo.class, detailCollectionName); + } + + /** + * 根据id查询问题列表,获取有效的问题 + * + * @param defectIds 问题id列表 + * @param collectionName 仓库名称 + * @return List 问题列表 + */ + public List getEffectiveDefectId(List defectIds, String defectStatus, String collectionName) { + Query query = Query.query(Criteria.where("_id").in(defectIds).and("defectStatus").is(defectStatus)); + return mongoTemplate.find(query, DefectVo.class, collectionName); + } + + /** + * 修改问题的版本号 + * + * @param defectIds 问题id列表 + * @param revision 版本信息 + * @param collectionName 集合名称 + * @return + */ + public long updateDetailRevision(List defectIds, Revision revision, String collectionName) { + Query query = Query.query(Criteria.where("defectId").in(defectIds)); + Update update = Update.update("revision", revision); + update.set("defectStatus", String.valueOf(revision.getNewStatus())); + UpdateResult updateResult = mongoTemplate.updateMulti(query, update, collectionName); + return updateResult.getModifiedCount(); + } + + /** + * 获取有效的issueKey + * + * @param defectIds 问题id + * @param defectStatus 问题状态 + * @return + */ + public List getEffectiveIssueKey(List defectIds, String defectStatus){ + Query query = Query.query(Criteria.where("_id").in(defectIds).and("defectStatus").is(defectStatus)); + return mongoTemplate.findDistinct(query, "issueKey",CodeCheckCollectionName.TASK_RESULT_DETAILS,String.class); + } + + /** + * 根据issueKey查询defectId + * + * @param taskId 任务id + * @param issueKeys issueKey列表 + * @param defectStatus 问题状态 + * @return List defectId列表 + */ + public List getDefectIdByIssueKey(String taskId, List issueKeys,String defectStatus) { + Query query = Query.query(Criteria.where("taskId").is(taskId).and("defectStatus").is(defectStatus).and("issueKey").in(issueKeys)); + return mongoTemplate.findDistinct(query, "defectId",CodeCheckCollectionName.TASK_RESULT_DETAILS,String.class); + } +} \ No newline at end of file diff --git a/ResultSummaryOperation.java b/ResultSummaryOperation.java new file mode 100644 index 0000000000000000000000000000000000000000..26d2bc5da65f637eafc375c461615d8d83c99682 --- /dev/null +++ b/ResultSummaryOperation.java @@ -0,0 +1,92 @@ +package com.huawei.operation; + +import com.huawei.entity.pojo.CodeCheckResultSummaryVo; +import com.huawei.enums.CodeCheckCollectionName; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.domain.Sort; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.aggregation.AggregationOperation; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class ResultSummaryOperation { + @Autowired + @Qualifier("codeCheckMongoTemplate") + private MongoTemplate mongoTemplate; + private static final Logger logger = LoggerFactory.getLogger(ResultSummaryOperation.class); + + private static final String[] sortedField = new String[]{ + "issueCount", "solve_count", "ignoreCount" + }; + + /** + * 通过日期和任务id查询summary + * + * @param currentDate 当前时间 + * @param taskId 任务id + * @return CodeCheckResultSummaryVo summary数据 + */ + public CodeCheckResultSummaryVo getSummaryByTaskIdAndDate(String currentDate, String taskId) { + Query query = Query.query(Criteria.where("taskId").is(taskId).and("date").is(currentDate)); + return mongoTemplate.findOne(query, CodeCheckResultSummaryVo.class, CodeCheckCollectionName.TASK_RESULT_SUMMARY); + } + + /** + * 根据taskId和日期更新summary数据 + * + * @param summaryByNameAndBranch summary数据 + * @param currentDate 当前日期 + */ + public void updateSummaryByTaskIdAndDate(CodeCheckResultSummaryVo summaryByNameAndBranch, String currentDate) { + Query query = Query.query(Criteria.where("taskId").is(summaryByNameAndBranch.getTaskId()).and("date").is(currentDate)); + //不存在则新增,存在则替换 + CodeCheckResultSummaryVo replacedOne = mongoTemplate.findOne(query, CodeCheckResultSummaryVo.class, CodeCheckCollectionName.TASK_RESULT_SUMMARY); + if (ObjectUtils.isEmpty(replacedOne)) { + mongoTemplate.save(summaryByNameAndBranch, CodeCheckCollectionName.TASK_RESULT_SUMMARY); + } else { + mongoTemplate.findAndReplace(query, summaryByNameAndBranch); + } + logger.info(" -------------------> summary info have saved <---------------- "); + //mongoTemplate.findAndReplace(query,summaryByNameAndBranch); + } + + /** + * + * @param taskId 任务Id + * @return 最新summary数据 + */ + public List getLastSummaryByTaskId(String taskId, String date) { + List operations = new ArrayList<>(); + Criteria criteria = Criteria.where("taskId").is(taskId); + if (StringUtils.isNotBlank(date)) { + criteria.and("date").is(date); + } + operations.add(Aggregation.match(criteria)); + operations.add(Aggregation.sort(Sort.by(Sort.Order.desc("date")))); + operations.add(Aggregation.limit(1)); + return mongoTemplate.aggregate(Aggregation.newAggregation(operations), + CodeCheckCollectionName.TASK_RESULT_SUMMARY, CodeCheckResultSummaryVo.class).getMappedResults(); + } + + /** + * 通过id更新summary + * + * @param summaryByNameAndBranch summary数据 + */ + public void updateById(CodeCheckResultSummaryVo summaryByNameAndBranch) { + Query query = Query.query(Criteria.where("id").is(summaryByNameAndBranch.getId())); + mongoTemplate.findAndReplace(query, summaryByNameAndBranch); + } +} +