Files
kamco-cd-api/src/main/java/com/kamco/cd/kamcoback/label/LabelAllocateApiController.java

439 lines
20 KiB
Java

package com.kamco.cd.kamcoback.label;
import com.kamco.cd.kamcoback.common.download.DownloadExecutor;
import com.kamco.cd.kamcoback.common.download.dto.DownloadSpec;
import com.kamco.cd.kamcoback.config.api.ApiResponseDto;
import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto;
import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto.InferenceDetail;
import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto.LabelerDetail;
import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto.LabelingStatDto;
import com.kamco.cd.kamcoback.label.dto.LabelAllocateDto.WorkHistoryDto;
import com.kamco.cd.kamcoback.label.dto.WorkerStatsDto.UpdateClosedRequest;
import com.kamco.cd.kamcoback.label.dto.WorkerStatsDto.WorkerListResponse;
import com.kamco.cd.kamcoback.label.service.LabelAllocateService;
import com.kamco.cd.kamcoback.log.dto.AuditLogDto;
import com.kamco.cd.kamcoback.log.dto.AuditLogDto.DownloadReq;
import com.kamco.cd.kamcoback.log.dto.AuditLogDto.searchReq;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
@Slf4j
@Tag(name = "라벨링 작업 관리", description = "라벨링 작업 배정 및 통계 조회 API")
@RequestMapping({"/api/training-data/stage"})
@RequiredArgsConstructor
@RestController
public class LabelAllocateApiController {
private final LabelAllocateService labelAllocateService;
private final DownloadExecutor downloadExecutor;
@Value("${file.dataset-response}")
private String responsePath;
@Operation(summary = "배정 가능한 사용자 목록 조회", description = "라벨링 작업 배정을 위한 활성 상태의 사용자 목록을 조회합니다.")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "조회 성공"),
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음"),
@ApiResponse(responseCode = "500", description = "서버 오류")
})
@GetMapping("/avail-user")
public ApiResponseDto<List<LabelAllocateDto.UserList>> availUserList(
@Parameter(
description = "사용자 역할",
example = "LABELER",
schema = @Schema(allowableValues = {"LABELER", "REVIEWER"}))
@RequestParam
String role) {
return ApiResponseDto.ok(labelAllocateService.availUserList(role));
}
@Operation(
summary = "작업현황 관리 > 프로젝트 및 진행 상황 정보",
description = "작업현황 관리 > 프로젝트 및 진행 상황 정보. UUID를 입력하면 해당 프로젝트 정보를 조회하고, 미입력 시 최신 프로젝트를 조회합니다.")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "조회 성공"),
@ApiResponse(responseCode = "404", description = "데이터를 찾을 수 없음"),
@ApiResponse(responseCode = "500", description = "서버 오류")
})
@GetMapping("/projectinfo")
public ApiResponseDto<WorkerListResponse> getWorkerStatistics(
@Parameter(
description = "프로젝트 UUID (선택) - 미입력 시 최신 프로젝트 조회",
example = "f97dc186-e6d3-4645-9737-3173dde8dc64")
@RequestParam(required = false)
String uuid,
@Parameter(
description = "작업자 유형 (선택) - 미입력 시 전체 조회",
example = "LABELER",
schema = @Schema(allowableValues = {"LABELER", "REVIEWER"}))
@RequestParam(required = false)
String type,
@Parameter(description = "검색어 (작업자 이름 또는 사번으로 검색, 부분 일치) - 미입력 시 전체 조회", example = "김라벨")
@RequestParam(required = false)
String search,
@Parameter(
description = "정렬 조건 (선택) - 미입력 시 이름 오름차순",
example = "REMAINING_DESC",
schema =
@Schema(
allowableValues = {
"REMAINING_DESC",
"REMAINING_ASC",
"COMPLETED_DESC",
"COMPLETED_ASC",
"NAME_ASC",
"NAME_DESC"
},
defaultValue = "NAME_ASC"))
@RequestParam(required = false)
String sort) {
return ApiResponseDto.ok(
labelAllocateService.getWorkerStatisticsByUuid(uuid, type, search, sort));
}
@Operation(summary = "라벨링작업 관리 > 작업 배정", description = "라벨링작업 관리 > 작업 배정")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "201",
description = "등록 성공",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = Long.class))),
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@PostMapping("/allocate")
public ApiResponseDto<ApiResponseDto.ResponseObj> labelAllocate(
@RequestBody @Valid LabelAllocateDto.AllocateDto dto) {
int compareYyyy = Integer.parseInt(dto.getYyyy().split("-")[0]);
int targetYyyy = Integer.parseInt(dto.getYyyy().split("-")[1]);
return ApiResponseDto.okObject(
labelAllocateService.allocateAsc(
dto.getUuid(), dto.getLabelers(), dto.getInspectors(), compareYyyy, targetYyyy));
}
// 같은 기능이 있어서 미사용하게 됨 -> /api/training-data/stage/label-work-mng-detail/{uuid}
@Hidden
@Operation(summary = "작업현황 관리 > 변화탐지 회차 정보", description = "작업현황 관리 > 변화탐지 회차 정보")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "조회 성공"),
@ApiResponse(responseCode = "404", description = "데이터를 찾을 수 없음"),
@ApiResponse(responseCode = "500", description = "서버 오류")
})
@GetMapping("/stage-detail")
public ApiResponseDto<InferenceDetail> findInferenceDetail(
@Parameter(
description = "회차 마스터 key",
required = true,
example = "8584e8d4-53b3-4582-bde2-28a81495a626")
@RequestParam
String uuid) {
return ApiResponseDto.ok(labelAllocateService.findInferenceDetail(uuid));
}
@Operation(
summary = "작업현황 관리 > 라벨러/검수자 상세 정보, 작업이관 팝업 내 라벨러 상세 정보 동일",
description = "작업현황 관리 > 라벨러/검수자 상세 정보, 작업이관 팝업 내 라벨러 상세 정보 동일")
@GetMapping("/user-detail")
public ApiResponseDto<LabelerDetail> findUserDetail(
@RequestParam(defaultValue = "01022223333", required = true) String userId,
@Parameter(
description = "회차 마스터 key",
required = true,
example = "8584e8d4-53b3-4582-bde2-28a81495a626")
@RequestParam
String uuid,
@Schema(
allowableValues = {"LABELER", "REVIEWER"},
defaultValue = "LABELER")
@Parameter(description = "라벨러/검수자(LABELER/REVIEWER)", required = true)
@RequestParam
String type) {
return ApiResponseDto.ok(labelAllocateService.findUserDetail(userId, uuid, type));
}
@Operation(summary = "작업현황 관리 > 상세 > 작업 이관", description = "작업현황 관리 > 상세 > 작업 이관")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "201",
description = "등록 성공",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = Long.class))),
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@PostMapping("/allocate-move")
public ApiResponseDto<ApiResponseDto.ResponseObj> labelAllocateMove(
@io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "라벨링 이관",
required = true,
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = LabelAllocateDto.AllocateMoveDto.class)))
@RequestBody
LabelAllocateDto.AllocateMoveDto dto) {
return ApiResponseDto.okObject(
labelAllocateService.allocateMove(
dto.getTotalCnt(), dto.getUuid(), dto.getLabelers(), dto.getUserId()));
}
@Operation(
summary = "라벨링작업 관리 > 상세 > 라벨러/검수자 일별 작업량 목록",
description = "라벨링작업 관리 > 상세 > 라벨러/검수자 일별 작업량 목록")
@GetMapping("/daily-list")
public ApiResponseDto<Page<LabelingStatDto>> findDaliyList(
@RequestParam(defaultValue = "0", required = true) int page,
@RequestParam(defaultValue = "20", required = true) int size,
@Parameter(
description = "회차 마스터 key",
required = true,
example = "8584e8d4-53b3-4582-bde2-28a81495a626")
@RequestParam
String uuid,
@Parameter(description = "사번", required = true, example = "123456") @RequestParam
String userId,
@Schema(
allowableValues = {"LABELER", "REVIEWER"},
defaultValue = "LABELER")
@Parameter(description = "라벨러/검수자(LABELER/REVIEWER)", required = true)
@RequestParam
String type) {
LabelAllocateDto.searchReq searchReq = new LabelAllocateDto.searchReq(page, size, "");
return ApiResponseDto.ok(labelAllocateService.findDaliyList(searchReq, uuid, userId, type));
}
@Operation(summary = "이관 가능한 사용자 목록 조회", description = "이관 가능한 사용자 목록 조회")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "조회 성공"),
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음"),
@ApiResponse(responseCode = "500", description = "서버 오류")
})
@GetMapping("/move-user")
public ApiResponseDto<LabelAllocateDto.MoveInfo> availMoveUserList(
@Parameter(description = "해당 사용자 사번", example = "01022223333") @RequestParam String userId,
@Parameter(description = "회차 마스터 key", example = "f97dc186-e6d3-4645-9737-3173dde8dc64")
@RequestParam
String uuid) {
return ApiResponseDto.ok(labelAllocateService.moveAvailUserList(userId, uuid));
}
@Operation(
summary = "작업현황 관리 > 라벨링/검수 종료 여부 업데이트",
description = "라벨링/검수 종료 여부를 업데이트합니다. uuid 생략 시 최신 프로젝트 대상")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "업데이트 성공"),
@ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@PostMapping("/projectinfo/closed")
public ApiResponseDto<ApiResponseDto.ResponseObj> updateClosedYn(
@io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "종료 여부 업데이트 요청",
required = true,
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = UpdateClosedRequest.class),
examples = {
@io.swagger.v3.oas.annotations.media.ExampleObject(
name = "특정 프로젝트 라벨링 전체 종료",
value =
"""
{"uuid": "f97dc186-e6d3-4645-9737-3173dde8dc64", "closedType": "LABELING", "closedYn": "Y"}
"""),
@io.swagger.v3.oas.annotations.media.ExampleObject(
name = "특정 프로젝트 검수 전체 종료",
value =
"""
{"uuid": "f97dc186-e6d3-4645-9737-3173dde8dc64", "closedType": "INSPECTION", "closedYn": "Y"}
"""),
@io.swagger.v3.oas.annotations.media.ExampleObject(
name = "특정 프로젝트 라벨링+검수 동시 종료",
value =
"""
{"uuid": "f97dc186-e6d3-4645-9737-3173dde8dc64", "closedType": "BOTH", "closedYn": "Y"}
""")
}))
@RequestBody
@Valid
UpdateClosedRequest request) {
labelAllocateService.updateClosedYn(
request.getUuid(), request.getClosedType(), request.getClosedYn());
String typeLabel;
if ("LABELING".equals(request.getClosedType())) {
typeLabel = "라벨링";
} else if ("INSPECTION".equals(request.getClosedType())) {
typeLabel = "검수";
} else if ("BOTH".equals(request.getClosedType())) {
typeLabel = "라벨링 및 검수";
} else {
typeLabel = "작업";
}
String statusMessage =
"Y".equals(request.getClosedYn())
? typeLabel + "이(가) 종료되었습니다."
: typeLabel + "이(가) 재개되었습니다.";
return ApiResponseDto.okObject(
new ApiResponseDto.ResponseObj(ApiResponseDto.ApiResponseCode.OK, statusMessage));
}
@Operation(summary = "라벨링작업 관리 > 상세 > 작업이력", description = "라벨링작업 관리 > 상세 > 작업이력")
@GetMapping("/work-history-list")
public ApiResponseDto<Page<WorkHistoryDto>> findWorkHistoryList(
@RequestParam(defaultValue = "0", required = true) int page,
@RequestParam(defaultValue = "20", required = true) int size,
@Parameter(description = "사번", required = true, example = "123456") @RequestParam
String userId,
@Schema(
allowableValues = {"LABELER", "REVIEWER"},
defaultValue = "LABELER")
@Parameter(description = "라벨러/검수자(LABELER/REVIEWER)", required = true)
@RequestParam
String type) {
LabelAllocateDto.searchReq searchReq = new LabelAllocateDto.searchReq(page, size, "");
return ApiResponseDto.ok(labelAllocateService.findWorkHistoryList(searchReq, userId, type));
}
@Operation(summary = "라벨링 작업 중인 회차 있는지 여부", description = "라벨링 작업 중인 회차 있는지 여부")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "조회 성공"),
@ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음"),
@ApiResponse(responseCode = "500", description = "서버 오류")
})
@GetMapping("/ing-process-cnt")
public ApiResponseDto<Long> labelingIngProcessCnt() {
return ApiResponseDto.ok(labelAllocateService.findLabelingIngProcessCnt());
}
@Operation(
summary = "라벨 파일 다운로드",
description = "라벨 파일 다운로드",
parameters = {
@Parameter(
name = "kamco-download-uuid",
in = ParameterIn.HEADER,
required = true,
description = "다운로드 요청 UUID",
schema =
@Schema(
type = "string",
format = "uuid",
example = "69c4e56c-e0bf-4742-9225-bba9aae39052"))
})
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "라벨 zip파일 다운로드",
content =
@Content(
mediaType = "application/octet-stream",
schema = @Schema(type = "string", format = "binary"))),
@ApiResponse(responseCode = "404", description = "파일 없음", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
@GetMapping("/download/{uuid}")
public ResponseEntity<StreamingResponseBody> download(
@Parameter(example = "69c4e56c-e0bf-4742-9225-bba9aae39052") @PathVariable UUID uuid)
throws IOException {
String uid = labelAllocateService.findLearnUid(uuid);
Path zipPath = Paths.get(responsePath).resolve(uid + ".zip");
return downloadExecutor.stream(
new DownloadSpec(uuid, zipPath, uid + ".zip", MediaType.APPLICATION_OCTET_STREAM));
}
@Operation(summary = "라벨 파일 다운로드 이력 조회", description = "라벨 파일 다운로드 이력 조회")
@GetMapping(value = "/download-audit/{uuid}")
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "검색 성공",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = Page.class))),
@ApiResponse(responseCode = "400", description = "잘못된 검색 조건", content = @Content),
@ApiResponse(responseCode = "500", description = "서버 오류", content = @Content)
})
public ApiResponseDto<Page<AuditLogDto.DownloadRes>> downloadAudit(
@Parameter(description = "UUID", example = "69c4e56c-e0bf-4742-9225-bba9aae39052")
@PathVariable
UUID uuid,
// @Parameter(description = "다운로드일 시작", example = "2025-01-01") @RequestParam(required =
// false)
// LocalDate strtDttm,
// @Parameter(description = "다운로드일 종료", example = "2026-04-01") @RequestParam(required =
// false)
// LocalDate endDttm,
// @Parameter(description = "키워드", example = "") @RequestParam(required = false)
// String searchValue,
@Parameter(description = "페이지 번호 (0부터 시작)", example = "0") @RequestParam(defaultValue = "0")
int page,
@Parameter(description = "페이지 크기", example = "20") @RequestParam(defaultValue = "20")
int size) {
AuditLogDto.searchReq searchReq = new searchReq();
searchReq.setPage(page);
searchReq.setSize(size);
DownloadReq downloadReq = new DownloadReq();
downloadReq.setUuid(uuid);
// downloadReq.setStartDate(strtDttm);
// downloadReq.setEndDate(endDttm);
// downloadReq.setSearchValue(searchValue);
downloadReq.setRequestUri("/api/training-data/stage/download/" + uuid);
return ApiResponseDto.ok(labelAllocateService.getDownloadAudit(searchReq, downloadReq));
}
}