From 968cf8b579dd914b359b7a75ad28feeb1dd95eb3 Mon Sep 17 00:00:00 2001 From: hexiangyun Date: Thu, 18 Aug 2022 11:59:33 +0000 Subject: [PATCH 1/5] =?UTF-8?q?CI=E6=B5=8B=E8=AF=95888=EF=BC=8C=E8=AF=B7?= =?UTF-8?q?=E5=8B=BF=E5=90=88=E5=85=A5=EF=BC=81=EF=BC=81=EF=BC=81=20CI?= =?UTF-8?q?=E6=B5=8B=E8=AF=95888=EF=BC=8C=E8=AF=B7=E5=8B=BF=E5=90=88?= =?UTF-8?q?=E5=85=A5=EF=BC=81=EF=BC=81=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hexiangyun --- ResourcesInitializerUtil.java | 145 +++++++++++++++++++++++++++ SensitiveWordsFilterCheckerUtil.java | 137 +++++++++++++++++++++++++ 2 files changed, 282 insertions(+) create mode 100644 ResourcesInitializerUtil.java create mode 100644 SensitiveWordsFilterCheckerUtil.java diff --git a/ResourcesInitializerUtil.java b/ResourcesInitializerUtil.java new file mode 100644 index 000000000..84f1bba52 --- /dev/null +++ b/ResourcesInitializerUtil.java @@ -0,0 +1,145 @@ +package com.huawei.ci.portal.provider.utils; + +import com.huawei.ci.portal.provider.enums.assistanthelper.AssistantConstants; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.io.*; +import java.util.*; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.stream.Collectors; + +/** + * @Description: 资源文件初始化处理类 + * @ClassName: com.huawei.ci.portal.provider.utils + * @Author: hwx1123794/hexiangyun + * @DateTime: 2022/7/21 10:04 + * @Params: + **/ +@Data +@AllArgsConstructor +@Component +public class ResourcesInitializerUtil { + + private static final Logger logger = LoggerFactory.getLogger(ResourcesInitializerUtil.class); + + private static StringBuilder replaceAll; + + private static String encoding = AssistantConstants.ENCODING; + + private static String replaceStr = AssistantConstants.BE_REPLACE_STR; + + private static int replaceSize = AssistantConstants.BE_REPLACE_SIZE; + + private static String fileName = AssistantConstants.FILE_PATH; + + private static HashSet strings = new HashSet<>(100); + + private static Set allSensitiveWordSet = new HashSet<>(); + + private static Map afterDealingSensitiveWords = new HashMap<>(); + + + /** + * 文件 + * + * @param fileName 词库文件名(含后缀) + */ + public ResourcesInitializerUtil(String fileName) { + this.fileName = fileName; + } + + + /** + * 初始化敏感词库 + * + * @return + */ + protected Map InitializationWorkUtil() { + replaceAll = new StringBuilder(replaceSize); + for (int x = 0; x < replaceSize; x++) { + replaceAll.append(replaceStr); + } + InputStreamReader read = null; + BufferedReader bufferedReader = null; + ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(); + // 读锁加锁 + reentrantReadWriteLock.readLock().lock(); + try { + logger.info("Init sensitive-words configuration start "); + read = new InputStreamReader(Objects.requireNonNull(ResourcesInitializerUtil.class.getClassLoader() + .getResourceAsStream(fileName)), encoding); + // 缓冲区大小 手动设置为80K + bufferedReader = new BufferedReader(read, 81920); + allSensitiveWordSet = bufferedReader.lines().collect(Collectors.toSet()); + // 构造属性 + afterDealingSensitiveWords = addSensitiveWords2HashConstruct(allSensitiveWordSet); + logger.info("Init sensitive-words configuration success "); + + } catch (IOException e) { + logger.error("read file failed"); + } finally { + try { + if (null != bufferedReader) + // 缓冲流关闭 + bufferedReader.close(); + } catch (IOException e) { + logger.error("shutdown Buffered streamline failed, caused by: " + e); + } + try { + if (null != read) + // IO流关闭 + read.close(); + } catch (IOException e) { + logger.error("shutdown IO streamline failed , caused by: " + e); + } + // 释放 + reentrantReadWriteLock.readLock().unlock(); + } + return afterDealingSensitiveWords; + } + + + /** + * DFA算法构造敏感词树形结构 (凡是相同字符开头的都在同一个HashMap + 树形结构 ) + * + * @param sensitiveWords 从文件中读取的敏感词集合 + * @return + */ + private HashMap addSensitiveWords2HashConstruct(Set sensitiveWords) { + // 关键字 整理成 Hash->树形 结构。 + HashMap sensitiveWordsMap = new HashMap(sensitiveWords.size()); + String currentWord = null; + Map childMap = null; + Map newWordMap = new HashMap<>(); + // 处理敏感词 + Iterator iterator = sensitiveWords.iterator(); + while (iterator.hasNext()) { + currentWord = iterator.next(); + childMap = sensitiveWordsMap; + // 关键字构造树形结构 + for (int i = 0; i < currentWord.length(); i++) { + char key = currentWord.charAt(i); + Object wordMap = childMap.get(key); + if (wordMap != null) { + childMap = (Map) wordMap; + } else { + newWordMap = new HashMap<>(); + // 添加标记位 + newWordMap.put("isEnd", "0"); + childMap.put(key, newWordMap); + childMap = newWordMap; + } + // 最后一位 + if (i == currentWord.length() - 1) { + childMap.put("isEnd", "1"); + } + } + } + return sensitiveWordsMap; + } +} diff --git a/SensitiveWordsFilterCheckerUtil.java b/SensitiveWordsFilterCheckerUtil.java new file mode 100644 index 000000000..5d0203d48 --- /dev/null +++ b/SensitiveWordsFilterCheckerUtil.java @@ -0,0 +1,137 @@ +package com.huawei.ci.portal.provider.utils; + +import com.huawei.ci.portal.provider.enums.assistanthelper.AssistantConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.util.*; + +/** + * @Description: 敏感词过滤工具,过滤不雅词汇 + * @ClassName: com.huawei.ci.portal.provider.utils + * @Author: hwx1123794/hexiangyun + * @DateTime: 2022/7/21 15:54 + * @Params: + **/ +@Component +public class SensitiveWordsFilterCheckerUtil { + + private static final Logger logger = LoggerFactory.getLogger(SensitiveWordsFilterCheckerUtil.class); + + private static final Map sensitiveWordsMap; + + // 最小匹配 + private static final Integer minMatchType = 1; + + // 完全匹配 + private static final Integer maxMatchType = 2; + + static { + sensitiveWordsMap = new ResourcesInitializerUtil().InitializationWorkUtil(); + } + + + /** + * 校验是否包含敏感词 + * + * @param txt 待判断文本 + * @param start 起始位置 + * @param matchType 匹配类型: 1 最小匹配原则;2 最大匹配原则 + * @return 大于0表示包含敏感词且表示敏感词匹配长度,否则不包含 + */ + private static Integer checkIfExistSensitiveWords(String txt, Integer start, Integer matchType) { + Boolean flag = false; + char word; + // 匹配标记位 + Integer matchFlag = 0; + Map childMap = sensitiveWordsMap; + for (int i = start; i < txt.length(); i++) { + word = txt.charAt(i); + childMap = (Map) childMap.get(word); + if (childMap == null) { + break; + } else { + // 匹配标记位+1 + matchFlag++; + // isEnd标记位 = 1时,匹配到了末尾 + if ("1".equals(childMap.get("isEnd"))) { + flag = true; + if (minMatchType.equals(matchType)) { + break; + } + } + } + } + if (matchFlag < 2 || !flag) { + // 匹配长度需大于2才为词,并且敏感词已结束 + matchFlag = 0; + } + return matchFlag; + } + + /** + * 获取所有敏感词 + * + * @param txt 待判断文本 + * @param matchType 匹配类型: 1 最小匹配原则;2 最大匹配原则 + * @return + */ + public static Set getSensitiveWords(String txt, Integer matchType) { + Set sensitiveWords = new HashSet<>(); + for (int i = 0; i < txt.length(); i++) { + // 判断敏感词所在文本内容中的 起始点 + Integer length = checkIfExistSensitiveWords(txt, i, matchType); + if (length > 0) { + sensitiveWords.add(txt.substring(i, i + length)); + // 循环i会+1,所以需-1 + i = i + length - 1; + } + } + return sensitiveWords; + } + + /** + * 替换敏感词 + * + * @param txtOfInput 文本 + * @param matchType 匹配类型: 1 最小匹配原则;2 最大匹配原则 + * @param replaceStr 替换字符 + * @return 处理后的文本 + */ + public String replaceSensitiveWords(String txtOfInput, Integer matchType, String replaceStr) { + if (txtOfInput == null || "".equals(txtOfInput)) { + return txtOfInput; + } + Set sensitiveWords = getSensitiveWords(txtOfInput, matchType); + Iterator iterator = sensitiveWords.iterator(); + String replaceString = ""; + while (iterator.hasNext()) { + String sWord = iterator.next(); + // 替换敏感词内容 + replaceString = getReplaceString(replaceStr, sWord.length()); + txtOfInput = txtOfInput.replaceAll(sWord, replaceString); + } + return txtOfInput; + } + + /** + * 获取需要替换的文本 + * + * @param length 要替换的文本长度 + * @param replaceStr 将要替换的文本符号 + */ + private static String getReplaceString(String replaceStr, Integer length) { + if (replaceStr == null) { + // 使用 * 替换敏感词。 在 AssistantConstants中配置 + replaceStr = AssistantConstants.BE_REPLACE_STR; + } + StringBuffer replaceString = new StringBuffer(); + for (int i = 0; i < length; i++) { + replaceString.append(replaceStr); + } + return replaceString.toString(); + } + +} + -- Gitee From c73bc2eca137c8c76d0e33fe4ffc4fb6467e11a2 Mon Sep 17 00:00:00 2001 From: hexiangyun Date: Fri, 19 Aug 2022 02:37:36 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Sens?= =?UTF-8?q?itiveWordsFilterCheckerUtil.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SensitiveWordsFilterCheckerUtil.java | 137 --------------------------- 1 file changed, 137 deletions(-) delete mode 100644 SensitiveWordsFilterCheckerUtil.java diff --git a/SensitiveWordsFilterCheckerUtil.java b/SensitiveWordsFilterCheckerUtil.java deleted file mode 100644 index 5d0203d48..000000000 --- a/SensitiveWordsFilterCheckerUtil.java +++ /dev/null @@ -1,137 +0,0 @@ -package com.huawei.ci.portal.provider.utils; - -import com.huawei.ci.portal.provider.enums.assistanthelper.AssistantConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import java.util.*; - -/** - * @Description: 敏感词过滤工具,过滤不雅词汇 - * @ClassName: com.huawei.ci.portal.provider.utils - * @Author: hwx1123794/hexiangyun - * @DateTime: 2022/7/21 15:54 - * @Params: - **/ -@Component -public class SensitiveWordsFilterCheckerUtil { - - private static final Logger logger = LoggerFactory.getLogger(SensitiveWordsFilterCheckerUtil.class); - - private static final Map sensitiveWordsMap; - - // 最小匹配 - private static final Integer minMatchType = 1; - - // 完全匹配 - private static final Integer maxMatchType = 2; - - static { - sensitiveWordsMap = new ResourcesInitializerUtil().InitializationWorkUtil(); - } - - - /** - * 校验是否包含敏感词 - * - * @param txt 待判断文本 - * @param start 起始位置 - * @param matchType 匹配类型: 1 最小匹配原则;2 最大匹配原则 - * @return 大于0表示包含敏感词且表示敏感词匹配长度,否则不包含 - */ - private static Integer checkIfExistSensitiveWords(String txt, Integer start, Integer matchType) { - Boolean flag = false; - char word; - // 匹配标记位 - Integer matchFlag = 0; - Map childMap = sensitiveWordsMap; - for (int i = start; i < txt.length(); i++) { - word = txt.charAt(i); - childMap = (Map) childMap.get(word); - if (childMap == null) { - break; - } else { - // 匹配标记位+1 - matchFlag++; - // isEnd标记位 = 1时,匹配到了末尾 - if ("1".equals(childMap.get("isEnd"))) { - flag = true; - if (minMatchType.equals(matchType)) { - break; - } - } - } - } - if (matchFlag < 2 || !flag) { - // 匹配长度需大于2才为词,并且敏感词已结束 - matchFlag = 0; - } - return matchFlag; - } - - /** - * 获取所有敏感词 - * - * @param txt 待判断文本 - * @param matchType 匹配类型: 1 最小匹配原则;2 最大匹配原则 - * @return - */ - public static Set getSensitiveWords(String txt, Integer matchType) { - Set sensitiveWords = new HashSet<>(); - for (int i = 0; i < txt.length(); i++) { - // 判断敏感词所在文本内容中的 起始点 - Integer length = checkIfExistSensitiveWords(txt, i, matchType); - if (length > 0) { - sensitiveWords.add(txt.substring(i, i + length)); - // 循环i会+1,所以需-1 - i = i + length - 1; - } - } - return sensitiveWords; - } - - /** - * 替换敏感词 - * - * @param txtOfInput 文本 - * @param matchType 匹配类型: 1 最小匹配原则;2 最大匹配原则 - * @param replaceStr 替换字符 - * @return 处理后的文本 - */ - public String replaceSensitiveWords(String txtOfInput, Integer matchType, String replaceStr) { - if (txtOfInput == null || "".equals(txtOfInput)) { - return txtOfInput; - } - Set sensitiveWords = getSensitiveWords(txtOfInput, matchType); - Iterator iterator = sensitiveWords.iterator(); - String replaceString = ""; - while (iterator.hasNext()) { - String sWord = iterator.next(); - // 替换敏感词内容 - replaceString = getReplaceString(replaceStr, sWord.length()); - txtOfInput = txtOfInput.replaceAll(sWord, replaceString); - } - return txtOfInput; - } - - /** - * 获取需要替换的文本 - * - * @param length 要替换的文本长度 - * @param replaceStr 将要替换的文本符号 - */ - private static String getReplaceString(String replaceStr, Integer length) { - if (replaceStr == null) { - // 使用 * 替换敏感词。 在 AssistantConstants中配置 - replaceStr = AssistantConstants.BE_REPLACE_STR; - } - StringBuffer replaceString = new StringBuffer(); - for (int i = 0; i < length; i++) { - replaceString.append(replaceStr); - } - return replaceString.toString(); - } - -} - -- Gitee From 2bac35954b1dc1f839b964f4727e38fb45eb6365 Mon Sep 17 00:00:00 2001 From: hexiangyun Date: Fri, 19 Aug 2022 02:37:46 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Reso?= =?UTF-8?q?urcesInitializerUtil.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ResourcesInitializerUtil.java | 145 ---------------------------------- 1 file changed, 145 deletions(-) delete mode 100644 ResourcesInitializerUtil.java diff --git a/ResourcesInitializerUtil.java b/ResourcesInitializerUtil.java deleted file mode 100644 index 84f1bba52..000000000 --- a/ResourcesInitializerUtil.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.huawei.ci.portal.provider.utils; - -import com.huawei.ci.portal.provider.enums.assistanthelper.AssistantConstants; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import java.io.*; -import java.util.*; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.stream.Collectors; - -/** - * @Description: 资源文件初始化处理类 - * @ClassName: com.huawei.ci.portal.provider.utils - * @Author: hwx1123794/hexiangyun - * @DateTime: 2022/7/21 10:04 - * @Params: - **/ -@Data -@AllArgsConstructor -@Component -public class ResourcesInitializerUtil { - - private static final Logger logger = LoggerFactory.getLogger(ResourcesInitializerUtil.class); - - private static StringBuilder replaceAll; - - private static String encoding = AssistantConstants.ENCODING; - - private static String replaceStr = AssistantConstants.BE_REPLACE_STR; - - private static int replaceSize = AssistantConstants.BE_REPLACE_SIZE; - - private static String fileName = AssistantConstants.FILE_PATH; - - private static HashSet strings = new HashSet<>(100); - - private static Set allSensitiveWordSet = new HashSet<>(); - - private static Map afterDealingSensitiveWords = new HashMap<>(); - - - /** - * 文件 - * - * @param fileName 词库文件名(含后缀) - */ - public ResourcesInitializerUtil(String fileName) { - this.fileName = fileName; - } - - - /** - * 初始化敏感词库 - * - * @return - */ - protected Map InitializationWorkUtil() { - replaceAll = new StringBuilder(replaceSize); - for (int x = 0; x < replaceSize; x++) { - replaceAll.append(replaceStr); - } - InputStreamReader read = null; - BufferedReader bufferedReader = null; - ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(); - // 读锁加锁 - reentrantReadWriteLock.readLock().lock(); - try { - logger.info("Init sensitive-words configuration start "); - read = new InputStreamReader(Objects.requireNonNull(ResourcesInitializerUtil.class.getClassLoader() - .getResourceAsStream(fileName)), encoding); - // 缓冲区大小 手动设置为80K - bufferedReader = new BufferedReader(read, 81920); - allSensitiveWordSet = bufferedReader.lines().collect(Collectors.toSet()); - // 构造属性 - afterDealingSensitiveWords = addSensitiveWords2HashConstruct(allSensitiveWordSet); - logger.info("Init sensitive-words configuration success "); - - } catch (IOException e) { - logger.error("read file failed"); - } finally { - try { - if (null != bufferedReader) - // 缓冲流关闭 - bufferedReader.close(); - } catch (IOException e) { - logger.error("shutdown Buffered streamline failed, caused by: " + e); - } - try { - if (null != read) - // IO流关闭 - read.close(); - } catch (IOException e) { - logger.error("shutdown IO streamline failed , caused by: " + e); - } - // 释放 - reentrantReadWriteLock.readLock().unlock(); - } - return afterDealingSensitiveWords; - } - - - /** - * DFA算法构造敏感词树形结构 (凡是相同字符开头的都在同一个HashMap + 树形结构 ) - * - * @param sensitiveWords 从文件中读取的敏感词集合 - * @return - */ - private HashMap addSensitiveWords2HashConstruct(Set sensitiveWords) { - // 关键字 整理成 Hash->树形 结构。 - HashMap sensitiveWordsMap = new HashMap(sensitiveWords.size()); - String currentWord = null; - Map childMap = null; - Map newWordMap = new HashMap<>(); - // 处理敏感词 - Iterator iterator = sensitiveWords.iterator(); - while (iterator.hasNext()) { - currentWord = iterator.next(); - childMap = sensitiveWordsMap; - // 关键字构造树形结构 - for (int i = 0; i < currentWord.length(); i++) { - char key = currentWord.charAt(i); - Object wordMap = childMap.get(key); - if (wordMap != null) { - childMap = (Map) wordMap; - } else { - newWordMap = new HashMap<>(); - // 添加标记位 - newWordMap.put("isEnd", "0"); - childMap.put(key, newWordMap); - childMap = newWordMap; - } - // 最后一位 - if (i == currentWord.length() - 1) { - childMap.put("isEnd", "1"); - } - } - } - return sensitiveWordsMap; - } -} -- Gitee From 1b58365e9082f5f4730b16751ba88079f39f399a Mon Sep 17 00:00:00 2001 From: hexiangyun Date: Wed, 28 Dec 2022 10:09:49 +0000 Subject: [PATCH 4/5] =?UTF-8?q?CI=E6=B5=8B=E8=AF=95=EF=BC=8C=E8=AF=B7?= =?UTF-8?q?=E5=8B=BF=E5=90=88=E5=85=A5=EF=BC=81=EF=BC=81=EF=BC=81=20CI?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=8C=E8=AF=B7=E5=8B=BF=E5=90=88=E5=85=A5?= =?UTF-8?q?=EF=BC=81=EF=BC=81=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hexiangyun --- IncResultDetailsOperation.java | 154 +++++++++++ IncResultSummaryOperation.java | 177 +++++++++++++ PipelineOperation.java | 100 ++++++++ ProjectInfoOperation.java | 29 +++ ResultDetailsOperation.java | 449 +++++++++++++++++++++++++++++++++ ResultSummaryOperation.java | 92 +++++++ 6 files changed, 1001 insertions(+) create mode 100644 IncResultDetailsOperation.java create mode 100644 IncResultSummaryOperation.java create mode 100644 PipelineOperation.java create mode 100644 ProjectInfoOperation.java create mode 100644 ResultDetailsOperation.java create mode 100644 ResultSummaryOperation.java diff --git a/IncResultDetailsOperation.java b/IncResultDetailsOperation.java new file mode 100644 index 000000000..1353a7ba8 --- /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 000000000..d3f858672 --- /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 000000000..3327d6382 --- /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 000000000..0c8982649 --- /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 000000000..3ab04c6c0 --- /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 000000000..26d2bc5da --- /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); + } +} + -- Gitee From d0b5771057dc4cdb558e0938498bbc0db7ca174d Mon Sep 17 00:00:00 2001 From: hexiangyun Date: Thu, 12 Jan 2023 01:58:33 +0000 Subject: [PATCH 5/5] =?UTF-8?q?CI=E6=B5=8B=E8=AF=95=EF=BC=8C=E8=AF=B7?= =?UTF-8?q?=E5=8B=BF=E5=90=88=E5=85=A5=EF=BC=81=20=E5=AD=99=E6=99=93?= =?UTF-8?q?=E8=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hexiangyun --- "CI\346\265\213\350\257\225" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "CI\346\265\213\350\257\225" diff --git "a/CI\346\265\213\350\257\225" "b/CI\346\265\213\350\257\225" new file mode 100644 index 000000000..e69de29bb -- Gitee