[KC-108] 분석도엽 ai api호출 테스트

This commit is contained in:
2026-01-12 13:42:25 +09:00
parent e42c99cd60
commit 0b45f42528
10 changed files with 155 additions and 71 deletions

View File

@@ -1,24 +0,0 @@
package com.kamco.cd.kamcoback.config;
import io.netty.channel.ChannelOption;
import java.time.Duration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
@Configuration
public class WebClientConfig {
@Bean
public WebClient webClient() {
HttpClient httpClient =
HttpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
.responseTimeout(Duration.ofSeconds(5));
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
}
}

View File

@@ -0,0 +1,30 @@
package com.kamco.cd.kamcoback.config.resttemplate;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
@RequiredArgsConstructor
public class ExternalHttpClient {
private final RestTemplate restTemplate;
public ResponseEntity<String> exchange(
String url, HttpMethod method, Object body, HttpHeaders headers) {
HttpEntity<Object> entity = new HttpEntity<>(body, headers);
return restTemplate.exchange(url, method, entity, String.class);
}
public ExternalCallResult call(String url, HttpMethod method, Object body, HttpHeaders headers) {
ResponseEntity<String> res = exchange(url, method, body, headers);
int code = res.getStatusCodeValue();
return new ExternalCallResult(code, code >= 200 && code < 300, res.getBody());
}
public record ExternalCallResult(int statusCode, boolean success, String body) {}
}

View File

@@ -0,0 +1,21 @@
package com.kamco.cd.kamcoback.config.resttemplate;
import java.time.Duration;
import java.util.List;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.connectTimeout(Duration.ofSeconds(2))
.readTimeout(Duration.ofSeconds(3))
.additionalInterceptors(List.of(new RetryInterceptor()))
.build();
}
}

View File

@@ -0,0 +1,49 @@
package com.kamco.cd.kamcoback.config.resttemplate;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
public class RetryInterceptor implements ClientHttpRequestInterceptor {
private static final int MAX_RETRY = 3;
private static final long WAIT_MILLIS = 3000;
@Override
public ClientHttpResponse intercept(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
IOException lastException = null;
for (int attempt = 1; attempt <= MAX_RETRY; attempt++) {
try {
// HTTP 응답을 받으면(2xx/4xx/5xx 포함) 그대로 반환
return execution.execute(request, body);
} catch (IOException e) {
// 네트워크/타임아웃 등 I/O 예외만 재시도
lastException = e;
}
// 마지막 시도가 아니면 대기
if (attempt < MAX_RETRY) {
sleep();
}
}
// 마지막 예외를 그대로 던져서 원인이 로그에 남게 함
throw lastException;
}
private void sleep() throws IOException {
try {
TimeUnit.MILLISECONDS.sleep(WAIT_MILLIS);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new IOException("Retry interrupted", ie);
}
}
}

View File

@@ -170,7 +170,7 @@ public class InferenceResultDto {
@NotNull
private Integer targetYyyy;
@Schema(description = "분석대상 도엽 - 전체(ALL), 부분(PART)", example = "PART")
@Schema(description = "분석대상 도엽 - 전체(ALL), 부분(PART)", example = "ALL")
@NotBlank
@EnumValid(enumClass = MapSheetScope.class, message = "분석대상 도엽 옵션은 '전체', '부분' 만 사용 가능합니다.")
private String mapSheetScope;
@@ -184,8 +184,16 @@ public class InferenceResultDto {
@Schema(
description = "5k 도협 번호 목록",
example = "[\"37914034\",\"37914024\",\"37914023\",\"37914014\",\"37914005\",\"37914004\"]")
example = "[{\"mapSheetNum\":37914034,\"mapSheetName\":\"죽변\"}]")
@NotNull
private List<String> mapSheetNum;
private List<MapSheetNumDto> mapSheetNum;
}
@Getter
@Setter
public static class MapSheetNumDto {
private String mapSheetNum;
private String mapSheetName;
}
}

View File

