|
|
@@ -1,12 +1,17 @@
|
|
|
package nju.seec.SEECdemo.logic.service.impl.assignment;
|
|
|
|
|
|
import nju.seec.SEECdemo.data.dao.assignment.CodeDAO;
|
|
|
+import nju.seec.SEECdemo.data.dao.assignment.CodeResultDAO;
|
|
|
import nju.seec.SEECdemo.data.entity.assignment.Code;
|
|
|
+import nju.seec.SEECdemo.data.entity.assignment.CodeResult;
|
|
|
import nju.seec.SEECdemo.logic.api.GitlabApi;
|
|
|
import nju.seec.SEECdemo.logic.service.CodeService;
|
|
|
+import nju.seec.SEECdemo.logic.service.InterCodeService;
|
|
|
+import nju.seec.SEECdemo.logic.service.impl.assignment.mock.MockGroup;
|
|
|
import nju.seec.SEECdemo.logic.service.impl.assignment.stub.CourseServiceStub;
|
|
|
import nju.seec.SEECdemo.logic.service.impl.assignment.stub.GroupServiceStub;
|
|
|
import nju.seec.SEECdemo.logic.vo.CodeWorkVO;
|
|
|
+import nju.seec.SEECdemo.logic.vo.InterCodeAppInfoVO;
|
|
|
import nju.seec.SEECdemo.util.AsyncWrapper;
|
|
|
import nju.seec.SEECdemo.util.ToolKit;
|
|
|
import nju.seec.SEECdemo.util.enums.AssignmentStatus;
|
|
|
@@ -17,22 +22,24 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@Component
|
|
|
-public class CodeServiceImpl implements CodeService {
|
|
|
+public class CodeServiceImpl implements CodeService, InterCodeService {
|
|
|
|
|
|
- private final CodeDAO assignmentDAO;
|
|
|
- private final GitlabApi gitlabApi;
|
|
|
+ private final CodeDAO codeDAO;
|
|
|
+ private final CodeResultDAO codeResultDAO;
|
|
|
private final AssignmentPaperBuilder assignmentPaperBuilder;
|
|
|
private final AsyncWrapper asyncWrapper;
|
|
|
private final CourseServiceStub courseServiceStub;
|
|
|
private final GroupServiceStub groupServiceStub;
|
|
|
|
|
|
@Autowired
|
|
|
- public CodeServiceImpl(CodeDAO assignmentDAO, GitlabApi gitlabApi, AssignmentPaperBuilder assignmentPaperBuilder,
|
|
|
+ public CodeServiceImpl(CodeDAO codeDAO, CodeResultDAO codeResultDAO, AssignmentPaperBuilder assignmentPaperBuilder,
|
|
|
AsyncWrapper asyncWrapper, CourseServiceStub courseServiceStub, GroupServiceStub groupServiceStub){
|
|
|
- this.assignmentDAO = assignmentDAO;
|
|
|
- this.gitlabApi = gitlabApi;
|
|
|
+ this.codeDAO = codeDAO;
|
|
|
+ this.codeResultDAO = codeResultDAO;
|
|
|
this.assignmentPaperBuilder = assignmentPaperBuilder;
|
|
|
this.asyncWrapper = asyncWrapper;
|
|
|
this.courseServiceStub = courseServiceStub;
|
|
|
@@ -42,22 +49,91 @@ public class CodeServiceImpl implements CodeService {
|
|
|
|
|
|
@Override
|
|
|
public CodeWorkVO createAssignment(int courseId, CodeWorkDTO codeWorkDTO) {
|
|
|
- Code assignment = new Code();
|
|
|
- assignment.setName(codeWorkDTO.getName());
|
|
|
- assignment.setDescription(codeWorkDTO.getDescription());
|
|
|
- assignment.setQuestionId(codeWorkDTO.getQuestion());
|
|
|
- assignment.setUuid(ToolKit.randomUUID());
|
|
|
- assignment.setCourseId(courseId);
|
|
|
+ Code codework = new Code();
|
|
|
+ codework.setName(codeWorkDTO.getName());
|
|
|
+ codework.setDescription(codeWorkDTO.getDescription());
|
|
|
+ codework.setQuestionId(codeWorkDTO.getQuestion());
|
|
|
+ codework.setUuid(ToolKit.randomUUID());
|
|
|
+ codework.setCourseId(courseId);
|
|
|
+ codework.setStartAt(codeWorkDTO.getStartAt());
|
|
|
+ codework.setEndAt(codeWorkDTO.getEndAt());
|
|
|
try {
|
|
|
- assignment.setId(gitlabApi.createProject(assignment.getUuid()));
|
|
|
- } catch (Exception e) {
|
|
|
+ assignmentPaperBuilder.buildCodePaper(codework);
|
|
|
+ } catch (IOException | GitAPIException | GitLabApiException e) {
|
|
|
System.out.println("创建作业时发生异常" + ToolKit.format(e));
|
|
|
}
|
|
|
- asyncWrapper.asyncInvoke(() -> assignmentPaperBuilder.buildCodePaper(assignment.getId()), 2000);
|
|
|
- return new CodeWorkVO(assignmentDAO.save(assignment), courseServiceStub.getCourse(courseId).getName(),
|
|
|
+ asyncWrapper.asyncInvoke(()->assignmentPaperBuilder.addGroupsToPaper(codework.getId()), 2000);
|
|
|
+ return new CodeWorkVO(codeDAO.save(codework),courseServiceStub.getCourse(courseId).getName(),
|
|
|
groupServiceStub.getGroupListByCourse(courseId));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取jenkins的构建job名称
|
|
|
+ * @param codeAssignmentId
|
|
|
+ * @param groupId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getJenkinsBuildJobName(int codeAssignmentId, int groupId) {
|
|
|
+ Code code = codeDAO.findById(codeAssignmentId);
|
|
|
+ CodeResult codeResult = codeResultDAO.findByCodeAndGroupId(code, groupId);
|
|
|
+ return codeResult!=null?codeResult.getBuildJob():null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询一个作业下一个小组的应部署应用必要信息
|
|
|
+ * @param codeAssignmentId 作业id
|
|
|
+ * @param groupId 小组id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public InterCodeAppInfoVO getInfo(int codeAssignmentId, int groupId) {
|
|
|
+ Code code = codeDAO.findById(codeAssignmentId);
|
|
|
+ CodeResult codeResult = codeResultDAO.findByCodeAndGroupId(code, groupId);
|
|
|
+ InterCodeAppInfoVO codeAppInfoVO = new InterCodeAppInfoVO();
|
|
|
+ codeAppInfoVO.setId(codeResult.getId());
|
|
|
+ codeAppInfoVO.setProjectId(codeResult.getDeployNamespace());
|
|
|
+ codeAppInfoVO.setApplicationName(code.getUuid() + codeResult.getGroupId());
|
|
|
+ return codeAppInfoVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取jenkins的测试job名称
|
|
|
+ * @param codeAssignmentId
|
|
|
+ * @param groupId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String getJenkinsTestJobName(int codeAssignmentId, int groupId) {
|
|
|
+ Code code = codeDAO.findById(codeAssignmentId);
|
|
|
+ CodeResult codeResult = codeResultDAO.findByCodeAndGroupId(code, groupId);
|
|
|
+ return codeResult!=null?codeResult.getTestJob():null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void groupAddedCheck(int groupId){
|
|
|
+ MockGroup group = groupServiceStub.getGroup(groupId);
|
|
|
+ List<AssignmentStatus> statuses = new ArrayList(){{
|
|
|
+ add(AssignmentStatus.AVAILABLE);
|
|
|
+ add(AssignmentStatus.PREPARING);
|
|
|
+ }};
|
|
|
+ try {
|
|
|
+ List<Code> codeList = codeDAO.findByCourseIdAndStatusIn(group.getCourseId(), statuses);
|
|
|
+ List<Code> preparingList = new ArrayList<>();
|
|
|
+ // TODO
|
|
|
+ // 对于AVAILABLE的作业,直接将组加入;对于PREPARING的作业,需要等到其状态变更结束后(AVAILABLE或UNAVAILABLE),
|
|
|
+ // 再根据其状态判断:AVAILABLE,判断该组是否已经在作业中,不在则加入;UNAVAILABLE,则不做操作。
|
|
|
+ for (Code code:codeList) {
|
|
|
+ if (code.getStatus()==AssignmentStatus.AVAILABLE) {
|
|
|
+ assignmentPaperBuilder.addGroupToPaper(code, group);
|
|
|
+ } else {
|
|
|
+ preparingList.add(code);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException | GitLabApiException e) {
|
|
|
+ System.out.println("将小组加入作业时发生异常" + ToolKit.format(e));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|