[KC-108] ai api batch 작업중
This commit is contained in:
@@ -1,19 +1,30 @@
|
||||
package com.kamco.cd.kamcoback.scheduler.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
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.InferenceBatchSheet;
|
||||
import com.kamco.cd.kamcoback.inference.dto.InferenceProgressDto;
|
||||
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto.SaveInferenceAiDto;
|
||||
import com.kamco.cd.kamcoback.inference.dto.InferenceResultDto.Status;
|
||||
import com.kamco.cd.kamcoback.inference.dto.InferenceSendDto;
|
||||
import com.kamco.cd.kamcoback.inference.dto.InferenceSendDto.pred_requests_areas;
|
||||
import com.kamco.cd.kamcoback.postgres.core.InferenceResultCoreService;
|
||||
import com.kamco.cd.kamcoback.scheduler.dto.JobStatusDto;
|
||||
import jakarta.transaction.Transactional;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -31,7 +42,14 @@ public class MapSheetInferenceJobService {
|
||||
@Value("${inference.batch-url}")
|
||||
private String batchUrl;
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
private String profile;
|
||||
|
||||
@Value("${inference.url}")
|
||||
private String inferenceUrl;
|
||||
|
||||
@Scheduled(fixedDelay = 60_000)
|
||||
@Transactional
|
||||
public void runBatch() {
|
||||
log.info("1분 배치 시작");
|
||||
|
||||
@@ -39,7 +57,7 @@ public class MapSheetInferenceJobService {
|
||||
// TODO: 배치 로직 작성
|
||||
|
||||
InferenceBatchSheet batchSheet =
|
||||
inferenceResultCoreService.getInferenceResultByStatus(Status.IN_PROGRESS.getId());
|
||||
inferenceResultCoreService.getInferenceResultByStatus(Status.IN_PROGRESS.getId());
|
||||
|
||||
if (batchSheet == null) {
|
||||
return;
|
||||
@@ -66,7 +84,7 @@ public class MapSheetInferenceJobService {
|
||||
String url = batchUrl + "/" + batchId;
|
||||
|
||||
ExternalCallResult<String> result =
|
||||
externalHttpClient.call(url, HttpMethod.GET, null, headers, String.class);
|
||||
externalHttpClient.call(url, HttpMethod.GET, null, headers, String.class);
|
||||
|
||||
int status = result.statusCode();
|
||||
if (status < 200 || status >= 300) {
|
||||
@@ -80,19 +98,34 @@ public class MapSheetInferenceJobService {
|
||||
String type = batchSheet.getRunningModelType();
|
||||
|
||||
if (type.equals("M1")) {
|
||||
|
||||
// M1 완료되었으면 M2 실행
|
||||
this.startInference(batchSheet.getId(), batchSheet.getUuid(), "M2", batchSheet.getM2ModelUuid());
|
||||
// 종료시간
|
||||
this.updateProcessingEndTimeByModel(batchSheet.getUuid(), ZonedDateTime.now(), "M1");
|
||||
} else if (type.equals("M2")) {
|
||||
|
||||
// M1 완료되었으면 M3 실행
|
||||
this.startInference(batchSheet.getId(), batchSheet.getUuid(), "M3", batchSheet.getM3ModelUuid());
|
||||
// 종료시간
|
||||
this.updateProcessingEndTimeByModel(batchSheet.getUuid(), ZonedDateTime.now(), "M2");
|
||||
} else if (type.equals("M3")) {
|
||||
|
||||
// 완료
|
||||
SaveInferenceAiDto saveInferenceAiDto = new SaveInferenceAiDto();
|
||||
saveInferenceAiDto.setUuid(batchSheet.getUuid());
|
||||
saveInferenceAiDto.setStatus(Status.END.getId());
|
||||
saveInferenceAiDto.setInferEndDttm(ZonedDateTime.now());
|
||||
inferenceResultCoreService.update(saveInferenceAiDto);
|
||||
// 종료시간
|
||||
this.updateProcessingEndTimeByModel(batchSheet.getUuid(), ZonedDateTime.now(), "M3");
|
||||
}
|
||||
} else {
|
||||
SaveInferenceAiDto saveInferenceAiDto = new SaveInferenceAiDto();
|
||||
saveInferenceAiDto.setUuid(batchSheet.getUuid());
|
||||
saveInferenceAiDto.setStatus(Status.IN_PROGRESS.getId());
|
||||
saveInferenceAiDto.setDetectEndCnt(dto.getCompletedJobs().longValue());
|
||||
inferenceResultCoreService.update(saveInferenceAiDto);
|
||||
}
|
||||
|
||||
System.out.println(dto);
|
||||
|
||||
Thread.sleep(3000); // 예시: 처리 시간 3초
|
||||
|
||||
} catch (InterruptedException | JsonProcessingException e) {
|
||||
} catch (JsonProcessingException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
log.error("배치 중 인터럽트 발생", e);
|
||||
}
|
||||
@@ -100,44 +133,92 @@ public class MapSheetInferenceJobService {
|
||||
log.info("1분 배치 종료");
|
||||
}
|
||||
|
||||
private void startInference(Long id, String type) {
|
||||
// InferenceResultDto.SaveInferenceAiDto req
|
||||
// inferenceResultCoreService.getInferenceResultByU
|
||||
//
|
||||
// List<MapSheetNumDto> mapSheetNum = req.getMapSheetNum();
|
||||
// List<String> mapSheetNumList = new ArrayList<>();
|
||||
//
|
||||
// for (MapSheetNumDto mapSheetDto : mapSheetNum) {
|
||||
// mapSheetNumList.add(mapSheetDto.getMapSheetNum());
|
||||
// }
|
||||
//
|
||||
// String modelComparePath =
|
||||
// this.getSceneInference(String.valueOf(req.getCompareYyyy()), mapSheetNumList);
|
||||
// String modelTargetPath =
|
||||
// this.getSceneInference(String.valueOf(req.getTargetYyyy()), mapSheetNumList);
|
||||
//
|
||||
// pred_requests_areas predRequestsAreas = new pred_requests_areas();
|
||||
// predRequestsAreas.setInput1_year(req.getCompareYyyy());
|
||||
// predRequestsAreas.setInput2_year(req.getTargetYyyy());
|
||||
// predRequestsAreas.setInput1_scene_path(modelComparePath);
|
||||
// predRequestsAreas.setInput2_scene_path(modelTargetPath);
|
||||
//
|
||||
// InferenceSendDto m1 = this.getModelInfo(req.getModel1Uuid());
|
||||
// InferenceSendDto m2 = this.getModelInfo(req.getModel2Uuid());
|
||||
// InferenceSendDto m3 = this.getModelInfo(req.getModel3Uuid());
|
||||
//
|
||||
// m1.setPred_requests_areas(predRequestsAreas);
|
||||
// m2.setPred_requests_areas(predRequestsAreas);
|
||||
// m3.setPred_requests_areas(predRequestsAreas);
|
||||
//
|
||||
// Long batchId = this.ensureAccepted(m1);
|
||||
//
|
||||
// SaveInferenceAiDto saveInferenceAiDto = new SaveInferenceAiDto();
|
||||
// saveInferenceAiDto.setUuid(uuid);
|
||||
// saveInferenceAiDto.setBatchId(batchId);
|
||||
// saveInferenceAiDto.setStatus(Status.IN_PROGRESS.getId());
|
||||
// saveInferenceAiDto.setType("M1");
|
||||
// saveInferenceAiDto.setInferStartDttm(ZonedDateTime.now());
|
||||
// inferenceResultCoreService.update(saveInferenceAiDto);
|
||||
private void startInference(Long id, UUID uuid, String type, UUID modelUuid) {
|
||||
|
||||
InferenceProgressDto progressDto = inferenceResultCoreService.getInferenceAiResultById(id, type, modelUuid);
|
||||
|
||||
pred_requests_areas predRequestsAreas = new pred_requests_areas();
|
||||
predRequestsAreas.setInput1_year(progressDto.getPred_requests_areas().getInput1_year());
|
||||
predRequestsAreas.setInput2_year(progressDto.getPred_requests_areas().getInput2_year());
|
||||
predRequestsAreas.setInput1_scene_path(progressDto.getPred_requests_areas().getInput1_scene_path());
|
||||
predRequestsAreas.setInput2_scene_path(progressDto.getPred_requests_areas().getInput2_scene_path());
|
||||
|
||||
InferenceSendDto m = new InferenceSendDto();
|
||||
m.setModel_version(progressDto.getModelVersion());
|
||||
m.setCd_model_path(progressDto.getCdModelPath() + "/" + progressDto.getCdModelFileName());
|
||||
m.setCd_model_config(progressDto.getCdModelConfigPath() + "/" + progressDto.getCdModelConfigFileName());
|
||||
m.setCls_model_path(progressDto.getCdModelClsPath() + "/" + progressDto.getCdModelClsFileName());
|
||||
m.setCls_model_version(progressDto.getClsModelVersion());
|
||||
m.setCd_model_type(type);
|
||||
m.setPriority(progressDto.getPriority());
|
||||
|
||||
// 추론 다음모델 실행
|
||||
Long batchId = this.ensureAccepted(m);
|
||||
|
||||
SaveInferenceAiDto saveInferenceAiDto = new SaveInferenceAiDto();
|
||||
saveInferenceAiDto.setUuid(uuid);
|
||||
saveInferenceAiDto.setBatchId(batchId);
|
||||
saveInferenceAiDto.setStatus(Status.IN_PROGRESS.getId());
|
||||
saveInferenceAiDto.setType(type);
|
||||
saveInferenceAiDto.setModelStartDttm(ZonedDateTime.now());
|
||||
inferenceResultCoreService.update(saveInferenceAiDto);
|
||||
}
|
||||
|
||||
private Long ensureAccepted(InferenceSendDto dto) {
|
||||
log.info("dto null? {}", dto == null);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
try {
|
||||
log.info("dto json={}", om.writeValueAsString(dto));
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
|
||||
|
||||
// TODO 추후 삭제
|
||||
if ("local".equals(profile)) {
|
||||
dto.getPred_requests_areas().setInput1_scene_path("/kamco-nfs/requests/2023_local.geojson");
|
||||
dto.getPred_requests_areas().setInput2_scene_path("/kamco-nfs/requests/2024_local.geojson");
|
||||
}
|
||||
|
||||
ExternalCallResult<String> result =
|
||||
externalHttpClient.call(inferenceUrl, HttpMethod.POST, dto, headers, String.class);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private void updateProcessingEndTimeByModel(UUID uuid, ZonedDateTime dateTime, String type) {
|
||||
SaveInferenceAiDto saveInferenceAiDto = new SaveInferenceAiDto();
|
||||
saveInferenceAiDto.setUuid(uuid);
|
||||
saveInferenceAiDto.setUpdateUid(0L);
|
||||
saveInferenceAiDto.setModelEndDttm(dateTime);
|
||||
saveInferenceAiDto.setType(type);
|
||||
inferenceResultCoreService.update(saveInferenceAiDto);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user