|
@@ -1,20 +1,21 @@
|
|
|
package edu.nju.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.aliyun.oss.OSS;
|
|
|
-import com.google.gson.JsonArray;
|
|
|
-import com.google.gson.JsonObject;
|
|
|
import edu.nju.dao.*;
|
|
|
import edu.nju.entities.*;
|
|
|
import edu.nju.util.OssAliyun;
|
|
|
import edu.nju.util.TransUtil;
|
|
|
import org.json.JSONArray;
|
|
|
-import org.json.JSONObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashSet;
|
|
@@ -52,6 +53,9 @@ public class DataService {
|
|
|
@Autowired
|
|
|
BugHistoryDao bugHistoryDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ BugDetailDao bugDetailDao;
|
|
|
+
|
|
|
@Value("${cpSerialNum}")
|
|
|
private String cpSerialNum;
|
|
|
|
|
@@ -165,11 +169,41 @@ public class DataService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- bugDetailToCsv(bugDetailList,caseId);
|
|
|
+ bugDetailToFile(bugDetailList,caseId);
|
|
|
return bugDetailList;
|
|
|
}
|
|
|
|
|
|
- public void bugDetailToCsv(List<BugDetail> bugDetailList,String caseId) {
|
|
|
+ public List<BugDetail> saveBugDetailFromOss(String filePath,String originalCaseId){
|
|
|
+ try {
|
|
|
+ //从oss下载文件
|
|
|
+ URL url = new URL(filePath);
|
|
|
+ URLConnection urlConnection = url.openConnection();
|
|
|
+ HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
|
|
|
+ httpURLConnection.setConnectTimeout(1000*5);
|
|
|
+ httpURLConnection.setRequestProperty("Charset", "UTF-8");
|
|
|
+ httpURLConnection.connect();
|
|
|
+ BufferedInputStream bufferedInputStream = new BufferedInputStream(httpURLConnection.getInputStream());
|
|
|
+ ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
|
|
+ int result = bufferedInputStream.read();
|
|
|
+ while(result != -1) {
|
|
|
+ buf.write((byte) result);
|
|
|
+ result = bufferedInputStream.read();
|
|
|
+ }
|
|
|
+ String json= buf.toString();
|
|
|
+ //转为bugDetail
|
|
|
+ List<BugDetail> bugDetailList = JSON.parseArray(json, BugDetail.class);
|
|
|
+ for(BugDetail bugDetail:bugDetailList){
|
|
|
+ bugDetail.setOriginalCaseId(originalCaseId);
|
|
|
+ bugDetailDao.save(bugDetail);
|
|
|
+ }
|
|
|
+ return bugDetailList;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void bugDetailToFile(List<BugDetail> bugDetailList, String caseId) {
|
|
|
String[] titles = {"bug_id", "bug_category", "severity", "recurrent", "bug_create_time", "bug_page", "title",
|
|
|
"description", "img_url",
|
|
|
"score", "parent", "children", "root", "good_num", "good_worker_id", "bad_num", "bad_worker_id",
|
|
@@ -184,9 +218,9 @@ public class DataService {
|
|
|
uploadToOss(jsonFile);
|
|
|
}
|
|
|
|
|
|
- public File exportJson(List<BugDetail> bugDetailList,String caseId){
|
|
|
+ private File exportJson(List<BugDetail> bugDetailList, String caseId){
|
|
|
try {
|
|
|
- File file = new File("data/" + caseId + ".json");
|
|
|
+ File file = new File("data/output" + caseId + ".json");
|
|
|
JSONArray jsonArray=new JSONArray(bugDetailList);
|
|
|
Writer write = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
|
|
|
write.write(jsonArray.toString());
|
|
@@ -200,9 +234,9 @@ public class DataService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public <T> File exportCsv(String[] titles, List<T> list,String caseId) {
|
|
|
+ private <T> File exportCsv(String[] titles, List<T> list,String caseId) {
|
|
|
try {
|
|
|
- File file = new File("data/"+caseId+".csv");
|
|
|
+ File file = new File("data/output"+caseId+".csv");
|
|
|
//构建输出流,同时指定编码
|
|
|
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
|
|
|
//csv文件是逗号分隔,除第一个外,每次写入一个单元格数据后需要输入逗号
|
|
@@ -241,9 +275,13 @@ public class DataService {
|
|
|
}
|
|
|
|
|
|
private void uploadToOss(File file){
|
|
|
- OSS ossClient= OssAliyun.initHangZhouOss();
|
|
|
- String objectName="crowd-dataset/"+file.getName();
|
|
|
- OssAliyun.uploadFile(ossClient,bucketName,objectName,file);
|
|
|
+ if(file!=null) {
|
|
|
+ OSS ossClient = OssAliyun.initHangZhouOss();
|
|
|
+ String objectName = "crowd-dataset/" + file.getName();
|
|
|
+ OssAliyun.uploadFile(ossClient, bucketName, objectName, file);
|
|
|
+ }else{
|
|
|
+ System.out.println("file is null");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|