package edu.nju.service; import java.util.*; import edu.nju.dao.*; import edu.nju.entities.*; import edu.nju.util.HTTP; import org.json.JSONArray; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class AnalyzeService { @Autowired CTBDao ctbdao; @Autowired BugScoreDao bsdao; @Autowired BugHistoryDao hdao; @Autowired BugMirrorDao mdao; @Autowired StuInfoDao studao; @Autowired BugDao bdao; @Autowired HistoryService hservice; @Autowired BugScoreByWorkerDao bugScoreByWorkerDao; @Autowired BugSimiliarScoreDao bugSimiliarScoreDao; //获取所有bug public List getValid(String case_take_id) { List result = new ArrayList(); List mirrors = mdao.findValid(case_take_id); for(BugMirror ctb : mirrors) { result.add(ctb.getId()); } return result; } //获取所有有测试用例的bug public List getValidTwo(String case_take_id) { List result = new ArrayList(); List lists = ctbdao.findByCase(case_take_id); for(CaseToBug ctb : lists) { for(String str: ctb.getBug_id()) { result.add(str); } } return result; } public List getReports(String case_take_id) { List result = new ArrayList(); List lists = ctbdao.findByCase(case_take_id); for(CaseToBug ctb : lists) { if(!result.contains(ctb.getReport_id())) {result.add(ctb.getReport_id());} } return result; } public int getGrade(String id) { BugScore bs = bsdao.findById(id); if(bs != null) {return bs.getGrade();} return -1; } public boolean saveGrade(String id, int grade) { try { bsdao.save(new BugScore(id, grade, 0)); return true; } catch(Exception e) { return false; } } public boolean saveGradeByWorker(String id, String workerId, int grade) { try { bsdao.save(new BugScore(id, grade, 0)); return true; } catch(Exception e) { return false; } } public boolean saveSimiliarGrade(String id, int grade,String similiarBug) { try { bugSimiliarScoreDao.save(new BugSimiliarScore(id, grade, similiarBug)); return true; } catch(Exception e) { return false; } } public int mark(String id, Map grades, BugMirror mirror) { int mark = 0; int grade = grades.get(id); BugHistory history = hdao.findByid(id); int parent = 0; if(!history.getParent().equals("null")) { parent = grades.getOrDefault(history.getParent(), 0); } int count = 0; for(String child : history.getChildren()) { if(grades.getOrDefault(child, 3) <= 2) {count ++;} } switch(grade) { case 1: if(parent == 1) {mark += 40;} else {mark += 100;} mark += count * 2; break; case 2: if(parent == 1) {break;} else if(parent == 2) {mark += 40;} else {mark += 80;} mark += count * 2; break; case 3: mark += count; break; } if(grade <= 2) { mark += 2 * (mirror.getGood().size() - mirror.getBad().size()); } return mark; } public JSONArray getScores(String case_take_id) { Map result = new HashMap(); //用户得分 Map grades = new HashMap(); //专家评价分 Map scores = new HashMap(); //计算bug得分 JSONArray json = new JSONArray(); List bugs = getValid(case_take_id); for(String bug: bugs) { BugScore temp = bsdao.findById(bug); if(temp != null) {grades.put(bug, temp.getGrade());} else {grades.put(bug, 0);} } // for(String bug: bugs) { // BugMirror mirror = mdao.findById(bug); // int grade = grades.get(bug); // if(grade == 0) {continue;} // if(grade == 1) {ThumsUp(5, result, mirror);} // else if(grade == 2) {ThumsUp(3, result, mirror);} // else {ThumsUp(-3, result, mirror);} // result.put(mirror.getReport_id(), result.getOrDefault(mirror.getReport_id(), 0) + mark(bug, grades, mirror)); // } countScore(case_take_id, scores, grades); for(String bug: bugs) { BugMirror mirror = mdao.findById(bug); if(mirror == null) { continue; } int grade = grades.getOrDefault(bug, 0); if(grade > 0) { ThumsUp(1, result, mirror); } if(grade == 0) { ThumsUp(-1, result, mirror); } } for(Map.Entry entry : result.entrySet()) { if(entry.getValue() > 20) { result.put(entry.getKey(), 20); } if(entry.getValue() < 0) { result.put(entry.getKey(), 0); } } Map temp = new HashMap(); for(String bug: bugs) { BugMirror mirror = mdao.findById(bug); if(mirror == null) { continue; } temp.put(mirror.getReport_id(), scores.getOrDefault(bug, 0) + temp.getOrDefault(mirror.getReport_id(), 0)); } for(Map.Entry entry : temp.entrySet()) { JSONObject json_temp = new JSONObject(); json_temp.put("report_id", entry.getKey()); json_temp.put("worker_id", findWorkerId(entry.getKey())); json_temp.put("名字", report_trans(entry.getKey())); json_temp.put("报告得分", entry.getValue()); json_temp.put("审查得分", result.getOrDefault(entry.getKey(), 0)); json.put(json_temp); } writeScores(case_take_id, json); return json; } public JSONArray getNewScores(JSONArray array) { if(array == null || array.length() <= 0) { return array; } for(int i = 0; i < array.length(); i ++) { JSONObject object = array.getJSONObject(i); if(object.keySet().size() < 5) { continue; } object.put("name", object.get("名字")); object.remove("名字"); int score = Integer.parseInt(object.get("报告得分").toString()) + Integer.parseInt(object.get("审查得分").toString()); if(score > 100) { object.put("score", 100); } else { object.put("score", score); } object.remove("报告得分"); object.remove("审查得分"); } return array; } //计算点赞得分 private void ThumsUp(int grade, Map result, BugMirror mirror) { for(String report : mirror.getGood()) { result.put(report, result.getOrDefault(report, 0) + grade); } for(String report : mirror.getBad()) { result.put(report, result.getOrDefault(report, 0) - grade); } } public Map getThums(String case_take_id) { Map result = new HashMap(); List bugs = getValid(case_take_id); for(String bug: bugs) { BugMirror mirror = mdao.findById(bug); if(mirror.getGood().size() > 0 || mirror.getBad().size() > 0) { result.put(bug, mirror.getGood().size() + "," + mirror.getBad().size()); } } return result; } public Map getBugDetail(String case_take_id) { Map page = new HashMap(); List bugs = getValid(case_take_id); for(String id : bugs) { Bug bug = bdao.findByid(id); page.put(bug.getBug_page(), page.getOrDefault(bug.getBug_page(), 0) + 1); } return page; } public JSONObject getCaseDetail(String case_take_id) { JSONObject result = new JSONObject(); Map kind = new HashMap(); List bugs = getValid(case_take_id); for(String id : bugs) { Bug bug = bdao.findByid(id); kind.put(bug.getBug_category(), kind.getOrDefault(bug.getBug_category(), 0) + 1); } result.put("page", new JSONObject(getBugDetail(case_take_id))); result.put("category", new JSONObject(kind)); return result; } public Map getAllGrades(String case_take_id) { Map result = new HashMap(); List mlist = mdao.findByCase(case_take_id); List idlist = new ArrayList(); for(BugMirror mirror : mlist) { idlist.add(mirror.getId()); } List slist = bsdao.findByIds(idlist); for(BugScore bugscore: slist) { result.put(bugscore.getId(), bugscore.getGrade()); } return result; } public List getDiff(String case_take_id) { List bugs = getValid(case_take_id); bugs.add("split"); for(Map.Entry entry: getAllGrades(case_take_id).entrySet()) { if(bugs.contains(entry.getKey())) {bugs.remove(entry.getKey());} else {bugs.add(entry.getKey());} } return bugs; } //评价页面获取评分 public List> getScores(List ids) { List> result = new ArrayList>(); List list = bsdao.findByIds(ids); for(BugScore bs: list) { List temp = new ArrayList(); temp.add(bs.getId()); temp.add(Integer.toString(bs.getGrade())); result.add(temp); } return result; } //根据树状结构计算分数 public void countScore(String case_take_id, Map result, Map grades) { List roots = hservice.getRoots(case_take_id); for(String root : roots) { List> lists = hservice.getDepth(root); for(List path : lists) { int max = 0; for(String id : path) { int grade = grades.getOrDefault(id, 0); result.put(id, Math.max(grade - max, 0)); max = Math.max(max, grade); } } } } private void writeScores(String case_take_id, JSONArray array) { String host = "http://www.mooctest.net"; String url = "/api/common/uploadCaseScore"; String[] ids = case_take_id.split("-"); String param1 = "caseId=" + ids[0] + "&examId=" + ids[1]; for(int i = 0; i < array.length(); i ++) { JSONObject json = (JSONObject)array.get(i); String worker_id = json.get("worker_id").toString(); int score = Integer.parseInt(json.get("报告得分").toString()) + Integer.parseInt(json.get("审查得分").toString()); if(score <= 0 || worker_id.equals("")) { continue; } if(score > 100) { score = 100; } String param2 = "&userId=" + worker_id + "&score=" + score; HTTP.sendPut(host, url, param1 + param2); } } private String report_trans(String report_id) { String name = studao.findById(report_id); if(name == null || name.equals("null")) { return report_id;} return name; } private String findWorkerId(String report_id) { String workerId = studao.findWorkerId(report_id); if(workerId == null || workerId.equals("null")) { return "";} return workerId; } public List getAfterSimilarBug(String bug_id){ Bug bug =bdao.findByid(bug_id); List bugList=bdao.findByCaseid(bug.getCase_id()); List result=new ArrayList<>(); for(Bug tempBug: bugList){ if(Long.parseLong(tempBug.getCreate_time_millis())>Long.parseLong(bug.getCreate_time_millis())){ if(checkSimilarity(bug,tempBug)){ result.add(tempBug); } } } return result; } //todo check similarity private boolean checkSimilarity(Bug bug1,Bug bug2){ return true; } }