소스 검색

添加二次分析服务,开始与状态查询

LiHaoyu 5 년 전
부모
커밋
1974697782

+ 37 - 0
src/main/java/net/mooctest/www/android_auto_test/common/constant/enums/SecondaryStatus.java

@@ -0,0 +1,37 @@
+package net.mooctest.www.android_auto_test.common.constant.enums;
+
+import lombok.Getter;
+
+@Getter
+public enum SecondaryStatus {
+    /**
+     * 报告生成中
+     */
+    RUNNING("1", "报告生成中"),
+
+    UPLOADING("2", "报告上传中"),
+
+    RUN_FAILED("3", "报告生成失败"),
+
+    UPLOAD_FAILED("4", "报告上传失败"),
+
+    SUCCESS("5", "成功");
+
+
+    private String code;
+    private String description;
+
+    SecondaryStatus(String code, String des){
+        this.code = code;
+        this.description = des;
+    }
+
+    public static SecondaryStatus getStatusByCode(String code){
+        for (SecondaryStatus ts: values()){
+            if (ts.code.equals(code)){
+                return ts;
+            }
+        }
+        return null;
+    }
+}

+ 10 - 4
src/main/java/net/mooctest/www/android_auto_test/controller/SecondaryController.java

@@ -1,8 +1,7 @@
 package net.mooctest.www.android_auto_test.controller;
 
 import net.mooctest.www.android_auto_test.services.SecondaryService;
