Skip to content

Commit

Permalink
Merge pull request #73 from julia-heo/develop
Browse files Browse the repository at this point in the history
[Test] ν˜œνƒ μš”μ•½ ν…ŒμŠ€νŠΈ μ½”λ“œ μΆ”κ°€
  • Loading branch information
Haewonny authored Apr 7, 2024
2 parents 679eee2 + 3555c1d commit a7e46ad
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package ewha.lux.once.domain.card.controller;

import ewha.lux.once.domain.card.dto.CardGoalRequestDto;
import ewha.lux.once.domain.card.dto.CardPerformanceRequestDto;
import ewha.lux.once.domain.card.dto.CodefCardListRequestDto;
import com.fasterxml.jackson.core.JsonProcessingException;
import ewha.lux.once.domain.card.dto.*;
import ewha.lux.once.domain.card.service.CardService;
import ewha.lux.once.domain.card.dto.MainCardRequestDto;
import ewha.lux.once.domain.card.service.CrawlingService;
import ewha.lux.once.global.common.CommonResponse;
import ewha.lux.once.global.common.CustomException;
Expand Down Expand Up @@ -37,6 +35,33 @@ public CommonResponse<?> testtest(@AuthenticationPrincipal UserAccount user, @Pa
return new CommonResponse<>(e.getStatus());
}
}

