MasterReportService.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.mooctest.service;
  2. import com.hankcs.hanlp.mining.word2vec.DocVectorModel;
  3. import com.mooctest.dao.MasterReportDao;
  4. import com.mooctest.data.BugDTO;
  5. import com.mooctest.model.MasterReport;
  6. import com.mooctest.util.Doc2VecUtil;
  7. import org.jgrapht.alg.interfaces.VertexScoringAlgorithm;
  8. import org.jgrapht.alg.scoring.PageRank;
  9. import org.jgrapht.graph.DefaultWeightedEdge;
  10. import org.jgrapht.graph.DirectedWeightedPseudograph;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import java.util.*;
  14. import java.util.concurrent.ConcurrentHashMap;
  15. import java.util.stream.Collectors;
  16. @Service
  17. public class MasterReportService {
  18. @Autowired
  19. MasterReportDao masterReportDao;
  20. public String findMasterReport(Set<String> cluster, Map<String, BugDTO> bugMap) {
  21. DirectedWeightedPseudograph<String, DefaultWeightedEdge> g = buildDirectedGraph(cluster, bugMap);
  22. VertexScoringAlgorithm<String, Double> pr = new PageRank<>(g, 0.85, 100, 0.0001);
  23. String masterId = pr.getScores().entrySet()
  24. .stream()
  25. .max(Comparator.comparing(Map.Entry::getValue))
  26. .get()
  27. .getKey();
  28. return masterId;
  29. }
  30. public DirectedWeightedPseudograph buildDirectedGraph(Set<String> cluster, Map<String, BugDTO> bugMap) {
  31. DirectedWeightedPseudograph<String, DefaultWeightedEdge> g =
  32. new DirectedWeightedPseudograph<>(DefaultWeightedEdge.class);
  33. cluster.forEach(bugId -> g.addVertex(bugId));
  34. cluster.forEach(outBugId -> {
  35. cluster.forEach(inBugId -> {
  36. if (!outBugId.equals(inBugId) && !g.containsEdge(outBugId, inBugId)) {
  37. g.setEdgeWeight(g.addEdge(outBugId, inBugId), getSim(outBugId, inBugId, bugMap));
  38. }
  39. });
  40. });
  41. return g;
  42. }
  43. public double getSim(String bugId1, String bugId2, Map<String, BugDTO> bugMap) {
  44. DocVectorModel docVectorModel = Doc2VecUtil.loadModel();
  45. return docVectorModel.similarity(bugMap.get(bugId1).getDescription(), bugMap.get(bugId2).getDescription());
  46. }
  47. public void saveMasterReport(String masterId, long examId, long caseId, Set<String> bugIds) {
  48. List<MasterReport> mrs = new ArrayList<>(bugIds.size() + 1);
  49. bugIds.forEach(bugId -> {
  50. MasterReport mr = MasterReport.builder()
  51. .masterId(masterId)
  52. .examId(examId)
  53. .caseId(caseId)
  54. .bugId(bugId)
  55. .build();
  56. mrs.add(mr);
  57. });
  58. masterReportDao.save(mrs);
  59. }
  60. public long[] getExamIdAndCaseIdByMasterId(String masterId) {
  61. List<MasterReport> mrs = masterReportDao.findByMasterId(masterId);
  62. MasterReport mr = mrs.stream().findAny().get();
  63. long[] ids = new long[2];
  64. ids[0] = mr.getExamId();
  65. ids[1] = mr.getCaseId();
  66. return ids;
  67. }
  68. public List<String> getAllMasterIdByExamIdAndCaseId(long examId, long caseId) {
  69. List<MasterReport> mr = masterReportDao.findByExamIdAndCaseId(examId, caseId);
  70. return mr.stream()
  71. .map(MasterReport::getMasterId)
  72. .distinct()
  73. .collect(Collectors.toList());
  74. }
  75. public List<MasterReport> getMaster2Item ( String bugId){
  76. List<MasterReport> data = masterReportDao.findByMasterId(bugId) ;
  77. return data;
  78. }
  79. public Map<String, List<String>> getMaster2BugIdsMap(List<String> masterIds) {
  80. List<MasterReport> mrs = masterReportDao.findByMasterIdIn(masterIds);
  81. return mrs.stream().collect(
  82. Collectors.groupingBy(
  83. MasterReport::getMasterId,
  84. Collectors.mapping(MasterReport::getBugId, Collectors.toList())));
  85. }
  86. //获得bug对masteride 的map
  87. // 获得普通报告dui master报告的对应关系关系
  88. public Map<String , String > getBugIds2Master(List<String> masterIds){
  89. List<MasterReport> nrs = masterReportDao.findByMasterIdIn(masterIds);
  90. return nrs.stream().collect(Collectors.toMap(MasterReport::getBugId,MasterReport::getMasterId));
  91. }
  92. public List<String> getAllBugIdsByMasterId(String masterId) {
  93. List<MasterReport> mrs = masterReportDao.findByMasterId(masterId);
  94. return mrs.stream().map(MasterReport::getBugId).collect(Collectors.toList());
  95. }
  96. public List<MasterReport> getByBugIds(List<String> bugIds) {
  97. return masterReportDao.findByBugIdIn(bugIds);
  98. }
  99. public MasterReport getByBugId(String bugId) {
  100. return masterReportDao.findByBugId(bugId);
  101. }
  102. public boolean isAggregated(long examId, long caseId) {
  103. long numOfAggReport = masterReportDao.countByExamIdAndCaseId(examId, caseId);
  104. return numOfAggReport > 0;
  105. }
  106. public void deleteAll(long examId, long caseId) {
  107. masterReportDao.deleteByExamIdAndCaseId(examId, caseId);
  108. }
  109. public long getAggNum(long examId, long caseId) {
  110. List<MasterReport> mrs = masterReportDao.findByExamIdAndCaseId(examId, caseId);
  111. return mrs.stream().map(MasterReport::getMasterId).distinct().count();
  112. }
  113. }