Pārlūkot izejas kodu

Merge branch 'feature-progress' into 'DEV'

Feature progress



See merge request !504

huangyong 7 gadi atpakaļ
vecāks
revīzija
2df6c8a60e

+ 10 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/ctrl/ExamController.java

@@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.util.HashMap;
 import java.util.List;
 
 /**
@@ -184,6 +185,15 @@ public class ExamController extends BaseController {
         return similarityLogic.getTopSimilarResultInCase(examId, caseId, type, userId);
     }
 
+    @RequestMapping(value = UrlConstants.API + "exam/progress/{examId}/{caseId}/{userId}", method = RequestMethod.GET)
+    public HashMap<String, int[]> getMatrixOfProgress(@PathVariable("examId") Long examId, @PathVariable("caseId") Long caseId, @PathVariable("userId") Long userId, @RequestParam(name = "type") String type ) {
+        if(userId==null || caseId==null || type==null) {
+            throw new IllegalArgumentException("缺少必要参数");
+        }
+        return similarityLogic.getMatrixOfProgress(examId, caseId, type, userId);
+    }
+
+
     private Pageable getPageInfo(String sortOrder, String sortBy,HttpServletRequest request) {
         String activePage = request.getHeader("activePage");
         String rowsOnPage = request.getHeader("rowsOnPage");

+ 3 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/SimilarityLogic.java

@@ -2,6 +2,7 @@ package cn.iselab.mooctest.site.web.logic;
 
 import cn.iselab.mooctest.site.web.data.SimilarityResultVO;
 
+import java.util.HashMap;
 import java.util.List;
 
 /**
@@ -14,4 +15,6 @@ public interface SimilarityLogic {
 
     public List<SimilarityResultVO> getTopSimilarResultInCase(Long examId, Long caseId, String type, Long userId);
 
+    public HashMap<String, int[]> getMatrixOfProgress(Long examId, Long caseId, String type, Long userId);
+
 }

+ 96 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/impl/SimilarityLogicImpl.java

@@ -1,6 +1,8 @@
 package cn.iselab.mooctest.site.web.logic.impl;
 
+import cn.iselab.mooctest.site.models.Task;
 import cn.iselab.mooctest.site.service.CaseGraphService;
+import cn.iselab.mooctest.site.service.ExamService;
 import cn.iselab.mooctest.site.service.UserCatchService;
 import cn.iselab.mooctest.site.web.data.SimilarityResultVO;
 import cn.iselab.mooctest.site.web.data.UserVO;
@@ -12,6 +14,7 @@ import cn.iselab.mooctest.site.web.logic.UserLogic;
 import com.google.common.collect.Ordering;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import java.sql.Timestamp;
 
 import java.util.*;
 import java.util.stream.Collectors;
@@ -31,6 +34,11 @@ public class SimilarityLogicImpl implements SimilarityLogic{
     @Autowired
     private UserLogic userLogic;
 
+    @Autowired
+    private ExamService examService;
+
+    private static int NUMBEROFUNIS = 40;
+
     @Override
     public List<SimilarityResultVO> getTopSimilarResultInCase(Long examId, Long caseId, String type) {
         List<SimilarityResultVO> result = new LinkedList<>();
@@ -87,6 +95,94 @@ public class SimilarityLogicImpl implements SimilarityLogic{
         return result;
     }
 
+    @Override
+    public HashMap<String, int[]> getMatrixOfProgress(Long examId, Long caseId, String type, Long userId) {
+        HashMap<String, int[]> result = new HashMap<>();
+        List<Node> nodeList = caseGraphService.getCaseGraph(caseId).getNodes().stream().filter(node -> node.getCategory().equals(type)).collect(Collectors.toList());
+        Task task = examService.getExamByIdAndParticipantIdIfPermited(examId, userId);
+        Set<String> nodeNameSet = new HashSet<>();
+        for(Node node: nodeList) {
+            nodeNameSet.add(node.getName());
+        }
+        List<UserCatchDTO> userCatchDTOS = userCatchService.getUserCatchDTOs(userId, examId, caseId);
+        if(userCatchDTOS.size()==0 || userCatchDTOS.get(0).getCatchDTOs().size()==0) {
+            return result;
+        }
+        List<CatchDTO> catchDTOS = userCatchDTOS.get(0).getCatchDTOs().stream().filter(node -> node.getCategory().equals(type)).collect(Collectors.toList());
+        List<String> sortedTimestamp = getSortedTimeStamp(catchDTOS, type);
+        HashMap<String, int[]> initMatrix = getNodeTimeInitMatrix(sortedTimestamp, catchDTOS, nodeNameSet);
+        result = getExtendedMatrix(task.getBeginTime(), task.getEndTime(), initMatrix, sortedTimestamp);
+        return result;
+    }
+
+    // 现在的需求是根据examId caseId userId type 得到一个二位矩阵,具体的形式用HashMap表达
+    // 根据type获得所有的node
+    // 初始化矩阵
+    //
+
+    private List<String> getSortedTimeStamp(List<CatchDTO> catchDTOS, String type) {
+        List<String> result = null;
+        Set<String> timestampSet = new HashSet<>();
+        for(CatchDTO catchDTO: catchDTOS) {
+            timestampSet.add(catchDTO.getUploadTime());
+        }
+        result = new ArrayList<String>(timestampSet);
+        Collections.sort(result);
+        return result;
+    }
+
+    private HashMap<String, int[]> getNodeTimeInitMatrix(List<String> sortedTimestamp,  List<CatchDTO> resourceCatchDTOS, Set<String> nodeNameSet) {
+        HashMap<String, int[]> initMatrix = new HashMap<String, int[]>();
+        for(String nodeName: nodeNameSet) {
+            initMatrix.put(nodeName, new int[sortedTimestamp.size()]);
+        }
+        for(CatchDTO catchDTO: resourceCatchDTOS) {
+            initMatrix.get(catchDTO.getNodeName())[sortedTimestamp.indexOf(catchDTO.getUploadTime())] = 1;
+        }
+        return initMatrix;
+    }
+
+    private HashMap<String, int[]> getExtendedMatrix(Timestamp startTime, Timestamp endTime, HashMap<String, int[]> initMatrix, List<String> sortedTimestamp) {
+        HashMap<String, int[]> result = new HashMap<>();
+        Long[] stamp = new Long[NUMBEROFUNIS];
+        Long start = startTime.getTime();
+        Long end = endTime.getTime();
+        Long unit = (end - start) / NUMBEROFUNIS;
+        for(int i=0; i< NUMBEROFUNIS; i++) {
+            stamp[i] = start + unit * (i+1);
+        }
+        for(String nodeName: initMatrix.keySet()) {
+            result.put(nodeName, extendOneRow(initMatrix.get(nodeName), sortedTimestamp, stamp));
+        }
+        return result;
+    }
+
+    // 先把提交的时间戳落入区间,0右边一直是0,1右边是1,
+    private int[] extendOneRow(int[] initRow, List<String> sortedTimestamp, Long[] standardStamp) {
+        int[] result = new int[NUMBEROFUNIS];
+        Long[] sortedTimestampLong = new Long[sortedTimestamp.size()];
+        for(int i=0; i < sortedTimestamp.size(); i++) {
+            sortedTimestampLong[i] = stamp2long(sortedTimestamp.get(i));
+        }
+        int flag = 0;
+        for(int i = 0, index = 0; i < NUMBEROFUNIS ; i++) {
+            if( sortedTimestampLong[index] > standardStamp[i] ) {
+                flag = initRow[index];
+                result[i] = flag;
+            }else{
+                if(index+1< sortedTimestamp.size()) {
+                    index ++;
+                    i -- ;
+                }
+            }
+        }
+        return result;
+    }
+
+    private Long stamp2long(String stamp) {
+        return new Long(stamp);
+    }
+
     private HashMap<Long, int[]> getUsersMap(Long examId, Long caseId, String type) {
         HashMap<Long, int[]> result = new HashMap<>();
         List<UserCatchDTO> userCatchDTOList = userCatchService.getUserCatchDTOs(null, examId, caseId);