test
youys 3 years ago
parent f2ed9b2fed
commit 53fe884463

@ -2,6 +2,7 @@ package net.educoder.ecsonar.model;
public class Metrics {
// 复杂度
private String complexity;
private int lines;

@ -31,7 +31,7 @@ public class ReportService {
public Metrics getMetrics(String name) {
Project project = projectDao.findByName(name);
if (project==null){
return null;
return getDefault();
}
String uuid = project.getProject_uuid();
@ -57,6 +57,29 @@ public class ReportService {
return metrics;
}
public Metrics getDefault(){
Metrics metrics = new Metrics();
metrics.setComplexity("0");
metrics.setLines(0);
metrics.setBlock_bugs(0);
metrics.setBlock_code_smells(0);
metrics.setBlock_violations(0);
metrics.setCritical_bugs(0);
metrics.setCritical_code_smells(0);
metrics.setCritical_violations(0);
metrics.setMajor_bugs(0);
metrics.setMajor_code_smells(0);
metrics.setMajor_violations(0);
metrics.setMinor_bugs(0);
metrics.setMinor_code_smells(0);
metrics.setMinor_violations(0);
metrics.setBugs("A");
metrics.setViolations("A");
metrics.setCode_smells("A");
return metrics;
}
/**
* excel
@ -70,6 +93,22 @@ public class ReportService {
ExcelUtil.saveExcel(workbook,outPath);
}
/**
*
* @param stuId
* @param name
* @param metrics
* @param templatePath
* @param outPath
* @throws Exception
*/
public void writeSpecifyTemplateToExcel(String stuId, String name,
Metrics metrics, String templatePath, String outPath) throws Exception {
Workbook workbook = ExcelUtil.getWorkbok(new File(templatePath));
ExcelUtil.writeToTemplateOne(metrics, stuId, name,workbook);
ExcelUtil.saveExcel(workbook,outPath);
}
/**
*
*

@ -36,8 +36,8 @@ public class SonarService {
@Value("${sonar.url}")
String sonarUrl;
// @Value("${sonar.token}")
// String sonarToken;
@Value("${sonar.token}")
String sonarToken;
@Value("${extract.path}")
String extractProgramPath;
@ -120,12 +120,14 @@ public class SonarService {
public void sonar(String projectPath, String key) {
String command = "source ~/.bash_profile && sonar-scanner " +
String command = "cppcheck --xml --xml-version=2 --enable=all "+projectPath+" 2> /tmp/"+key+"-result.xml &&" +
"source ~/.bash_profile && sonar-scanner " +
"-Dsonar.host.url=" + sonarUrl + " " +
"-Dsonar.sourceEncoding=utf-8 " +
"-Dsonar.projectKey=" + key + " " +
// "-Dsonar.login=" + sonarToken + " " +
"-Dsonar.login=" + sonarToken + " " +
"-Dsonar.java.binaries=./ " +
"-Dsonar.cxx.cppcheck.reportPath=/tmp/"+ key+"-result.xml " +
"-Dsonar.projectBaseDir=" + projectPath;
log.info("projectPath:{},key:{}, command: {}", projectPath,key, command);
SystemUtil.executeAndGetExitStatus(command);

@ -44,15 +44,15 @@ public class ReadExcelRunnable implements Runnable{
if(!file.exists()){
file.mkdir();
}
for (Person person : sonarRequest.getPersonList()) {
String[] split = StringUtils.split(person.getDownloadUrl(), "/");
String zipFilename = UrlUtil.getURLDecoderString(split[split.length - 1]);
try {
download(person.getDownloadUrl(),path+zipFilename);
} catch (IOException e) {
e.printStackTrace();
}
}
// for (Person person : sonarRequest.getPersonList()) {
// String[] split = StringUtils.split(person.getDownloadUrl(), "/");
// String zipFilename = UrlUtil.getURLDecoderString(split[split.length - 1]);
// try {
// download(person.getDownloadUrl(),path+zipFilename);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
String key = String.format("%d-%s", homeworkId, uid);
log.info("开始调用sonar:{}",key);
sonarService.sonar(path,key);

@ -3,6 +3,7 @@ package net.educoder.ecsonar.utils;
import net.educoder.ecsonar.model.Metrics;
import net.educoder.ecsonar.model.api.Person;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -18,6 +19,11 @@ public class ExcelUtil {
private static final String EXCEL_XLS = "xls";
private static final String EXCEL_XLSX = "xlsx";
private static final String SCORE ="IFERROR(分数转化说明!B$4*V#line+分数转化说明!G$4*W#line+分数转化说明!L$4*X#line,\" \")";
private static final String BUG ="IFERROR(100-SUMPRODUCT(代码检测结果!D#line:G#line,分数转化说明!B$6:E$6)/代码检测结果!$T#line*100,\" \")";
private static final String VULNERABILITY ="IFERROR(100-SUMPRODUCT(代码检测结果!I#line:L#line,分数转化说明!G$6:J$6)/代码检测结果!$T#line*100,\" \")";
private static final String CODE_SMELL ="IFERROR(100-SUMPRODUCT(代码检测结果!N#line:Q#line,分数转化说明!L$6:O$6)/代码检测结果!$T#line*100,\" \")";
/**
*
@ -62,6 +68,8 @@ public class ExcelUtil {
// sheet 对应一个工作页
Sheet sheet = workBook.getSheetAt(0);
/**
*
*/
@ -75,6 +83,7 @@ public class ExcelUtil {
if (row != null) {
Cell cell = row.getCell(1);
cell.setCellType(CellType.STRING);
if (StringUtils.isEmpty(cell.getStringCellValue())
|| StringUtils.equals(cell.getStringCellValue(), uid)) {
@ -175,6 +184,161 @@ public class ExcelUtil {
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getLines());
}
/**
* template1excel
* @param metrics
* @param uid
* @param name
* @param workBook
* @throws IOException
*/
public static void writeToTemplateOne(Metrics metrics, String uid,
String name,
Workbook workBook) throws IOException {
// sheet 对应一个工作页
Sheet sheet = workBook.getSheetAt(0);
sheet.setForceFormulaRecalculation(true);
CellStyle cellStyle = workBook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
/**
*
*/
int rowNumber = sheet.getLastRowNum(); // 第一行从0开始算
//从第三行开始
Row newRow = null;
for (int i = 3; i < rowNumber+1; ++i) {
Row row = sheet.getRow(i);
if (row != null) {
Cell cell = row.getCell(1);
cell.setCellType(CellType.STRING);
if (StringUtils.isEmpty(cell.getStringCellValue())
|| StringUtils.equals(cell.getStringCellValue(), uid)) {
newRow = row;
break;
}
}
}
if (newRow == null) {
newRow = sheet.createRow(rowNumber+1);
}
//一直往下找,直到找到空的,或者与学号匹配的行,进行写入
//学号
Cell uidCell = newRow.createCell(1);
uidCell.setCellType(CellType.STRING);
uidCell.setCellValue(uid);
Cell cell2 = newRow.createCell(2);
cell2.setCellType(CellType.STRING);
cell2.setCellValue(name);
Cell cell3 = newRow.createCell(3);
cell3.setCellType(CellType.NUMERIC);
cell3.setCellValue(metrics.getBlock_bugs());
Cell cell4 = newRow.createCell(4);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getCritical_bugs());
cell4 = newRow.createCell(5);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getMajor_bugs());
cell4 = newRow.createCell(6);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getMinor_bugs());
cell4 = newRow.createCell(7);
cell4.setCellType(CellType.STRING);
cell4.setCellValue(metrics.getBugs());
cell4 = newRow.createCell(8);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getBlock_violations());
cell4 = newRow.createCell(9);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getCritical_violations());
cell4 = newRow.createCell(10 );
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getMajor_violations());
cell4 = newRow.createCell(11 );
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getMinor_violations());
cell4 = newRow.createCell(12);
cell4.setCellType(CellType.STRING);
cell4.setCellValue(metrics.getViolations());
cell4 = newRow.createCell(13);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getBlock_code_smells());
cell4 = newRow.createCell(14);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getCritical_code_smells());
cell4 = newRow.createCell(15);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getMajor_violations());
cell4 = newRow.createCell(16);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getMinor_code_smells());
cell4 = newRow.createCell(17);
cell4.setCellType(CellType.STRING);
cell4.setCellValue(metrics.getCode_smells());
cell4 = newRow.createCell(18);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getComplexity());
cell4 = newRow.createCell(19);
cell4.setCellType(CellType.NUMERIC);
cell4.setCellValue(metrics.getLines());
String rowStr = String.valueOf(rowNumber+2);
cell4 = newRow.createCell(20);
cell4.setCellType(CellType.FORMULA);
cell4.setCellStyle(cellStyle);
cell4.setCellFormula(SCORE.replaceAll("#line", rowStr));
cell4 = newRow.createCell(21);
cell4.setCellType(CellType.FORMULA);
cell4.setCellStyle(cellStyle);
cell4.setCellFormula(BUG.replaceAll("#line", rowStr));
cell4 = newRow.createCell(22);
cell4.setCellType(CellType.FORMULA);
cell4.setCellStyle(cellStyle);
cell4.setCellFormula(VULNERABILITY.replaceAll("#line", rowStr));
cell4 = newRow.createCell(23);
cell4.setCellType(CellType.FORMULA);
cell4.setCellStyle(cellStyle);
cell4.setCellFormula(CODE_SMELL.replaceAll("#line", rowStr));
}

@ -1,7 +1,7 @@
#spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/sonar7.7
spring.datasource.url=jdbc:postgresql://117.50.14.123:5432/sonar
spring.datasource.username=sonar
spring.datasource.password=sonar
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/sonar7.7
#spring.datasource.url=jdbc:postgresql://117.50.14.123:5432/sonar
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.postgresql.Driver

Binary file not shown.

@ -100,7 +100,7 @@ public class EcsonarApplicationTests {
Metrics metrics = reportService.getMetrics(projectName);
String templatePath = this.getClass().getClassLoader().getResource("template1.xlsx").getPath();
String outPath = "/Users/youyongsheng/Desktop/20210101.xlsx";
String outPath = "/Users/youyongsheng/Desktop/202100000004.xlsx";
System.out.println("第"+(i++)+"个学生," + uid);
if (CollectionUtils.isEmpty(collect.get(uid))) {
@ -109,12 +109,12 @@ public class EcsonarApplicationTests {
continue;
}
reportService.writeToExcel(uid,
reportService.writeSpecifyTemplateToExcel(uid,
collect.get(uid).get(0).getName(),
metrics,
outPath,
outPath,
1);
i == 2? templatePath : outPath,
outPath);
}

Loading…
Cancel
Save