云脑质量分析接口

dev
youys 2 years ago
parent 8361590210
commit 9cba668ba3

@ -17,4 +17,6 @@ public class Constant {
public static final String PYTHON = "python";
public static final List<String> language = Arrays.asList(JAVA, C, CXX, PYTHON);
public static final String SUCCESS = "SUCCESS";
}

@ -0,0 +1,53 @@
package net.educoder.ecsonar.controller;
import com.alibaba.fastjson.JSONObject;
import net.educoder.ecsonar.model.api.QualityInspect;
import net.educoder.ecsonar.model.api.QualityInspectIsCompleted;
import net.educoder.ecsonar.model.vo.CloudBrainQualityInspectVO;
import net.educoder.ecsonar.services.CloudBrainService;
import net.educoder.ecsonar.utils.ResponseResult;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @Author: youys
* @Date: 2022/7/5
* @Description:
*/
@RestController
@RequestMapping("/api")
public class CloudBrainController {
private final Logger logger = LoggerFactory.getLogger(CloudBrainController.class);
@Autowired
private CloudBrainService cloudBrainService;
@PostMapping("/cloudQualityInspect")
public ResponseResult cloudQualityInspect(@RequestBody List<CloudBrainQualityInspectVO> cloudBrainQualityInspectVO) {
logger.info("cloudQualityInspect请求参数:{}", JSONObject.toJSONString(cloudBrainQualityInspectVO));
if(CollectionUtils.isEmpty(cloudBrainQualityInspectVO)){
ResponseResult.error("参数不能为空");
}
QualityInspect qualityInspect = cloudBrainService.startQualityInspect(cloudBrainQualityInspectVO);
return ResponseResult.success(qualityInspect);
}
/**
*
* @param taskId
* @return
*/
@GetMapping(value = "/cloudQualityInspectIsCompleted/{taskId}")
public ResponseResult<QualityInspectIsCompleted> cloudQualityInspectIsCompleted(@PathVariable String taskId){
QualityInspectIsCompleted result = cloudBrainService.cloudQualityInspectIsCompleted(taskId);
return ResponseResult.success(result);
}
}

@ -0,0 +1,39 @@
package net.educoder.ecsonar.dao;
import net.educoder.ecsonar.model.CloudTaskInfo;
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: 2022/7/5
* @Description:
*/
public interface CloudTaskInfoDao {
/**
*
* @param cloudTaskInfo
*/
@Insert("insert into cloud_task_info(id, people_number) values(#{id},#{peopleNumber})")
void insertCloudTaskInfo(CloudTaskInfo cloudTaskInfo);
/**
*
* @param id
* @return
*/
@Select("select id,status,people_number peopleNumber from cloud_task_info where id=#{id}")
CloudTaskInfo queryById(String id);
/**
*
* @param id
* @param status
*/
@Update("update cloud_task_info set status=#{status} where id=#{id}")
void updateCloudTaskInfoStatus(@Param("id") String id, @Param("status") Integer status);
}

@ -0,0 +1,36 @@
package net.educoder.ecsonar.dao;
import net.educoder.ecsonar.model.CloudTaskInfoDetail;
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: 2022/1/18
* @Description:
*/
public interface CloudTaskInfoDetailDao {
/**
*
*
* @param cloudTaskInfoDetail
*/
@Insert("insert into cloud_task_info_detail(id,task_id,project_name,user_id,language,git_url) values(#{id},#{taskId},#{projectName},#{userId},#{language},#{gitUrl} )")
void insertCloudTaskInfoDetail(CloudTaskInfoDetail cloudTaskInfoDetail);
/**
*
* @param id
* @param status
*/
@Update("update cloud_task_info_detail set status=#{status},update_time=current_timestamp where id=#{id}")
void updateCloudTaskInfoDetailStatus(@Param("id") String id, @Param("status") Integer status);
@Select("select count(1) from cloud_task_info_detail where task_id=#{taskId} and status=1")
Integer selectCountByTaskId(String taskId);
}

