package com.mooctest.controller; import com.mooctest.data.BugDTO; import com.mooctest.data.FinalReportDTO; import com.mooctest.data.SupplementDTO; import com.mooctest.data.TaskDTO; import com.mooctest.model.FinalReport; import com.mooctest.model.MasterReport; import com.mooctest.service.*; import com.mooctest.util.ReportUtil; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @Controller @RequestMapping(value = "/history") @CrossOrigin(origins = "*", maxAge = 3600, allowCredentials = "true") public class HistoryController { @Autowired HistoryService hisservice; @Autowired TaskService taskService; @Autowired AnalyzeService aservice; @Autowired RecommendService recservice; @Autowired BugReportService bugReportService; @Autowired FinalReportService finalReportService; @Autowired MasterReportService masterReportService; @Autowired BugReviewService bugReviewService; //获取指定节点的历史信息 // @RequestMapping(value = "/getHistory") // @ResponseBody // public void getHistory(String id, HttpServletResponse response) { // try { // PrintWriter out = response.getWriter(); // out.print(new JSONObject(hisservice.getHistory(id))); // out.flush(); // out.close(); // } catch (IOException e) { // e.printStackTrace(); // } catch (JSONException e) { // e.printStackTrace(); // } // } //获取所有根节点 @RequestMapping(value = "/getRoots") @ResponseBody public void getRoots(String case_take_id, HttpServletResponse response) { try { PrintWriter out = response.getWriter(); JSONObject result = new JSONObject(); List list = hisservice.getRoots(case_take_id); result.put("Count", list.size()); result.put("TreeRoot", new JSONArray(list)); out.print(result); out.flush(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } @GetMapping(value = "/tree_list") public String showTreeList(@RequestParam("caseId") long caseId,@RequestParam("examId") long examId, Model model){ String case_take_id = caseId+"-"+examId; JSONObject result = new JSONObject(); // 用来存放树状结构信息的reuslt; List treeRootids = hisservice.getTreeRoots(caseId+"-"+examId); // 获得所有的根结点的id TaskDTO task = taskService.getByExamIdAndCaseId(examId, caseId); // infomation of task // List> list = new ArrayList>(); // for(String id: treeRootids) { // list.add(hisservice.getDetail(id)); // } // 获得树的信息。 //获得每份报告的具体信息 //history 报告对应的report列表列 Map> tree2BugIdsMap = hisservice.getTree2BugIdsMap(treeRootids); Map bugsMap = bugReportService.getAllBugsMap(examId, caseId); //得到所有bug的对应map model.addAttribute("tree2BugIdsMap", tree2BugIdsMap); // model.addAttribute("bugMap", bugsMap); // model.addAttribute("examId", examId); model.addAttribute("caseId", caseId); model.addAttribute("task", task); model.addAttribute("aggNum", treeRootids.size()); // return "tree_report_list"; } @RequestMapping(value = "/getTrees2") public String getTrees2(@RequestParam("case_take_id") String case_take_id) { return "managerCheck"; } @GetMapping(value = "/report") public String showAggrReport(@RequestParam("treeId") String treeId, @RequestParam("examId") long examId, @RequestParam("caseId") long caseId, @RequestParam(value = "finalReportId", required = false) Long finalReportId, Model model) { Map bugMap = bugReportService.getAllBugsMap(examId, caseId); BugDTO treeReport = bugMap.get(treeId); // 从bugmap中取得主报告的信息 List treeRootids = hisservice.getTreeRoots(caseId+"-"+examId); // 获得所有的根结点的id List childReportIds = hisservice.getSingleRootReports(treeId); // 获得所有自报告的信息 List childReports = new ArrayList<>(); childReportIds.forEach(s->{ childReports.add(bugMap.get(s)); }); //按照时间排序 childReports.sort((l,r)->{ return (int) (Long.parseLong(l.getCreateTimeMillis()) - Long.parseLong(r.getCreateTimeMillis())); }); Map categoryCounts = childReports.stream().collect(Collectors.groupingBy(BugDTO::getBugCategory, Collectors.counting())); Map pageCounts = childReports.stream().collect(Collectors.groupingBy(BugDTO::getBug_page,Collectors.counting())); Map recurrentCounts = childReports.stream() .map(BugDTO::getRecurrent) .map((recurrentNum) -> ReportUtil.recurrent2String.get(recurrentNum)) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); Map severityCounts = childReports.stream() .map(BugDTO::getSeverity) .map((severityNum) -> ReportUtil.severity2String.get(severityNum)) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); // List finalReports = new ArrayList<>();// 暂时还没有finalreport的数据 Map report2master = masterReportService.getBugIds2Master(masterReportService.getAllMasterIdByExamIdAndCaseId(examId, caseId)); List finalReports = finalReportService.getBySourceId(treeId); model.addAttribute("categoryCounts", categoryCounts); model.addAttribute("severityCounts", severityCounts); model.addAttribute("pageCounts",pageCounts); model.addAttribute("recurrentCounts",recurrentCounts); model.addAttribute("aggReportId", "ML-TR-" + treeId.substring(10)); // 树报告的信息 model.addAttribute("masterReport", treeReport); model.addAttribute("createTime", new Date(Long.parseLong(treeReport.getCreateTimeMillis()))); model.addAttribute("supplements", childReports); model.addAttribute("finalReports", finalReports); model.addAttribute("category2String", ReportUtil.category2String); model.addAttribute("recurrent2String", ReportUtil.recurrent2String); model.addAttribute("severity2String", ReportUtil.severity2String); model.addAttribute("reviewed",bugReviewService.isBugReviewed(treeId)); model.addAttribute("examId", examId); model.addAttribute("report2master",report2master); model.addAttribute("caseId", caseId); model.addAttribute("showReference",true);//是否展示其他聚合报告的参考标签 return "tree_report_new"; } @GetMapping(value = "/report_part") public String showTreerReportPart(@RequestParam("treeId") String treeId, @RequestParam("examId") long examId, @RequestParam("caseId") long caseId, @RequestParam(value = "finalReportId", required = false) Long finalReportId, Model model) { Map bugMap = bugReportService.getAllBugsMap(examId, caseId); BugDTO treeReport = bugMap.get(treeId); // 从bugmap中取得主报告的信息 List treeRootids = hisservice.getTreeRoots(caseId+"-"+examId); // 获得所有的根结点的id List childReportIds = hisservice.getSingleRootReports(treeId); // 获得所有自报告的信息 List childReports = new ArrayList<>(); childReportIds.forEach(s->{ childReports.add(bugMap.get(s)); }); Map categoryCounts = childReports.stream().collect(Collectors.groupingBy(BugDTO::getBugCategory, Collectors.counting())); Map pageCounts = childReports.stream().collect(Collectors.groupingBy(BugDTO::getBug_page,Collectors.counting())); Map recurrentCounts = childReports.stream() .map(BugDTO::getRecurrent) .map((recurrentNum) -> ReportUtil.recurrent2String.get(recurrentNum)) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); Map severityCounts = childReports.stream() .map(BugDTO::getSeverity) .map((severityNum) -> ReportUtil.severity2String.get(severityNum)) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); List finalReports = new ArrayList<>();// 暂时还没有finalreport的数据 //report to master Map report2master = masterReportService.getBugIds2Master(masterReportService.getAllMasterIdByExamIdAndCaseId(examId, caseId)); model.addAttribute("severityCounts", severityCounts); model.addAttribute("categoryCounts", categoryCounts); model.addAttribute("pageCounts",pageCounts); model.addAttribute("recurrentCounts",recurrentCounts); model.addAttribute("aggReportId", "ML-TR-" + treeId.substring(10)); // 树报告的信息 model.addAttribute("masterReport", treeReport); model.addAttribute("createTime", new Date(Long.parseLong(treeReport.getCreateTimeMillis()))); model.addAttribute("supplements", childReports); model.addAttribute("finalReports", finalReports); model.addAttribute("category2String", ReportUtil.category2String); model.addAttribute("recurrent2String", ReportUtil.recurrent2String); model.addAttribute("severity2String", ReportUtil.severity2String); model.addAttribute("report2master",report2master); // report 2 master report// model.addAttribute("reviewed",false); model.addAttribute("examId", examId); model.addAttribute("caseId", caseId); model.addAttribute("showReference",false);//是否展示其他聚合报告的参考标签 return "tree_report_new::treeBody"; } //获取所有形成树状结构的bug根节点 // case_take_id=1281-2724&start=0&count=10&page=null&identity=0&report_id=5cbc1a9f825a8960cdc7bd4f&worker_id=22383 @RequestMapping(value = "/getTrees") public String getTrees(@RequestParam("case_take_id") String case_take_id, @RequestParam("start") String start, @RequestParam("count") String count, @RequestParam("page") String page, @RequestParam("identity") String identity, @RequestParam("report_id") String report_id, @RequestParam("worker_id") String worker_id,Model model) { System.out.println("case_take_id = " + case_take_id); String[] split = case_take_id.split("-"); long caseId = Long.parseLong(split[0]); long examId = Long.parseLong(split[1]); try { JSONObject result = new JSONObject(); List all = hisservice.getTreeRoots(case_take_id);//获得所有的根结点。 System.out.println("all.toString(): " + all.toString()); hisservice.pageFilter(all, page);//清洗其他界面的bug报告,只留下本界面的。 List ids = all.subList(Integer.parseInt(start), Math.min(all.size(), Integer.parseInt(start) + Integer.parseInt(count))); // 做一个筛选。 List> list = new ArrayList>(); System.out.println(ids.toString()); for(String id: ids) { list.add(hisservice.getDetail(id)); } TaskDTO task = taskService.getByExamIdAndCaseId(examId, caseId); result.put("Count", all.size()); result.put("TreeRoot", new JSONArray(list)); // System.out.println("list = " + list); System.out.println("/getTrees result = " + result); model.addAttribute("examId", examId); model.addAttribute("caseId", caseId); model.addAttribute("task", task); model.addAttribute("Count", all.size()); model.addAttribute("TreeRoot", list); model.addAttribute("identity", identity); model.addAttribute("report_id", report_id); model.addAttribute("worker_id", worker_id); } catch (JSONException e) { e.printStackTrace(); } return "managerCheck"; } //获取所有形成树状结构的bug根节点 @RequestMapping(value = "/getTreesList/{case_take_id}/{start}/{count}/{page}") @ResponseBody public String getTreesList(@PathVariable String case_take_id, @PathVariable String start, @PathVariable String count, @PathVariable String page) { System.out.println("case_take_id = " + case_take_id); JSONObject result = new JSONObject(); String[] split = case_take_id.split("-"); long caseId = Long.parseLong(split[0]); long examId = Long.parseLong(split[1]); List all = hisservice.getTreeRoots(case_take_id); hisservice.pageFilter(all, page); List ids = all.subList(Integer.parseInt(start), Math.min(all.size(), Integer.parseInt(start) + Integer.parseInt(count))); List> list = new ArrayList>(); for(String id: ids) { list.add(hisservice.getDetail(id)); } TaskDTO task = taskService.getByExamIdAndCaseId(examId, caseId); try { result.put("Count", all.size()); result.put("TreeRoot", new JSONArray(list)); } catch (JSONException e) { e.printStackTrace(); } return result.toString(); } //获取所有单个节点的数据 @RequestMapping(value = "/getSingle/{case_take_id}/{start}/{count}/{page}") @ResponseBody public String getSingle(@PathVariable String case_take_id, @PathVariable String start, @PathVariable String count, @PathVariable String page) { JSONObject result = new JSONObject(); try { List all = new ArrayList(); for(String id : hisservice.getRoots(case_take_id)) { if(hisservice.getHistory(id).getChildren().size() == 0) {all.add(id);} } hisservice.pageFilter(all, page); List ids = all.subList(Integer.parseInt(start), Math.min(all.size(), Integer.parseInt(start) + Integer.parseInt(count))); List invalid = hisservice.getInvalid(ids); for(String id: invalid) { if(ids.contains(id)) {ids.remove(id);} } List> list = new ArrayList>(); for(String id : ids) { List temp = new ArrayList(); temp.add(id); int score = aservice.getGrade(id); if(score != -1) {temp.add("true");} else {temp.add("false");} temp.add(recservice.getTitle(id)); list.add(temp); } result.put("Count", all.size()); result.put("TreeRoot", new JSONArray(list)); } catch (JSONException e) { e.printStackTrace(); } return result.toString(); } //获取指定bug的所有路径 @RequestMapping(value = "/getPath/{id}") @ResponseBody public String getPath(@PathVariable String id) { JSONObject result = new JSONObject(); try { List> lists = hisservice.getDepth(id); Set filter = hisservice.filter(lists); result.put("path", new JSONArray(lists)); result.put("invalid", new JSONArray(hisservice.getInvalid(filter))); List ids = new ArrayList(filter); result.put("score", new JSONArray(aservice.getScores(ids))); System.out.println("/getPath path = " + result); System.out.println("result.toString() = " + result.toString()); } catch (JSONException e) { e.printStackTrace(); } return result.toString(); } @RequestMapping(value = "/fresh") @ResponseBody public void fresh(HttpSession session, HttpServletResponse response) { if(session.getAttribute("trees") != null) {session.removeAttribute("trees");} if(session.getAttribute("single") != null) {session.removeAttribute("single");} } }