AnalyzeService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package edu.nju.service;
  2. import java.util.*;
  3. import edu.nju.dao.*;
  4. import edu.nju.entities.*;
  5. import edu.nju.model.*;
  6. import edu.nju.util.HTTP;
  7. import org.apache.commons.lang3.EnumUtils;
  8. import org.json.JSONArray;
  9. import org.json.JSONObject;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. @Service
  13. public class AnalyzeService {
  14. @Autowired
  15. CTBDao ctbdao;
  16. @Autowired
  17. BugScoreDao bsdao;
  18. @Autowired
  19. BugHistoryDao hdao;
  20. @Autowired
  21. BugMirrorDao mdao;
  22. @Autowired
  23. StuInfoDao studao;
  24. @Autowired
  25. BugDao bdao;
  26. @Autowired
  27. HistoryService hservice;
  28. @Autowired
  29. BugScoreByWorkerDao bugScoreByWorkerDao;
  30. @Autowired
  31. BugSimilarScoreDao bugSimilarScoreDao;
  32. @Autowired
  33. ReportDao reportDao;
  34. @Autowired
  35. TaskDao taskDao;
  36. @Autowired
  37. TestCaseDao testCaseDao;
  38. //获取所有bug
  39. public List<String> getValid(String case_take_id) {
  40. List<String> result = new ArrayList<String>();
  41. List<BugMirror> mirrors = mdao.findValid(case_take_id);
  42. for(BugMirror ctb : mirrors) {
  43. result.add(ctb.getId());
  44. }
  45. return result;
  46. }
  47. public List<String> getValidByBugId(String id) {
  48. List<String> result = new ArrayList<String>();
  49. BugMirror bugMirror=mdao.findById(id);
  50. String case_take_id=bugMirror.getCase_take_id();
  51. List<BugMirror> mirrors = mdao.findValid(case_take_id);
  52. for(BugMirror ctb : mirrors) {
  53. result.add(ctb.getId());
  54. }
  55. return result;
  56. }
  57. //获取所有有测试用例的bug
  58. public List<String> getValidTwo(String case_take_id) {
  59. List<String> result = new ArrayList<String>();
  60. List<CaseToBug> lists = ctbdao.findByCase(case_take_id);
  61. for(CaseToBug ctb : lists) {
  62. for(String str: ctb.getBug_id()) {
  63. result.add(str);
  64. }
  65. }
  66. return result;
  67. }
  68. public List<BugDataVO>getBugDataVO(String case_take_id){
  69. List<BugDataVO>result=new ArrayList<>();
  70. List<BugMirror> mirrors = mdao.findValid(case_take_id);
  71. List<Bug>bugs=bdao.findByCaseid(case_take_id);
  72. Map<String,Bug>bugMap=new HashMap<>();
  73. for(Bug bug:bugs){
  74. if(bug!=null){
  75. bugMap.put(bug.getId(),bug);
  76. }
  77. }
  78. for(BugMirror bugMirror : mirrors) {
  79. if(bugMirror!=null){
  80. Bug bug=bugMap.get(bugMirror.getId());
  81. BugHistory bugHistory=hdao.findByid(bugMirror.getId());
  82. if(bug!=null&&bugHistory!=null){
  83. BugSeverity bugSeveritsy=BugSeverity.getValue(2);
  84. String bugSeverity=bugSeveritsy.toString();
  85. BugScore bugScore=bsdao.findById(bug.getId());
  86. int score=bugScore==null?0:bugScore.getGrade();
  87. String reportId=bug.getReport_id();
  88. String workerId=findWorkerId(reportId);
  89. BugDataVO bugDataVO=new BugDataVO(bug.getId(),bug.getBug_category(),bugSeverity,bug.getCreate_time_millis(),bug.getBug_page(),score,bugHistory.getParent(),bugHistory.getChildren(),bugHistory.getRoot(),bugMirror.getGood().size(),bugMirror.getBad().size(),reportId,workerId);
  90. result.add(bugDataVO);
  91. }
  92. }
  93. }
  94. return result;
  95. }
  96. public List<String> getReports(String case_take_id) {
  97. List<String> result = new ArrayList<String>();
  98. List<CaseToBug> lists = ctbdao.findByCase(case_take_id);
  99. for(CaseToBug ctb : lists) {
  100. if(!result.contains(ctb.getReport_id())) {result.add(ctb.getReport_id());}
  101. }
  102. return result;
  103. }
  104. public List<Long> getUsers(String examId) {
  105. List<Long> result = new ArrayList<Long>();
  106. List<Report> reports = reportDao.findByExamId(examId);
  107. for(Report report : reports) {
  108. if(report!=null) {
  109. String workerId=findWorkerId(report.getId());
  110. if(!workerId.equals("")&&!result.contains(workerId))
  111. result.add(Long.parseLong(workerId));
  112. }
  113. }
  114. return result;
  115. }
  116. public int getGrade(String id) {
  117. BugScore bs = bsdao.findById(id);
  118. if(bs != null) {return bs.getGrade();}
  119. return -1;
  120. }
  121. public boolean saveGrade(String id, int grade) {
  122. try {
  123. bsdao.save(new BugScore(id, grade, 0));
  124. return true;
  125. } catch(Exception e) {
  126. return false;
  127. }
  128. }
  129. public BugSimilarScore getSimilarScore(String id) {
  130. BugSimilarScore bss = bugSimilarScoreDao.findById(id);
  131. return bss;
  132. }
  133. public boolean saveGradeByWorker(String id, String workerId, int grade) {
  134. try {
  135. bsdao.save(new BugScore(id, grade, 0));
  136. return true;
  137. } catch(Exception e) {
  138. return false;
  139. }
  140. }
  141. public boolean saveSimiliarGrade(String id, int grade,String similiarBug) {
  142. try {
  143. bugSimilarScoreDao.save(new BugSimilarScore(id, grade, similiarBug));
  144. return true;
  145. } catch(Exception e) {
  146. return false;
  147. }
  148. }
  149. public int mark(String id, Map<String, Integer> grades, BugMirror mirror) {
  150. int mark = 0;
  151. int grade = grades.get(id);
  152. BugHistory history = hdao.findByid(id);
  153. int parent = 0;
  154. if(!history.getParent().equals("null")) {
  155. parent = grades.getOrDefault(history.getParent(), 0);
  156. }
  157. int count = 0;
  158. for(String child : history.getChildren()) {
  159. if(grades.getOrDefault(child, 3) <= 2) {count ++;}
  160. }
  161. switch(grade) {
  162. case 1:
  163. if(parent == 1) {mark += 40;}
  164. else {mark += 100;}
  165. mark += count * 2;
  166. break;
  167. case 2:
  168. if(parent == 1) {break;}
  169. else if(parent == 2) {mark += 40;}
  170. else {mark += 80;}
  171. mark += count * 2;
  172. break;
  173. case 3:
  174. mark += count;
  175. break;
  176. }
  177. if(grade <= 2) {
  178. mark += 2 * (mirror.getGood().size() - mirror.getBad().size());
  179. }
  180. return mark;
  181. }
  182. //todo 搞清楚究竟是通过哪种方式写回主站分数
  183. public JSONArray getScores(String case_take_id) {
  184. Map<String, Integer> reviewScore = new HashMap<String, Integer>(); //评审他人bug得分
  185. Map<String, Integer> bugScore = new HashMap<String, Integer>(); //系统或专家评审你的bug得分
  186. Map<String, Integer> finalScores = new HashMap<String, Integer>(); //计算bug得分
  187. JSONArray json = new JSONArray();
  188. //获取所有bugID
  189. List<String> bugIdList = getValid(case_take_id);
  190. //获取bug本身得分
  191. for(String bugId: bugIdList) {
  192. BugScore temp = bsdao.findById(bugId);
  193. if(temp != null) {bugScore.put(bugId, temp.getGrade());}
  194. else {bugScore.put(bugId, 0);}
  195. }
  196. //计算树状bug得分
  197. countScore(case_take_id, finalScores, bugScore);
  198. //计算审查得分
  199. for(String bug: bugIdList) {
  200. BugMirror mirror = mdao.findById(bug);
  201. if(mirror == null) { continue; }
  202. int grade = bugScore.getOrDefault(bug, 0);
  203. int thumbsScore=0;
  204. if(grade>=0&&grade<3){
  205. thumbsScore=-2;
  206. }else if(grade<5){
  207. thumbsScore=-1;
  208. }else if(grade<8){
  209. thumbsScore=1;
  210. }else{
  211. thumbsScore=2;
  212. }
  213. //点赞点踩积分减分
  214. ThumsUp(thumbsScore,reviewScore,mirror);
  215. }
  216. int maxReviewScore=0;
  217. int maxReportScore=0;
  218. for(Map.Entry<String, Integer> entry : reviewScore.entrySet()) {
  219. //审查得分低于零分按零分算
  220. if(entry.getValue() < 0) { reviewScore.put(entry.getKey(), 0); }
  221. if(entry.getValue()>maxReviewScore){
  222. maxReviewScore=entry.getValue();
  223. }
  224. }
  225. //将bug得分汇总为report得分
  226. Map<String, Integer> reportScore = new HashMap<String, Integer>();
  227. for(String bugId: bugIdList) {
  228. BugMirror mirror = mdao.findById(bugId);
  229. if(mirror == null) { continue; }
  230. String reportId=mirror.getReport_id();
  231. //出现过bug和bugMirror中没有reportId的问题,需要单独检查
  232. if(reportId!=null) {
  233. reportScore.put(reportId, finalScores.getOrDefault(bugId, 0) + reportScore.getOrDefault(reportId, 0));
  234. if (reportScore.get(reportId) > maxReportScore) {
  235. maxReportScore = reportScore.get(reportId);
  236. }
  237. }
  238. }
  239. //确保被除数不为0
  240. if(maxReportScore==0){
  241. maxReportScore=1;
  242. }
  243. if(maxReviewScore==0){
  244. maxReviewScore=1;
  245. }
  246. //得分要以最高分为满分
  247. for(Map.Entry<String, Integer> entry : reportScore.entrySet()) {
  248. JSONObject json_temp = new JSONObject();
  249. json_temp.put("report_id", entry.getKey());
  250. json_temp.put("worker_id", findWorkerId(entry.getKey()));
  251. json_temp.put("名字", report_trans(entry.getKey()));
  252. json_temp.put("报告得分", (entry.getValue()/maxReportScore)*100);
  253. json_temp.put("审查得分", (reviewScore.getOrDefault(entry.getKey(), 0)/maxReviewScore)*100);
  254. json.put(json_temp);
  255. }
  256. //把分写回主站
  257. writeScores(case_take_id, json);
  258. return json;
  259. }
  260. public JSONArray getNewScores(JSONArray array) {
  261. if(array == null || array.length() <= 0) { return array; }
  262. for(int i = 0; i < array.length(); i ++) {
  263. JSONObject object = array.getJSONObject(i);
  264. if(object.keySet().size() < 5) { continue; }
  265. object.put("name", object.get("名字"));
  266. object.remove("名字");
  267. int bugScore=Integer.parseInt(object.get("报告得分").toString());
  268. int reviewScore=Integer.parseInt(object.get("审查得分").toString());
  269. double score = bugScore*0.7+reviewScore*0.3;
  270. if(score > 100) { object.put("score", 100); }
  271. else { object.put("score", score); }
  272. object.remove("报告得分");
  273. object.remove("审查得分");
  274. }
  275. return array;
  276. }
  277. //计算点赞得分
  278. private void ThumsUp(int grade, Map<String, Integer> result, BugMirror mirror) {
  279. //给好的点赞加分,点踩减分,不好的反之
  280. for(String report : mirror.getGood()) {
  281. result.put(report, result.getOrDefault(report, 0) + grade);
  282. }
  283. for(String report : mirror.getBad()) {
  284. result.put(report, result.getOrDefault(report, 0) - grade);
  285. }
  286. }
  287. public Map<String, String> getThums(String case_take_id) {
  288. Map<String, String> result = new HashMap<String, String>();
  289. List<String> bugs = getValid(case_take_id);
  290. for(String bug: bugs) {
  291. BugMirror mirror = mdao.findById(bug);
  292. if(mirror.getGood().size() > 0 || mirror.getBad().size() > 0) {
  293. result.put(bug, mirror.getGood().size() + "," + mirror.getBad().size());
  294. }
  295. }
  296. return result;
  297. }
  298. public Map<String, Integer> getBugDetail(String case_take_id) {
  299. Map<String, Integer> page = new HashMap<String, Integer>();
  300. List<String> bugs = getValid(case_take_id);
  301. for(String id : bugs) {
  302. Bug bug = bdao.findByid(id);
  303. page.put(bug.getBug_page(), page.getOrDefault(bug.getBug_page(), 0) + 1);
  304. }
  305. return page;
  306. }
  307. public JSONObject getCaseDetail(String case_take_id) {
  308. JSONObject result = new JSONObject();
  309. Map<String, Integer> kind = new HashMap<String, Integer>();
  310. List<String> bugs = getValid(case_take_id);
  311. for(String id : bugs) {
  312. Bug bug = bdao.findByid(id);
  313. kind.put(bug.getBug_category(), kind.getOrDefault(bug.getBug_category(), 0) + 1);
  314. }
  315. result.put("page", new JSONObject(getBugDetail(case_take_id)));
  316. result.put("category", new JSONObject(kind));
  317. return result;
  318. }
  319. public Map<String, Integer> getAllGrades(String case_take_id) {
  320. Map<String, Integer> result = new HashMap<String, Integer>();
  321. List<BugMirror> mlist = mdao.findByCase(case_take_id);
  322. List<String> idlist = new ArrayList<String>();
  323. for(BugMirror mirror : mlist) {
  324. idlist.add(mirror.getId());
  325. }
  326. List<BugScore> slist = bsdao.findByIds(idlist);
  327. for(BugScore bugscore: slist) {
  328. result.put(bugscore.getId(), bugscore.getGrade());
  329. }
  330. return result;
  331. }
  332. public List<String> getDiff(String case_take_id) {
  333. List<String> bugs = getValid(case_take_id);
  334. bugs.add("split");
  335. for(Map.Entry<String, Integer> entry: getAllGrades(case_take_id).entrySet()) {
  336. if(bugs.contains(entry.getKey())) {bugs.remove(entry.getKey());}
  337. else {bugs.add(entry.getKey());}
  338. }
  339. return bugs;
  340. }
  341. //评价页面获取评分
  342. public List<List<String>> getScores(List<String> ids) {
  343. List<List<String>> result = new ArrayList<List<String>>();
  344. List<BugScore> list = bsdao.findByIds(ids);
  345. for(BugScore bs: list) {
  346. List<String> temp = new ArrayList<String>();
  347. temp.add(bs.getId());
  348. temp.add(Integer.toString(bs.getGrade()));
  349. result.add(temp);
  350. }
  351. return result;
  352. }
  353. //根据树状结构计算分数,会根据fork关系减去父节点的分数
  354. public void countScore(String case_take_id, Map<String, Integer> result, Map<String, Integer> grades) {
  355. List<String> roots = hservice.getRoots(case_take_id);
  356. for(String root : roots) {
  357. List<List<String>> lists = hservice.getDepth(root);
  358. for(List<String> path : lists) {
  359. int max = 0;
  360. for(String id : path) {
  361. int grade = grades.getOrDefault(id, 0);
  362. result.put(id, Math.max(grade - max, 0));
  363. max = Math.max(max, grade);
  364. }
  365. }
  366. }
  367. }
  368. private void writeScores(String case_take_id, JSONArray array) {
  369. String host = "http://www.mooctest.net";
  370. String url = "/api/common/uploadCaseScore";
  371. String[] ids = case_take_id.split("-");
  372. String param1 = "caseId=" + ids[0] + "&examId=" + ids[1];
  373. for(int i = 0; i < array.length(); i ++) {
  374. JSONObject json = (JSONObject)array.get(i);
  375. String worker_id = json.get("worker_id").toString();
  376. //报告得分*0.7+审查得分*0.3
  377. int bugScore=Integer.parseInt(json.get("报告得分").toString());
  378. int reviewScore=Integer.parseInt(json.get("审查得分").toString());
  379. double score = bugScore*0.7+reviewScore*0.3;
  380. if(score <= 0 || worker_id.equals("")) { continue; }
  381. if(score > 100) { score = 100; }
  382. String param2 = "&userId=" + worker_id + "&score=" + score;
  383. HTTP.sendPut(host, url, param1 + param2);
  384. }
  385. }
  386. private String report_trans(String report_id) {
  387. String name = studao.findById(report_id);
  388. if(name == null || name.equals("null")) { return report_id;}
  389. return name;
  390. }
  391. private String findWorkerId(String report_id) {
  392. String workerId = studao.findWorkerId(report_id);
  393. if(workerId == null || workerId.equals("null")) { return "";}
  394. return workerId;
  395. }
  396. public List<Bug> getAfterSimilarBug(String bug_id){
  397. Bug bug =bdao.findByid(bug_id);
  398. List<Bug> bugList=bdao.findByCaseid(bug.getCase_id());
  399. List<Bug> result=new ArrayList<>();
  400. for(Bug tempBug: bugList){
  401. if(Long.parseLong(tempBug.getCreate_time_millis())>Long.parseLong(bug.getCreate_time_millis())){
  402. if(checkSimilarity(bug,tempBug)){
  403. result.add(tempBug);
  404. }
  405. }
  406. }
  407. return result;
  408. }
  409. //todo check similarity
  410. private boolean checkSimilarity(Bug bug1,Bug bug2){
  411. return true;
  412. }
  413. public AnalyseVO getReviewAnalyseVO(String caseId, String taskId){
  414. Task task=taskDao.findById(taskId);
  415. long startTime=0;
  416. long endTime=0;
  417. String taskName="";
  418. double writeMins=0.0;
  419. if(task!=null) {
  420. startTime = task.getStart_time();
  421. endTime = task.getEnd_time();
  422. taskName = task.getName();
  423. writeMins = task.getTotal_mins();
  424. }
  425. String caseTakeId=caseId+"-"+taskId;
  426. List<Bug>bugs=bdao.findByCaseid(caseTakeId);
  427. int bugNum=bugs.size();
  428. List<Report>reports=reportDao.findByCaseTakeId(caseTakeId);
  429. int participateNum=reports.size();
  430. int testCaseNum=0;
  431. for(Report report:reports){
  432. List<TestCase>testCases=testCaseDao.findByReport(report.getId());
  433. if(testCases!=null){
  434. testCaseNum+=testCases.size();
  435. }
  436. }
  437. Map<Integer,Integer>gradeDistribution=new HashMap<>();
  438. Map<String,Integer>workerDistribution=new HashMap<>();
  439. for(Bug bug:bugs){
  440. BugScore bugScore=bsdao.findById(bug.getId());
  441. String reportId=bug.getReport_id();
  442. if(reportId!=null) {
  443. Report report = reportDao.findById(reportId);
  444. if(report !=null) {
  445. String workerId=report.getWorker_id();
  446. if (workerId!=null) {
  447. int grade=0;
  448. if(bugScore!=null) {
  449. grade = bugScore.getGrade();
  450. if (gradeDistribution.containsKey(grade)) {
  451. gradeDistribution.replace(grade, gradeDistribution.get(grade) + 1);
  452. } else {
  453. gradeDistribution.put(grade, 1);
  454. }
  455. }
  456. if (workerDistribution.containsKey(workerId)) {
  457. workerDistribution.replace(workerId, workerDistribution.get(workerId) + grade);
  458. } else {
  459. workerDistribution.put(workerId, grade);
  460. }
  461. }
  462. }
  463. }
  464. }
  465. //分数排序
  466. List<Map.Entry<String, Integer>> list = new ArrayList<>(workerDistribution.entrySet());
  467. Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
  468. {
  469. @Override
  470. public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
  471. {
  472. return o2.getValue().compareTo(o1.getValue());
  473. }
  474. });
  475. List<WorkerVO>workerVOS=new ArrayList<>();
  476. Map<String,Integer>provinceDistribute=new HashMap<>();
  477. for(int i=0;i<list.size();i++) {
  478. String workerId = list.get(i).getKey();
  479. int grade=list.get(i).getValue();
  480. String result = HTTP.sendGet("http://114.55.91.83:8191/api/user/" + workerId, "");
  481. String name = "";
  482. String school = "";
  483. String province="";
  484. if (result != null && !result.equals("")) {
  485. JSONObject json = new JSONObject(result);
  486. if (json.has("name") && !json.isNull("name")) {
  487. name = json.getString("name");
  488. }
  489. if (json.has("school") && !json.isNull("school")) {
  490. school = json.getString("school");
  491. }
  492. if (json.has("province") && !json.isNull("province")) {
  493. province = json.getString("province");
  494. if(province.endsWith("省")||province.endsWith("市")){
  495. province=province.substring(0,province.length()-1);
  496. }
  497. if (provinceDistribute.containsKey(province)) {
  498. provinceDistribute.replace(province, provinceDistribute.get(province) + 1);
  499. } else {
  500. provinceDistribute.put(province, 1);
  501. }
  502. }
  503. }
  504. WorkerVO workerVO=new WorkerVO(workerId,name,school,grade);
  505. workerVOS.add(workerVO);
  506. }
  507. JSONArray jsonArray=new JSONArray();
  508. for(Map.Entry<String, Integer> entry : provinceDistribute.entrySet()){
  509. String mapKey = entry.getKey();
  510. int mapValue = entry.getValue();
  511. JSONObject jsonObject=new JSONObject();
  512. jsonObject.put("name",mapKey);
  513. jsonObject.put("value",mapValue);
  514. jsonArray.put(jsonObject);
  515. }
  516. AnalyseVO analyseVO=new AnalyseVO(startTime,endTime,taskName,participateNum,bugNum,testCaseNum,gradeDistribution,workerVOS,jsonArray);
  517. return analyseVO;
  518. }
  519. public HistoricalDataVO getHistoricalData(Long workerId, int caseTypeId){
  520. List<Report>reports=reportDao.findReportsByWorker(String.valueOf(workerId));
  521. int reportNum=reports.size();
  522. List<Double>reportScoreList=new ArrayList<>();
  523. double totalScore=0;
  524. int participateSimilarNum=0;
  525. for(Report report:reports){
  526. String reportId=report.getId();
  527. List<String>bugIds=bdao.findByReport(reportId);
  528. double score=0;
  529. for(String bugId:bugIds){
  530. BugScore bugScore=bsdao.findById(bugId);
  531. if(bugScore!=null) {
  532. score += bugScore.getGrade();
  533. }
  534. }
  535. totalScore+=score;
  536. reportScoreList.add(score);
  537. int itemCaseType=1;
  538. String brand=report.getDevice_os();
  539. if(EnumUtils.isValidEnum(WebBrand.class, brand)){
  540. itemCaseType=0;
  541. }
  542. if(itemCaseType==caseTypeId){
  543. participateSimilarNum++;
  544. }
  545. }
  546. HistoricalDataVO historicalDataVO=new HistoricalDataVO(reportNum,reportScoreList,totalScore,participateSimilarNum,reportNum);
  547. return historicalDataVO;
  548. }
  549. }