From 0797edb1f311a57cb5b5721a8c5f94045bb9a33f Mon Sep 17 00:00:00 2001 From: ysh Date: Thu, 19 Sep 2024 13:43:19 +0900 Subject: [PATCH] workflow Run History Development --- .../mcmp/workflow/Entity/WorkflowHistory.java | 16 +- .../controller/WorkflowHistoryController.java | 9 +- .../entityMappingDto/WorkflowHistoryDto.java | 42 +++-- .../workflow/service/WorkflowService.java | 2 +- .../workflow/service/WorkflowServiceImpl.java | 169 ++++++++++++++---- 5 files changed, 171 insertions(+), 67 deletions(-) diff --git a/src/main/java/kr/co/mcmp/workflow/Entity/WorkflowHistory.java b/src/main/java/kr/co/mcmp/workflow/Entity/WorkflowHistory.java index 4deb2c8..df281fc 100644 --- a/src/main/java/kr/co/mcmp/workflow/Entity/WorkflowHistory.java +++ b/src/main/java/kr/co/mcmp/workflow/Entity/WorkflowHistory.java @@ -21,12 +21,16 @@ public class WorkflowHistory { @JoinColumn(name = "workflow_idx", nullable = false) private Workflow workflow; - @Column(name = "run_script") - private String runScript; + @Column(name = "data_type") + private String dataType; - @Column(name = "run_user_id", nullable = false) - private String runUserId; + @Lob + @Column(name = "data", columnDefinition = "CLOB") + private String data; - @Column(name = "run_date") - private LocalDateTime runDate; + @Column(name = "user_id", nullable = false) + private String userId; + + @Column(name = "date") + private LocalDateTime date; } \ No newline at end of file diff --git a/src/main/java/kr/co/mcmp/workflow/controller/WorkflowHistoryController.java b/src/main/java/kr/co/mcmp/workflow/controller/WorkflowHistoryController.java index a4b1869..15e0817 100644 --- a/src/main/java/kr/co/mcmp/workflow/controller/WorkflowHistoryController.java +++ b/src/main/java/kr/co/mcmp/workflow/controller/WorkflowHistoryController.java @@ -6,10 +6,7 @@ import kr.co.mcmp.workflow.dto.entityMappingDto.WorkflowHistoryDto; import kr.co.mcmp.workflow.service.WorkflowService; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -23,7 +20,7 @@ public class WorkflowHistoryController { @Operation(summary="워크플로우 이력 조회") @GetMapping("/{workflowIdx}") - public ResponseWrapper> getWorkflowHistoryList(@PathVariable Long workflowIdx) { - return new ResponseWrapper<>(workflowService.getWorkflowHistoryList(workflowIdx)); + public ResponseWrapper> getWorkflowHistoryList(@PathVariable Long workflowIdx, @RequestParam String dataType) { + return new ResponseWrapper<>(workflowService.getWorkflowHistoryList(workflowIdx, dataType)); } } diff --git a/src/main/java/kr/co/mcmp/workflow/dto/entityMappingDto/WorkflowHistoryDto.java b/src/main/java/kr/co/mcmp/workflow/dto/entityMappingDto/WorkflowHistoryDto.java index 11a745c..0807865 100644 --- a/src/main/java/kr/co/mcmp/workflow/dto/entityMappingDto/WorkflowHistoryDto.java +++ b/src/main/java/kr/co/mcmp/workflow/dto/entityMappingDto/WorkflowHistoryDto.java @@ -13,18 +13,20 @@ public class WorkflowHistoryDto { private Long workflowHistoryIdx; private Long workflowIdx; - private String runScript; - private String runUserId; - private LocalDateTime runDate; + private String dataType; + private String data; + private String userId; + private LocalDateTime date; // from : 외부 (entity -> dto) public static WorkflowHistoryDto from(WorkflowHistory workflowHistory) { return WorkflowHistoryDto.builder() .workflowHistoryIdx(workflowHistory.getWorkflowHistoryIdx()) .workflowIdx(workflowHistory.getWorkflow().getWorkflowIdx()) - .runScript(workflowHistory.getRunScript()) - .runUserId(workflowHistory.getRunUserId()) - .runDate(workflowHistory.getRunDate()) + .dataType(workflowHistory.getDataType()) + .data(workflowHistory.getData()) + .userId(workflowHistory.getUserId()) + .date(workflowHistory.getDate()) .build(); } @@ -33,9 +35,10 @@ public static WorkflowHistoryDto of(WorkflowHistoryDto workflowHistoryDto) { return WorkflowHistoryDto.builder() .workflowHistoryIdx(workflowHistoryDto.getWorkflowHistoryIdx()) .workflowIdx(workflowHistoryDto.getWorkflowIdx()) - .runScript(workflowHistoryDto.getRunScript()) - .runUserId(workflowHistoryDto.getRunUserId()) - .runDate(workflowHistoryDto.getRunDate()) + .dataType(workflowHistoryDto.getDataType()) + .data(workflowHistoryDto.getData()) + .userId(workflowHistoryDto.getUserId()) + .date(workflowHistoryDto.getDate()) .build(); } @@ -44,9 +47,10 @@ public static WorkflowHistory toEntity(WorkflowHistoryDto workflowHistoryDto, Wo return WorkflowHistory.builder() .workflowHistoryIdx(workflowHistoryDto.getWorkflowHistoryIdx()) .workflow(WorkflowDto.toEntity(workflowDto, ossDto, ossTypeDto)) - .runScript(workflowHistoryDto.getRunScript()) - .runUserId(workflowHistoryDto.getRunUserId()) - .runDate(workflowHistoryDto.getRunDate()) + .dataType(workflowHistoryDto.getDataType()) + .data(workflowHistoryDto.getData()) + .userId(workflowHistoryDto.getUserId()) + .date(workflowHistoryDto.getDate()) .build(); } @@ -55,14 +59,16 @@ public static WorkflowHistory buildEntity( WorkflowDto workflowDto, OssDto ossDto, OssTypeDto ossTypeDto, - String runScript, - String runUserId, - LocalDateTime runDate) { + String dataType, + String data, + String userId, + LocalDateTime date) { return WorkflowHistory.builder() .workflow(WorkflowDto.toEntity(workflowDto, ossDto, ossTypeDto)) - .runScript(runScript) - .runUserId(runUserId) - .runDate(runDate) + .dataType(dataType) + .data(data) + .userId(userId) + .date(date) .build(); } } diff --git a/src/main/java/kr/co/mcmp/workflow/service/WorkflowService.java b/src/main/java/kr/co/mcmp/workflow/service/WorkflowService.java index 820d805..3b192df 100644 --- a/src/main/java/kr/co/mcmp/workflow/service/WorkflowService.java +++ b/src/main/java/kr/co/mcmp/workflow/service/WorkflowService.java @@ -44,7 +44,7 @@ public interface WorkflowService { List getWorkflowTemplate(String workflowName); // Workflow History - List getWorkflowHistoryList(Long workflowIdx); + List getWorkflowHistoryList(Long workflowIdx, String dataType); // Workflow Param List getWorkflowParamList(); diff --git a/src/main/java/kr/co/mcmp/workflow/service/WorkflowServiceImpl.java b/src/main/java/kr/co/mcmp/workflow/service/WorkflowServiceImpl.java index 1206edf..c6dd1ff 100644 --- a/src/main/java/kr/co/mcmp/workflow/service/WorkflowServiceImpl.java +++ b/src/main/java/kr/co/mcmp/workflow/service/WorkflowServiceImpl.java @@ -38,8 +38,10 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.Stream; @Slf4j @RequiredArgsConstructor @@ -80,9 +82,7 @@ public List getWorkflowList() { List list = new ArrayList<>(); workflowList.forEach((workflow)-> { - - Workflow workflowEntity = workflowRepository.findByWorkflowIdx(workflow.getWorkflowIdx()); - WorkflowDto workflowDto = WorkflowDto.from(workflowEntity); + WorkflowDto workflowDto = getWorkflowDto(workflow.getWorkflowIdx()); List paramList = workflowParamRepository.findByWorkflow_WorkflowIdx(workflow.getWorkflowIdx()) @@ -116,8 +116,7 @@ public Long registWorkflow(WorkflowReqDto workflowReqDto) { boolean isCreate = false; // jenkins 정보 조회 - Oss ossEntity = ossRepository.findByOssIdx(workflowReqDto.getWorkflowInfo().getOssIdx()); - OssDto ossDto = OssDto.from(ossEntity); + OssDto ossDto = getOssDto(workflowReqDto.getWorkflowInfo().getOssIdx()); try { // jenkins > job 생성 @@ -129,13 +128,12 @@ public Long registWorkflow(WorkflowReqDto workflowReqDto) { // DB if ( isCreate ) { - OssType ossType = ossTypeRepository.findByOssTypeIdx(ossDto.getOssTypeIdx()); - OssTypeDto ossTypeDto = OssTypeDto.from(ossType); + OssTypeDto ossTypeDto = getOssTypeDto(ossDto.getOssTypeIdx()); // 1. Workflow Workflow workflowEntity = WorkflowDto.toEntity(workflowReqDto.getWorkflowInfo(), ossDto, ossTypeDto); workflowEntity = workflowRepository.save(workflowEntity); - WorkflowDto workflowDto = WorkflowDto.from(workflowEntity); + WorkflowDto workflowDto = getWorkflowDto(workflowEntity.getWorkflowIdx()); // 2. Workflow Param if ( !CollectionUtils.isEmpty(workflowReqDto.getWorkflowParams()) ) { @@ -156,7 +154,6 @@ public Long registWorkflow(WorkflowReqDto workflowReqDto) { } catch (IOException e) { if(isCreate) jenkinsService.deleteJenkinsJob(ossDto, workflowReqDto.getWorkflowInfo().getWorkflowName()); log.error(e.getMessage()); - } return result; } @@ -172,7 +169,7 @@ public Long registWorkflow(WorkflowReqDto workflowReqDto) { public Boolean updateWorkflow(WorkflowReqDto workflowReqDto) { Boolean result = false; try { - OssDto ossDto = OssDto.from(ossRepository.findByOssIdx(workflowReqDto.getWorkflowInfo().getOssIdx())); + OssDto ossDto = getOssDto(workflowReqDto.getWorkflowInfo().getOssIdx()); boolean isUpdate = jenkinsService.updateJenkinsJobPipeline_v2( ossDto, @@ -181,13 +178,12 @@ public Boolean updateWorkflow(WorkflowReqDto workflowReqDto) { workflowReqDto.getWorkflowParams()); if ( isUpdate ) { - OssType ossTypeEntity = ossTypeRepository.findByOssTypeIdx(ossDto.getOssTypeIdx()); - OssTypeDto ossTypeDto = OssTypeDto.from(ossTypeEntity); + OssTypeDto ossTypeDto = getOssTypeDto(ossDto.getOssTypeIdx()); // 1. Workflow Workflow workflowEntity = WorkflowDto.toEntity(workflowReqDto.getWorkflowInfo(), ossDto, ossTypeDto); workflowEntity = workflowRepository.save(workflowEntity); - WorkflowDto workflowDto = WorkflowDto.from(workflowEntity); + WorkflowDto workflowDto = getWorkflowDto(workflowEntity.getWorkflowIdx()); // 2. Workflow Param (삭제 후 재등록) if ( !CollectionUtils.isEmpty(workflowReqDto.getWorkflowParams()) ) { @@ -232,8 +228,7 @@ public Boolean deleteWorkflow(Long workflowIdx) { Workflow workflowEntity = workflowRepository.findByWorkflowIdx(workflowIdx); WorkflowDto workflowDto = WorkflowDto.from(workflowEntity); - Oss ossEntity = ossRepository.findByOssIdx(workflowDto.getOssIdx()); - OssDto ossDto = OssDto.from(ossEntity); + OssDto ossDto = getOssDto(workflowDto.getOssIdx()); boolean isDelete = jenkinsService.deleteJenkinsJob(ossDto, workflowDto.getWorkflowName()); @@ -264,10 +259,9 @@ public Boolean deleteWorkflow(Long workflowIdx) { @Override public WorkflowDetailResDto getWorkflow(Long workflowIdx) { try { - Workflow workflowEntity = workflowRepository.findByWorkflowIdx(workflowIdx); - WorkflowDto workflowDto = WorkflowDto.from(workflowEntity); + WorkflowDto workflowDto = getWorkflowDto(workflowIdx); - List paramList = workflowParamRepository.findByWorkflow_WorkflowIdx(workflowIdx) + List paramList = workflowParamRepository.findByWorkflow_WorkflowIdx(workflowIdx) .stream() .map(WorkflowParamDto::from) .collect(Collectors.toList()); @@ -316,8 +310,7 @@ public Boolean isWorkflowNameDuplicated(String workflowName) { @Override public Boolean runWorkflow(Long workflowIdx) { // 배포 실행 관련 사용자 이력 정보 수정 - Workflow workflow = workflowRepository.findByWorkflowIdx(workflowIdx); - WorkflowDto workflowDto = WorkflowDto.from(workflow); + WorkflowDto workflowDto = getWorkflowDto(workflowIdx); List paramList = workflowParamRepository.findByWorkflow_WorkflowIdx(workflowIdx) .stream() @@ -350,38 +343,66 @@ public Boolean runWorkflowCallback(WorkflowReqDto workflowReqDto) { Map> jenkinsJobParams = null; if(!StringUtils.isEmpty(workflowReqDto.getWorkflowParams())) { - Map> finalJenkinsJobParams = new HashMap<>(); for(WorkflowParamDto param : workflowReqDto.getWorkflowParams()) { finalJenkinsJobParams.put(param.getParamKey(), Arrays.asList(param.getParamValue())); -// List valueList = new ArrayList<>(); -// valueList.add(param.getParamValue()); -// finalJenkinsJobParams.put(param.getParamKey(), valueList); } jenkinsJobParams = finalJenkinsJobParams; } // OSS 접속 정보 조회 - OssType ossTypeEntity = ossTypeRepository.findByOssTypeIdx(workflowReqDto.getWorkflowInfo().getOssIdx()); - OssTypeDto ossTypeDto = OssTypeDto.from(ossTypeEntity); - - Oss ossEntity = ossRepository.findByOssIdx(workflowReqDto.getWorkflowInfo().getOssIdx()); - OssDto ossDto = OssDto.from(ossEntity); + OssTypeDto ossTypeDto = getOssTypeDto(workflowReqDto.getWorkflowInfo().getOssIdx()); + OssDto ossDto = getOssDto(workflowReqDto.getWorkflowInfo().getOssIdx()); // Jenkins Job 실행 int jenkinsBuildId = jenkinsService.buildJenkinsJob(ossDto, workflowReqDto.getWorkflowInfo().getWorkflowName(), jenkinsJobParams); int buildNumber = jenkinsService.getQueueExecutableNumber(ossDto, jenkinsBuildId); // 배포 이력 정보 등록 -// WorkflowHistory WorkflowHistoryEntity = WorkflowHistoryDto.buildEntity(workflowReqDto.getWorkflowInfo(), ossDto, ossTypeDto, workflowReqDto.getWorkflowInfo().getScript(), "root", null); -// workflowHistoryRepository.save(WorkflowHistoryEntity); + saveWorkflowHistory( + workflowReqDto, + ossDto, + ossTypeDto, + "script", + workflowReqDto.getWorkflowInfo().getScript(), + "root", + null); + + for(WorkflowParamDto paramDto : workflowReqDto.getWorkflowParams()) { + saveWorkflowHistory( + workflowReqDto, + ossDto, + ossTypeDto, + "paramKey", + paramDto.getParamKey(), + "root", + null); + + saveWorkflowHistory( + workflowReqDto, + ossDto, + ossTypeDto, + "paramValue", + paramDto.getParamValue(), + "root", + null); + } // Jenkins Job 실행 대기 BuildInfo buildInfo = jenkinsService.waitJenkinsBuild(ossDto, workflowReqDto.getWorkflowInfo().getWorkflowName(), jenkinsBuildId, buildNumber); log.info("BuildInfo ==> {}", buildInfo.toString()); + // TODO : 실행후 액션 필요 (기존 : History에 결과값 추가) + saveWorkflowHistory( + workflowReqDto, + ossDto, + ossTypeDto, + "result", + buildInfo.result(), + "root", + null); return true; } @@ -425,14 +446,29 @@ public List getWorkflowTemplate(String workflowName) { return pipelineService.getWorkflowTemplate(workflowName); } + /** + * Workflow History 목록 + * @param workflowIdx + * @param dataType + * @return + */ @Override - public List getWorkflowHistoryList(Long workflowIdx) { - return workflowHistoryRepository.findByWorkflow_WorkflowIdx(workflowIdx) - .stream() + public List getWorkflowHistoryList(Long workflowIdx, String dataType) { + Stream historyStream = workflowHistoryRepository.findByWorkflow_WorkflowIdx(workflowIdx).stream(); + + if (dataType != null && !dataType.isEmpty()) { + historyStream = historyStream.filter(history -> dataType.equals(history.getDataType())); + } + + return historyStream .map(WorkflowHistoryDto::from) .collect(Collectors.toList()); } + /** + * 파라미터 목록 + * @return + */ @Override public List getWorkflowParamList() { return workflowParamRepository.findAll() @@ -441,6 +477,11 @@ public List getWorkflowParamList() { .collect(Collectors.toList()); } + /** + * Jenkins Job 자동 생성 + * @param ossTypeDto + * @param ossDto + */ public void createJenkinsJob(OssTypeDto ossTypeDto, OssDto ossDto) { try { // OSS 를 등록을 할때 존재하지 않을 경우 Jenkins Job 자동 생성 @@ -464,9 +505,14 @@ public void createJenkinsJob(OssTypeDto ossTypeDto, OssDto ossDto) { } } + /** + * Workflow 로그 + * @param workflowIdx + * @return + */ public List getWorkflowLog(Long workflowIdx) { - WorkflowDto workflowDto = WorkflowDto.from(workflowRepository.findByWorkflowIdx(workflowIdx)); - OssDto ossDto = OssDto.from(ossRepository.findByOssIdx(workflowDto.getOssIdx())); + WorkflowDto workflowDto = getWorkflowDto(workflowIdx); + OssDto ossDto = getOssDto(workflowDto.getOssIdx()); List resList = WorkflowLogResDto.createList(); @@ -489,4 +535,55 @@ public List getWorkflowLog(Long workflowIdx) { } return resList; } + + + /** + * OSSTypeDto 정보 조회 + * @param ossTypeIdx + * @return + */ + public OssTypeDto getOssTypeDto(Long ossTypeIdx) { + OssType ossType = ossTypeRepository.findByOssTypeIdx(ossTypeIdx); + return OssTypeDto.from(ossType); + } + /** + * OSSDto 정보 조회 + * @param ossIdx + * @return + */ + public OssDto getOssDto(Long ossIdx) { + Oss ossEntity = ossRepository.findByOssIdx(ossIdx); + return OssDto.from(ossEntity); + } + + /** + * WorkflowDto 정보 조회 + * @param workflowIdx + * @return + */ + public WorkflowDto getWorkflowDto(Long workflowIdx) { + Workflow workflow = workflowRepository.findByWorkflowIdx(workflowIdx); + return WorkflowDto.from(workflow); + } + + /** + * WorkflowHistory 저장 + * @param workflowReqDto + * @param ossDto + * @param ossTypeDto + * @param dataType + * @param data + */ + private void saveWorkflowHistory(WorkflowReqDto workflowReqDto, OssDto ossDto, OssTypeDto ossTypeDto, String dataType, String data, String userId, LocalDateTime date) { + WorkflowHistory workflowHistoryEntity = WorkflowHistoryDto.buildEntity( + workflowReqDto.getWorkflowInfo(), + ossDto, + ossTypeDto, + dataType, + data, + userId, + date + ); + workflowHistoryRepository.save(workflowHistoryEntity); + } }