package edu.nju.service; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import edu.nju.dao.BugDao; import edu.nju.dao.BugHistoryDao; import edu.nju.dao.BugMirrorDao; import edu.nju.dao.BugScoreDao; import edu.nju.dao.CTBDao; import edu.nju.dao.KWDao; import edu.nju.dao.StuInfoDao; import edu.nju.dao.ThumsUpDao; import edu.nju.entities.Bug; import edu.nju.entities.BugHistory; import edu.nju.entities.BugMirror; import edu.nju.entities.BugScore; import edu.nju.entities.KeyWords; import edu.nju.entities.ThumsUp; @Service public class ReportService { @Autowired CTBDao ctbdao; @Autowired BugScoreDao bsdao; @Autowired BugHistoryDao hdao; @Autowired BugMirrorDao mdao; @Autowired BugDao bdao; @Autowired ThumsUpDao tdao; @Autowired KWDao kdao; @Autowired StuInfoDao studao; @Autowired AnalyzeService aservice; @Autowired HistoryService hservice; public List getUserBugs(String report_id, String case_take_id) { return mdao.findIdsByReport(report_id, case_take_id); } public Map getUserPath(String report_id, String case_take_id) { Map result = new HashMap(); List ids = getUserBugs(report_id, case_take_id); for(String id: ids) { Bug bug = bdao.findByid(id); result.put(bug.getBug_page(), result.getOrDefault(bug.getBug_page(), 0) + 1); } return result; } public int getValid(String report_id, String case_take_id) { List ids = getUserBugs(report_id, case_take_id); List bugscore = bsdao.findByIds(ids); int count = 0; for(BugScore bs: bugscore) { if(bs.getGrade() >= 8) {count ++;} } return count; } public double getTimeGap(String report_id, String case_take_id) { List ids = getUserBugs(report_id, case_take_id); if(ids.size() <= 1) {return 0;} Bug start = bdao.findByid(ids.get(0)); Bug end = bdao.findByid(ids.get(ids.size() - 1)); return Double.parseDouble(end.getCreate_time_millis()) - Double.parseDouble(start.getCreate_time_millis()); } public Set getAllThums(String report_id) { ThumsUp thumsup = tdao.findByReport(report_id); if(thumsup != null) {return thumsup.getThums();} return null; } public int getAllThumsNum(String report_id) { Setthums=getAllThums(report_id); if(thums==null) return 0; else return thums.size(); } public Set getAllDiss(String report_id) { ThumsUp thumsup = tdao.findByReport(report_id); if(thumsup != null) {return thumsup.getDiss();} return null; } public int getAllDissNum(String report_id) { Setthums=getAllDiss(report_id); if(thums==null) return 0; else return thums.size(); } public int getValidThums(String report_id) { Set thums = getAllThums(report_id); if(thums == null) {return 0;} int count = 0; for(String id: thums) { BugScore bs = bsdao.findById(id); if(bs.getGrade() >= 8) {count ++;} } return count; } public int getValidDiss(String report_id) { Set diss = getAllDiss(report_id); if(diss == null) {return 0;} int count = 0; for(String id: diss) { BugScore bs = bsdao.findById(id); if(bs.getGrade() <= 2) {count ++;} } return count; } // public JSONObject getThumsRank(String case_take_id) { // Map result = new HashMap(); // List reports = aservice.getReports(case_take_id); // for(String report : reports) { // Set thums = getAllThums(report); // if(thums != null && thums.size() != 0) { result.put(report, thums.size()); } // } // return rank_sort(result); // } public JSONObject getThumsRank(String case_take_id) { Map result = new HashMap(); List bugs = aservice.getValid(case_take_id); for(String bug: bugs) { BugMirror bugmirror = mdao.findById(bug); if(bugmirror == null) { continue; } String report = bugmirror.getReport_id(); int thums = bugmirror.getGood().size(); result.put(report, result.getOrDefault(report, 0) + thums); } return rank_sort(result); } public JSONObject getForkRank(String case_take_id) { Map result = new HashMap(); List list = aservice.getValid(case_take_id); for(String id : list) { BugHistory history = hdao.findByid(id); if(history == null) { continue; } if(!history.getParent().equals("null")) { String key = bdao.findByid(id).getReport_id(); result.put(key, result.getOrDefault(key, 0) + 1); } } return rank_sort(result); } public JSONObject relations(String case_take_id) { JSONObject result = new JSONObject(); List trees = hservice.getTreeRoots(case_take_id); Set reports = new HashSet(); List> links = new ArrayList>(); for(String tree: trees) { for(List path : hservice.getDepth(tree)) { for(String id: path) { String report = report_trans(bdao.findByid(id).getReport_id()); reports.add(report); List temp = new ArrayList(); temp.add(report); temp.add(tree); links.add(temp); } } } result.put("TreeNode", trees); result.put("PersonNode", reports); result.put("Link", links); return result; } public JSONArray charm(String case_take_id) { JSONArray array = new JSONArray(); Map thumsup = new HashMap(); Map bugTotal = new HashMap(); List bugs = aservice.getValid(case_take_id); List reports = aservice.getReports(case_take_id); for(String id: bugs) { Bug bug = bdao.findByid(id); BugScore bugscore = bsdao.findById(id); if(bug == null || bugscore == null || bug.getId().equals("10010000034853")) { continue; } String report_id = bug.getReport_id(); int[] temp_score = bugTotal.getOrDefault(report_id, new int[2]); if(bugscore.getGrade() > 0) { temp_score[0] += 1; } temp_score[1] += 1; bugTotal.put(report_id, temp_score); } for(String report: reports) { int[] temp_score = new int[2]; ThumsUp thums = tdao.findByReport(report); if(thums == null) { continue; } for(String id: thums.getThums()) { Bug bug = bdao.findByid(id); if(bug == null || bug.getId().equals("10010000034853")) { continue; } BugScore bugscore = bsdao.findById(id); if(bugscore == null) { continue; } if(bugscore.getGrade() > 0) { temp_score[0] += 1; } temp_score[1] += 1; } thumsup.put(report, temp_score); } for(Map.Entry entry: bugTotal.entrySet()) { JSONObject json = new JSONObject(); String key = entry.getKey(); int[] bug_score = entry.getValue(); int[] thums_score = thumsup.getOrDefault(key, new int[2]); String value = "bug-" + bug_score[0] + "/" + bug_score[1] + ";thumsup-" + thums_score[0] + "/" + thums_score[1]; json.put(report_trans(key), value); array.put(json); } return array; } public JSONObject keyWords(String id) { Map map = new HashMap(); if(id != null || id != "") { for(List paths : hservice.getDepth(id)) { for(String path : paths) { KeyWords keywords = kdao.findById(path); if(keywords == null) { continue; } String[] words = keywords.getDescription().split(","); for(String word : words) { map.put(word, map.getOrDefault(word, 0) + 1); } } } } return map_sort(map); } public Bug findBugById(String budId){ return bdao.findByid(budId); } private JSONObject rank_sort(Map result) { JSONObject json = new JSONObject(true); // List> map_list = new ArrayList<>(result.entrySet()); // Collections.sort(map_list, (a, b) -> (b.getValue() - a.getValue())); for(Entry entry : result.entrySet()) { json.put(report_trans(entry.getKey()), entry.getValue()); } return json; } private JSONObject map_sort(Map result) { JSONObject json = new JSONObject(true); for(Entry entry : result.entrySet()) { json.put(entry.getKey(), entry.getValue()); } return json; } private String report_trans(String report_id) { String name = studao.findById(report_id); if(name == null || name.equals("null")) { return report_id;} return name; } }