Compare commits

...

1 Commits
master ... test

Author SHA1 Message Date
youys 68ca328265 用于质量评分导出Excel
2 years ago

@ -63,6 +63,12 @@
<version>5.7.19</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>5.7.19</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>

@ -31,7 +31,7 @@ public class Metrics {
public String getComplexity() {
return complexity+"";
return complexity == null ? "0" : complexity+"";
}
public void setComplexity(Float complexity) {
@ -79,6 +79,9 @@ public class Metrics {
}
public String getViolations() {
if(lines <= 0){
return "-";
}
return violations;
}
@ -119,6 +122,9 @@ public class Metrics {
}
public String getBugs() {
if(lines <= 0){
return "-";
}
return bugs;
}
@ -159,6 +165,9 @@ public class Metrics {
}
public String getCode_smells() {
if(lines <= 0){
return "-";
}
return code_smells;
}

@ -234,7 +234,7 @@ public class SonarService {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
log.error("文件下载失败: {}-{}", zipUrl, zipPath);
log.error("文件下载失败: {}-{}", zipUrl, zipPath, e);
throw e;
}
@ -248,13 +248,14 @@ public class SonarService {
*/
public void scanExcel(String excelPath) {
int homeworkId = 20210101;
int homeworkId = 557155;
try {
List<Person> personList = ExcelUtil.readExcel(excelPath);
Map<String, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(Person::getUid));
log.info("共有:{}位学生提交的代码", collect.size());
collect.forEach((k, v) -> {
SonarRequest request = new SonarRequest();
request.setHomeworkId(homeworkId);

@ -1,7 +1,15 @@
package net.educoder.ecsonar.task;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import net.educoder.ecsonar.model.api.Person;
import net.educoder.ecsonar.model.api.SonarRequest;
import net.educoder.ecsonar.services.SonarService;
import net.educoder.ecsonar.utils.SslUtils;
import net.educoder.ecsonar.utils.UrlUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -10,8 +18,11 @@ import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
/**
* @Author: youys
@ -39,15 +50,15 @@ public class ReadExcelRunnable implements Runnable{
String path = String.format("/tmp/%d/%s/", homeworkId, uid);
File file = new File(path);
if(!file.exists()){
file.mkdir();
file.mkdirs();
}
// 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();
// } catch (Exception e) {
// log.error("download exception", e);
// }
// }
String key = String.format("%d-%s", homeworkId, uid);
@ -55,11 +66,13 @@ public class ReadExcelRunnable implements Runnable{
sonarService.sonar(path,key);
}
private void download(String zipUrl, String zipPath) throws IOException {
private void download(String zipUrl, String zipPath) throws Exception {
log.info("下载文件: {}-{}", zipUrl, zipPath);
String cookie = "_educoder_session=1f4bf34409fec2180b99aa1cbf0b7586";
String cookie = "_educoder_session=2237164bc8ff253aa43c8e089015da7a";
URL url = new URL(zipUrl);
SslUtils.ignoreSsl();
URLConnection conn = url.openConnection();
conn.setRequestProperty("Cookie", cookie);
conn.setDoInput(true);
@ -67,19 +80,37 @@ public class ReadExcelRunnable implements Runnable{
String fileName = conn.getHeaderField("Content-Disposition");
if(StringUtils.isNotEmpty(fileName)){
fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
fileName = URLDecoder.decode(fileName,"UTF-8");
fileName = fileName.substring(fileName.indexOf("filename=")+10,fileName.length()-1);
zipPath = zipPath.substring(0, zipPath.lastIndexOf("/")+1) + fileName;
}
try (BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
FileOutputStream fileOutputStream = new FileOutputStream(zipPath)) {
byte dataBuffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
log.error("文件下载失败: {}-{}", zipUrl, zipPath);
throw e;
// if(StringUtils.isNotEmpty(fileName)){
// fileName = zipUrl.substring(zipUrl.lastIndexOf("/")) + fileName.substring(fileName.lastIndexOf("."), fileName.length()-1);
// zipPath = zipPath.substring(0, zipPath.lastIndexOf("/")+1) + fileName;
// }
// try (BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
// FileOutputStream fileOutputStream = new FileOutputStream(zipPath)) {
// byte dataBuffer[] = new byte[1024];
// int bytesRead;
// while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
// fileOutputStream.write(dataBuffer, 0, bytesRead);
// }
// } catch (IOException e) {
// log.error("文件下载失败: {}-{}", zipUrl, zipPath);
// throw e;
// }
if(fileName.contains(".zip") || fileName.contains(".rar")){
String command1 = "unar " + zipPath + " -o " + zipPath.substring(0, zipPath.lastIndexOf("/")+1) + zipUrl.substring(zipUrl.lastIndexOf("/")+1);
String command2 = "rm -rf " + zipPath;
log.info("command1:{},command2:{}", command1, command2);
String s = RuntimeUtil.execForStr(command1);
// RuntimeUtil.execForStr(command2);
// String s = RuntimeUtil.execForStr("ls "+ zipPath.substring(0, zipPath.lastIndexOf("/")+1));
System.out.println(s);
// log.info("result1:{}, result2:{}", result, result2);
}
}

@ -0,0 +1,67 @@
package net.educoder.ecsonar.utils;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* @Author: youys
* @Date: 2022/5/30
* @Description:
*/
public class SslUtils {
private static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements TrustManager,X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
}
/**
* HTTPSSSLopenConnection
* @throws Exception
*/
public static void ignoreSsl() throws Exception{
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}

@ -43,7 +43,7 @@ zip.save.path=/tmp/
#excel.template.path=/Users/guange/work/java/ecsonar/src/main/resources/template.xlsx
excel.template.path=template.xlsx
sonar.url=http://127.0.0.1:9000
sonar.url=https://sonar.educoder.net
#sonar.url=http://117.50.14.123:9000
# token令牌
sonar.token=0253a518e824a976ea2f11aec17938cb0f8c0495

@ -15,6 +15,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
@ -67,8 +68,8 @@ public class EcsonarApplicationTests {
public void scanExcel(){
CountDownLatch countDownLatch = new CountDownLatch(1);
String excelPath = "/Users/youyongsheng/Desktop/res.xls";
// String excelPath = "/Users/youyongsheng/Desktop/res-test.xlsx";
String excelPath = "/Users/youyongsheng/Desktop/智擎/质量分析/2022-05-30/res.xls";
// String excelPath = "/Users/youyongsheng/Desktop/111111.xlsx";
sonarService.scanExcel(excelPath);
try {
@ -81,13 +82,17 @@ public class EcsonarApplicationTests {
@Test
public void writeExcel() throws Exception {
int homeworkId = 20210101;
String directory = "/tmp/20210101/";
int homeworkId = 557155;
String directory = "/tmp/557155/";
File file = new File(directory);
File[] files = file.listFiles();
System.out.println("学生数:" + files.length);
List<Person> personList = ExcelUtil.readExcel("/Users/youyongsheng/Desktop/res.xls");
List<Person> personList = ExcelUtil.readExcel("/Users/youyongsheng/Desktop/智擎/质量分析/2022-05-30/res.xls");
// List<Person> personList = new ArrayList<>();
// Person person = new Person();
// person.setUid("201805550117");
// person.setUid("201805550117");
Map<String, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(Person::getUid));
@ -100,7 +105,7 @@ public class EcsonarApplicationTests {
Metrics metrics = reportService.getMetrics(projectName);
String templatePath = this.getClass().getClassLoader().getResource("template1.xlsx").getPath();
String outPath = "/Users/youyongsheng/Desktop/202100000004.xlsx";
String outPath = "/Users/youyongsheng/Desktop/2022-05-30-111.xlsx";
System.out.println("第"+(i++)+"个学生," + uid);
if (CollectionUtils.isEmpty(collect.get(uid))) {
@ -121,5 +126,20 @@ public class EcsonarApplicationTests {
System.out.println("---------end------------");
}
@Test
public void writeOne() throws Exception {
String templatePath = this.getClass().getClassLoader().getResource("template1.xlsx").getPath();
String outPath = "/Users/youyongsheng/Desktop/2022-05-18-tlj.xlsx";
Metrics metrics = reportService.getMetrics("20220520-tanglijin");
reportService.writeSpecifyTemplateToExcel("201805550117","唐立锦"
,
metrics,
templatePath,
outPath);
}
}

Loading…
Cancel
Save