@GetMapping("/test/summary")
@ResponseBody
public CommonResponse<?> testSummary(@AuthenticationPrincipal UserAccount user, @RequestBody TestSummaryDto testSummaryDto) {
try {
cardService.updateBenefitSummaryTest(testSummaryDto.getPrompt(), testSummaryDto.getModel_name());
return new CommonResponse<>(ResponseCode.SUCCESS);
} catch (CustomException e) {
return new CommonResponse<>(e.getStatus());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@GetMapping("/test/summary/index")
@ResponseBody
public CommonResponse<?> testSummaryByIndex(@AuthenticationPrincipal UserAccount user, @RequestBody TestSummaryIndexDto testSummaryIndexDto) {
try {
cardService.updateBenefitSummaryTestByIndex(testSummaryIndexDto.getPrompt(), testSummaryIndexDto.getModel_name(), testSummaryIndexDto.getStart_index(), testSummaryIndexDto.getEnd_index());
return new CommonResponse<>(ResponseCode.SUCCESS);
} catch (CustomException e) {
return new CommonResponse<>(e.getStatus());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

// ============================================================

// [Get] λ§ˆμ΄μ›”λ › 쑰회
Expand Down Expand Up @@ -84,12 +109,13 @@ public CommonResponse<?> cardGoal(@AuthenticationPrincipal UserAccount user, @Re
return new CommonResponse<>(e.getStatus());
}
}

// [Get] CODEF 보유 μΉ΄λ“œ 쑰회
@GetMapping("/list")
@ResponseBody
public CommonResponse<?> codefCardList (@AuthenticationPrincipal UserAccount user, @RequestBody CodefCardListRequestDto codefCardListRequestDto) {
public CommonResponse<?> codefCardList(@AuthenticationPrincipal UserAccount user, @RequestBody CodefCardListRequestDto codefCardListRequestDto) {
try {
return new CommonResponse<>(ResponseCode.SUCCESS,cardService.getCodefCardList(user.getUsers(), codefCardListRequestDto));
return new CommonResponse<>(ResponseCode.SUCCESS, cardService.getCodefCardList(user.getUsers(), codefCardListRequestDto));
} catch (CustomException e) {
return new CommonResponse<>(e.getStatus());
}
Expand All @@ -98,7 +124,7 @@ public CommonResponse<?> codefCardList (@AuthenticationPrincipal UserAccount use
// [Post] μ£ΌμΉ΄λ“œ 등둝
@PostMapping("/main")
@ResponseBody
public CommonResponse<?> registerMainCard (@AuthenticationPrincipal UserAccount user, @RequestBody MainCardRequestDto mainCardRequestDto) {
public CommonResponse<?> registerMainCard(@AuthenticationPrincipal UserAccount user, @RequestBody MainCardRequestDto mainCardRequestDto) {
try {
cardService.postRegisterCard(user.getUsers(), mainCardRequestDto);
return new CommonResponse<>(ResponseCode.SUCCESS);
Expand All @@ -110,7 +136,7 @@ public CommonResponse<?> registerMainCard (@AuthenticationPrincipal UserAccount
// [Get] μ£ΌμΉ΄λ“œ 싀적 μ—…λ°μ΄νŠΈ
@GetMapping("/main/performance")
@ResponseBody
public CommonResponse<?> registerMainCard (@AuthenticationPrincipal UserAccount user) {
public CommonResponse<?> registerMainCard(@AuthenticationPrincipal UserAccount user) {
try {
cardService.updateOwnedCardsPerformance(user.getUsers());
return new CommonResponse<>(ResponseCode.SUCCESS);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/ewha/lux/once/domain/card/dto/TestSummaryDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ewha.lux.once.domain.card.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TestSummaryDto {
private String prompt;
private String model_name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ewha.lux.once.domain.card.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TestSummaryIndexDto {
private String prompt;
private String model_name;
private long start_index;
private long end_index;
}
79 changes: 78 additions & 1 deletion src/main/java/ewha/lux/once/domain/card/service/CardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void updateOwnedCardsPerformance(Users nowUser) throws CustomException {
}

// 맀주 μ›”μš”μΌ 04:00 AM μΉ΄λ“œ ν˜œνƒ 정보 μš”μ•½ μž‘μ—…
@Scheduled(cron = "0 0 4 ? * 1")
// @Scheduled(cron = "0 0 4 ? * 1")
public void updateBenefitSummary() throws CustomException, JsonProcessingException {

List<Card> cardList = cardRepository.findAll();
Expand All @@ -243,4 +243,81 @@ public void updateBenefitSummary() throws CustomException, JsonProcessingExcepti
}
log.info("전체 μΉ΄λ“œ ν˜œνƒ μš”μ•½ μ™„λ£Œ");
}

// ** μΆ”ν›„ μ‚­μ œν•΄μ•Ό 함 - ν…ŒμŠ€νŠΈμš© ** ==================================
// GET /card/test/summary : 전체 μΉ΄λ“œ ν˜œνƒ μš”μ•½
public void updateBenefitSummaryTest(String prompt, String model_name) throws CustomException, JsonProcessingException {

List<Card> cardList = cardRepository.findAll();

int index = 1;
for (Card card : cardList) {
// 기쑴의 BenefitSummary μ‚­μ œ
List<BenefitSummary> existingSummaries = benefitSummaryRepository.findByCard(card);
benefitSummaryRepository.deleteAll(existingSummaries);

log.info("[" + card.getName() + "] - μΉ΄λ“œ ν˜œνƒ μš”μ•½ 쀑... (" + index + "/" + cardList.size() + ")");

BenefitDto[] benefitJson = openaiService.gptBenefitSummaryTest(card.getBenefits(), prompt, model_name);

if (benefitJson == null) {
System.out.println("===========PASS=========" + card.getName());
// cardRepository.delete(card);
continue;
}
for (BenefitDto benefit : benefitJson) {
BenefitSummary benefitSummary = BenefitSummary.builder()
.benefitField(benefit.getBenefit_field())
.benefitContents(benefit.getContent())
.card(card)
.build();

benefitSummaryRepository.save(benefitSummary);
}
index++;
}
log.info("전체 μΉ΄λ“œ ν˜œνƒ μš”μ•½ μ™„λ£Œ");
}

/*
GET /card/test/summary/index : 일뢀 μΉ΄λ“œ ν˜œνƒ μš”μ•½
"prompt": ν”„λ‘¬ν”„νŠΈ λ‚΄μš©,
"model_name": open_ai model,
"start_index": μ‹œμž‘ 인덱슀,
"end_index": μ’…λ£Œ 인덱슀 (포함)
*/
public void updateBenefitSummaryTestByIndex(String prompt, String model_name, long start_index, long end_index) throws CustomException, JsonProcessingException {

List<Card> cardList = cardRepository.findByIdBetween(start_index, end_index);


int index = 1;
for (Card card : cardList) {
// 기쑴의 BenefitSummary μ‚­μ œ
List<BenefitSummary> existingSummaries = benefitSummaryRepository.findByCard(card);
benefitSummaryRepository.deleteAll(existingSummaries);

log.info("[" + card.getName() + " (card_id=" + card.getId() + ")] - μΉ΄λ“œ ν˜œνƒ μš”μ•½ 쀑... (" + index + "/" + cardList.size() + ")");

BenefitDto[] benefitJson = openaiService.gptBenefitSummaryTest(card.getBenefits(), prompt, model_name);

if (benefitJson == null) {
System.out.println("===========PASS=========" + card.getName());
// cardRepository.delete(card);
continue;
}
for (BenefitDto benefit : benefitJson) {
BenefitSummary benefitSummary = BenefitSummary.builder()
.benefitField(benefit.getBenefit_field())
.benefitContents(benefit.getContent())
.card(card)
.build();

benefitSummaryRepository.save(benefitSummary);
}
index++;
}
log.info("일뢀 μΉ΄λ“œ ν˜œνƒ μš”μ•½ μ™„λ£Œ");
}
// ============================================================
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public class CrawlingService {
private static final Logger LOG = LoggerFactory.getLogger(CrawlingService.class);

// 맀주 μ›”μš”μΌ 00:00 μΉ΄λ“œ ν˜œνƒ 크둀링
@Scheduled(cron = "0 0 0 ? * 1")
// ** 크둀링 μžλ™ν™” ν…ŒμŠ€νŠΈ -ing ** =========
@Scheduled(cron = "0 40 9 ? * *")
public void cardCrawling() throws CustomException {
String[] cardCompanyList = {"Kookmin", "Hyundai", "Samsung", "Shinhan", "Lotte", "Hana"};
String[] cardCompanyList = {"Kookmin", "Hana", "Samsung", "Shinhan", "Lotte", "Hyundai"};
for (String cardCompany : cardCompanyList){
crawling(cardCompany);
}
Expand Down Expand Up @@ -88,4 +89,4 @@ private static void executeInsertData(String firstInput, String secondInput) thr
throw new CustomException(ResponseCode.CARD_BENEFITS_INSERT_FAIL);
}
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/ewha/lux/once/domain/home/service/OpenaiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.List;
Expand Down Expand Up @@ -110,4 +112,44 @@ public BenefitDto[] gptBenefitSummary(String benefits) throws CustomException, J

return benefitJson;
}
// ** μΆ”ν›„ μ‚­μ œν•΄μ•Ό 함 - ν…ŒμŠ€νŠΈμš© ** ==================================
public BenefitDto[] gptBenefitSummaryTest(String benefits, String prompt, String model_name) throws CustomException, JsonProcessingException {
try {
// gpt μš”μ²­ λ³΄λ‚΄λŠ” λΆ€λΆ„
OpenaiChatRequest request = new OpenaiChatRequest(model_name, prompt, benefits);
OpenaiChatResponse response = restTemplate.postForObject(apiUrl, request, OpenaiChatResponse.class);

if (response == null || response.getChoices() == null || response.getChoices().isEmpty()) {
throw new CustomException(ResponseCode.FAILED_TO_OPENAI);
}

String result = response.getChoices().get(0).getMessage().getContent();

ObjectMapper objectMapper = new ObjectMapper();
BenefitDto[] benefitJson = objectMapper.readValue(result, BenefitDto[].class);

return benefitJson;
} catch(CustomException | JsonProcessingException | HttpClientErrorException e){
e.printStackTrace();
System.out.println("===========였λ₯˜==========");
int i=2;
while (i>0) {
try {
OpenaiChatRequest request = new OpenaiChatRequest("gpt-4-turbo-preview", prompt, benefits);
OpenaiChatResponse response = restTemplate.postForObject(apiUrl, request, OpenaiChatResponse.class);
String result = response.getChoices().get(0).getMessage().getContent();

ObjectMapper objectMapper = new ObjectMapper();
BenefitDto[] benefitJson = objectMapper.readValue(result, BenefitDto[].class);
return benefitJson;
// } catch (JsonProcessingException | InterruptedException| HttpClientErrorException ex) {
} catch (JsonProcessingException | HttpClientErrorException ex) {
ex.printStackTrace();
i--;
}
}
return null;
}
}
// ============================================================
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface CardRepository extends JpaRepository<Card, Long> {
List<Card> findAllByNameContains(String name);
Optional<Card> findCardByName(String name);
List<Card> findByNameContainingAndCardCompanyIn(String name, List<CardCompany> cardCompanies);

List<Card> findByIdBetween(long startIndex, long endIndex);
}

0 comments on commit a7e46ad

Please sign in to comment.