Skip to content

Commit 5059635

Browse files
authored
Merge pull request #241 from FunD-StockProject/refactor/sector-20
Refactor: 섹터별 추천 20개씩 반환하도록 고정
2 parents 9f45643 + 7bb71df commit 5059635

1 file changed

Lines changed: 28 additions & 34 deletions

File tree

src/main/java/com/fund/stockProject/stock/service/StockService.java

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,8 @@ public PageResponse<ShortViewResponse> getRecommendedStocksByDomesticSectorPaged
10361036
if (sector == null || sector == DomesticSector.UNKNOWN) {
10371037
return PageResponse.<ShortViewResponse>builder()
10381038
.items(java.util.List.of())
1039-
.page(page)
1040-
.size(size)
1039+
.page(0)
1040+
.size(20)
10411041
.totalElements(0)
10421042
.totalPages(0)
10431043
.build();
@@ -1073,21 +1073,18 @@ public PageResponse<ShortViewResponse> getRecommendedStocksByDomesticSectorPaged
10731073
})
10741074
.toList();
10751075

1076-
// sort by score desc
1077-
List<Stock> sorted = stocksWithScore.stream()
1078-
.sorted((a, b) -> {
1079-
int sa = getScoreByCountry(scoreMap.get(a.getId()), a.getExchangeNum());
1080-
int sb = getScoreByCountry(scoreMap.get(b.getId()), b.getExchangeNum());
1081-
return Integer.compare(sb, sa);
1082-
})
1083-
.toList();
1076+
// 사용자 요청에서의 페이징을 무시하고, 무작위로 20개를 추천합니다.
1077+
final int RECOMMEND_SIZE = 20;
10841078

1085-
final int total = sorted.size();
1086-
final int totalPages = (int) Math.ceil((double) total / size);
1087-
final int from = Math.min(page * size, total);
1088-
final int to = Math.min(from + size, total);
1079+
// Shuffle to pick random items (preserve stocksWithScore list)
1080+
java.util.List<Stock> shuffled = new java.util.ArrayList<>(stocksWithScore);
1081+
java.util.Collections.shuffle(shuffled, new Random(System.currentTimeMillis()));
10891082

1090-
List<ShortViewResponse> items = sorted.subList(from, to).stream()
1083+
final int total = shuffled.size();
1084+
final int totalPages = (int) Math.ceil((double) total / RECOMMEND_SIZE);
1085+
1086+
List<ShortViewResponse> items = shuffled.stream()
1087+
.limit(RECOMMEND_SIZE)
10911088
.map(stock -> {
10921089
try {
10931090
var stockInfo = securityService.getRealTimeStockPrice(stock).block();
@@ -1102,8 +1099,8 @@ public PageResponse<ShortViewResponse> getRecommendedStocksByDomesticSectorPaged
11021099
.toList();
11031100
return PageResponse.<ShortViewResponse>builder()
11041101
.items(items)
1105-
.page(page)
1106-
.size(size)
1102+
.page(0)
1103+
.size(RECOMMEND_SIZE)
11071104
.totalElements(total)
11081105
.totalPages(totalPages)
11091106
.build();
@@ -1181,8 +1178,8 @@ public PageResponse<ShortViewResponse> getRecommendedStocksByOverseasSectorPaged
11811178
if (sector == null || sector == OverseasSector.UNKNOWN) {
11821179
return PageResponse.<ShortViewResponse>builder()
11831180
.items(java.util.List.of())
1184-
.page(page)
1185-
.size(size)
1181+
.page(0)
1182+
.size(20)
11861183
.totalElements(0)
11871184
.totalPages(0)
11881185
.build();
@@ -1218,21 +1215,18 @@ public PageResponse<ShortViewResponse> getRecommendedStocksByOverseasSectorPaged
12181215
})
12191216
.toList();
12201217

1221-
// sort by score desc
1222-
List<Stock> sorted = stocksWithScore.stream()
1223-
.sorted((a, b) -> {
1224-
int sa = getScoreByCountry(scoreMap.get(a.getId()), a.getExchangeNum());
1225-
int sb = getScoreByCountry(scoreMap.get(b.getId()), b.getExchangeNum());
1226-
return Integer.compare(sb, sa);
1227-
})
1228-
.toList();
1218+
// 사용자 요청에서의 페이징을 무시하고, 무작위로 20개를 추천합니다.
1219+
final int RECOMMEND_SIZE = 20;
12291220

1230-
final int total = sorted.size();
1231-
final int totalPages = (int) Math.ceil((double) total / size);
1232-
final int from = Math.min(page * size, total);
1233-
final int to = Math.min(from + size, total);
1221+
// Shuffle to pick random items (preserve stocksWithScore list)
1222+
java.util.List<Stock> shuffled = new java.util.ArrayList<>(stocksWithScore);
1223+
java.util.Collections.shuffle(shuffled, new Random(System.currentTimeMillis()));
12341224

1235-
List<ShortViewResponse> items = sorted.subList(from, to).stream()
1225+
final int total = shuffled.size();
1226+
final int totalPages = (int) Math.ceil((double) total / RECOMMEND_SIZE);
1227+
1228+
List<ShortViewResponse> items = shuffled.stream()
1229+
.limit(RECOMMEND_SIZE)
12361230
.map(stock -> {
12371231
try {
12381232
var stockInfo = securityService.getRealTimeStockPrice(stock).block();
@@ -1247,8 +1241,8 @@ public PageResponse<ShortViewResponse> getRecommendedStocksByOverseasSectorPaged
12471241
.toList();
12481242
return PageResponse.<ShortViewResponse>builder()
12491243
.items(items)
1250-
.page(page)
1251-
.size(size)
1244+
.page(0)
1245+
.size(RECOMMEND_SIZE)
12521246
.totalElements(total)
12531247
.totalPages(totalPages)
12541248
.build();

0 commit comments

Comments
 (0)