-import net.mooctest.www.android_auto_test.vo.TraceMetaInfo;
-import net.mooctest.www.android_auto_test.vo.TraceStatusResult;
+import net.mooctest.www.android_auto_test.vo.SecondaryResult;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
@@ -13,8 +12,15 @@ public class SecondaryController {
     SecondaryService secondaryService;
 
     @RequestMapping(value = "/api/v1/secondaryAnalysis", method = RequestMethod.POST)
-    public void runTest(@RequestParam(name = "traceId") String traceId,
+    public boolean doSecondaryTest(@RequestParam(name = "traceId") String traceId,
                         @RequestParam(name = "toolName") String toolName){
-        secondaryService.generateCrowdSourced(traceId, toolName);
+        secondaryService.generateSecondaryReport(traceId, toolName);
+        return true;
+    }
+
+    @RequestMapping(value = "/api/v1/secondaryAnalysis", method = RequestMethod.GET)
+    public SecondaryResult getStatus(@RequestParam(name = "traceId") String traceId,
+                                     @RequestParam(name = "toolName") String toolName){
+        return secondaryService.getSecondaryStatus(traceId, toolName);
     }
 }

+ 29 - 0
src/main/java/net/mooctest/www/android_auto_test/dao/RedisMappers/SecondaryStatusMap.java

@@ -0,0 +1,29 @@
+package net.mooctest.www.android_auto_test.dao.RedisMappers;
+
+import net.mooctest.www.android_auto_test.common.constant.enums.SecondaryStatus;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author henrylee
+ */
+@Component
+public class SecondaryStatusMap {
+    private final String TRACE_SECONDARY_PREFIX = "TRACE_SECONDARY_STATUS_%s_%s";
+
+    @Autowired
+    StringRedisTemplate redisTemplate;
+
+    public SecondaryStatus get(String traceId, String toolName){
+        String code = redisTemplate.opsForValue().get(String.format(TRACE_SECONDARY_PREFIX, traceId, toolName));
+        if (code == null){
+            return null;
+        }
+        return SecondaryStatus.getStatusByCode(code);
+    }
+
+    public void put(String traceId, String toolName, SecondaryStatus ss){
+        redisTemplate.opsForValue().set(String.format(TRACE_SECONDARY_PREFIX, traceId, toolName), ss.getCode());
+    }
+}

+ 1 - 1
src/main/java/net/mooctest/www/android_auto_test/services/Impl/AutoTestServiceImpl.java

@@ -146,7 +146,7 @@ public class AutoTestServiceImpl implements AutoTestService {
         extraInfo.put("deviceStatus", deviceStatus);
         traceResult.setExtraInfo(extraInfo);
         if (TraceStatus.FINISH.equals(currentStatus)){
-            String downloadUrl = ossService.getDadaJsonDownloadPath(traceId);
+            String downloadUrl = ossService.getDadaJsonDownloadPath(traceId, Consts.REPORT_FILE_NAME);
             traceResult.setDownloadUrl(downloadUrl);
         }
         return traceResult;

+ 2 - 2
src/main/java/net/mooctest/www/android_auto_test/services/Impl/OssServiceImpl.java

@@ -47,8 +47,8 @@ public class OssServiceImpl implements OssService {
     }
 
     @Override
-    public String getDadaJsonDownloadPath(String traceId) {
-        String filePath = Consts.TRACE_DATA_PATH + traceId + "/" + Consts.REPORT_FILE_NAME;
+    public String getDadaJsonDownloadPath(String traceId, String fileName) {
+        String filePath = Consts.TRACE_DATA_PATH + traceId + "/" + fileName;
         return buildHttpPath(filePath);
     }
 

+ 66 - 4
src/main/java/net/mooctest/www/android_auto_test/services/Impl/SecondaryServiceImpl.java

@@ -1,17 +1,35 @@
 package net.mooctest.www.android_auto_test.services.Impl;
 
+import net.mooctest.www.android_auto_test.common.constant.enums.SecondaryStatus;
 import net.mooctest.www.android_auto_test.common.exceptions.HttpNotFoundException;
+import net.mooctest.www.android_auto_test.dao.RedisMappers.SecondaryStatusMap;
+import net.mooctest.www.android_auto_test.services.OssService;
 import net.mooctest.www.android_auto_test.services.SecondaryService;
 import net.mooctest.www.android_auto_test.utils.AddressUtil;
+import net.mooctest.www.android_auto_test.utils.OsUtil;
+import net.mooctest.www.android_auto_test.utils.PrintUtil;
+import net.mooctest.www.android_auto_test.vo.SecondaryResult;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.io.File;
 
+/**
+ * @author henrylee
+ */
 @Service
 public class SecondaryServiceImpl implements SecondaryService {
 
+    public static final String TAG = Thread.currentThread() .getStackTrace()[1].getClassName();
+
+    @Autowired
+    SecondaryStatusMap secondaryStatusMap;
+
+    @Autowired
+    OssService ossService;
+
     @Override
-    public void generateCrowdSourced(String traceId, String toolName) {
+    public void generateSecondaryReport(String traceId, String toolName) {
         File traceDir = new File(AddressUtil.getTraceDir(traceId));
         if (!traceDir.exists()) {
             throw new HttpNotFoundException("任务不存在或信息已被清理,请重新测试");
@@ -20,6 +38,23 @@ public class SecondaryServiceImpl implements SecondaryService {
         t.start();
     }
 
+    @Override
+    public SecondaryResult getSecondaryStatus(String traceId, String toolName) {
+        SecondaryStatus ss = secondaryStatusMap.get(traceId, toolName);
+        if (ss == null){
+            throw new HttpNotFoundException("任务不存在或信息已被清理");
+        }
+        SecondaryResult sr = new SecondaryResult();
+        sr.setTraceId(traceId);
+        sr.setToolName(toolName);
+        sr.setStatusCode(ss.getCode());
+        sr.setDescription(ss.getDescription());
+        if (ss == SecondaryStatus.SUCCESS){
+            sr.setDownloadUrl(ossService.getDadaJsonDownloadPath(traceId, toolName + ".json"));
+        }
+        return sr;
+    }
+
     private class RunTraceThread extends Thread{
         private String traceId;
         private String toolName;
@@ -30,9 +65,36 @@ public class SecondaryServiceImpl implements SecondaryService {
 
         @Override
         public void run(){
-            //TODO 执行韦志宾的命令,然后生成文件,发给众测平台
-            System.out.println("Generate " + toolName + " for trace " + traceId);
-            System.out.println("Send Crowdsourced requirements to Mooctest.");
+            try {
+                secondaryStatusMap.put(traceId, toolName, SecondaryStatus.RUNNING);
+                PrintUtil.print(String.format("Generate bug report by %s.", toolName), TAG);
+                String command = String.format("java -jar tasks/%s.jar %s", toolName, traceId);
+                OsUtil.runCommand(command);
+                String reportName = toolName + ".json";
+                uploadReport(reportName);
+                secondaryStatusMap.put(traceId, toolName, SecondaryStatus.SUCCESS);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+
+        private void uploadReport(String reportName){
+            PrintUtil.print(String.format("Upload %s's report.", toolName), TAG);
+            secondaryStatusMap.put(traceId, toolName, SecondaryStatus.UPLOADING);
+            String path = AddressUtil.getReportJsonPath(traceId, reportName);
+            File file = new File(path);
+            if (!file.exists()) {
+                secondaryStatusMap.put(traceId, toolName, SecondaryStatus.RUN_FAILED);
+                PrintUtil.print(String.format("Tool %s generate report failed.", toolName), TAG);
+                throw new RuntimeException("报告生成失败");
+            }
+            try {
+                ossService.uploadFileToTraceDir(file, traceId, reportName);
+            } catch (Exception e){
+                secondaryStatusMap.put(traceId, toolName, SecondaryStatus.UPLOAD_FAILED);
+                PrintUtil.print(String.format("Tool %s upload report failed.", toolName), TAG);
+                throw e;
+            }
         }
     }
 }

+ 1 - 1
src/main/java/net/mooctest/www/android_auto_test/services/OssService.java

@@ -9,7 +9,7 @@ import java.io.InputStream;
 public interface OssService {
     String downloadFile(String downloadUrl, String traceId);
 
-    String getDadaJsonDownloadPath(String traceId);
+    String getDadaJsonDownloadPath(String traceId, String fileName);
 
     String uploadFileToTraceDir(File file, String traceId, String fileName);
 

+ 5 - 1
src/main/java/net/mooctest/www/android_auto_test/services/SecondaryService.java

@@ -1,6 +1,10 @@
 package net.mooctest.www.android_auto_test.services;
 
+import net.mooctest.www.android_auto_test.vo.SecondaryResult;
+
 public interface SecondaryService{
 
-    public void generateCrowdSourced(String traceId, String toolName);
+    void generateSecondaryReport(String traceId, String toolName);
+
+    SecondaryResult getSecondaryStatus(String traceId, String toolName);
 }

+ 5 - 1
src/main/java/net/mooctest/www/android_auto_test/utils/AddressUtil.java

@@ -15,7 +15,11 @@ public class AddressUtil {
     }
 
     public static String getDataJsonPath(String traceId){
-        return "tasks" + File.separator + traceId + File.separator + Consts.REPORT_FILE_NAME;
+        return getReportJsonPath(traceId, Consts.REPORT_FILE_NAME);
+    }
+
+    public static String getReportJsonPath(String traceId, String reportName){
+        return "tasks" + File.separator + traceId + File.separator + reportName;
     }
 
     public static String getTraceDir(String traceId){

+ 19 - 0
src/main/java/net/mooctest/www/android_auto_test/vo/SecondaryResult.java

@@ -0,0 +1,19 @@
+package net.mooctest.www.android_auto_test.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @author henrylee
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class SecondaryResult {
+    private String traceId;
+    private String toolName;
+    private String statusCode;
+    private String description;
+    private String downloadUrl;
+}