123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- package edu.nju.service;
- import java.util.*;
- import edu.nju.dao.*;
- import edu.nju.entities.*;
- import edu.nju.model.*;
- import edu.nju.util.HTTP;
- import org.apache.commons.lang3.EnumUtils;
- 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
- BugSimilarScoreDao bugSimilarScoreDao;
- @Autowired
- ReportDao reportDao;
- @Autowired
- TaskDao taskDao;
- @Autowired
- TestCaseDao testCaseDao;
-
- //获取所有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;
- }
- public List<String> getValidByBugId(String id) {
- List<String> result = new ArrayList<String>();
- BugMirror bugMirror=mdao.findById(id);
- String case_take_id=bugMirror.getCase_take_id();
- 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<BugDataVO>getBugDataVO(String case_take_id){
- List<BugDataVO>result=new ArrayList<>();
- List<BugMirror> mirrors = mdao.findValid(case_take_id);
- List<Bug>bugs=bdao.findByCaseid(case_take_id);
- Map<String,Bug>bugMap=new HashMap<>();
- for(Bug bug:bugs){
- if(bug!=null){
- bugMap.put(bug.getId(),bug);
- }
- }
- for(BugMirror bugMirror : mirrors) {
- if(bugMirror!=null){
- Bug bug=bugMap.get(bugMirror.getId());
- BugHistory bugHistory=hdao.findByid(bugMirror.getId());
- if(bug!=null&&bugHistory!=null){
- BugSeverity bugSeveritsy=BugSeverity.getValue(2);
- String bugSeverity=bugSeveritsy.toString();
- BugScore bugScore=bsdao.findById(bug.getId());
- int score=bugScore==null?0:bugScore.getGrade();
- String reportId=bug.getReport_id();
- String workerId=findWorkerId(reportId);
- 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);
- result.add(bugDataVO);
- }
- }
- }
- 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 List<Long> getUsers(String examId) {
- List<Long> result = new ArrayList<Long>();
- List<Report> reports = reportDao.findByExamId(examId);
- for(Report report : reports) {
- if(report!=null) {
- String workerId=findWorkerId(report.getId());
- if(!workerId.equals("")&&!result.contains(workerId))
- result.add(Long.parseLong(workerId));
- }
- }
- 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 BugSimilarScore getSimilarScore(String id) {
- BugSimilarScore bss = bugSimilarScoreDao.findById(id);
- return bss;
- }
- 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 {
- bugSimilarScoreDao.save(new BugSimilarScore(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;
- }
- //todo 搞清楚究竟是通过哪种方式写回主站分数
- public JSONArray getScores(String case_take_id) {
- Map<String, Integer> reviewScore = new HashMap<String, Integer>(); //评审他人bug得分
- Map<String, Integer> bugScore = new HashMap<String, Integer>(); //系统或专家评审你的bug得分
- Map<String, Integer> finalScores = new HashMap<String, Integer>(); //计算bug得分
- JSONArray json = new JSONArray();
- //获取所有bugID
- List<String> bugIdList = getValid(case_take_id);
- //获取bug本身得分
- for(String bugId: bugIdList) {
- BugScore temp = bsdao.findById(bugId);
- if(temp != null) {bugScore.put(bugId, temp.getGrade());}
- else {bugScore.put(bugId, 0);}
- }
- //计算树状bug得分
- countScore(case_take_id, finalScores, bugScore);
- //计算审查得分
- for(String bug: bugIdList) {
- BugMirror mirror = mdao.findById(bug);
- if(mirror == null) { continue; }
- int grade = bugScore.getOrDefault(bug, 0);
- int thumbsScore=0;
- if(grade>=0&&grade<3){
- thumbsScore=-2;
- }else if(grade<5){
- thumbsScore=-1;
- }else if(grade<8){
- thumbsScore=1;
- }else{
- thumbsScore=2;
- }
- //点赞点踩积分减分
- ThumsUp(thumbsScore,reviewScore,mirror);
- }
- int maxReviewScore=0;
- int maxReportScore=0;
- for(Map.Entry<String, Integer> entry : reviewScore.entrySet()) {
- //审查得分低于零分按零分算
- if(entry.getValue() < 0) { reviewScore.put(entry.getKey(), 0); }
- if(entry.getValue()>maxReviewScore){
- maxReviewScore=entry.getValue();
- }
- }
- //将bug得分汇总为report得分
- Map<String, Integer> reportScore = new HashMap<String, Integer>();
- for(String bugId: bugIdList) {
- BugMirror mirror = mdao.findById(bugId);
- if(mirror == null) { continue; }
- String reportId=mirror.getReport_id();
- //出现过bug和bugMirror中没有reportId的问题,需要单独检查
- if(reportId!=null) {
- reportScore.put(reportId, finalScores.getOrDefault(bugId, 0) + reportScore.getOrDefault(reportId, 0));
- if (reportScore.get(reportId) > maxReportScore) {
- maxReportScore = reportScore.get(reportId);
- }
- }
- }
- //确保被除数不为0
- if(maxReportScore==0){
- maxReportScore=1;
- }
- if(maxReviewScore==0){
- maxReviewScore=1;
- }
- //得分要以最高分为满分
- for(Map.Entry<String, Integer> entry : reportScore.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()/maxReportScore)*100);
- json_temp.put("审查得分", (reviewScore.getOrDefault(entry.getKey(), 0)/maxReviewScore)*100);
- 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 bugScore=Integer.parseInt(object.get("报告得分").toString());
- int reviewScore=Integer.parseInt(object.get("审查得分").toString());
- double score = bugScore*0.7+reviewScore*0.3;
- 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;
- }
-
- //根据树状结构计算分数,会根据fork关系减去父节点的分数
- 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();
- //报告得分*0.7+审查得分*0.3
- int bugScore=Integer.parseInt(json.get("报告得分").toString());
- int reviewScore=Integer.parseInt(json.get("审查得分").toString());
- double score = bugScore*0.7+reviewScore*0.3;
- 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;
- }
- public AnalyseVO getReviewAnalyseVO(String caseId, String taskId){
- Task task=taskDao.findById(taskId);
- long startTime=0;
- long endTime=0;
- String taskName="";
- double writeMins=0.0;
- if(task!=null) {
- startTime = task.getStart_time();
- endTime = task.getEnd_time();
- taskName = task.getName();
- writeMins = task.getTotal_mins();
- }
- String caseTakeId=caseId+"-"+taskId;
- List<Bug>bugs=bdao.findByCaseid(caseTakeId);
- int bugNum=bugs.size();
- List<Report>reports=reportDao.findByCaseTakeId(caseTakeId);
- int participateNum=reports.size();
- int testCaseNum=0;
- for(Report report:reports){
- List<TestCase>testCases=testCaseDao.findByReport(report.getId());
- if(testCases!=null){
- testCaseNum+=testCases.size();
- }
- }
- Map<Integer,Integer>gradeDistribution=new HashMap<>();
- Map<String,Integer>workerDistribution=new HashMap<>();
- for(Bug bug:bugs){
- BugScore bugScore=bsdao.findById(bug.getId());
- String reportId=bug.getReport_id();
- if(reportId!=null) {
- Report report = reportDao.findById(reportId);
- if(report !=null) {
- String workerId=report.getWorker_id();
- if (workerId!=null) {
- int grade=0;
- if(bugScore!=null) {
- grade = bugScore.getGrade();
- if (gradeDistribution.containsKey(grade)) {
- gradeDistribution.replace(grade, gradeDistribution.get(grade) + 1);
- } else {
- gradeDistribution.put(grade, 1);
- }
- }
- if (workerDistribution.containsKey(workerId)) {
- workerDistribution.replace(workerId, workerDistribution.get(workerId) + grade);
- } else {
- workerDistribution.put(workerId, grade);
- }
- }
- }
- }
- }
- //分数排序
- List<Map.Entry<String, Integer>> list = new ArrayList<>(workerDistribution.entrySet());
- Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
- {
- @Override
- public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
- {
- return o2.getValue().compareTo(o1.getValue());
- }
- });
- List<WorkerVO>workerVOS=new ArrayList<>();
- Map<String,Integer>provinceDistribute=new HashMap<>();
- for(int i=0;i<list.size();i++) {
- String workerId = list.get(i).getKey();
- int grade=list.get(i).getValue();
- String result = HTTP.sendGet("http://114.55.91.83:8191/api/user/" + workerId, "");
- String name = "";
- String school = "";
- String province="";
- if (result != null && !result.equals("")) {
- JSONObject json = new JSONObject(result);
- if (json.has("name") && !json.isNull("name")) {
- name = json.getString("name");
- }
- if (json.has("school") && !json.isNull("school")) {
- school = json.getString("school");
- }
- if (json.has("province") && !json.isNull("province")) {
- province = json.getString("province");
- if(province.endsWith("省")||province.endsWith("市")){
- province=province.substring(0,province.length()-1);
- }
- if (provinceDistribute.containsKey(province)) {
- provinceDistribute.replace(province, provinceDistribute.get(province) + 1);
- } else {
- provinceDistribute.put(province, 1);
- }
- }
- }
- WorkerVO workerVO=new WorkerVO(workerId,name,school,grade);
- workerVOS.add(workerVO);
- }
- JSONArray jsonArray=new JSONArray();
- for(Map.Entry<String, Integer> entry : provinceDistribute.entrySet()){
- String mapKey = entry.getKey();
- int mapValue = entry.getValue();
- JSONObject jsonObject=new JSONObject();
- jsonObject.put("name",mapKey);
- jsonObject.put("value",mapValue);
- jsonArray.put(jsonObject);
- }
- AnalyseVO analyseVO=new AnalyseVO(startTime,endTime,taskName,participateNum,bugNum,testCaseNum,gradeDistribution,workerVOS,jsonArray);
- return analyseVO;
- }
- public HistoricalDataVO getHistoricalData(Long workerId, int caseTypeId){
- List<Report>reports=reportDao.findReportsByWorker(String.valueOf(workerId));
- int reportNum=reports.size();
- List<Double>reportScoreList=new ArrayList<>();
- double totalScore=0;
- int participateSimilarNum=0;
- for(Report report:reports){
- String reportId=report.getId();
- List<String>bugIds=bdao.findByReport(reportId);
- double score=0;
- for(String bugId:bugIds){
- BugScore bugScore=bsdao.findById(bugId);
- if(bugScore!=null) {
- score += bugScore.getGrade();
- }
- }
- totalScore+=score;
- reportScoreList.add(score);
- int itemCaseType=1;
- String brand=report.getDevice_os();
- if(EnumUtils.isValidEnum(WebBrand.class, brand)){
- itemCaseType=0;
- }
- if(itemCaseType==caseTypeId){
- participateSimilarNum++;
- }
- }
- HistoricalDataVO historicalDataVO=new HistoricalDataVO(reportNum,reportScoreList,totalScore,participateSimilarNum,reportNum);
- return historicalDataVO;
- }
- }
|