@ -0,0 +1,60 @@
package net.educoder.ecsonar.model;
import java.util.Date;
/**
* @Author: youys
* @Date: 2022/7/5
* @Description:
*/
public class CloudTaskInfo {
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,96 @@
package net.educoder.ecsonar.model;
import java.util.Date;
/**
* @Author: youys
* @Date: 2022/7/5
* @Description:
*/
public class CloudTaskInfoDetail {
private String id;
private String taskId;
private String projectName;
private String userId;
private String language;
private String gitUrl;
/**
* 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 getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
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 getGitUrl() {
return gitUrl;
}
public void setGitUrl(String gitUrl) {
this.gitUrl = gitUrl;
}
}

@ -0,0 +1,62 @@
package net.educoder.ecsonar.model.vo;
/**
* @Author: youys
* @Date: 2022/7/5
* @Description:
*/
public class CloudBrainQualityInspectVO {
/**
* id
*/
private String userId;
/**
* id
*/
private String gitUrl;
/**
*
*/
private String projectName;
/**
*
*/
private String language;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getGitUrl() {
return gitUrl;
}
public void setGitUrl(String gitUrl) {
this.gitUrl = gitUrl;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
}

@ -0,0 +1,111 @@
package net.educoder.ecsonar.services;
import net.educoder.ecsonar.dao.CloudTaskInfoDao;
import net.educoder.ecsonar.dao.CloudTaskInfoDetailDao;
import net.educoder.ecsonar.exception.BusinessException;
import net.educoder.ecsonar.model.CloudTaskInfo;
import net.educoder.ecsonar.model.CloudTaskInfoDetail;
import net.educoder.ecsonar.model.api.QualityInspect;
import net.educoder.ecsonar.model.api.QualityInspectIsCompleted;
import net.educoder.ecsonar.model.vo.CloudBrainQualityInspectVO;
import net.educoder.ecsonar.task.CloudBrainQualityInspectRunnable;
import net.educoder.ecsonar.utils.IdUtils;
import net.educoder.ecsonar.utils.SystemUtil;
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 org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.concurrent.ExecutorService;
/**
* @Author: youys
* @Date: 2022/7/5
* @Description:
*/
@Service
public class CloudBrainService {
@Autowired
private CloudTaskInfoDao cloudTaskInfoDao;
@Autowired
private CloudTaskInfoDetailDao cloudTaskInfoDetailDao;
@Autowired
private SonarService sonarService;
@Autowired
private DbOperateService dbOperateService;
@Autowired
@Qualifier("sonarScannerPool")
private ExecutorService sonarScannerPool;
@Autowired
@Qualifier("sonarQueryResultPool")
private ExecutorService sonarQueryResultPool;
@Transactional(rollbackFor = Exception.class)
public QualityInspect startQualityInspect(List<CloudBrainQualityInspectVO> cloudBrainQualityInspectVO) {
CloudTaskInfo cloudTaskInfo = new CloudTaskInfo();
cloudTaskInfo.setId(IdUtils.nextStrId());
cloudTaskInfo.setPeopleNumber(cloudBrainQualityInspectVO.size());
cloudTaskInfoDao.insertCloudTaskInfo(cloudTaskInfo);
for (CloudBrainQualityInspectVO brainQualityInspectVO : cloudBrainQualityInspectVO) {
// taskId-userId-projectName
String finalProjectName = String.format("%s-%s-%s", cloudTaskInfo.getId(), brainQualityInspectVO.getUserId(), brainQualityInspectVO.getProjectName());
CloudTaskInfoDetail cloudTaskInfoDetail = new CloudTaskInfoDetail();
cloudTaskInfoDetail.setId(IdUtils.nextStrId());
cloudTaskInfoDetail.setTaskId(cloudTaskInfo.getId());
cloudTaskInfoDetail.setLanguage(brainQualityInspectVO.getLanguage());
cloudTaskInfoDetail.setProjectName(finalProjectName);
cloudTaskInfoDetail.setUserId(brainQualityInspectVO.getUserId());
cloudTaskInfoDetail.setGitUrl(brainQualityInspectVO.getGitUrl());
cloudTaskInfoDetailDao.insertCloudTaskInfoDetail(cloudTaskInfoDetail);
// 创建一个扫描任务
CloudBrainQualityInspectRunnable runnable = new CloudBrainQualityInspectRunnable(cloudTaskInfoDetail, sonarService, dbOperateService, sonarQueryResultPool);
sonarScannerPool.execute(runnable);
}
return new QualityInspect(cloudTaskInfo.getId());
}
public QualityInspectIsCompleted cloudQualityInspectIsCompleted(String taskId) {
CloudTaskInfo cloudTaskInfo = cloudTaskInfoDao.queryById(taskId);
if (cloudTaskInfo == null) {
throw new BusinessException(2, "任务id不存在");
}
if (cloudTaskInfo.getStatus() == 1) {
return new QualityInspectIsCompleted(taskId, cloudTaskInfo.getStatus());
}
Integer taskNumber = cloudTaskInfoDetailDao.selectCountByTaskId(taskId);
int completed = cloudTaskInfo.getPeopleNumber().equals(taskNumber) ? 1 : 0;
// 数据库未更新状态但sonar任务已经全部执行完更新task_info状态
if (cloudTaskInfo.getStatus() == 0 && completed == 1) {
cloudTaskInfoDao.updateCloudTaskInfoStatus(cloudTaskInfo.getId(), 1);
// 并且删除文件 /tmp/$taskId /tmp/homeworkId/*
// String command = String.format("rm -rf /tmp/%s && rm -rf /tmp/%s/*", taskId, taskInfo.getHomeworkId());
// SystemUtil.executeAndGetExitStatus(command);
}
QualityInspectIsCompleted qualityInspectIsCompleted = new QualityInspectIsCompleted(taskId, completed);
return qualityInspectIsCompleted;
}
}

@ -2,13 +2,9 @@ package net.educoder.ecsonar.services;
import net.educoder.ecsonar.config.DynamicDataSourceConfig;
import net.educoder.ecsonar.config.DynamicDataSourceContextHolder;
import net.educoder.ecsonar.dao.CeActivityDao;
import net.educoder.ecsonar.dao.GameCodesDao;
import net.educoder.ecsonar.dao.ProjectDao;
import net.educoder.ecsonar.dao.TaskInfoDetailDao;
import net.educoder.ecsonar.dao.*;
import net.educoder.ecsonar.model.GameCodes;
import net.educoder.ecsonar.model.Project;
import net.educoder.ecsonar.model.TaskInfoDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -33,12 +29,16 @@ public class DbOperateService {
@Autowired
private TaskInfoDetailDao taskInfoDetailDao;
@Autowired
private CloudTaskInfoDetailDao cloudTaskInfoDetailDao;
/**
* idcode
*
* @param id
* @return
*/
public GameCodes queryGameCodesById(Long id){
public GameCodes queryGameCodesById(Long id) {
DynamicDataSourceContextHolder.setContextKey(DynamicDataSourceConfig.READONLY);
GameCodes gameCode = gameCodesDao.queryOriginalCodeById(id);
DynamicDataSourceContextHolder.removeContextKey();
@ -47,21 +47,26 @@ public class DbOperateService {
/**
*
*
* @param projectName
* @return
*/
public String queryCeActivityStatus(String projectName){
public String queryCeActivityStatus(String projectName) {
Project project = projectDao.findByName(projectName);
if(project == null ){
if (project == null) {
return null;
}
return ceActivityDao.queryActivityStatus(project.getProject_uuid());
}
public void updateTaskInfoDetail(String id, Integer status){
public void updateTaskInfoDetail(String id, Integer status) {
taskInfoDetailDao.updateTaskInfoDetailStatus(id, status);
}
public void updateCloudTaskInfoDetail(String id, Integer status) {
cloudTaskInfoDetailDao.updateCloudTaskInfoDetailStatus(id, status);
}
}

@ -0,0 +1,80 @@
package net.educoder.ecsonar.task;
import com.alibaba.fastjson.JSONObject;
import net.educoder.ecsonar.model.CloudTaskInfoDetail;
import net.educoder.ecsonar.model.SonarScannerParam;
import net.educoder.ecsonar.services.DbOperateService;
import net.educoder.ecsonar.services.SonarService;
import net.educoder.ecsonar.utils.SystemUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutorService;
/**
* @Author: youys
* @Date: 2022/7/5
* @Description: 线
*/
public class CloudBrainQualityInspectRunnable implements Runnable {
private final Logger LOGGER = LoggerFactory.getLogger(CloudBrainQualityInspectRunnable.class);
private CloudTaskInfoDetail cloudTaskInfoDetail;
private SonarService sonarService;
private DbOperateService dbOperateService;
private ExecutorService queryResultPool;
public CloudBrainQualityInspectRunnable(CloudTaskInfoDetail cloudTaskInfoDetail,
SonarService sonarService,
DbOperateService dbOperateService,
ExecutorService queryResultPool) {
this.cloudTaskInfoDetail = cloudTaskInfoDetail;
this.sonarService = sonarService;
this.dbOperateService = dbOperateService;
this.queryResultPool = queryResultPool;
}
@Override
public void run() {
String projectName = cloudTaskInfoDetail.getProjectName();
// 调用sonar服务
LOGGER.info("projectName:{}, detailId:{} 开始调用sonar分析,语言:{}", projectName, cloudTaskInfoDetail.getId(), cloudTaskInfoDetail.getLanguage());
// 需要先git clone 到某个目录下面
String path = String.format("/tmp/%s", projectName);
SystemUtil.ExecuteResp executeResp = gitClone(path, cloudTaskInfoDetail.getGitUrl());
if (executeResp.getStatus() != 0) {
LOGGER.warn("projectName:{} git clone失败", cloudTaskInfoDetail.getProjectName());
return;
}
String projectKey = projectName;
String projectPath = path;
sonarService.sonar(cloudTaskInfoDetail.getLanguage(), new SonarScannerParam(projectKey, projectPath));
CloudBrainQueryResultRunnable queryResultRunnable = new CloudBrainQueryResultRunnable(projectName, cloudTaskInfoDetail.getId(), dbOperateService);
queryResultPool.execute(queryResultRunnable);
}
/**
*
*
* @param path
* @param gitUrl
* @return
*/
private SystemUtil.ExecuteResp gitClone(String path, String gitUrl) {
String command = StringUtils.join("rm -rf ", path, " && mkdir -p ", path, " && cd ", path, " && git clone ", gitUrl);
LOGGER.info("git clonecommand:{}", command);
SystemUtil.ExecuteResp executeResp = SystemUtil.executeAndGetExitStatus(command);
LOGGER.info("git clone结果:{}", JSONObject.toJSONString(executeResp));
return executeResp;
}
}

@ -0,0 +1,45 @@
package net.educoder.ecsonar.task;
import net.educoder.ecsonar.constant.Constant;
import net.educoder.ecsonar.services.DbOperateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Author: youys
* @Date: 2022/7/5
* @Description:
*/
public class CloudBrainQueryResultRunnable implements Runnable {
private final Logger LOGGER = LoggerFactory.getLogger(CloudBrainQueryResultRunnable.class);
private String projectName;
private String detailId;
private DbOperateService dbOperateService;
public CloudBrainQueryResultRunnable(String projectName, String detailId, DbOperateService dbOperateService) {
this.projectName = projectName;
this.detailId = detailId;
this.dbOperateService = dbOperateService;
}
@Override
public void run() {
int count = 0;
String status;
while ((status = dbOperateService.queryCeActivityStatus(projectName)) == null && count++ <= 60) {
LOGGER.info("CloudBrainQueryResultRunnable#detailId:[{}],sonar还未执行完,次数:{}", detailId, count);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
// 查询
LOGGER.info("CloudBrainQueryResultRunnable#detailId:[{}], 查询sonar执行状态[{}]", detailId, status);
dbOperateService.updateCloudTaskInfoDetail(detailId, Constant.SUCCESS.equals(status) ? 1 : -1);
}
}
Loading…
Cancel
Save