-
Notifications
You must be signed in to change notification settings - Fork 34
[volume-9] Product Ranking with Redis #227
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
Merged
Merged
Changes from all commits
Commits
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
107 changes: 107 additions & 0 deletions
107
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,107 @@ | ||
| package com.loopers.application.ranking; | ||
|
|
||
| import com.loopers.domain.product.Product; | ||
| import com.loopers.domain.product.ProductService; | ||
| import com.loopers.domain.ranking.RankingService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * ๋ญํน Facade | ||
| * <p> | ||
| * ๋ญํน ์กฐํ ๋ก์ง์ ๋ด๋นํ๋ฉฐ, ZSET์์ ๋ญํน ์ ๋ณด๋ฅผ ์กฐํํ๊ณ | ||
| * ์ํ ์ ๋ณด๋ฅผ Aggregationํ์ฌ ๋ฐํํฉ๋๋ค. | ||
| */ | ||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class RankingFacade { | ||
|
|
||
| private final RankingService rankingService; | ||
| private final ProductService productService; | ||
|
|
||
| /** | ||
| * ๋ญํน ํ์ด์ง ์กฐํ | ||
| * 1. ZSET์์ Top-N ์ํ ID ์กฐํ | ||
| * 2. ์ํ ์ ๋ณด ์กฐํ ๋ฐ Aggregation | ||
| * 3. ์์ ์ ๋ณด ํฌํจํ์ฌ ๋ฐํ | ||
| */ | ||
| @Transactional(readOnly = true) | ||
| public RankingInfo.RankingsPageResponse getRankings(String date, Pageable pageable) { | ||
| String rankingKey = rankingService.getRankingKey(date); | ||
|
|
||
| // ํ์ด์ง๋ค์ด์ ๊ณ์ฐ | ||
| long start = pageable.getPageNumber() * pageable.getPageSize(); | ||
| long end = start + pageable.getPageSize() - 1; | ||
|
|
||
| // ZSET์์ ์ํ ID์ ์ ์ ์กฐํ | ||
| List<RankingService.RankingEntry> entries = | ||
| rankingService.getTopNWithScores(rankingKey, start, end); | ||
|
|
||
| if (entries.isEmpty()) { | ||
| return RankingInfo.RankingsPageResponse.empty(pageable); | ||
| } | ||
|
|
||
| // ์ํ ID ๋ฆฌ์คํธ ์ถ์ถ | ||
| List<Long> productIds = entries.stream() | ||
| .map(RankingService.RankingEntry::getProductId) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| // ์ํ ์ ๋ณด ์กฐํ | ||
| Map<Long, Product> productMap = productService.getProductMapByIds(productIds); | ||
|
|
||
| // ๋ญํน ์ ๋ณด์ ์ํ ์ ๋ณด ๊ฒฐํฉ | ||
| List<RankingInfo.RankingItem> rankingItems = new ArrayList<>(); | ||
| long rank = start + 1; // 1๋ถํฐ ์์ํ๋ ์์ | ||
|
|
||
| for (RankingService.RankingEntry entry : entries) { | ||
| Product product = productMap.get(entry.getProductId()); | ||
| if (product != null) { | ||
| rankingItems.add(RankingInfo.RankingItem.of( | ||
| rank++, | ||
| entry.getProductId(), | ||
| entry.getScore(), | ||
| product | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| // ์ ์ฒด ๋ญํน ์ ์กฐํ (์ด ํ์ด์ง ์ ๊ณ์ฐ์ฉ) | ||
| Long totalSize = rankingService.getRankingSize(rankingKey); | ||
|
|
||
| return RankingInfo.RankingsPageResponse.of( | ||
| rankingItems, | ||
| pageable.getPageNumber(), | ||
| pageable.getPageSize(), | ||
| totalSize != null ? totalSize.intValue() : 0 | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * ํน์ ์ํ์ ๋ญํน ์ ๋ณด ์กฐํ | ||
| */ | ||
| @Transactional(readOnly = true) | ||
| public RankingInfo.ProductRankingInfo getProductRanking(Long productId, String date) { | ||
| String rankingKey = rankingService.getRankingKey(date); | ||
| Long rank = rankingService.getRank(rankingKey, productId); | ||
| Double score = rankingService.getScore(rankingKey, productId); | ||
|
|
||
| if (rank == null || score == null) { | ||
| return null; // ๋ญํน์ ์์ | ||
| } | ||
|
|
||
| return RankingInfo.ProductRankingInfo.of( | ||
| rank + 1, // 1๋ถํฐ ์์ํ๋ ์์๋ก ๋ณํ | ||
| score | ||
| ); | ||
| } | ||
| } | ||
|
|
77 changes: 77 additions & 0 deletions
77
apps/commerce-api/src/main/java/com/loopers/application/ranking/RankingInfo.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,77 @@ | ||
| package com.loopers.application.ranking; | ||
|
|
||
| import com.loopers.domain.product.Product; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * ๋ญํน ์ ๋ณด DTO | ||
| */ | ||
| public class RankingInfo { | ||
|
|
||
| /** | ||
| * ๋ญํน ํ์ด์ง ์๋ต | ||
| */ | ||
| public record RankingsPageResponse( | ||
| List<RankingItem> items, | ||
| int page, | ||
| int size, | ||
| int total | ||
| ) { | ||
| public static RankingsPageResponse empty(Pageable pageable) { | ||
| return new RankingsPageResponse( | ||
| Collections.emptyList(), | ||
| pageable.getPageNumber(), | ||
| pageable.getPageSize(), | ||
| 0 | ||
| ); | ||
| } | ||
|
|
||
| public static RankingsPageResponse of( | ||
| List<RankingItem> items, | ||
| int page, | ||
| int size, | ||
| int total | ||
| ) { | ||
| return new RankingsPageResponse(items, page, size, total); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * ๋ญํน ํญ๋ชฉ (์ํ ์ ๋ณด + ์์ + ์ ์) | ||
| */ | ||
| public record RankingItem( | ||
| Long rank, | ||
| Long productId, | ||
| Double score, | ||
| String productName, | ||
| Long brandId, | ||
| Integer price | ||
| ) { | ||
| public static RankingItem of(Long rank, Long productId, Double score, Product product) { | ||
| return new RankingItem( | ||
| rank, | ||
| productId, | ||
| score, | ||
| product.getName(), | ||
| product.getBrandId(), | ||
| product.getPrice().amount() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * ์ํ ๋ญํน ์ ๋ณด (์์ธ ์กฐํ์ฉ) | ||
| */ | ||
| public record ProductRankingInfo( | ||
| Long rank, | ||
| Double score | ||
| ) { | ||
| public static ProductRankingInfo of(Long rank, Double score) { | ||
| return new ProductRankingInfo(rank, score); | ||
| } | ||
| } | ||
| } | ||
|
|
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
29 changes: 29 additions & 0 deletions
29
apps/commerce-api/src/main/java/com/loopers/interfaces/api/ranking/RankingV1ApiSpec.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,29 @@ | ||
| package com.loopers.interfaces.api.ranking; | ||
|
|
||
| import com.loopers.interfaces.api.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.Parameter; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
|
|
||
| @Tag(name = "Ranking V1 API", description = "๋ญํน API ์ ๋๋ค.") | ||
| public interface RankingV1ApiSpec { | ||
|
|
||
| @Operation( | ||
| method = "GET", | ||
| summary = "๋ญํน ํ์ด์ง ์กฐํ", | ||
| description = "์ผ๊ฐ ๋ญํน์ ํ์ด์ง๋ค์ด์ ์ผ๋ก ์กฐํํฉ๋๋ค." | ||
| ) | ||
| ApiResponse<RankingV1Dto.RankingsPageResponse> getRankings( | ||
| @Parameter(description = "๋ ์ง (yyyyMMdd ํ์, ๋ฏธ์ง์ ์ ์ค๋ ๋ ์ง)", example = "20241219") | ||
| @Schema(description = "๋ ์ง (yyyyMMdd ํ์, ๋ฏธ์ง์ ์ ์ค๋ ๋ ์ง)", example = "20241219") | ||
| String date, | ||
| @Parameter(description = "ํ์ด์ง ํฌ๊ธฐ", example = "20") | ||
| @Schema(description = "ํ์ด์ง ํฌ๊ธฐ", example = "20") | ||
| int size, | ||
| @Parameter(description = "ํ์ด์ง ๋ฒํธ (0๋ถํฐ ์์)", example = "0") | ||
| @Schema(description = "ํ์ด์ง ๋ฒํธ (0๋ถํฐ ์์)", example = "0") | ||
| int page | ||
| ); | ||
| } | ||
|
|
41 changes: 41 additions & 0 deletions
41
apps/commerce-api/src/main/java/com/loopers/interfaces/api/ranking/RankingV1Controller.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,41 @@ | ||
| package com.loopers.interfaces.api.ranking; | ||
|
|
||
| import com.loopers.application.ranking.RankingFacade; | ||
| import com.loopers.application.ranking.RankingInfo; | ||
| import com.loopers.interfaces.api.ApiResponse; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @RestController | ||
| @RequestMapping("/api/v1/rankings") | ||
| public class RankingV1Controller implements RankingV1ApiSpec { | ||
|
|
||
| private final RankingFacade rankingFacade; | ||
|
|
||
| @GetMapping | ||
| @Override | ||
| public ApiResponse<RankingV1Dto.RankingsPageResponse> getRankings( | ||
| @RequestParam(required = false) String date, | ||
| @RequestParam(defaultValue = "20") int size, | ||
| @RequestParam(defaultValue = "0") int page | ||
| ) { | ||
| String rankingDate = date != null ? date : | ||
| LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); | ||
|
|
||
| Pageable pageable = PageRequest.of(page, size); | ||
| RankingInfo.RankingsPageResponse response = | ||
| rankingFacade.getRankings(rankingDate, pageable); | ||
|
|
||
| return ApiResponse.success(RankingV1Dto.RankingsPageResponse.from(response)); | ||
| } | ||
| } | ||
|
|
||
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.
์ ๋ ฅ ํ๋ผ๋ฏธํฐ ์ ํจ์ฑ ๊ฒ์ฆ ํ์
sizeํ๋ผ๋ฏธํฐ์ ์ํ์ด ์์ด ๋งค์ฐ ํฐ ๊ฐ(์: 1000000) ์์ฒญ ์ Redis์ DB์ ๋ถํ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค. ๋ํdateํ๋ผ๋ฏธํฐ์ ํ์(yyyyMMdd) ๊ฒ์ฆ์ด ์์ต๋๋ค.๐ ์ ์๋ ์์
๐ค Prompt for AI Agents