123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- 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<String> getValid(String case_take_id) {
- List<String> result = new ArrayList<String>();
- List<BugMirror> mirrors = mdao.findValid(case_take_id);
- for(BugMirror ctb : mirrors) {
- result.add(ctb.getId());
- }
- return result;
- }
-
- //获取所有有测试用例的bug
- public List<String> getValidTwo(String case_take_id) {
- List<String> result = new ArrayList<String>();
- List<CaseToBug> lists = ctbdao.findByCase(case_take_id);
- for(CaseToBug ctb : lists) {
- for(String str: ctb.getBug_id()) {
- result.add(str);
- }
- }
- return result;
- }
-
- public List<String> getReports(String case_take_id) {
- List<String> result = new ArrayList<String>();
- List<CaseToBug> 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<String, Integer> 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<String, Integer> result = new HashMap<String, Integer>(); //用户得分
- Map<String, Integer> grades = new HashMap<String, Integer>(); //专家评价分
- Map<String, Integer> scores = new HashMap<String, Integer>(); //计算bug得分
- JSONArray json = new JSONArray();
-
- List<String> 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<String, Integer> entry : result.entrySet()) {
- if(entry.getValue() > 20) { result.put(entry.getKey(), 20); }
- if(entry.getValue() < 0) { result.put(entry.getKey(), 0); }
- }
- Map<String, Integer> temp = new HashMap<String, Integer>();
- 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<String, Integer> 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<String, Integer> 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<String, String> getThums(String case_take_id) {
- Map<String, String> result = new HashMap<String, String>();
- List<String> 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<String, Integer> getBugDetail(String case_take_id) {
- Map<String, Integer> page = new HashMap<String, Integer>();
- List<String> 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<String, Integer> kind = new HashMap<String, Integer>();
- List<String> 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<String, Integer> getAllGrades(String case_take_id) {
- Map<String, Integer> result = new HashMap<String, Integer>();
- List<BugMirror> mlist = mdao.findByCase(case_take_id);
- List<String> idlist = new ArrayList<String>();
- for(BugMirror mirror : mlist) {
- idlist.add(mirror.getId());
- }
- List<BugScore> slist = bsdao.findByIds(idlist);
- for(BugScore bugscore: slist) {
- result.put(bugscore.getId(), bugscore.getGrade());
- }
- return result;
- }
-
- public List<String> getDiff(String case_take_id) {
- List<String> bugs = getValid(case_take_id);
- bugs.add("split");
- for(Map.Entry<String, Integer> entry: getAllGrades(case_take_id).entrySet()) {
- if(bugs.contains(entry.getKey())) {bugs.remove(entry.getKey());}
- else {bugs.add(entry.getKey());}
- }
- return bugs;
- }
-
- //评价页面获取评分
- public List<List<String>> getScores(List<String> ids) {
- List<List<String>> result = new ArrayList<List<String>>();
- List<BugScore> list = bsdao.findByIds(ids);
- for(BugScore bs: list) {
- List<String> temp = new ArrayList<String>();
- temp.add(bs.getId());
- temp.add(Integer.toString(bs.getGrade()));
- result.add(temp);
- }
- return result;
- }
-
- //根据树状结构计算分数
- public void countScore(String case_take_id, Map<String, Integer> result, Map<String, Integer> grades) {
- List<String> roots = hservice.getRoots(case_take_id);
- for(String root : roots) {
- List<List<String>> lists = hservice.getDepth(root);
- for(List<String> 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<Bug> getAfterSimilarBug(String bug_id){
- Bug bug =bdao.findByid(bug_id);
- List<Bug> bugList=bdao.findByCaseid(bug.getCase_id());
- List<Bug> 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;
- }
- }
|