AnalyzeService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. package edu.nju.service;
  2. import java.util.*;
  3. import edu.nju.dao.*;
  4. import edu.nju.entities.*;
  5. import edu.nju.util.HTTP;
  6. import org.json.JSONArray;
  7. import org.json.JSONObject;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. @Service
  11. public class AnalyzeService {
  12. @Autowired
  13. CTBDao ctbdao;
  14. @Autowired
  15. BugScoreDao bsdao;
  16. @Autowired
  17. BugHistoryDao hdao;
  18. @Autowired
  19. BugMirrorDao mdao;
  20. @Autowired
  21. StuInfoDao studao;
  22. @Autowired
  23. BugDao bdao;
  24. @Autowired
  25. HistoryService hservice;
  26. @Autowired
  27. BugScoreByWorkerDao bugScoreByWorkerDao;
  28. @Autowired
  29. BugSimiliarScoreDao bugSimiliarScoreDao;
  30. //获取所有bug
  31. public List<String> getValid(String case_take_id) {
  32. List<String> result = new ArrayList<String>();
  33. List<BugMirror> mirrors = mdao.findValid(case_take_id);
  34. for(BugMirror ctb : mirrors) {
  35. result.add(ctb.getId());
  36. }
  37. return result;
  38. }
  39. //获取所有有测试用例的bug
  40. public List<String> getValidTwo(String case_take_id) {
  41. List<String> result = new ArrayList<String>();
  42. List<CaseToBug> lists = ctbdao.findByCase(case_take_id);
  43. for(CaseToBug ctb : lists) {
  44. for(String str: ctb.getBug_id()) {
  45. result.add(str);
  46. }
  47. }
  48. return result;
  49. }
  50. public List<String> getReports(String case_take_id) {
  51. List<String> result = new ArrayList<String>();
  52. List<CaseToBug> lists = ctbdao.findByCase(case_take_id);
  53. for(CaseToBug ctb : lists) {
  54. if(!result.contains(ctb.getReport_id())) {result.add(ctb.getReport_id());}
  55. }
  56. return result;
  57. }
  58. public int getGrade(String id) {
  59. BugScore bs = bsdao.findById(id);
  60. if(bs != null) {return bs.getGrade();}
  61. return -1;
  62. }
  63. public boolean saveGrade(String id, int grade) {
  64. try {
  65. bsdao.save(new BugScore(id, grade, 0));
  66. return true;
  67. } catch(Exception e) {
  68. return false;
  69. }
  70. }
  71. public boolean saveGradeByWorker(String id, String workerId, int grade) {
  72. try {
  73. bsdao.save(new BugScore(id, grade, 0));
  74. return true;
  75. } catch(Exception e) {
  76. return false;
  77. }
  78. }
  79. public boolean saveSimiliarGrade(String id, int grade,String similiarBug) {
  80. try {
  81. bugSimiliarScoreDao.save(new BugSimiliarScore(id, grade, similiarBug));
  82. return true;
  83. } catch(Exception e) {
  84. return false;
  85. }
  86. }
  87. public int mark(String id, Map<String, Integer> grades, BugMirror mirror) {
  88. int mark = 0;
  89. int grade = grades.get(id);
  90. BugHistory history = hdao.findByid(id);
  91. int parent = 0;
  92. if(!history.getParent().equals("null")) {
  93. parent = grades.getOrDefault(history.getParent(), 0);
  94. }
  95. int count = 0;
  96. for(String child : history.getChildren()) {
  97. if(grades.getOrDefault(child, 3) <= 2) {count ++;}
  98. }
  99. switch(grade) {
  100. case 1:
  101. if(parent == 1) {mark += 40;}
  102. else {mark += 100;}
  103. mark += count * 2;
  104. break;
  105. case 2:
  106. if(parent == 1) {break;}
  107. else if(parent == 2) {mark += 40;}
  108. else {mark += 80;}
  109. mark += count * 2;
  110. break;
  111. case 3:
  112. mark += count;
  113. break;
  114. }
  115. if(grade <= 2) {
  116. mark += 2 * (mirror.getGood().size() - mirror.getBad().size());
  117. }
  118. return mark;
  119. }
  120. public JSONArray getScores(String case_take_id) {
  121. Map<String, Integer> result = new HashMap<String, Integer>(); //用户得分
  122. Map<String, Integer> grades = new HashMap<String, Integer>(); //专家评价分
  123. Map<String, Integer> scores = new HashMap<String, Integer>(); //计算bug得分
  124. JSONArray json = new JSONArray();
  125. List<String> bugs = getValid(case_take_id);
  126. for(String bug: bugs) {
  127. BugScore temp = bsdao.findById(bug);
  128. if(temp != null) {grades.put(bug, temp.getGrade());}
  129. else {grades.put(bug, 0);}
  130. }
  131. // for(String bug: bugs) {
  132. // BugMirror mirror = mdao.findById(bug);
  133. // int grade = grades.get(bug);
  134. // if(grade == 0) {continue;}
  135. // if(grade == 1) {ThumsUp(5, result, mirror);}
  136. // else if(grade == 2) {ThumsUp(3, result, mirror);}
  137. // else {ThumsUp(-3, result, mirror);}
  138. // result.put(mirror.getReport_id(), result.getOrDefault(mirror.getReport_id(), 0) + mark(bug, grades, mirror));
  139. // }
  140. countScore(case_take_id, scores, grades);
  141. for(String bug: bugs) {
  142. BugMirror mirror = mdao.findById(bug);
  143. if(mirror == null) { continue; }
  144. int grade = grades.getOrDefault(bug, 0);
  145. if(grade > 0) { ThumsUp(1, result, mirror); }
  146. if(grade == 0) { ThumsUp(-1, result, mirror); }
  147. }
  148. for(Map.Entry<String, Integer> entry : result.entrySet()) {
  149. if(entry.getValue() > 20) { result.put(entry.getKey(), 20); }
  150. if(entry.getValue() < 0) { result.put(entry.getKey(), 0); }
  151. }
  152. Map<String, Integer> temp = new HashMap<String, Integer>();
  153. for(String bug: bugs) {
  154. BugMirror mirror = mdao.findById(bug);
  155. if(mirror == null) { continue; }
  156. temp.put(mirror.getReport_id(), scores.getOrDefault(bug, 0) + temp.getOrDefault(mirror.getReport_id(), 0));
  157. }
  158. for(Map.Entry<String, Integer> entry : temp.entrySet()) {
  159. JSONObject json_temp = new JSONObject();
  160. json_temp.put("report_id", entry.getKey());
  161. json_temp.put("worker_id", findWorkerId(entry.getKey()));
  162. json_temp.put("名字", report_trans(entry.getKey()));
  163. json_temp.put("报告得分", entry.getValue());
  164. json_temp.put("审查得分", result.getOrDefault(entry.getKey(), 0));
  165. json.put(json_temp);
  166. }
  167. writeScores(case_take_id, json);
  168. return json;
  169. }
  170. public JSONArray getNewScores(JSONArray array) {
  171. if(array == null || array.length() <= 0) { return array; }
  172. for(int i = 0; i < array.length(); i ++) {
  173. JSONObject object = array.getJSONObject(i);
  174. if(object.keySet().size() < 5) { continue; }
  175. object.put("name", object.get("名字"));
  176. object.remove("名字");
  177. int score = Integer.parseInt(object.get("报告得分").toString()) + Integer.parseInt(object.get("审查得分").toString());
  178. if(score > 100) { object.put("score", 100); }
  179. else { object.put("score", score); }
  180. object.remove("报告得分");
  181. object.remove("审查得分");
  182. }
  183. return array;
  184. }
  185. //计算点赞得分
  186. private void ThumsUp(int grade, Map<String, Integer> result, BugMirror mirror) {
  187. for(String report : mirror.getGood()) {
  188. result.put(report, result.getOrDefault(report, 0) + grade);
  189. }
  190. for(String report : mirror.getBad()) {
  191. result.put(report, result.getOrDefault(report, 0) - grade);
  192. }
  193. }
  194. public Map<String, String> getThums(String case_take_id) {
  195. Map<String, String> result = new HashMap<String, String>();
  196. List<String> bugs = getValid(case_take_id);
  197. for(String bug: bugs) {
  198. BugMirror mirror = mdao.findById(bug);
  199. if(mirror.getGood().size() > 0 || mirror.getBad().size() > 0) {
  200. result.put(bug, mirror.getGood().size() + "," + mirror.getBad().size());
  201. }
  202. }
  203. return result;
  204. }
  205. public Map<String, Integer> getBugDetail(String case_take_id) {
  206. Map<String, Integer> page = new HashMap<String, Integer>();
  207. List<String> bugs = getValid(case_take_id);
  208. for(String id : bugs) {
  209. Bug bug = bdao.findByid(id);
  210. page.put(bug.getBug_page(), page.getOrDefault(bug.getBug_page(), 0) + 1);
  211. }
  212. return page;
  213. }
  214. public JSONObject getCaseDetail(String case_take_id) {
  215. JSONObject result = new JSONObject();
  216. Map<String, Integer> kind = new HashMap<String, Integer>();
  217. List<String> bugs = getValid(case_take_id);
  218. for(String id : bugs) {
  219. Bug bug = bdao.findByid(id);
  220. kind.put(bug.getBug_category(), kind.getOrDefault(bug.getBug_category(), 0) + 1);
  221. }
  222. result.put("page", new JSONObject(getBugDetail(case_take_id)));
  223. result.put("category", new JSONObject(kind));
  224. return result;
  225. }
  226. public Map<String, Integer> getAllGrades(String case_take_id) {
  227. Map<String, Integer> result = new HashMap<String, Integer>();
  228. List<BugMirror> mlist = mdao.findByCase(case_take_id);
  229. List<String> idlist = new ArrayList<String>();
  230. for(BugMirror mirror : mlist) {
  231. idlist.add(mirror.getId());
  232. }
  233. List<BugScore> slist = bsdao.findByIds(idlist);
  234. for(BugScore bugscore: slist) {
  235. result.put(bugscore.getId(), bugscore.getGrade());
  236. }
  237. return result;
  238. }
  239. public List<String> getDiff(String case_take_id) {
  240. List<String> bugs = getValid(case_take_id);
  241. bugs.add("split");
  242. for(Map.Entry<String, Integer> entry: getAllGrades(case_take_id).entrySet()) {
  243. if(bugs.contains(entry.getKey())) {bugs.remove(entry.getKey());}
  244. else {bugs.add(entry.getKey());}
  245. }
  246. return bugs;
  247. }
  248. //评价页面获取评分
  249. public List<List<String>> getScores(List<String> ids) {
  250. List<List<String>> result = new ArrayList<List<String>>();
  251. List<BugScore> list = bsdao.findByIds(ids);
  252. for(BugScore bs: list) {
  253. List<String> temp = new ArrayList<String>();
  254. temp.add(bs.getId());
  255. temp.add(Integer.toString(bs.getGrade()));
  256. result.add(temp);
  257. }
  258. return result;
  259. }
  260. //根据树状结构计算分数
  261. public void countScore(String case_take_id, Map<String, Integer> result, Map<String, Integer> grades) {
  262. List<String> roots = hservice.getRoots(case_take_id);
  263. for(String root : roots) {
  264. List<List<String>> lists = hservice.getDepth(root);
  265. for(List<String> path : lists) {
  266. int max = 0;
  267. for(String id : path) {
  268. int grade = grades.getOrDefault(id, 0);
  269. result.put(id, Math.max(grade - max, 0));
  270. max = Math.max(max, grade);
  271. }
  272. }
  273. }
  274. }
  275. private void writeScores(String case_take_id, JSONArray array) {
  276. String host = "http://www.mooctest.net";
  277. String url = "/api/common/uploadCaseScore";
  278. String[] ids = case_take_id.split("-");
  279. String param1 = "caseId=" + ids[0] + "&examId=" + ids[1];
  280. for(int i = 0; i < array.length(); i ++) {
  281. JSONObject json = (JSONObject)array.get(i);
  282. String worker_id = json.get("worker_id").toString();
  283. int score = Integer.parseInt(json.get("报告得分").toString()) + Integer.parseInt(json.get("审查得分").toString());
  284. if(score <= 0 || worker_id.equals("")) { continue; }
  285. if(score > 100) { score = 100; }
  286. String param2 = "&userId=" + worker_id + "&score=" + score;
  287. HTTP.sendPut(host, url, param1 + param2);
  288. }
  289. }
  290. private String report_trans(String report_id) {
  291. String name = studao.findById(report_id);
  292. if(name == null || name.equals("null")) { return report_id;}
  293. return name;
  294. }
  295. private String findWorkerId(String report_id) {
  296. String workerId = studao.findWorkerId(report_id);
  297. if(workerId == null || workerId.equals("null")) { return "";}
  298. return workerId;
  299. }
  300. public List<Bug> getAfterSimilarBug(String bug_id){
  301. Bug bug =bdao.findByid(bug_id);
  302. List<Bug> bugList=bdao.findByCaseid(bug.getCase_id());
  303. List<Bug> result=new ArrayList<>();
  304. for(Bug tempBug: bugList){
  305. if(Long.parseLong(tempBug.getCreate_time_millis())>Long.parseLong(bug.getCreate_time_millis())){
  306. if(checkSimilarity(bug,tempBug)){
  307. result.add(tempBug);
  308. }
  309. }
  310. }
  311. return result;
  312. }
  313. //todo check similarity
  314. private boolean checkSimilarity(Bug bug1,Bug bug2){
  315. return true;
  316. }
  317. }