-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from tipi-tapi/develop
Develop 1.3.0
- Loading branch information
Showing
59 changed files
with
994 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/tipitapi/drawmytoday/common/config/TransactionTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package tipitapi.drawmytoday.common.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
@Configuration | ||
public class TransactionTemplateConfig { | ||
|
||
@Bean | ||
public TransactionTemplate writeTransactionTemplate( | ||
PlatformTransactionManager transactionManager) { | ||
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); | ||
transactionTemplate.setReadOnly(false); | ||
return transactionTemplate; | ||
} | ||
|
||
@Bean | ||
public TransactionTemplate readTransactionTemplate( | ||
PlatformTransactionManager transactionManager) { | ||
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); | ||
transactionTemplate.setReadOnly(true); | ||
return transactionTemplate; | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
src/main/java/tipitapi/drawmytoday/common/exception/BusinessException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/tipitapi/drawmytoday/domain/admin/dto/GetDiaryNoteAndPromptResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tipitapi.drawmytoday.domain.admin.dto; | ||
|
||
import com.querydsl.core.annotations.QueryProjection; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class GetDiaryNoteAndPromptResponse { | ||
|
||
private final Long promptId; | ||
private String notes; | ||
private final String prompt; | ||
|
||
@QueryProjection | ||
public GetDiaryNoteAndPromptResponse(Long promptId, String notes, String prompt) { | ||
this.promptId = promptId; | ||
this.notes = notes; | ||
this.prompt = prompt; | ||
} | ||
|
||
public void updateNotes(String notes) { | ||
this.notes = notes; | ||
} | ||
|
||
public String getGptPrompt() { | ||
String[] promptTexts = prompt.split("Impressionist oil painting,"); | ||
return promptTexts[promptTexts.length - 1].trim(); | ||
} | ||
} |
83 changes: 82 additions & 1 deletion
83
src/main/java/tipitapi/drawmytoday/domain/admin/service/AdminService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,106 @@ | ||
package tipitapi.drawmytoday.domain.admin.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Sort.Direction; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
import tipitapi.drawmytoday.domain.admin.dto.GetDiaryAdminResponse; | ||
import tipitapi.drawmytoday.domain.admin.dto.GetDiaryNoteAndPromptResponse; | ||
import tipitapi.drawmytoday.domain.diary.domain.Prompt; | ||
import tipitapi.drawmytoday.domain.diary.domain.PromptGeneratorResult; | ||
import tipitapi.drawmytoday.domain.diary.repository.PromptRepository; | ||
import tipitapi.drawmytoday.domain.diary.service.AdminDiaryService; | ||
import tipitapi.drawmytoday.domain.generator.api.gpt.domain.ChatCompletionsRole; | ||
import tipitapi.drawmytoday.domain.generator.api.gpt.domain.Message; | ||
import tipitapi.drawmytoday.domain.generator.api.gpt.dto.GptChatCompletionsRequest; | ||
import tipitapi.drawmytoday.domain.generator.service.TranslateTextService; | ||
import tipitapi.drawmytoday.domain.user.service.ValidateUserService; | ||
|
||
@Slf4j | ||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class AdminService { | ||
|
||
private final ValidateUserService validateUserService; | ||
private final AdminDiaryService adminDiaryService; | ||
private final TranslateTextService translateTextService; | ||
private final TransactionTemplate writeTransactionTemplate; | ||
private final PromptRepository promptRepository; | ||
private final ObjectMapper objectMapper; | ||
@Value("${openai.gpt.chat_completions_prompt}") | ||
private String gptChatCompletionsPrompt; | ||
|
||
@Transactional(readOnly = true) | ||
public Page<GetDiaryAdminResponse> getDiaries(Long userId, int size, int page, | ||
Direction direction, Long emotionId, boolean withTest) { | ||
validateUserService.validateAdminUserById(userId); | ||
return adminDiaryService.getDiaries(size, page, direction, emotionId, withTest); | ||
} | ||
|
||
public int addGptGeneratorContent(Long userId) { | ||
validateUserService.validateAdminUserById(userId); | ||
List<GetDiaryNoteAndPromptResponse> responses = adminDiaryService.getDiaryNoteAndPrompt(); | ||
ExecutorService executor = Executors.newFixedThreadPool(10); | ||
CountDownLatch latch = new CountDownLatch(responses.size()); | ||
for (int i = 0; i < responses.size(); i++) { | ||
GetDiaryNoteAndPromptResponse response = responses.get(i); | ||
int finalI = i; | ||
executor.execute(() -> { | ||
try { | ||
String translatedNotes = translateTextService.translateAutoToEnglish( | ||
response.getNotes()); | ||
response.updateNotes(translatedNotes); | ||
} catch (Exception e) { | ||
log.error("번역 API 예외가 발생했습니다.", e); | ||
responses.remove(finalI); | ||
} finally { | ||
latch.countDown(); | ||
} | ||
}); | ||
} | ||
|
||
try { | ||
latch.await(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
log.error("작업이 중단되었습니다.", e); | ||
throw new RuntimeException(e); | ||
} | ||
|
||
executor.shutdown(); | ||
|
||
if (responses.isEmpty()) { | ||
throw new RuntimeException("번역할 데이터가 없거나 모두 실패했습니다."); | ||
} | ||
|
||
writeTransactionTemplate.executeWithoutResult(status -> { | ||
for (GetDiaryNoteAndPromptResponse response : responses) { | ||
Prompt prompt = promptRepository.findById(response.getPromptId()).get(); | ||
List<Message> messages = GptChatCompletionsRequest.createFirstMessage( | ||
gptChatCompletionsPrompt, response.getNotes()) | ||
.getMessages(); | ||
messages.add(new Message(ChatCompletionsRole.assistant, response.getGptPrompt())); | ||
String gptResponses; | ||
try { | ||
gptResponses = objectMapper.writeValueAsString(messages); | ||
} catch (JsonProcessingException e) { | ||
log.error("GPT Message를 JSON으로 변환하는데 실패했습니다.", e); | ||
throw new RuntimeException(e); | ||
} | ||
PromptGeneratorResult result = PromptGeneratorResult.createGpt3Result(gptResponses); | ||
prompt.updatePromptGeneratorResult(result); | ||
} | ||
}); | ||
return responses.size(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.