[KC-108] ai api 실행
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.kamco.cd.kamcoback.inference.service;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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;
|
||||
@@ -23,6 +25,7 @@ import com.kamco.cd.kamcoback.postgres.core.ModelMngCoreService;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
@@ -33,6 +36,7 @@ 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;
|
||||
@@ -93,7 +97,7 @@ public class InferenceResultService {
|
||||
|
||||
// 추론 테이블 저장
|
||||
UUID uuid = inferenceResultCoreService.saveInferenceInfo(req);
|
||||
this.startInference(req);
|
||||
this.startInference(req, uuid);
|
||||
|
||||
return uuid;
|
||||
}
|
||||
@@ -165,7 +169,7 @@ public class InferenceResultService {
|
||||
*
|
||||
* @param req
|
||||
*/
|
||||
private void startInference(InferenceResultDto.RegReq req) {
|
||||
private void startInference(InferenceResultDto.RegReq req, UUID uuid) {
|
||||
|
||||
List<MapSheetNumDto> mapSheetNum = req.getMapSheetNum();
|
||||
List<String> mapSheetNumList = new ArrayList<>();
|
||||
@@ -193,9 +197,8 @@ public class InferenceResultService {
|
||||
m2.setPred_requests_areas(predRequestsAreas);
|
||||
m3.setPred_requests_areas(predRequestsAreas);
|
||||
|
||||
ensureAccepted(m1);
|
||||
// ensureAccepted(m2);
|
||||
// ensureAccepted(m3);
|
||||
Long batchId = this.ensureAccepted(m1);
|
||||
inferenceResultCoreService.update(uuid, batchId, "IN_PROGRESS");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,7 +206,7 @@ public class InferenceResultService {
|
||||
*
|
||||
* @param dto
|
||||
*/
|
||||
private void ensureAccepted(InferenceSendDto dto) {
|
||||
private Long ensureAccepted(InferenceSendDto dto) {
|
||||
log.info("dto null? {}", dto == null);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
try {
|
||||
@@ -227,6 +230,28 @@ public class InferenceResultService {
|
||||
|
||||
int status = result.statusCode();
|
||||
String body = result.body();
|
||||
|
||||
if (status < 200 || status >= 300) {
|
||||
throw new CustomApiException("BAD_GATEWAY", HttpStatus.BAD_GATEWAY);
|
||||
}
|
||||
|
||||
Long batchId = 0L;
|
||||
|
||||
try {
|
||||
List<Map<String, Object>> list =
|
||||
om.readValue(body, new TypeReference<List<Map<String, Object>>>() {});
|
||||
|
||||
Integer batchIdInt = (Integer) list.get(0).get("batch_id");
|
||||
batchId = batchIdInt.longValue();
|
||||
if (list.isEmpty()) {
|
||||
throw new IllegalStateException("Inference response is empty");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
|
||||
return batchId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -216,4 +216,14 @@ public class InferenceResultCoreService {
|
||||
.map(MapInkx5kEntity::toEntity)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public void update(UUID uuid, Long batchId, String status) {
|
||||
MapSheetLearnEntity entity =
|
||||
mapSheetLearnRepository
|
||||
.getInferenceResultByUuid(uuid)
|
||||
.orElseThrow(() -> new EntityNotFoundException(uuid.toString()));
|
||||
|
||||
entity.setBatchId(batchId);
|
||||
entity.setStatus(status);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class MapSheetLearnEntity {
|
||||
@Column(name = "title", nullable = false, length = 200)
|
||||
private String title;
|
||||
|
||||
@Size(max = 10)
|
||||
@Size(max = 20)
|
||||
@Column(name = "status", length = 10)
|
||||
private String status;
|
||||
|
||||
@@ -103,6 +103,9 @@ public class MapSheetLearnEntity {
|
||||
@Column(name = "updated_uid")
|
||||
private Long updatedUid;
|
||||
|
||||
@Column(name = "batch_id")
|
||||
private Long batchId;
|
||||
|
||||
public InferenceResultDto.ResultList toDto() {
|
||||
return new InferenceResultDto.ResultList(
|
||||
this.uuid,
|
||||
|
||||
@@ -2,9 +2,13 @@ package com.kamco.cd.kamcoback.postgres.repository.Inference;
|
||||
|
||||
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto;
|
||||
import com.kamco.cd.kamcoback.postgres.entity.MapSheetLearnEntity;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public interface MapSheetLearnRepositoryCustom {
|
||||
|
||||
Page<MapSheetLearnEntity> getInferenceMgnResultList(InferenceResultDto.SearchListReq req);
|
||||
|
||||
Optional<MapSheetLearnEntity> getInferenceResultByUuid(UUID uuid);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import com.querydsl.core.types.dsl.CaseBuilder;
|
||||
import com.querydsl.core.types.dsl.NumberExpression;
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -73,4 +75,13 @@ public class MapSheetLearnRepositoryImpl implements MapSheetLearnRepositoryCusto
|
||||
|
||||
return new PageImpl<>(content, pageable, total == null ? 0L : total);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<MapSheetLearnEntity> getInferenceResultByUuid(UUID uuid) {
|
||||
return Optional.ofNullable(
|
||||
queryFactory
|
||||
.selectFrom(mapSheetLearnEntity)
|
||||
.where(mapSheetLearnEntity.uuid.eq(uuid))
|
||||
.fetchOne());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.kamco.cd.kamcoback.scheduler.service;
|
||||
|
||||
import com.kamco.cd.kamcoback.postgres.core.InferenceResultCoreService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Log4j2
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class MapSheetInferenceJobService {
|
||||
|
||||
private final InferenceResultCoreService inferenceResultCoreService;
|
||||
|
||||
@Scheduled(fixedDelay = 60_000)
|
||||
public void runBatch() {
|
||||
log.info("1분 배치 시작");
|
||||
|
||||
try {
|
||||
// TODO: 배치 로직 작성
|
||||
Thread.sleep(3000); // 예시: 처리 시간 3초
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
log.error("배치 중 인터럽트 발생", e);
|
||||
}
|
||||
|
||||
log.info("1분 배치 종료");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user