quality-analyse
youys 2 years ago
parent 6fde10384e
commit 4874377f89

@ -16,8 +16,9 @@ public class Constant {
public static final String CXX = "cpp"; public static final String CXX = "cpp";
public static final String CPP = "c++"; public static final String CPP = "c++";
public static final String PYTHON = "python"; public static final String PYTHON = "python";
public static final String OTHER = "other";
public static final List<String> language = Arrays.asList(JAVA, C, CXX, PYTHON); public static final List<String> language = Arrays.asList(JAVA, C, CXX, PYTHON, OTHER);
public static final String SUCCESS = "SUCCESS"; public static final String SUCCESS = "SUCCESS";

@ -9,6 +9,7 @@ import net.educoder.ecsonar.model.api.QualityInspectResultData;
import net.educoder.ecsonar.model.dto.AnalyseDetailDTO; import net.educoder.ecsonar.model.dto.AnalyseDetailDTO;
import net.educoder.ecsonar.model.dto.AnalyseDetailListDTO; import net.educoder.ecsonar.model.dto.AnalyseDetailListDTO;
import net.educoder.ecsonar.model.dto.CodeDetailDTO; import net.educoder.ecsonar.model.dto.CodeDetailDTO;
import net.educoder.ecsonar.model.dto.ProblemAnalysisDTO;
import net.educoder.ecsonar.model.vo.*; import net.educoder.ecsonar.model.vo.*;
import net.educoder.ecsonar.services.QualityInspectService; import net.educoder.ecsonar.services.QualityInspectService;
import net.educoder.ecsonar.utils.ResponseResult; import net.educoder.ecsonar.utils.ResponseResult;
@ -129,8 +130,8 @@ public class QualityInspectController {
@GetMapping("/problemAnalysis") @GetMapping("/problemAnalysis")
@ResponseBody @ResponseBody
public ResponseResult<String> problemAnalysis(@RequestParam Integer ruleId) { public ResponseResult<String> problemAnalysis(@RequestParam Integer ruleId) {
String description = qualityInspectService.getProblemAnalysis(ruleId); ProblemAnalysisDTO problemAnalysis = qualityInspectService.getProblemAnalysis(ruleId);
return ResponseResult.success(description); return ResponseResult.success(problemAnalysis);
} }

@ -41,4 +41,12 @@ public interface IssuesDao {
* @return * @return
*/ */
DegreeDTO queryDegree(String projectUuid, Integer issueType); DegreeDTO queryDegree(String projectUuid, Integer issueType);
/**
* id
* @param issueId
* @return
*/
Issues queryById(Long issueId);
} }

@ -43,12 +43,12 @@ public interface ProjectDao {
"(SELECT COALESCE(count(*),0) critical_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='CRITICAL')," + "(SELECT COALESCE(count(*),0) critical_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='CRITICAL')," +
"(SELECT COALESCE(count(*),0) major_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='MAJOR')," + "(SELECT COALESCE(count(*),0) major_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='MAJOR')," +
"(SELECT COALESCE(count(*),0) minor_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='MINOR')," + "(SELECT COALESCE(count(*),0) minor_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='MINOR')," +
"(select text_value bugs from project_measures where component_uuid = #{projectUuid} and metric_id = 89 order by id desc limit 1)," + "(select text_value bugs from project_measures where component_uuid = #{projectUuid} and metric_id = 80 order by id desc limit 1)," +
"(SELECT COALESCE(count(*),0) block_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='BLOCKER'), " + "(SELECT COALESCE(count(*),0) block_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='BLOCKER'), " +
"(SELECT COALESCE(count(*),0) critical_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='CRITICAL')," + "(SELECT COALESCE(count(*),0) critical_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='CRITICAL')," +
"(SELECT COALESCE(count(*),0) major_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='MAJOR')," + "(SELECT COALESCE(count(*),0) major_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='MAJOR')," +
"(SELECT COALESCE(count(*),0) minor_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='MINOR')," + "(SELECT COALESCE(count(*),0) minor_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='MINOR')," +
"(select text_value violations from project_measures where component_uuid = #{projectUuid} and metric_id = 93 order by id desc limit 1)") "(select text_value violations from project_measures where component_uuid = #{projectUuid} and metric_id = 80 order by id desc limit 1)")
Metrics selectMetricsByProjectUuid(String projectUuid); Metrics selectMetricsByProjectUuid(String projectUuid);

