feat(api) 毕设质量分析

sonar7.7
youys 1 year ago
parent bada54f8cb
commit 0783540035

@ -0,0 +1,38 @@
package net.educoder.ecsonar.controller;
import net.educoder.ecsonar.model.api.QualityInspect;
import net.educoder.ecsonar.model.vo.GraduationProjectQualityInspectVO;
import net.educoder.ecsonar.services.GraduationProjectQualityInspectService;
import net.educoder.ecsonar.utils.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: youys
* @Date: 2023/8/30
* @Description:
*/
@RestController
@RequestMapping("/graduation/project")
public class GraduationProjectController {
@Resource
private GraduationProjectQualityInspectService qualityInspectService;
@GetMapping("/qualityInspect")
public ResponseResult qualityInspect(@RequestBody List<GraduationProjectQualityInspectVO> qualityInspectVOList){
QualityInspect qualityInspect = qualityInspectService.graduationProjectQualityInspect(qualityInspectVOList);
return ResponseResult.success(qualityInspect);
}
}

@ -0,0 +1,27 @@
package net.educoder.ecsonar.dao;
import net.educoder.ecsonar.model.GraduationProjectTaskInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/**
* @Author: youys
* @Date: 2023/8/31
* @Description: dao
*/
public interface GraduationProjectTaskInfoDao {
@Insert("insert into graduation_project_task_info(id,people_number) values(#{id},#{peopleNumber} )")
void insertTaskInfo(GraduationProjectTaskInfo taskInfo);
@Update("update graduation_project_task_info set status=#{status},update_time=current_timestamp where id=#{id}")
void updateTaskInfoStatus(@Param("id") String id, @Param("status") Integer status);
@Select("select id,status,people_number peopleNumber,create_time createTime,update_time updateTime " +
"from graduation_project_task_info where id=#{id}")
GraduationProjectTaskInfo selectById(String id);
}

@ -0,0 +1,87 @@
package net.educoder.ecsonar.dao;
import net.educoder.ecsonar.model.GraduationProjectTaskInfoDetail;
import net.educoder.ecsonar.model.TaskInfoDetail;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
/**
* @Author: youys
* @Date: 2023/8/31
* @Description: dao
*/
public interface GraduationProjectTaskInfoDetailDao {
/**
*
*
* @param taskInfoDetail
*/
@Insert("insert into graduation_project_task_info_detail(id,task_id,project_name,language,student_id,user_id,name,git_url) " +
"values(#{id},#{taskId},#{projectName},#{language},#{studentId},#{userId} ,#{name},#{gitUrl})")
void insertTaskInfoDetail(GraduationProjectTaskInfoDetail taskInfoDetail);
/**
*
*
* @param id
* @param status
*/
@Update("update graduation_project_task_info_detail " +
"set status=#{status},update_time=current_timestamp where id=#{id}")
void updateGraduationProjectTaskInfoDetailStatus(@Param("id") String id, @Param("status") Integer status);
/**
* taskIdsonar
*
* @param taskId
* @return
*/
@Select("select count(1) from graduation_project_task_info_detail " +
"where task_id=#{taskId} and status != 0")
Integer selectCountByTaskId(@Param("taskId") String taskId);
/**
* idTaskInfoDetail
*
* @param id
* @return
*/
@Select("select id,task_id taskId,project_name projectName,language,student_id studentId,user_id " +
"userId,name,git_url gitUrl,status,create_time createTime,update_time updateTime " +
"from task_info_detail where id=#{id}")
GraduationProjectTaskInfoDetail selectById(String id);
/**
* count
*
* @param taskId
* @param pageSize
* @param offset
* @return
*/
@Select("select count(1) from graduation_project_task_info_detail " +
"where task_id=#{taskId} limit #{pageSize} offset #{offset}")
Integer selectGraduationProjectTaskInfoDetailPageCount(@Param("taskId") String taskId,
@Param("pageSize") Integer pageSize,
@Param("offset") Integer offset);
/**
*
*
* @param taskId
* @param pageSize
* @param position
* @return
*/
@Select("select id,task_id taskId,project_name projectName,language,student_id studentId,user_id userId," +
"name,git_url gitUrl,status,create_time createTime,update_time updateTime " +
"from graduation_project_task_info_detail where task_id=#{taskId} limit #{pageSize} offset #{position}")
List<GraduationProjectTaskInfoDetail> selectGraduationProjectTaskInfoDetailPageList(@Param("taskId") String taskId,
@Param("pageSize") Integer pageSize,
@Param("position") Integer position);
}

