Skip to content

Commit

Permalink
feat: STT 요청상태 반환하는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yugyeom-ghim committed Nov 13, 2024
1 parent 4d75d7b commit c4e726d
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/notai/stt/query/SttQueryRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

public interface SttQueryRepository {
List<Stt> findAllByDocumentIdAndPageNumber(Long documentId, Integer pageNumber);
List<Stt> findAllByDocumentId(Long documentId);
}
9 changes: 9 additions & 0 deletions src/main/java/notai/stt/query/SttQueryRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@ public List<Stt> findAllByDocumentIdAndPageNumber(Long documentId, Integer pageN
.and(stt.pageNumber.eq(pageNumber)))
.fetch();
}

@Override
public List<Stt> findAllByDocumentId(Long documentId) {
return queryFactory
.selectFrom(stt)
.join(stt.recording).fetchJoin()
.where(stt.recording.document.id.eq(documentId))
.fetch();
}
}
53 changes: 53 additions & 0 deletions src/main/java/notai/sttTask/application/SttTaskQueryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package notai.sttTask.application;

import lombok.RequiredArgsConstructor;
import notai.document.domain.Document;
import notai.document.domain.DocumentRepository;
import notai.llm.domain.TaskStatus;
import static notai.llm.domain.TaskStatus.*;
import notai.member.domain.Member;
import notai.member.domain.MemberRepository;
import notai.stt.domain.Stt;
import notai.stt.domain.SttRepository;
import notai.sttTask.application.result.SttTaskOverallStatusResult;
import notai.sttTask.domain.SttTask;
import notai.sttTask.domain.SttTaskRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Transactional(readOnly = true)
@RequiredArgsConstructor
@Service
public class SttTaskQueryService {

private final DocumentRepository documentRepository;
private final MemberRepository memberRepository;
private final SttTaskRepository sttTaskRepository;
private final SttRepository sttRepository;

public SttTaskOverallStatusResult fetchOverallStatus(Long memberId, Long documentId) {
Document foundDocument = documentRepository.getById(documentId);
Member member = memberRepository.getById(memberId);
foundDocument.validateOwner(member);

List<Stt> sttResults = sttRepository.findAllByDocumentId(documentId);

if (sttResults.isEmpty()) {
return SttTaskOverallStatusResult.of(documentId, NOT_REQUESTED, 0, 0);
}
List<TaskStatus> taskStatuses =
sttTaskRepository.findAllBySttIn(sttResults).stream().map(SttTask::getStatus).toList();


int totalPages = taskStatuses.size();
int completedPages = Collections.frequency(taskStatuses, COMPLETED);

if (totalPages == completedPages) {
return SttTaskOverallStatusResult.of(documentId, COMPLETED, totalPages, completedPages);
}
return SttTaskOverallStatusResult.of(documentId, IN_PROGRESS, totalPages, completedPages);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package notai.sttTask.application.result;

import notai.llm.domain.TaskStatus;

public record SttTaskOverallStatusResult(
Long documentId,
TaskStatus overallStatus,
int totalPages,
int completedPages
) {
public static SttTaskOverallStatusResult of(
Long documentId,
TaskStatus taskStatus,
int totalPages,
int completedPages
) {
return new SttTaskOverallStatusResult(documentId, taskStatus, totalPages, completedPages);
}
}
4 changes: 4 additions & 0 deletions src/main/java/notai/sttTask/domain/SttTaskRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import static notai.common.exception.ErrorMessages.AI_SERVER_ERROR;
import notai.common.exception.type.NotFoundException;
import notai.stt.domain.Stt;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.UUID;

public interface SttTaskRepository extends JpaRepository<SttTask, UUID> {

default SttTask getById(UUID id) {
return findById(id).orElseThrow(() -> new NotFoundException(AI_SERVER_ERROR));
}

List<SttTask> findAllBySttIn(List<Stt> stts);
}
26 changes: 26 additions & 0 deletions src/main/java/notai/sttTask/presentation/SttTaskController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package notai.sttTask.presentation;

import lombok.RequiredArgsConstructor;
import notai.auth.Auth;
import notai.sttTask.application.SttTaskQueryService;
import notai.sttTask.application.result.SttTaskOverallStatusResult;
import notai.sttTask.presentation.response.SttTaskOverallStatusResponse;
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.RestController;

@RequiredArgsConstructor
@RestController
public class SttTaskController {

private final SttTaskQueryService sttTaskQueryService;

@GetMapping("/status/{documentId}")
public ResponseEntity<SttTaskOverallStatusResponse> fetchOverallStatus(
@Auth Long memberId, @PathVariable("documentId") Long documentId
) {
SttTaskOverallStatusResult result = sttTaskQueryService.fetchOverallStatus(memberId, documentId);
return ResponseEntity.ok(SttTaskOverallStatusResponse.from(result));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package notai.sttTask.presentation.response;

import notai.llm.domain.TaskStatus;
import notai.sttTask.application.result.SttTaskOverallStatusResult;

public record SttTaskOverallStatusResponse(
Long documentId,
TaskStatus overallStatus,
Integer totalPages,
Integer completedPages
) {
public static SttTaskOverallStatusResponse from(SttTaskOverallStatusResult result) {
return new SttTaskOverallStatusResponse(
result.documentId(),
result.overallStatus(),
result.totalPages(),
result.completedPages()
);
}
}

0 comments on commit c4e726d

Please sign in to comment.