@ -44,6 +44,15 @@ public enum DegreeEnum {
throw new RuntimeException("Not Found DegreeEnum by type=" + type); throw new RuntimeException("Not Found DegreeEnum by type=" + type);
} }
public static DegreeEnum getDegreeEnumByValue(String value) {
for (DegreeEnum de : values()) {
if (de.value.equals(value)) {
return de;
}
}
throw new RuntimeException("Not Found DegreeEnum by value=" + value);
}
public Integer getType() { public Integer getType() {
return type; return type;

@ -1,11 +1,14 @@
package net.educoder.ecsonar.model; package net.educoder.ecsonar.model;
import lombok.Data;
/** /**
* @Author: youys * @Author: youys
* @Date: 2022/9/19 * @Date: 2022/9/19
* @Description: * @Description:
*/ */
@Data
public class Issues { public class Issues {
private Long id; private Long id;
@ -16,6 +19,7 @@ public class Issues {
private String status; private String status;
private String projectUuid; private String projectUuid;
private Integer issueType; private Integer issueType;
private Integer lines;
private byte[] locations = new byte[0]; private byte[] locations = new byte[0];
/** /**
@ -37,108 +41,9 @@ public class Issues {
*/ */
private String uuid; private String uuid;
/**
*
*/
private String language;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getKee() {
return kee;
}
public void setKee(String kee) {
this.kee = kee;
}
public Integer getRuleId() {
return ruleId;
}
public void setRuleId(Integer ruleId) {
this.ruleId = ruleId;
}
public String getSeverity() {
return severity;
}
public void setSeverity(String severity) {
this.severity = severity;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getProjectUuid() {
return projectUuid;
}
public void setProjectUuid(String projectUuid) {
this.projectUuid = projectUuid;
}
public Integer getIssueType() {
return issueType;
}
public void setIssueType(Integer issueType) {
this.issueType = issueType;
}
public byte[] getLocations() {
return locations;
}
public void setLocations(byte[] locations) {
this.locations = locations;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
} }

@ -2,6 +2,8 @@ package net.educoder.ecsonar.model;
import lombok.Data; import lombok.Data;
import java.util.Date;
/** /**
* @Author: youys * @Author: youys
* @Date: 2022/10/17 * @Date: 2022/10/17
@ -13,4 +15,24 @@ public class Rule {
private Long id; private Long id;
private String name; private String name;
private String description; private String description;
/**
*
*/
private String language;
/**
* 20min
*/
private String defRemediationBaseEffort;
/**
* ","
*/
private String tags;
/**
*
*/
private Long createTime;
} }

@ -3,7 +3,6 @@ package net.educoder.ecsonar.model.dto;
import lombok.Data; import lombok.Data;
import java.util.Date;
/** /**
* @Author: youys * @Author: youys
@ -28,15 +27,16 @@ public class AnalyseDetailListDTO {
/** /**
* *
*/ */
private String rowNumber; private Integer rowNumber;
/** /**
* *
*/ */
private String level; private String language;
/** /**
* *
*/ */
private Date detectTime; private String level;
/** /**
* *
@ -45,4 +45,6 @@ public class AnalyseDetailListDTO {
private Integer ruleId; private Integer ruleId;
private Long issueId;
} }

@ -21,4 +21,14 @@ public class CodeDetailDTO {
*/ */
private String example; private String example;
/**
*
*/
private String title;
/**
*
*/
private String errMessage;
} }

@ -0,0 +1,38 @@
package net.educoder.ecsonar.model.dto;
import lombok.Data;
import java.util.List;
/**
* @Author: youys
* @Date: 2022/12/22
* @Description:
*/
@Data
public class ProblemAnalysisDTO {
private String title;
private String example;
/**
*
*/
private String language;
/**
* 20min
*/
private String constantIssue;
/**
*
*/
private List<String> tags;
/**
*
*/
private String createTime;
}

@ -14,9 +14,9 @@ public class AnalyseDetailListVO extends PageVO {
/** /**
* *
* 1 bug * 1
* 2 * 2 bug
* 3 * 3
*/ */
private Integer type; private Integer type;

@ -13,4 +13,5 @@ public class CodeDetailVO {
private String uuid; private String uuid;
private Integer ruleId; private Integer ruleId;
private Long issueId;
} }

@ -1,5 +1,6 @@
package net.educoder.ecsonar.services; package net.educoder.ecsonar.services;
import cn.hutool.core.date.DateUtil;
import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.InvalidProtocolBufferException;
import net.educoder.ecsonar.dao.*; import net.educoder.ecsonar.dao.*;
import net.educoder.ecsonar.enums.AnalyseTypeEnum; import net.educoder.ecsonar.enums.AnalyseTypeEnum;
@ -28,6 +29,8 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -201,7 +204,7 @@ public class QualityInspectService {
private BigDecimal calcQualityScore(QualityInspectResultData resultData) { private BigDecimal calcQualityScore(QualityInspectResultData resultData) {
if (resultData.getTotalRowNumber() <= 0) { if (resultData.getTotalRowNumber() <= 0) {
return null; return BigDecimal.ZERO;
} }
// 100 -(阻断 * 10 + 严重 * 5 + 主要* 3 + 次要 * 1/ 总行数 * 100 // 100 -(阻断 * 10 + 严重 * 5 + 主要* 3 + 次要 * 1/ 总行数 * 100
@ -300,17 +303,33 @@ public class QualityInspectService {
return rollPage; return rollPage;
} }
public String getProblemAnalysis(Integer ruleId) { public ProblemAnalysisDTO getProblemAnalysis(Integer ruleId) {
Rule rule = projectDao.findRuleById(ruleId); Rule rule = projectDao.findRuleById(ruleId);
ProblemAnalysisDTO problemAnalysisDTO = new ProblemAnalysisDTO();
if (rule != null) { if (rule != null) {
String example = rule.getDescription().replaceAll("<p>", "<p style=\"color: #d50000;font-size: 17px;\">") String example = rule.getDescription().replaceAll("<p>", "<p style=\"color: #d50000;font-size: 17px;\">")
.replaceAll("Noncompliant Code Example", "错误代码示范") .replaceAll("Noncompliant Code Example", "错误代码示范")
.replaceAll("Compliant Solution", "正确代码示范") .replaceAll("Compliant Solution", "正确代码示范")
.replaceAll("Exceptions", "异常代码") .replaceAll("Exceptions", "异常代码")
.replaceAll("See", "链接"); .replaceAll("See", "链接");
return example;
problemAnalysisDTO.setTitle(rule.getName());
problemAnalysisDTO.setExample(example);
problemAnalysisDTO.setLanguage(rule.getLanguage());
problemAnalysisDTO.setConstantIssue(rule.getDefRemediationBaseEffort());
problemAnalysisDTO.setTags(Arrays.asList(rule.getTags().split(",")));
problemAnalysisDTO.setCreateTime(DateUtil.formatDateTime(new Date(rule.getCreateTime())));
}else{
problemAnalysisDTO.setTitle("");
problemAnalysisDTO.setExample("");
problemAnalysisDTO.setLanguage("");
problemAnalysisDTO.setConstantIssue("");
problemAnalysisDTO.setTags(new ArrayList<>());
problemAnalysisDTO.setCreateTime(DateUtil.formatTime(new Date()));
} }
return "";
return problemAnalysisDTO;
} }
/** /**
@ -336,7 +355,13 @@ public class QualityInspectService {
CodeDetailDTO codeDetail = new CodeDetailDTO(); CodeDetailDTO codeDetail = new CodeDetailDTO();
codeDetail.setCodes(fileSourceDTOList); codeDetail.setCodes(fileSourceDTOList);
codeDetail.setExample(getProblemAnalysis(codeDetailVO.getRuleId()));
Issues issues = issuesDao.queryById(codeDetailVO.getIssueId());
ProblemAnalysisDTO problemAnalysis = getProblemAnalysis(codeDetailVO.getRuleId());
codeDetail.setExample(problemAnalysis.getExample());
codeDetail.setTitle(problemAnalysis.getTitle());
codeDetail.setErrMessage(issues.getMessage());
return codeDetail; return codeDetail;
@ -353,14 +378,17 @@ public class QualityInspectService {
detailListDTO.setName(pageIssue.getName()); detailListDTO.setName(pageIssue.getName());
detailListDTO.setDescription(pageIssue.getMessage()); detailListDTO.setDescription(pageIssue.getMessage());
detailListDTO.setIssueId(pageIssue.getId());
detailListDTO.setUuid(pageIssue.getUuid()); detailListDTO.setUuid(pageIssue.getUuid());
detailListDTO.setRuleId(pageIssue.getRuleId()); detailListDTO.setRuleId(pageIssue.getRuleId());
detailListDTO.setFilePath(pageIssue.getPath()); detailListDTO.setFilePath(pageIssue.getPath());
detailListDTO.setLanguage(pageIssue.getLanguage());
detailListDTO.setLevel(DegreeEnum.getDegreeEnumByValue(pageIssue.getSeverity()).getDesc());
try { try {
DbIssues.Locations locations = DbIssues.Locations.parseFrom(pageIssue.getLocations()); DbIssues.Locations locations = DbIssues.Locations.parseFrom(pageIssue.getLocations());
detailListDTO.setRowNumber(String.valueOf(locations.getTextRange().getStartLine())); detailListDTO.setRowNumber(locations.getTextRange().getStartLine());
} catch (InvalidProtocolBufferException e) { } catch (InvalidProtocolBufferException e) {
detailListDTO.setRowNumber("0"); detailListDTO.setRowNumber(0);
logger.error("Fail to read ISSUES.LOCATIONS [KEE=%s]", e); logger.error("Fail to read ISSUES.LOCATIONS [KEE=%s]", e);
} }
} }

@ -59,6 +59,7 @@ public class QualityInspectRunnable implements Runnable {
String path = String.format("/tmp/%s/%s/", homeworkId, projectName); String path = String.format("/tmp/%s/%s/", homeworkId, projectName);
List<Long> codeIds = userData.getCodeIds(); List<Long> codeIds = userData.getCodeIds();
boolean constantCFlag = true,constantCPPFlag = true,constantJavaFlag = true, constantPyFlag = true;
for (Long codeId : codeIds) { for (Long codeId : codeIds) {
File file = new File(path); File file = new File(path);
try { try {
@ -72,6 +73,27 @@ public class QualityInspectRunnable implements Runnable {
continue; continue;
} }
FileUtil.writeString(gameCodes.getCode(), path + gameCodes.getPath(), Charset.forName("UTF-8")); FileUtil.writeString(gameCodes.getCode(), path + gameCodes.getPath(), Charset.forName("UTF-8"));
if (constantCFlag && gameCodes.getPath().toLowerCase().contains(".c")) {
constantCFlag = false;
} else if (constantCPPFlag && gameCodes.getPath().toLowerCase().contains(".cpp")) {
constantCPPFlag = false;
} else if (constantJavaFlag && gameCodes.getPath().toLowerCase().contains(".java")) {
constantJavaFlag = false;
} else if (constantPyFlag && gameCodes.getPath().toLowerCase().contains(".py")) {
constantPyFlag = false;
}
}
// 需要自己判别语言,Java优先
if(!constantJavaFlag){
language = Constant.JAVA;
}else if(!constantPyFlag){
language = Constant.PYTHON;
} else if(!constantCFlag){
language = Constant.C;
}else if(!constantCPPFlag){
language = Constant.CXX;
} }
// 写完所有文件开始用sonar进行质量分析 // 写完所有文件开始用sonar进行质量分析

@ -23,7 +23,7 @@
<select id="getPageIssues" resultType="net.educoder.ecsonar.model.Issues"> <select id="getPageIssues" resultType="net.educoder.ecsonar.model.Issues">
select select
i.id,i.kee,i.rule_id ruleId,i.severity,i.status,i.project_uuid projectUuid,i.issue_type issueType,i.locations,r.name,r.description,p.path,p.uuid,i.message i.id,i.kee,i.rule_id ruleId,i.severity,i.status,i.project_uuid projectUuid,i.issue_type issueType,i.locations,i.line lines,r.name,r.description,r.language,p.path,p.uuid,i.message
from issues i from issues i
inner join rules r on i.rule_id =r.id inner join rules r on i.rule_id =r.id
inner join projects p on p.uuid =i.component_uuid inner join projects p on p.uuid =i.component_uuid
@ -49,4 +49,9 @@
</select> </select>
<select id="queryById" parameterType="java.lang.Long" resultType="net.educoder.ecsonar.model.Issues">
select i.id,i.kee,i.rule_id ruleId,i.severity,i.status,i.project_uuid projectUuid,i.issue_type issueType,i.locations,i.line lines,i.message from issues i where id=#{issueId}
</select>
</mapper> </mapper>

@ -7,7 +7,11 @@
select select
id, id,
name, name,
description description,
language,
def_remediation_base_effort defRemediationBaseEffort,
system_tags tags,
created_at createTime
from rules where id=#{id,jdbcType=INTEGER} from rules where id=#{id,jdbcType=INTEGER}
</select> </select>

Loading…
Cancel
Save