@@ -1,11 +1,14 @@
package com.kamco.cd.kamcoback.inference.service;
import com.kamco.cd.kamcoback.common.exception.CustomApiException;
import com.kamco.cd.kamcoback.config.resttemplate.ExternalHttpClient;
import com.kamco.cd.kamcoback.config.resttemplate.ExternalHttpClient.ExternalCallResult;
import com.kamco.cd.kamcoback.inference.dto.InferenceDetailDto;
import com.kamco.cd.kamcoback.inference.dto.InferenceDetailDto.Dashboard;
import com.kamco.cd.kamcoback.inference.dto.InferenceDetailDto.Detail;
import com.kamco.cd.kamcoback.inference.dto.InferenceDetailDto.MapSheet;
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto;
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto.MapSheetNumDto;
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto.MapSheetScope;
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto.ResultList;
import com.kamco.cd.kamcoback.inference.dto.InferenceSendDto;
@@ -22,12 +25,12 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Service
@Log4j2
@@ -38,7 +41,7 @@ public class InferenceResultService {
private final InferenceResultCoreService inferenceResultCoreService;
private final MapSheetMngCoreService mapSheetMngCoreService;
private final ModelMngCoreService modelMngCoreService;
private final WebClient webClient;
private final ExternalHttpClient externalHttpClient;
@Value("${inference.url}")
private String inferenceUrl;
@@ -60,34 +63,41 @@ public class InferenceResultService {
*/
@Transactional
public UUID saveInferenceInfo(InferenceResultDto.RegReq req) {
// TODO tif 없으면 전년도 파일 조회 쿼리 추가해야함
// 전체일때 5k도협 가져오기
List<MapSheetNumDto> mapSheetNum = new ArrayList<>();
if (MapSheetScope.ALL.getId().equals(req.getMapSheetScope())) {
Search5kReq mapReq = new Search5kReq();
mapReq.setUseInference("USE");
List<String> mapSheetIds = new ArrayList<>();
MapSheetNumDto mapSheetNumDto = new MapSheetNumDto();
inferenceResultCoreService
.findByMapidList(mapReq)
.forEach(
mapInkx5kEntity -> {
mapSheetIds.add(mapInkx5kEntity.getMapidcdNo());
mapSheetNumDto.setMapSheetNum(mapInkx5kEntity.getMapidcdNo());
mapSheetNumDto.setMapSheetName(mapInkx5kEntity.getMapidNm());
mapSheetNum.add(mapSheetNumDto);
});
req.setMapSheetNum(mapSheetIds);
}
// 추론 테이블 저장
UUID uuid = inferenceResultCoreService.saveInferenceInfo(req);
// TODO tif 없으면 전년도 파일 조회 쿼리 추가해야함
// TODO 도엽 개수를 target 기준으로 맞춰야함
this.getSceneInference(String.valueOf(req.getCompareYyyy()), req.getMapSheetNum());
this.getSceneInference(String.valueOf(req.getTargetYyyy()), req.getMapSheetNum());
List<String> mapSheetNumList = new ArrayList<>();
for (MapSheetNumDto mapSheetDto : mapSheetNum) {
mapSheetNumList.add(mapSheetDto.getMapSheetNum());
}
this.getSceneInference(String.valueOf(req.getCompareYyyy()), mapSheetNumList);
this.getSceneInference(String.valueOf(req.getTargetYyyy()), mapSheetNumList);
InferenceSendDto m1 = this.getModelInfo(req.getModel1Uuid());
InferenceSendDto m2 = this.getModelInfo(req.getModel2Uuid());
InferenceSendDto m3 = this.getModelInfo(req.getModel3Uuid());
//
// ensureAccepted(m1);
ensureAccepted(m1);
// ensureAccepted(m2);
// ensureAccepted(m3);
@@ -99,33 +109,15 @@ public class InferenceResultService {
*
* @param dto
*/
private Mono<Boolean> inferenceSend(InferenceSendDto dto) {
return webClient
.post()
.uri("http://localhost:8080/test") // inferenceUrl
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(dto)
.retrieve()
.toBodilessEntity()
.map(res -> res.getStatusCode().is2xxSuccessful())
.doOnNext(ok -> log.info("발송결과={}", ok))
.onErrorReturn(false);
}
/**
* api 호출 실패시 예외처리
*
* @param dto
*/
private void ensureAccepted(InferenceSendDto dto) {
Boolean ok =
this.inferenceSend(dto)
.timeout(java.time.Duration.ofSeconds(3))
.onErrorReturn(false) // 타임아웃/통신오류도 실패 처리
.block(java.time.Duration.ofSeconds(4));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
if (!Boolean.TRUE.equals(ok)) {
ExternalCallResult result =
externalHttpClient.call(inferenceUrl, HttpMethod.POST, dto, headers);
if (!result.success()) {
throw new CustomApiException("BAD_GATEWAY", HttpStatus.BAD_GATEWAY);
}
}

View File

@@ -6,6 +6,7 @@ import com.kamco.cd.kamcoback.inference.dto.InferenceDetailDto;
import com.kamco.cd.kamcoback.inference.dto.InferenceDetailDto.Dashboard;
import com.kamco.cd.kamcoback.inference.dto.InferenceDetailDto.MapSheet;
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto;
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto.MapSheetNumDto;
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto.ResultList;
import com.kamco.cd.kamcoback.postgres.entity.MapInkx5kEntity;
import com.kamco.cd.kamcoback.postgres.entity.MapSheetAnalDataInferenceEntity;
@@ -59,6 +60,14 @@ public class InferenceResultCoreService {
* @param req
*/
public UUID saveInferenceInfo(InferenceResultDto.RegReq req) {
String mapSheetName =
req.getMapSheetNum().get(0).getMapSheetName() + "" + req.getMapSheetNum().size() + "";
if (req.getMapSheetNum().size() == 1) {
mapSheetName =
req.getMapSheetNum().get(0).getMapSheetName() + " " + req.getMapSheetNum().size() + "";
}
MapSheetLearnEntity mapSheetLearnEntity = new MapSheetLearnEntity();
mapSheetLearnEntity.setTitle(req.getTitle());
mapSheetLearnEntity.setM1ModelUuid(req.getModel1Uuid());
@@ -69,19 +78,20 @@ public class InferenceResultCoreService {
mapSheetLearnEntity.setMapSheetScope(req.getMapSheetScope());
mapSheetLearnEntity.setDetectOption(req.getDetectOption());
mapSheetLearnEntity.setCreatedUid(userUtil.getId());
mapSheetLearnEntity.setMapSheetCnt(mapSheetName);
mapSheetLearnEntity.setDetectingCnt((long) req.getMapSheetNum().size());
// learn 테이블 저장
MapSheetLearnEntity savedLearn = mapSheetLearnRepository.save(mapSheetLearnEntity);
final int CHUNK = 1000;
List<MapSheetLearn5kEntity> buffer = new ArrayList<>(CHUNK);
List<String> mapSheetNumList = req.getMapSheetNum();
// learn 도엽 저장
for (String mapSheetNum : mapSheetNumList) {
for (MapSheetNumDto mapSheetNum : req.getMapSheetNum()) {
MapSheetLearn5kEntity e = new MapSheetLearn5kEntity();
e.setLearn(savedLearn);
e.setMapSheetNum(Long.parseLong(mapSheetNum));
e.setMapSheetNum(Long.parseLong(mapSheetNum.getMapSheetNum()));
e.setCreatedUid(userUtil.getId());
buffer.add(e);

View File

@@ -166,10 +166,11 @@ public class MapInkx5kRepositoryImpl extends QuerydslRepositorySupport
mapInkx5kEntity.fid,
mapInkx5kEntity.mapidcdNo,
mapInkx5kEntity.mapidNm,
mapInkx5kEntity.geom,
mapInkx5kEntity.useInference))
.from(mapInkx5kEntity)
.where(builder)
// TODO limit 제거 필요
.limit(100)
.fetch();
}

View File

@@ -15,7 +15,6 @@ import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.locationtech.jts.geom.Geometry;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
@@ -69,7 +68,6 @@ public class MapInkxMngDto {
private Integer fid;
private String mapidcdNo;
private String mapidNm;
private Geometry geom;
private CommonUseStatus useInference;
}