Prechádzať zdrojové kódy

读取oss文件,保存bugDetail

xujiawei 4 rokov pred
rodič
commit
d99044b60f

+ 9 - 0
src/main/java/edu/nju/controller/DataController.java

@@ -29,4 +29,13 @@ public class DataController {
     public List<BugDetail> getBugDetailByCaseId(String caseId) {
         return dataService.getBugDetailByCaseId(caseId);
     }
+
+
+    @RequestMapping(value = "/inputFromOSS")
+    @ResponseBody
+    public List<BugDetail> saveBugDetailFromOss(String filePath,String originalCaseId) {
+        return dataService.saveBugDetailFromOss(filePath,originalCaseId);
+    }
+
+
 }

+ 31 - 0
src/main/java/edu/nju/dao/BugDetailDao.java

@@ -0,0 +1,31 @@
+package edu.nju.dao;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.mongodb.core.MongoOperations;
+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.Repository;
+
+import edu.nju.entities.BugDetail;
+
+/**
+ * @Author JiaWei Xu
+ * @Date 2020-12-28 15:24
+ * @Email xjwhhh233@outlook.com
+ */
+@Repository
+public class BugDetailDao {
+    @Autowired
+    private MongoOperations mongoOperations;
+
+    //save存在则更新,不存在则插入
+    public String save(BugDetail bugDetail) {
+        mongoOperations.save(bugDetail);
+        return bugDetail.getId();
+    }
+}

+ 48 - 1
src/main/java/edu/nju/entities/BugDetail.java

@@ -1,6 +1,7 @@
 package edu.nju.entities;
 
 import org.springframework.data.annotation.Id;
+import org.springframework.data.annotation.PersistenceConstructor;
 import org.springframework.data.mongodb.core.mapping.Document;
 
 import java.util.List;
@@ -64,7 +65,7 @@ public class BugDetail implements java.io.Serializable {
     private String caseRequireDoc;
 
 
-    //自己的case_take_id
+    //原系统中的case_take_id
     private String caseTakeId;
     //与目前系统中的哪个case对应
     private String originalCaseId;
@@ -72,6 +73,52 @@ public class BugDetail implements java.io.Serializable {
     //cp系统序列号
     private String cpSerialNum;
 
+    public BugDetail(){};
+
+    @PersistenceConstructor
+    public BugDetail(String id, String bugCategory, String severity, String recurrent, String bugCreateTime, String bugPage, String title, String bugDescription, String imgUrl, int score, String parent, List<String> children, String root, int goodNum, Set<String> goodWorkerId, int badNum, Set<String> badWorkerId, String testCaseId, String testCaseName, String testCaseFront, String testCaseBehind, String testCaseDescription, String testCaseCreateTime, String reportId, String reportName, String reportCreateTime, String scriptLocation, String reportLocation, String logLocation, String deviceModel, String deviceBrand, String deviceOs, String workerId, String caseAppName, String casePaperType, String caseTestType, String caseDescription, String caseRequireDoc, String caseTakeId, String originalCaseId, String cpSerialNum) {
+        this.id = id;
+        this.bugCategory = bugCategory;
+        this.severity = severity;
+        this.recurrent = recurrent;
+        this.bugCreateTime = bugCreateTime;
+        this.bugPage = bugPage;
+        this.title = title;
+        this.bugDescription = bugDescription;
+        this.imgUrl = imgUrl;
+        this.score = score;
+        this.parent = parent;
+        this.children = children;
+        this.root = root;
+        this.goodNum = goodNum;
+        this.goodWorkerId = goodWorkerId;
+        this.badNum = badNum;
+        this.badWorkerId = badWorkerId;
+        this.testCaseId = testCaseId;
+        this.testCaseName = testCaseName;
+        this.testCaseFront = testCaseFront;
+        this.testCaseBehind = testCaseBehind;
+        this.testCaseDescription = testCaseDescription;
+        this.testCaseCreateTime = testCaseCreateTime;
+        this.reportId = reportId;
+        this.reportName = reportName;
+        this.reportCreateTime = reportCreateTime;
+        this.scriptLocation = scriptLocation;
+        this.reportLocation = reportLocation;
+        this.logLocation = logLocation;
+        this.deviceModel = deviceModel;
+        this.deviceBrand = deviceBrand;
+        this.deviceOs = deviceOs;
+        this.workerId = workerId;
+        this.caseAppName = caseAppName;
+        this.casePaperType = casePaperType;
+        this.caseTestType = caseTestType;
+        this.caseDescription = caseDescription;
+        this.caseRequireDoc = caseRequireDoc;
+        this.caseTakeId = caseTakeId;
+        this.originalCaseId = originalCaseId;
+        this.cpSerialNum = cpSerialNum;
+    }
 
     public String getId() {
         return id;

+ 50 - 12
src/main/java/edu/nju/service/DataService.java

@@ -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");
+        }
     }
 
 }