Skip to content

Commit

Permalink
style: spotless Apply
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomo committed Sep 23, 2023
1 parent 8ff4420 commit d17737c
Show file tree
Hide file tree
Showing 62 changed files with 471 additions and 493 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.flickspick.auth.application;

import static com.flickspick.auth.AuthConstants.AUTH_TOKEN_HEADER_KEY;

import com.flickspick.auth.model.AuthToken;
import com.flickspick.auth.model.AuthUser;
import com.flickspick.auth.model.AuthUserImpl;
Expand All @@ -10,16 +12,13 @@
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

import static com.flickspick.auth.AuthConstants.AUTH_TOKEN_HEADER_KEY;

@Slf4j
@Component
@RequiredArgsConstructor
Expand Down
24 changes: 9 additions & 15 deletions src/main/java/com/flickspick/batch/BatchController.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package com.flickspick.batch;

import java.util.List;

import com.flickspick.client.chatgpt.ChatGPTClient;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.flickspick.client.chatgpt.ChatGPTClient;
import com.flickspick.rec.dto.RecRequest.QuestionModel;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "배치 서비스")
@RestController
@RequestMapping(path = "/api/v1/batch", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class BatchController {
private final BatchService batchService;
private final ChatGPTClient chatGPTClient;
private final BatchService batchService;
private final ChatGPTClient chatGPTClient;

@PostMapping
public void batch() {
batchService.batch();
}
@PostMapping
public void batch() {
batchService.batch();
}
}
173 changes: 98 additions & 75 deletions src/main/java/com/flickspick/batch/BatchService.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package com.flickspick.batch;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.stereotype.Service;

import com.flickspick.client.chatgpt.ChatGPTClient;
import com.flickspick.client.dto.ResponseContent;
import com.flickspick.client.dto.ResponseMessage;
import com.flickspick.movie.domain.Movie;
import com.flickspick.movie.infrastructure.MovieRepository;
import com.flickspick.movie_recommendtype.domain.MovieRecommendType;
import com.flickspick.movie_recommendtype.infrastructure.MovieRecommendTypeRepository;
import com.flickspick.user_movie_history.domain.UserMovieHistory;
import com.flickspick.user_movie_history.infrastructure.UserMovieHistoryRepository;
import com.flickspick.ott.domain.Ott;
import com.flickspick.ott.infrastructure.OttRepository;
import com.flickspick.question.domain.Question;
Expand All @@ -23,81 +16,111 @@
import com.flickspick.rec.dto.RecRequest;
import com.flickspick.recommendtype.domain.RecommendType;
import com.flickspick.recommendtype.infrastructure.RecommendTypeRepository;

import com.flickspick.user_movie_history.domain.UserMovieHistory;
import com.flickspick.user_movie_history.infrastructure.UserMovieHistoryRepository;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class BatchService {
private final UserMovieHistoryRepository userMovieHistoryRepository;
private final MovieRecommendTypeRepository movieRecommendTypeRepository;
private final RecommendTypeRepository recommendTypeRepository;
private final MovieRepository movieRepository;
private final OttRepository ottRepository;
private final QuestionRepository questionRepository;
private final QuestionAnswerRepository questionAnswerRepository;
private final ChatGPTClient chatGPTClient;

public void batch() {
log.info("start batch job");
List<Ott> ottList = ottRepository.findAll();
List<Question> questionList = questionRepository.findAll();
AtomicInteger count = new AtomicInteger(0);
for (Question question: questionList) {
List<QuestionAnswer> questionAnswerList = questionAnswerRepository.findAllByQuestionId(question.getId());
for (QuestionAnswer questionAnswer: questionAnswerList) {
List<RecRequest.QuestionModel> answers = List.of(new RecRequest.QuestionModel(question.getId(), questionAnswer.getId()));
for (Ott ott: ottList) {
List<Long> ottIds = List.of(ott.getId());
try {
count.addAndGet(1);
ResponseMessage responseMessage = chatGPTClient.query(answers, ottIds);
ResponseContent responseContent = responseMessage.getContent();
RecommendType recommendType = recommendTypeRepository.findByRecommendType(
responseContent.getRecommend_type())
.orElseThrow(() -> new IllegalArgumentException("RecommendType이 없습니다."));
Movie movie = Movie.builder()
.title(responseContent.getTitle())
.reason(responseContent.getReason())
.plot(responseContent.getPlot())
.recommendTypeId(recommendType.getId())
.build();
movieRepository.save(movie);
UserMovieHistory userMovieHistory = userMovieHistoryRepository.findByRecommendTypeIdAndMovieId(
recommendType.getId(), movie.getId())
.orElseGet(() -> {
UserMovieHistory newMovieResult = UserMovieHistory.builder()
.recommendTypeId(recommendType.getId())
.movieId(movie.getId())
.build();
return userMovieHistoryRepository.save(newMovieResult);
});
userMovieHistory.addQuestionAndAnswer(question.getId(), questionAnswer.getId());
userMovieHistoryRepository.save(userMovieHistory);
private final UserMovieHistoryRepository userMovieHistoryRepository;
private final MovieRecommendTypeRepository movieRecommendTypeRepository;
private final RecommendTypeRepository recommendTypeRepository;
private final MovieRepository movieRepository;
private final OttRepository ottRepository;
private final QuestionRepository questionRepository;
private final QuestionAnswerRepository questionAnswerRepository;
private final ChatGPTClient chatGPTClient;

MovieRecommendType movieRecommendType = MovieRecommendType.builder()
.recommendTypeId(recommendType.getId())
.movieId(movie.getId())
.build();
movieRecommendTypeRepository.save(movieRecommendType);
log.info("성공 질문 count: {}, id: {}, 답변id: {}, ottId: {}", count.get(), question.getId(), questionAnswer.getId(), ott.getId());
} catch (Exception e) {
if (e instanceof IllegalArgumentException) {
log.info("RecommendType이 없습니다.");
}
log.error("실패 질문 count: {}, id: {}, 답변id: {}, ottId: {}", count.get(), question.getId(), questionAnswer.getId(), ott.getId());
}
try {
Thread.sleep(22000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
log.info("finish batch job");
}
public void batch() {
log.info("start batch job");
List<Ott> ottList = ottRepository.findAll();
List<Question> questionList = questionRepository.findAll();
AtomicInteger count = new AtomicInteger(0);
for (Question question : questionList) {
List<QuestionAnswer> questionAnswerList =
questionAnswerRepository.findAllByQuestionId(question.getId());
for (QuestionAnswer questionAnswer : questionAnswerList) {
List<RecRequest.QuestionModel> answers =
List.of(
new RecRequest.QuestionModel(
question.getId(), questionAnswer.getId()));
for (Ott ott : ottList) {
List<Long> ottIds = List.of(ott.getId());
try {
count.addAndGet(1);
ResponseMessage responseMessage = chatGPTClient.query(answers, ottIds);
ResponseContent responseContent = responseMessage.getContent();
RecommendType recommendType =
recommendTypeRepository
.findByRecommendType(responseContent.getRecommend_type())
.orElseThrow(
() ->
new IllegalArgumentException(
"RecommendType이 없습니다."));
Movie movie =
Movie.builder()
.title(responseContent.getTitle())
.reason(responseContent.getReason())
.plot(responseContent.getPlot())
.recommendTypeId(recommendType.getId())
.build();
movieRepository.save(movie);
UserMovieHistory userMovieHistory =
userMovieHistoryRepository
.findByRecommendTypeIdAndMovieId(
recommendType.getId(), movie.getId())
.orElseGet(
() -> {
UserMovieHistory newMovieResult =
UserMovieHistory.builder()
.recommendTypeId(
recommendType.getId())
.movieId(movie.getId())
.build();
return userMovieHistoryRepository.save(
newMovieResult);
});
userMovieHistory.addQuestionAndAnswer(
question.getId(), questionAnswer.getId());
userMovieHistoryRepository.save(userMovieHistory);

MovieRecommendType movieRecommendType =
MovieRecommendType.builder()
.recommendTypeId(recommendType.getId())
.movieId(movie.getId())
.build();
movieRecommendTypeRepository.save(movieRecommendType);
log.info(
"성공 질문 count: {}, id: {}, 답변id: {}, ottId: {}",
count.get(),
question.getId(),
questionAnswer.getId(),
ott.getId());
} catch (Exception e) {
if (e instanceof IllegalArgumentException) {
log.info("RecommendType이 없습니다.");
}
log.error(
"실패 질문 count: {}, id: {}, 답변id: {}, ottId: {}",
count.get(),
question.getId(),
questionAnswer.getId(),
ott.getId());
}
try {
Thread.sleep(22000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
log.info("finish batch job");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.flickspick.client.chatgpt;

import java.util.List;

import com.flickspick.client.dto.ResponseMessage;
import com.flickspick.rec.dto.RecRequest;
import java.util.List;

public interface ChatGPTClient {
ResponseMessage query(List<RecRequest.QuestionModel> answers, List<Long> ottIds);
ResponseMessage query(List<RecRequest.QuestionModel> answers, List<Long> ottIds);
}
Loading

0 comments on commit d17737c

Please sign in to comment.