package kr.re.etri.autoflow.controllers; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import kr.re.etri.autoflow.entity.ExperimentsEntity; import kr.re.etri.autoflow.payload.request.ProjectBaseSearchRequest; import kr.re.etri.autoflow.service.ExperimentsService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springdoc.core.annotations.ParameterObject; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; import java.util.Map; @Tag(name = "Experiments", description = "Kubeflow 및 MLflow Experiment API") @RestController @RequestMapping("/api/experiments") @RequiredArgsConstructor @Slf4j public class ExperimentsController { private final ExperimentsService experimentsService; private final WebClient.Builder webClientBuilder; @Value("${kubeflow.url}") private String kubeflowBaseUrl; // 예: http://192.168.10.135:32473/ @Operation(summary = "모든 Experiments 조회") @GetMapping public ResponseEntity> getAllExperiments() { return ResponseEntity.ok(experimentsService.findAll()); } @Operation(summary = "Experiment 단건 조회") @GetMapping("/{id}") public ResponseEntity getExperiment( @Parameter(description = "Experiment ID", example = "1") @PathVariable("id") Long id) { return experimentsService.findById(id) .map(ResponseEntity::ok) .orElse(ResponseEntity.notFound().build()); } @Operation(summary = "Experiment 검색 및 페이지네이션") @GetMapping("/search") public ResponseEntity> searchExperiments( @ParameterObject @ModelAttribute ProjectBaseSearchRequest request) { Page page = experimentsService.search(request); return ResponseEntity.ok(page); } // @Operation(summary = "Experiment 등록") // @PostMapping // public Mono> createExperiment(@RequestBody ExperimentsEntity experiment) { // // // 1️⃣ DB 저장 // ExperimentsEntity saved = experimentsService.save(experiment); // // // 2️⃣ Kubeflow POST 요청 payload // Map payload = new HashMap<>(); // payload.put("display_name", saved.getDisplayName()); // payload.put("description", saved.getDescription()); // payload.put("namespace", "default"); // 필요에 따라 변경 // // // 3️⃣ WebClient POST // return webClientBuilder.build() // .post() // .uri(kubeflowBaseUrl + "/apis/v2beta1/experiments") // .contentType(MediaType.APPLICATION_JSON) // .bodyValue(payload) // .retrieve() // .bodyToMono(Map.class) // Kubeflow 응답 // .map(resp -> { // // resp에서 필요한 값 추출 후 entity에 반영 // if(resp.get("id") != null) { // saved.setKubeFlowId(resp.get("id").toString()); // } // // if (resp.get("storage_state") != null) { // saved.setKubeflowStorageState(resp.get("storage_state").toString()); // } // // if (resp.get("created_at") != null) { // String lastRunStr = resp.get("created_at").toString(); // OffsetDateTime odt = OffsetDateTime.parse(lastRunStr); // saved.setKubeflowCreatedAt(odt.withOffsetSameInstant(ZoneId.of("Asia/Seoul").getRules().getOffset(odt.toInstant())) // .toLocalDateTime()); // } // // if (resp.get("last_run_created_at") != null) { // String lastRunStr = resp.get("last_run_created_at").toString(); // OffsetDateTime odt = OffsetDateTime.parse(lastRunStr); // saved.setKubeflowLastRunCreatedAt(odt.withOffsetSameInstant(ZoneId.of("Asia/Seoul").getRules().getOffset(odt.toInstant())) // .toLocalDateTime()); // } // return saved; // }) // .map(ResponseEntity::ok) // .doOnError(e -> log.error("Kubeflow experiment 등록 실패", e)); // } @Operation(summary = "Experiment 등록") @PostMapping public Mono> createExperiment(@RequestBody ExperimentsEntity experiment) { // 1️⃣ DB 저장 ExperimentsEntity saved = experimentsService.save(experiment); // 2️⃣ Kubeflow POST 요청 payload Map payload = new HashMap<>(); payload.put("display_name", saved.getDisplayName()); payload.put("description", saved.getDescription()); payload.put("namespace", "default"); // 필요에 따라 변경 // 날짜를 ISO 8601 UTC 포맷으로 DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT; payload.computeIfPresent("created_at", (k, v) -> saved.getKubeflowCreatedAt() != null ? saved.getKubeflowCreatedAt().atZone(ZoneId.of("Asia/Seoul")).format(formatter) : null ); // 3️⃣ WebClient POST return webClientBuilder.build() .post() .uri(kubeflowBaseUrl + "/apis/v2beta1/experiments") .contentType(MediaType.APPLICATION_JSON) .bodyValue(payload) .retrieve() .bodyToMono(Map.class) // 응답 필요 없으면 Void.class .doOnNext(resp -> log.info("Kubeflow experiment 등록 완료: {}", resp)) .then(Mono.just(ResponseEntity.ok(saved))) // DB 저장된 엔티티 반환 .doOnError(e -> log.error("Kubeflow experiment 등록 실패", e)); } @Operation(summary = "Experiment 수정") @PutMapping("/{id}") public ResponseEntity updateExperiment( @Parameter(description = "Experiment ID", example = "1") @PathVariable("id") Long id, @RequestBody ExperimentsEntity experiment) { return experimentsService.findById(id) .map(existing -> { experiment.setId(id); return ResponseEntity.ok(experimentsService.save(experiment)); }) .orElse(ResponseEntity.notFound().build()); } @Operation(summary = "Experiment 삭제") @DeleteMapping("/{id}") public ResponseEntity deleteExperiment( @Parameter(description = "Experiment ID", example = "1") @PathVariable("id") Long id) { if (experimentsService.deleteById(id)) { return ResponseEntity.noContent().build(); } return ResponseEntity.notFound().build(); } }