@ -0,0 +1,61 @@
package net.educoder.ecsonar.model;
import java.util.Date;
/**
* @Author: youys
* @Date: 2023/8/31
* @Description:
*/
public class GraduationProjectTaskInfo {
private String id;
/**
* 0 1
*/
private Integer status;
private Integer peopleNumber;
private Date createTime;
private Date updateTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getPeopleNumber() {
return peopleNumber;
}
public void setPeopleNumber(Integer peopleNumber) {
this.peopleNumber = peopleNumber;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}

@ -0,0 +1,139 @@
package net.educoder.ecsonar.model;
import java.util.Date;
/**
* @Author: youys
* @Date: 2023/8/31
* @Description:
*/
public class GraduationProjectTaskInfoDetail {
private String id;
/**
* id
*/
private String taskId;
/**
*
*/
private String projectName;
/**
*
*/
private String language;
/**
*
*/
private String studentId;
/**
* id
*/
private Long userId;
/**
* git
*/
private String gitUrl;
/**
*
*/
private String name;
/**
* 0 1 -1
*/
private Integer status;
private Date createTime;
private Date updateTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getGitUrl() {
return gitUrl;
}
public void setGitUrl(String gitUrl) {
this.gitUrl = gitUrl;
}
}

@ -0,0 +1,33 @@
package net.educoder.ecsonar.model.vo;
import lombok.Data;
/**
* @Author: youys
* @Date: 2023/8/30
* @Description:
*/
@Data
public class GraduationProjectQualityInspectVO {
/**
*
*/
private String language;
/**
* id
*/
private Long userId;
/**
* git
*/
private String gitUrl;
/**
*
*/
private String studentId;
/**
*
*/
private String name;
}

@ -32,6 +32,8 @@ public class DbOperateService {
@Autowired @Autowired
private CloudTaskInfoDetailDao cloudTaskInfoDetailDao; private CloudTaskInfoDetailDao cloudTaskInfoDetailDao;
private GraduationProjectTaskInfoDao graduationProjectTaskInfoDao;
/** /**
* idcode * idcode
* *
@ -64,6 +66,10 @@ public class DbOperateService {
taskInfoDetailDao.updateTaskInfoDetailStatus(id, status); taskInfoDetailDao.updateTaskInfoDetailStatus(id, status);
} }
public void updateGraduationProjectTaskInfoDetail(String id, Integer status) {
graduationProjectTaskInfoDao.updateTaskInfoStatus(id, status);
}
public void updateCloudTaskInfoDetail(String id, Integer status) { public void updateCloudTaskInfoDetail(String id, Integer status) {
cloudTaskInfoDetailDao.updateCloudTaskInfoDetailStatus(id, status); cloudTaskInfoDetailDao.updateCloudTaskInfoDetailStatus(id, status);
} }

@ -0,0 +1,175 @@
package net.educoder.ecsonar.services;
import cn.hutool.core.util.RandomUtil;
import net.educoder.ecsonar.dao.*;
import net.educoder.ecsonar.exception.BusinessException;
import net.educoder.ecsonar.model.*;
import net.educoder.ecsonar.model.api.QualityInspect;
import net.educoder.ecsonar.model.api.QualityInspectIsCompleted;
import net.educoder.ecsonar.model.api.QualityInspectResultData;
import net.educoder.ecsonar.model.dto.*;
import net.educoder.ecsonar.model.vo.*;
import net.educoder.ecsonar.task.GraduationProjectQualityInspectRunnable;
import net.educoder.ecsonar.utils.IdUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutorService;
/**
* @Author: youys
* @Date: 2023/8/31
* @Description: service
*/
@Service
public class GraduationProjectQualityInspectService {
private static final Logger LOGGER = LoggerFactory.getLogger(GraduationProjectQualityInspectService.class);
@Autowired
private GraduationProjectTaskInfoDao taskInfoDao;
@Autowired
private GraduationProjectTaskInfoDetailDao taskInfoDetailDao;
@Autowired
private DbOperateService dbOperateService;
@Autowired
private SonarService sonarService;
@Autowired
private ReportService reportService;
@Autowired
private IssuesDao issuesDao;
@Autowired
private ProjectDao projectDao;
@Autowired
private FileSourceDao fileSourceDao;
@Autowired
@Qualifier("sonarScannerPool")
private ExecutorService sonarScannerPool;
@Autowired
@Qualifier("sonarQueryResultPool")
private ExecutorService sonarQueryResultPool;
public QualityInspect graduationProjectQualityInspect(List<GraduationProjectQualityInspectVO> qualityInspectVOList) {
if (CollectionUtils.isEmpty(qualityInspectVOList)) {
throw new BusinessException(2, "用户数据不能为空");
}
GraduationProjectTaskInfo taskInfo = new GraduationProjectTaskInfo();
taskInfo.setId(IdUtils.nextStrId());
taskInfo.setPeopleNumber(qualityInspectVOList.size());
taskInfoDao.insertTaskInfo(taskInfo);
LOGGER.info("taskId:{}, {}个人提交了代码", taskInfo.getId(), qualityInspectVOList.size());
for (GraduationProjectQualityInspectVO qualityInspectVO : qualityInspectVOList) {
GraduationProjectTaskInfoDetail graduationProjectTaskInfoDetail = new GraduationProjectTaskInfoDetail();
graduationProjectTaskInfoDetail.setId(IdUtils.nextStrId());
graduationProjectTaskInfoDetail.setTaskId(taskInfo.getId());
graduationProjectTaskInfoDetail.setProjectName(String.format("%s-%s", qualityInspectVO.getStudentId(), RandomUtil.randomString(8)));
graduationProjectTaskInfoDetail.setName(qualityInspectVO.getName());
graduationProjectTaskInfoDetail.setStudentId(qualityInspectVO.getStudentId());
graduationProjectTaskInfoDetail.setUserId(qualityInspectVO.getUserId());
graduationProjectTaskInfoDetail.setGitUrl(qualityInspectVO.getGitUrl());
graduationProjectTaskInfoDetail.setLanguage(qualityInspectVO.getLanguage());
taskInfoDetailDao.insertTaskInfoDetail(graduationProjectTaskInfoDetail);
// 提交一个sonar扫描任务
GraduationProjectQualityInspectRunnable runnable = new GraduationProjectQualityInspectRunnable(taskInfo.getId(),
qualityInspectVO.getLanguage(), graduationProjectTaskInfoDetail, sonarQueryResultPool);
runnable.setDbOperateService(dbOperateService);
runnable.setSonarService(sonarService);
// 提交任务
sonarScannerPool.execute(runnable);
}
// 提交之后返回taskId
return new QualityInspect(taskInfo.getId());
}
public RollPage<QualityInspectResultData> qualityInspectResultQuery(Integer pageNum, Integer pageSize, String taskId) {
RollPage<QualityInspectResultData> rollPage = new RollPage();
rollPage.setCurrentPage(pageNum);
rollPage.setPageSize(pageSize);
return rollPage;
}
public QualityInspectIsCompleted qualityInspectIsCompleted(String taskId) {
return null;
}
/**
*
* 10 5 3 1
*
* @param resultData
* @return
*/
private BigDecimal calcQualityScore(QualityInspectResultData resultData) {
return null;
}
/**
*
*
* @param analyseDetailVO
* @return
*/
public AnalyseDetailDTO getAnalyseDetail(AnalyseDetailVO analyseDetailVO) {
return null;
}
/**
*
*
* @param analyseDetailListVO
* @return
*/
public RollPage<AnalyseDetailListDTO> getAnalyseDetailList(AnalyseDetailListVO analyseDetailListVO) {
return null;
}
/**
*
*
* @param codeDetailVO
* @return
*/
public CodeDetailDTO getCodeDetail(CodeDetailVO codeDetailVO) {
return null;
}
private void processPageIssues(List<Issues> pageIssues, RollPage<AnalyseDetailListDTO> rollPage) {
}
}

@ -180,6 +180,8 @@ public class SonarService {
if (consumer != null) { if (consumer != null) {
log.info("语言:{},projectName:{},path:{}找到了consumer", language, sonarScannerParam.getProjectKey(),sonarScannerParam.getProjectPath()); log.info("语言:{},projectName:{},path:{}找到了consumer", language, sonarScannerParam.getProjectKey(),sonarScannerParam.getProjectPath());
consumer.accept(sonarScannerParam); consumer.accept(sonarScannerParam);
}else{
sonar(sonarScannerParam.getProjectPath(), sonarScannerParam.getProjectKey());
} }
} }

@ -0,0 +1,92 @@
package net.educoder.ecsonar.task;
import net.educoder.ecsonar.constant.Constant;
import net.educoder.ecsonar.model.GraduationProjectTaskInfoDetail;
import net.educoder.ecsonar.model.SonarScannerParam;
import net.educoder.ecsonar.services.DbOperateService;
import net.educoder.ecsonar.services.SonarService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
/**
* @Author: youys
* @Date: 2023/8/31
* @Description:
*/
public class GraduationProjectQualityInspectRunnable implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(GraduationProjectQualityInspectRunnable.class);
private String taskId;
public String language;
private ExecutorService queryResultPool;
private GraduationProjectTaskInfoDetail taskInfoDetail;
private DbOperateService dbOperateService;
private SonarService sonarService;
private static final String SUCCESS = "SUCCESS";
public GraduationProjectQualityInspectRunnable(String taskId, String language,
GraduationProjectTaskInfoDetail taskInfoDetail,
ExecutorService queryResultPool) {
this.taskId = taskId;
this.language = language;
this.taskInfoDetail = taskInfoDetail;
this.queryResultPool = queryResultPool;
}
@Override
public void run() {
String projectName = taskInfoDetail.getProjectName();
String path = String.format("/tmp/%s/%s/", taskId, projectName);
// 写完所有文件开始用sonar进行质量分析
SonarScannerParam param = new SonarScannerParam(projectName, path);
if (Constant.C.equalsIgnoreCase(language) || Constant.CXX.equalsIgnoreCase(language)) {
String resultPath = String.format("/tmp/%s", taskId);
File f = new File(resultPath);
if (!f.exists()) {
f.mkdirs();
}
param.setCppCheckReportPath(resultPath + String.format("/%s-result.xml", projectName));
}
// 调用sonar服务
LOGGER.info("taskId:{}, projectName:{}, detailId:{} 开始调用sonar分析,语言:{}", taskId, projectName, taskInfoDetail.getId(), language);
// sonarUrl 启动多个,随机一个
sonarService.sonar(language, param);
// 提交一个查结果的任务
GraduationProjectQueryResultRunnable queryResultRunnable = new GraduationProjectQueryResultRunnable(projectName, taskInfoDetail.getId(), dbOperateService);
LOGGER.info("taskId:{}, projectName:{}, detailId:{} 提交了查询结果的任务", taskId, projectName, taskInfoDetail.getId());
Future<String> submit = queryResultPool.submit(queryResultRunnable);
String status;
try {
status = submit.get();
} catch (Exception e) {
LOGGER.error("GraduationProjectQueryResultRunnable is Exception", e);
status = "FAILED";
}
dbOperateService.updateGraduationProjectTaskInfoDetail(taskInfoDetail.getId(), SUCCESS.equals(status) ? 1 : -1);
}
public void setDbOperateService(DbOperateService dbOperateService) {
this.dbOperateService = dbOperateService;
}
public void setSonarService(SonarService sonarService) {
this.sonarService = sonarService;
}
}

