ReportService.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package edu.nju.service;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Map.Entry;
  8. import java.util.Set;
  9. import org.json.JSONArray;
  10. import org.json.JSONObject;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import edu.nju.dao.BugDao;
  14. import edu.nju.dao.BugHistoryDao;
  15. import edu.nju.dao.BugMirrorDao;
  16. import edu.nju.dao.BugScoreDao;
  17. import edu.nju.dao.CTBDao;
  18. import edu.nju.dao.KWDao;
  19. import edu.nju.dao.StuInfoDao;
  20. import edu.nju.dao.ThumsUpDao;
  21. import edu.nju.entities.Bug;
  22. import edu.nju.entities.BugHistory;
  23. import edu.nju.entities.BugMirror;
  24. import edu.nju.entities.BugScore;
  25. import edu.nju.entities.KeyWords;
  26. import edu.nju.entities.ThumsUp;
  27. @Service
  28. public class ReportService {
  29. @Autowired
  30. CTBDao ctbdao;
  31. @Autowired
  32. BugScoreDao bsdao;
  33. @Autowired
  34. BugHistoryDao hdao;
  35. @Autowired
  36. BugMirrorDao mdao;
  37. @Autowired
  38. BugDao bdao;
  39. @Autowired
  40. ThumsUpDao tdao;
  41. @Autowired
  42. KWDao kdao;
  43. @Autowired
  44. StuInfoDao studao;
  45. @Autowired
  46. AnalyzeService aservice;
  47. @Autowired
  48. HistoryService hservice;
  49. public List<String> getUserBugs(String report_id, String case_take_id) {
  50. return mdao.findIdsByReport(report_id, case_take_id);
  51. }
  52. public Map<String, Integer> getUserPath(String report_id, String case_take_id) {
  53. Map<String, Integer> result = new HashMap<String, Integer>();
  54. List<String> ids = getUserBugs(report_id, case_take_id);
  55. for(String id: ids) {
  56. Bug bug = bdao.findByid(id);
  57. result.put(bug.getBug_page(), result.getOrDefault(bug.getBug_page(), 0) + 1);
  58. }
  59. return result;
  60. }
  61. public int getValid(String report_id, String case_take_id) {
  62. List<String> ids = getUserBugs(report_id, case_take_id);
  63. List<BugScore> bugscore = bsdao.findByIds(ids);
  64. int count = 0;
  65. for(BugScore bs: bugscore) {
  66. if(bs.getGrade() >= 8) {count ++;}
  67. }
  68. return count;
  69. }
  70. public double getTimeGap(String report_id, String case_take_id) {
  71. List<String> ids = getUserBugs(report_id, case_take_id);
  72. if(ids.size() <= 1) {return 0;}
  73. Bug start = bdao.findByid(ids.get(0));
  74. Bug end = bdao.findByid(ids.get(ids.size() - 1));
  75. return Double.parseDouble(end.getCreate_time_millis()) - Double.parseDouble(start.getCreate_time_millis());
  76. }
  77. public Set<String> getAllThums(String report_id) {
  78. ThumsUp thumsup = tdao.findByReport(report_id);
  79. if(thumsup != null) {return thumsup.getThums();}
  80. return null;
  81. }
  82. public int getAllThumsNum(String report_id) {
  83. Set<String>thums=getAllThums(report_id);
  84. if(thums==null)
  85. return 0;
  86. else
  87. return thums.size();
  88. }
  89. public Set<String> getAllDiss(String report_id) {
  90. ThumsUp thumsup = tdao.findByReport(report_id);
  91. if(thumsup != null) {return thumsup.getDiss();}
  92. return null;
  93. }
  94. public int getAllDissNum(String report_id) {
  95. Set<String>thums=getAllDiss(report_id);
  96. if(thums==null)
  97. return 0;
  98. else
  99. return thums.size();
  100. }
  101. public int getValidThums(String report_id) {
  102. Set<String> thums = getAllThums(report_id);
  103. if(thums == null) {return 0;}
  104. int count = 0;
  105. for(String id: thums) {
  106. BugScore bs = bsdao.findById(id);
  107. if(bs.getGrade() >= 8) {count ++;}
  108. }
  109. return count;
  110. }
  111. public int getValidDiss(String report_id) {
  112. Set<String> diss = getAllDiss(report_id);
  113. if(diss == null) {return 0;}
  114. int count = 0;
  115. for(String id: diss) {
  116. BugScore bs = bsdao.findById(id);
  117. if(bs.getGrade() <= 2) {count ++;}
  118. }
  119. return count;
  120. }
  121. // public JSONObject getThumsRank(String case_take_id) {
  122. // Map<String, Integer> result = new HashMap<String, Integer>();
  123. // List<String> reports = aservice.getReports(case_take_id);
  124. // for(String report : reports) {
  125. // Set<String> thums = getAllThums(report);
  126. // if(thums != null && thums.size() != 0) { result.put(report, thums.size()); }
  127. // }
  128. // return rank_sort(result);
  129. // }
  130. public JSONObject getThumsRank(String case_take_id) {
  131. Map<String, Integer> result = new HashMap<String, Integer>();
  132. List<String> bugs = aservice.getValid(case_take_id);
  133. for(String bug: bugs) {
  134. BugMirror bugmirror = mdao.findById(bug);
  135. if(bugmirror == null) { continue; }
  136. String report = bugmirror.getReport_id();
  137. int thums = bugmirror.getGood().size();
  138. result.put(report, result.getOrDefault(report, 0) + thums);
  139. }
  140. return rank_sort(result);
  141. }
  142. public JSONObject getForkRank(String case_take_id) {
  143. Map<String, Integer> result = new HashMap<String, Integer>();
  144. List<String> list = aservice.getValid(case_take_id);
  145. for(String id : list) {
  146. BugHistory history = hdao.findByid(id);
  147. if(history == null) { continue; }
  148. if(!history.getParent().equals("null")) {
  149. String key = bdao.findByid(id).getReport_id();
  150. result.put(key, result.getOrDefault(key, 0) + 1);
  151. }
  152. }
  153. return rank_sort(result);
  154. }
  155. public JSONObject relations(String case_take_id) {
  156. JSONObject result = new JSONObject();
  157. List<String> trees = hservice.getTreeRoots(case_take_id);
  158. Set<String> reports = new HashSet<String>();
  159. List<List<String>> links = new ArrayList<List<String>>();
  160. for(String tree: trees) {
  161. for(List<String> path : hservice.getDepth(tree)) {
  162. for(String id: path) {
  163. String report = report_trans(bdao.findByid(id).getReport_id());
  164. reports.add(report);
  165. List<String> temp = new ArrayList<String>();
  166. temp.add(report);
  167. temp.add(tree);
  168. links.add(temp);
  169. }
  170. }
  171. }
  172. result.put("TreeNode", trees);
  173. result.put("PersonNode", reports);
  174. result.put("Link", links);
  175. return result;
  176. }
  177. public JSONArray charm(String case_take_id) {
  178. JSONArray array = new JSONArray();
  179. Map<String, int[]> thumsup = new HashMap<String, int[]>();
  180. Map<String, int[]> bugTotal = new HashMap<String, int[]>();
  181. List<String> bugs = aservice.getValid(case_take_id);
  182. List<String> reports = aservice.getReports(case_take_id);
  183. for(String id: bugs) {
  184. Bug bug = bdao.findByid(id);
  185. BugScore bugscore = bsdao.findById(id);
  186. if(bug == null || bugscore == null || bug.getId().equals("10010000034853")) { continue; }
  187. String report_id = bug.getReport_id();
  188. int[] temp_score = bugTotal.getOrDefault(report_id, new int[2]);
  189. if(bugscore.getGrade() > 0) { temp_score[0] += 1; }
  190. temp_score[1] += 1;
  191. bugTotal.put(report_id, temp_score);
  192. }
  193. for(String report: reports) {
  194. int[] temp_score = new int[2];
  195. ThumsUp thums = tdao.findByReport(report);
  196. if(thums == null) { continue; }
  197. for(String id: thums.getThums()) {
  198. Bug bug = bdao.findByid(id);
  199. if(bug == null || bug.getId().equals("10010000034853")) { continue; }
  200. BugScore bugscore = bsdao.findById(id);
  201. if(bugscore == null) { continue; }
  202. if(bugscore.getGrade() > 0) { temp_score[0] += 1; }
  203. temp_score[1] += 1;
  204. }
  205. thumsup.put(report, temp_score);
  206. }
  207. for(Map.Entry<String, int[]> entry: bugTotal.entrySet()) {
  208. JSONObject json = new JSONObject();
  209. String key = entry.getKey();
  210. int[] bug_score = entry.getValue();
  211. int[] thums_score = thumsup.getOrDefault(key, new int[2]);
  212. String value = "bug-" + bug_score[0] + "/" + bug_score[1]
  213. + ";thumsup-" + thums_score[0] + "/" + thums_score[1];
  214. json.put(report_trans(key), value);
  215. array.put(json);
  216. }
  217. return array;
  218. }
  219. public JSONObject keyWords(String id) {
  220. Map<String, Integer> map = new HashMap<String, Integer>();
  221. if(id != null || id != "") {
  222. for(List<String> paths : hservice.getDepth(id)) {
  223. for(String path : paths) {
  224. KeyWords keywords = kdao.findById(path);
  225. if(keywords == null) { continue; }
  226. String[] words = keywords.getDescription().split(",");
  227. for(String word : words) {
  228. map.put(word, map.getOrDefault(word, 0) + 1);
  229. }
  230. }
  231. }
  232. }
  233. return map_sort(map);
  234. }
  235. public Bug findBugById(String budId){
  236. return bdao.findByid(budId);
  237. }
  238. private JSONObject rank_sort(Map<String, Integer> result) {
  239. JSONObject json = new JSONObject(true);
  240. // List<Entry<String, Integer>> map_list = new ArrayList<>(result.entrySet());
  241. // Collections.sort(map_list, (a, b) -> (b.getValue() - a.getValue()));
  242. for(Entry<String, Integer> entry : result.entrySet()) {
  243. json.put(report_trans(entry.getKey()), entry.getValue());
  244. }
  245. return json;
  246. }
  247. private JSONObject map_sort(Map<String, Integer> result) {
  248. JSONObject json = new JSONObject(true);
  249. for(Entry<String, Integer> entry : result.entrySet()) {
  250. json.put(entry.getKey(), entry.getValue());
  251. }
  252. return json;
  253. }
  254. private String report_trans(String report_id) {
  255. String name = studao.findById(report_id);
  256. if(name == null || name.equals("null")) { return report_id;}
  257. return name;
  258. }
  259. }