-
Notifications
You must be signed in to change notification settings - Fork 35
Feat/volume 10 #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jeonga1022
wants to merge
31
commits into
Loopers-dev-lab:jeonga1022
Choose a base branch
from
jeonga1022:feat/volume-10
base: jeonga1022
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/volume 10 #242
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
61b0df6
docs: 9์ฃผ์ฐจ ์๊ตฌ์ฌํญ ๋ถ์ ๋ฌธ์ ์์ฑ
jeonga1022 77225a4
docs: 9์ฃผ์ฐจ ์ํ์ค ๋ค์ด์ด๊ทธ๋จ ์์ฑ
jeonga1022 bca67df
feat: ๋ญํน Redis ์๋น์ค ๊ธฐ๋ณธ ๊ตฌํ (getTopProducts, incrementScoreForView)
jeonga1022 7275f5b
feat: ์ข์์ ์ ๋ญํน ์ ์ ์ฆ๊ฐ ๊ธฐ๋ฅ ๊ตฌํ
jeonga1022 bc18cc4
feat: ์ฃผ๋ฌธ ์ ๋ญํน ์ ์ ์ฆ๊ฐ ๊ธฐ๋ฅ ๊ตฌํ
jeonga1022 d47efeb
feat: ์ํ ์กฐํ ๋ก๊ทธ ์ํฐํฐ ๋ฐ Repository ์์ฑ
jeonga1022 e38d21b
feat: ๋ญํน ์์คํ
์ ์ํ ์ํ ์กฐํ ์ด๋ฒคํธ ์ถ๊ฐ
jeonga1022 3cf0dd6
feat: ์ํ ์กฐํ ์ด๋ฒคํธ ๋ฐฐ์น ์ฒ๋ฆฌ Consumer ์ถ๊ฐ
jeonga1022 dc137cf
feat: ZSET TTL 2์ผ ์ค์
jeonga1022 82bd67b
feat: ์ํ ์กฐํ ์ด๋ฒคํธ ๋ฐํ ๊ตฌํ
jeonga1022 e4a6149
feat: ์ข์์ ์ด๋ฒคํธ ์ ๋ญํน ์ ์ ๋ฐ์
jeonga1022 569b5ae
refactor: ์ด๋ฒคํธ๋ณ Consumer ๋ถ๋ฆฌ ๋ฐ ํ ํฝ ๋ณ๊ฒฝ
jeonga1022 045e6cb
feat: Ranking API ๊ตฌํ
jeonga1022 c5a89f6
feat: ์ํ ์์ธ ์กฐํ ์ ๋ญํน ์์ ๋ฐํ
jeonga1022 f994a91
fix: ProductViewedEvent Jackson ์ญ์ง๋ ฌํ ํธํ์ฑ ์์
jeonga1022 7c253b2
refactor: EXPIRE๋ฅผ ํค ์์ฑ ์์๋ง ํธ์ถํ๋๋ก ๋ณ๊ฒฝ
jeonga1022 ed9adee
docs: 10์ฃผ์ฐจ ์๊ตฌ์ฌํญ ๋ฐ ์ํ์ค ๋ค์ด์ด๊ทธ๋จ ๋ฌธ์ ์ถ๊ฐ
jeonga1022 b88de75
refactor: ProductMetrics์ date, viewCount ์ปฌ๋ผ ์ถ๊ฐ
jeonga1022 b1e72df
feat: Consumer์์ ProductMetrics ์ผ๋ณ ์ ์ฅ ์ ์ฉ
jeonga1022 cdd6f76
feat: Spring Batch ์ค์ ์ถ๊ฐ
jeonga1022 3dc3c47
feat: ์ฃผ๊ฐ ๋ญํน MV ์ํฐํฐ ์ถ๊ฐ
jeonga1022 fc113a2
feat: ์ฃผ๊ฐ ๋ญํน ์ง๊ณ Batch Job ๊ตฌํ
jeonga1022 d2369ac
feat: ์๊ฐ ๋ญํน ์ง๊ณ Batch Job ๊ตฌํ
jeonga1022 4a065de
refactor: Redis ๋ญํน ์ ์๋ฅผ ์ ์(1:2:6)๋ก ํต์ผ
jeonga1022 db4572f
feat: Ranking API์ period ํ๋ผ๋ฏธํฐ ์ถ๊ฐ (daily/weekly/monthly)
jeonga1022 dc52982
refactor: Batch Writer์์ Step Listener๋ก ์ญ์ ์ด๋
jeonga1022 08290df
refactor: @Modifying ๋ฉ์๋์ @Transactional ์ถ๊ฐ
jeonga1022 01ed56a
test: RankingApiE2ETest ํ์ฑํ
jeonga1022 f162468
feat: ์ฃผ๊ฐ/์๊ฐ ๋ญํน Redis ์บ์ fallback ๋ฐ ๋ฐฐ์น ํ ์บ์ ๊ฐฑ์ ์ถ๊ฐ
jeonga1022 fe77482
fix: Kafka admin bootstrap.servers ํฌํธ 19092๋ก ํต์ผ
jeonga1022 cec5275
fix: Consumer์ ProductMetrics Lost Update ๋ฌธ์ ํด๊ฒฐ
jeonga1022 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
231 changes: 231 additions & 0 deletions
231
apps/commerce-api/src/main/java/com/loopers/application/ranking/RankingFacade.java
This file contains hidden or 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,231 @@ | ||
| package com.loopers.application.ranking; | ||
|
|
||
| import com.loopers.domain.product.Product; | ||
| import com.loopers.domain.product.ProductRepository; | ||
| import com.loopers.infrastructure.ranking.ProductRankMonthly; | ||
| import com.loopers.infrastructure.ranking.ProductRankMonthlyRepository; | ||
| import com.loopers.infrastructure.ranking.ProductRankWeekly; | ||
| import com.loopers.infrastructure.ranking.ProductRankWeeklyRepository; | ||
| import com.loopers.infrastructure.ranking.RankingEntry; | ||
| import com.loopers.infrastructure.ranking.RankingRedisService; | ||
| import com.loopers.interfaces.api.ranking.RankingDto; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.time.DayOfWeek; | ||
| import java.time.LocalDate; | ||
| import java.time.YearMonth; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class RankingFacade { | ||
|
|
||
| private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); | ||
|
|
||
| private final RankingRedisService rankingRedisService; | ||
| private final ProductRepository productRepository; | ||
| private final ProductRankWeeklyRepository productRankWeeklyRepository; | ||
| private final ProductRankMonthlyRepository productRankMonthlyRepository; | ||
|
|
||
| public RankingDto.RankingListResponse getRankings(String period, String dateStr, int page, int size) { | ||
| return switch (period.toLowerCase()) { | ||
| case "weekly" -> getWeeklyRankings(dateStr, page, size); | ||
| case "monthly" -> getMonthlyRankings(dateStr, page, size); | ||
| default -> getDailyRankings(dateStr, page, size); | ||
| }; | ||
| } | ||
|
|
||
| private RankingDto.RankingListResponse getDailyRankings(String dateStr, int page, int size) { | ||
| LocalDate date = parseDate(dateStr); | ||
| int offset = page * size; | ||
|
|
||
| List<RankingEntry> entries = rankingRedisService.getTopProducts(date, offset, size); | ||
|
|
||
| if (entries.isEmpty()) { | ||
| return new RankingDto.RankingListResponse(List.of(), page, size, 0); | ||
| } | ||
|
|
||
| long totalCount = rankingRedisService.getTotalCount(date); | ||
|
|
||
| List<Long> productIds = entries.stream() | ||
| .map(RankingEntry::productId) | ||
| .toList(); | ||
|
|
||
| Map<Long, Product> productMap = productRepository.findAllByIdIn(productIds).stream() | ||
| .collect(Collectors.toMap(Product::getId, p -> p)); | ||
|
|
||
| List<RankingDto.RankingResponse> rankings = new ArrayList<>(); | ||
| int rank = offset + 1; | ||
| for (RankingEntry entry : entries) { | ||
| Product product = productMap.get(entry.productId()); | ||
| if (product != null) { | ||
| rankings.add(new RankingDto.RankingResponse( | ||
| rank++, | ||
| product.getId(), | ||
| product.getName(), | ||
| product.getPrice(), | ||
| entry.score() | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| return new RankingDto.RankingListResponse(rankings, page, size, totalCount); | ||
| } | ||
|
|
||
| private RankingDto.RankingListResponse getWeeklyRankings(String dateStr, int page, int size) { | ||
| LocalDate date = parseDate(dateStr); | ||
| LocalDate weekStart = date.with(DayOfWeek.MONDAY); | ||
| int offset = page * size; | ||
|
|
||
| // 1. Redis ์บ์ ๋จผ์ ์กฐํ | ||
| List<RankingEntry> cachedEntries = rankingRedisService.getWeeklyRankingCache(weekStart, offset, size); | ||
| if (!cachedEntries.isEmpty()) { | ||
| long totalCount = rankingRedisService.getWeeklyRankingCacheCount(weekStart); | ||
| return buildRankingResponse(cachedEntries, offset, page, size, totalCount); | ||
| } | ||
|
|
||
| // 2. ์บ์ ์์ผ๋ฉด DB ์กฐํ | ||
| List<ProductRankWeekly> weeklyRanks = productRankWeeklyRepository | ||
| .findByPeriodStartOrderByRankingAsc(weekStart); | ||
|
|
||
| if (weeklyRanks.isEmpty()) { | ||
| return new RankingDto.RankingListResponse(List.of(), page, size, 0); | ||
| } | ||
|
|
||
| // 3. DB ๊ฒฐ๊ณผ๋ฅผ Redis์ ์บ์ | ||
| List<RankingEntry> allEntries = weeklyRanks.stream() | ||
| .map(r -> new RankingEntry(r.getProductId(), r.getScore().doubleValue())) | ||
| .toList(); | ||
| rankingRedisService.cacheWeeklyRanking(weekStart, allEntries); | ||
|
|
||
| // 4. ํ์ด์ง ์ฒ๋ฆฌ ํ ๋ฐํ | ||
| int toIndex = Math.min(offset + size, weeklyRanks.size()); | ||
| if (offset >= weeklyRanks.size()) { | ||
| return new RankingDto.RankingListResponse(List.of(), page, size, weeklyRanks.size()); | ||
| } | ||
|
|
||
| List<ProductRankWeekly> pagedRanks = weeklyRanks.subList(offset, toIndex); | ||
|
|
||
| List<Long> productIds = pagedRanks.stream() | ||
| .map(ProductRankWeekly::getProductId) | ||
| .toList(); | ||
|
|
||
| Map<Long, Product> productMap = productRepository.findAllByIdIn(productIds).stream() | ||
| .collect(Collectors.toMap(Product::getId, p -> p)); | ||
|
|
||
| List<RankingDto.RankingResponse> rankings = new ArrayList<>(); | ||
| for (ProductRankWeekly rank : pagedRanks) { | ||
| Product product = productMap.get(rank.getProductId()); | ||
| if (product != null) { | ||
| rankings.add(new RankingDto.RankingResponse( | ||
| rank.getRanking(), | ||
| product.getId(), | ||
| product.getName(), | ||
| product.getPrice(), | ||
| rank.getScore().doubleValue() | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| return new RankingDto.RankingListResponse(rankings, page, size, weeklyRanks.size()); | ||
| } | ||
|
|
||
| private RankingDto.RankingListResponse getMonthlyRankings(String dateStr, int page, int size) { | ||
| LocalDate date = parseDate(dateStr); | ||
| YearMonth yearMonth = YearMonth.from(date); | ||
| LocalDate monthStart = yearMonth.atDay(1); | ||
| int offset = page * size; | ||
|
|
||
| // 1. Redis ์บ์ ๋จผ์ ์กฐํ | ||
| List<RankingEntry> cachedEntries = rankingRedisService.getMonthlyRankingCache(monthStart, offset, size); | ||
| if (!cachedEntries.isEmpty()) { | ||
| long totalCount = rankingRedisService.getMonthlyRankingCacheCount(monthStart); | ||
| return buildRankingResponse(cachedEntries, offset, page, size, totalCount); | ||
| } | ||
|
|
||
| // 2. ์บ์ ์์ผ๋ฉด DB ์กฐํ | ||
| List<ProductRankMonthly> monthlyRanks = productRankMonthlyRepository | ||
| .findByPeriodStartOrderByRankingAsc(monthStart); | ||
|
|
||
| if (monthlyRanks.isEmpty()) { | ||
| return new RankingDto.RankingListResponse(List.of(), page, size, 0); | ||
| } | ||
|
|
||
| // 3. DB ๊ฒฐ๊ณผ๋ฅผ Redis์ ์บ์ | ||
| List<RankingEntry> allEntries = monthlyRanks.stream() | ||
| .map(r -> new RankingEntry(r.getProductId(), r.getScore().doubleValue())) | ||
| .toList(); | ||
| rankingRedisService.cacheMonthlyRanking(monthStart, allEntries); | ||
|
|
||
| // 4. ํ์ด์ง ์ฒ๋ฆฌ ํ ๋ฐํ | ||
| int toIndex = Math.min(offset + size, monthlyRanks.size()); | ||
| if (offset >= monthlyRanks.size()) { | ||
| return new RankingDto.RankingListResponse(List.of(), page, size, monthlyRanks.size()); | ||
| } | ||
|
|
||
| List<ProductRankMonthly> pagedRanks = monthlyRanks.subList(offset, toIndex); | ||
|
|
||
| List<Long> productIds = pagedRanks.stream() | ||
| .map(ProductRankMonthly::getProductId) | ||
| .toList(); | ||
|
|
||
| Map<Long, Product> productMap = productRepository.findAllByIdIn(productIds).stream() | ||
| .collect(Collectors.toMap(Product::getId, p -> p)); | ||
|
|
||
| List<RankingDto.RankingResponse> rankings = new ArrayList<>(); | ||
| for (ProductRankMonthly rank : pagedRanks) { | ||
| Product product = productMap.get(rank.getProductId()); | ||
| if (product != null) { | ||
| rankings.add(new RankingDto.RankingResponse( | ||
| rank.getRanking(), | ||
| product.getId(), | ||
| product.getName(), | ||
| product.getPrice(), | ||
| rank.getScore().doubleValue() | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| return new RankingDto.RankingListResponse(rankings, page, size, monthlyRanks.size()); | ||
| } | ||
|
|
||
| private RankingDto.RankingListResponse buildRankingResponse( | ||
| List<RankingEntry> entries, int offset, int page, int size, long totalCount) { | ||
|
|
||
| List<Long> productIds = entries.stream() | ||
| .map(RankingEntry::productId) | ||
| .toList(); | ||
|
|
||
| Map<Long, Product> productMap = productRepository.findAllByIdIn(productIds).stream() | ||
| .collect(Collectors.toMap(Product::getId, p -> p)); | ||
|
|
||
| List<RankingDto.RankingResponse> rankings = new ArrayList<>(); | ||
| int rank = offset + 1; | ||
| for (RankingEntry entry : entries) { | ||
| Product product = productMap.get(entry.productId()); | ||
| if (product != null) { | ||
| rankings.add(new RankingDto.RankingResponse( | ||
| rank++, | ||
| product.getId(), | ||
| product.getName(), | ||
| product.getPrice(), | ||
| entry.score() | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| return new RankingDto.RankingListResponse(rankings, page, size, totalCount); | ||
| } | ||
|
|
||
| private LocalDate parseDate(String dateStr) { | ||
| if (dateStr == null || dateStr.isBlank()) { | ||
| return LocalDate.now(); | ||
| } | ||
| return LocalDate.parse(dateStr, DATE_FORMATTER); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
periodํ๋ผ๋ฏธํฐ null ์ฒดํฌ ๋๋ฝperiod.toLowerCase()ํธ์ถ ์period๊ฐ null์ด๋ฉดNullPointerException์ด ๋ฐ์ํฉ๋๋ค. API ๋ ์ด์ด์์ ๊ธฐ๋ณธ๊ฐ์ ๋ณด์ฅํ์ง ์๋๋ค๋ฉด ๋ฐฉ์ด ๋ก์ง์ด ํ์ํฉ๋๋ค.๐ ์์ ์ ์
public RankingDto.RankingListResponse getRankings(String period, String dateStr, int page, int size) { + String normalizedPeriod = (period == null) ? "daily" : period.toLowerCase(); - return switch (period.toLowerCase()) { + return switch (normalizedPeriod) { case "weekly" -> getWeeklyRankings(dateStr, page, size); case "monthly" -> getMonthlyRankings(dateStr, page, size); default -> getDailyRankings(dateStr, page, size); }; }๐ค Prompt for AI Agents