@ -0,0 +1,44 @@
package net.educoder.ecsonar.task;
import net.educoder.ecsonar.services.DbOperateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Callable;
/**
* @Author: youys
* @Date: 2023/8/31
* @Description:
*/
public class GraduationProjectQueryResultRunnable implements Callable<String> {
private static final Logger LOGGER = LoggerFactory.getLogger(GraduationProjectQueryResultRunnable.class);
private String projectName;
private String detailId;
private DbOperateService dbOperateService;
public GraduationProjectQueryResultRunnable(String projectName, String detailId, DbOperateService dbOperateService) {
this.projectName = projectName;
this.detailId = detailId;
this.dbOperateService = dbOperateService;
}
@Override
public String call() throws Exception {
int count = 0;
String status;
while ((status = dbOperateService.queryCeActivityStatus(projectName)) == null && count++ <= 60) {
LOGGER.info("detailId:[{}],sonar还未执行完,次数:{}", detailId, count);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
// 查询
LOGGER.info("detailId:[{}], 查询sonar执行状态[{}]", detailId, status);
return status;
}
}

@ -22,7 +22,7 @@ import java.util.concurrent.ExecutorService;
/** /**
* @Author: youys * @Author: youys
* @Date: 2022/1/18 * @Date: 2022/1/18
* @Description: * @Description:
*/ */
public class QualityInspectRunnable implements Runnable { public class QualityInspectRunnable implements Runnable {

@ -7,7 +7,7 @@ import org.slf4j.LoggerFactory;
/** /**
* @Author: youys * @Author: youys
* @Date: 2022/1/18 * @Date: 2022/1/18
* @Description: * @Description:
*/ */
public class SonarQueryResultRunnable implements Runnable { public class SonarQueryResultRunnable implements Runnable {

Loading…
Cancel
Save