|
| 1 | +package com.loopers.batch.job.ranking.reader; |
| 2 | + |
| 3 | +import com.loopers.batch.domain.RankingWeightReader; |
| 4 | +import com.loopers.dto.ProductMetricsSummary; |
| 5 | +import com.loopers.dto.RankedProduct; |
| 6 | +import com.loopers.infrastructure.metrics.ProductMetricsJpaRepository; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.springframework.batch.core.configuration.annotation.StepScope; |
| 9 | +import org.springframework.batch.item.ItemReader; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.stereotype.Component; |
| 12 | + |
| 13 | +import java.time.DayOfWeek; |
| 14 | +import java.time.LocalDate; |
| 15 | +import java.time.format.DateTimeFormatter; |
| 16 | +import java.util.Comparator; |
| 17 | +import java.util.Iterator; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +@Slf4j |
| 23 | +@StepScope |
| 24 | +@Component |
| 25 | +public class WeeklyRankingReader implements ItemReader<RankedProduct> { |
| 26 | + |
| 27 | + private final ProductMetricsJpaRepository metricsRepository; |
| 28 | + private final RankingWeightReader rankingWeightReader; |
| 29 | + private Iterator<RankedProduct> iterator; |
| 30 | + private boolean initialized = false; |
| 31 | + |
| 32 | + @Value("#{jobParameters['startDate']}") |
| 33 | + private String startDateStr; |
| 34 | + |
| 35 | + @Value("#{jobParameters['endDate']}") |
| 36 | + private String endDateStr; |
| 37 | + |
| 38 | + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); |
| 39 | + private static final int TOP_N = 100; |
| 40 | + |
| 41 | + public WeeklyRankingReader(ProductMetricsJpaRepository metricsRepository, |
| 42 | + RankingWeightReader rankingWeightReader) { |
| 43 | + this.metricsRepository = metricsRepository; |
| 44 | + this.rankingWeightReader = rankingWeightReader; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public RankedProduct read() { |
| 49 | + if (!initialized) { |
| 50 | + initialize(); |
| 51 | + initialized = true; |
| 52 | + } |
| 53 | + |
| 54 | + if (iterator != null && iterator.hasNext()) { |
| 55 | + return iterator.next(); |
| 56 | + } |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + private void initialize() { |
| 61 | + LocalDate startDate = parseStartDate(); |
| 62 | + LocalDate endDate = parseEndDate(); |
| 63 | + |
| 64 | + log.info("주간 랭킹 Reader 초기화: startDate={}, endDate={}", startDate, endDate); |
| 65 | + |
| 66 | + List<ProductMetricsSummary> summaries = metricsRepository.findAllByProductIdAndDateBetween(startDate, endDate); |
| 67 | + |
| 68 | + if (summaries.isEmpty()) { |
| 69 | + log.warn("집계할 메트릭 데이터가 없습니다."); |
| 70 | + iterator = List.<RankedProduct>of().iterator(); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // 상품별 집계 |
| 75 | + Map<Long, double[]> aggregatedMap = summaries.stream() |
| 76 | + .collect(Collectors.groupingBy( |
| 77 | + ProductMetricsSummary::getProductId, |
| 78 | + Collectors.collectingAndThen( |
| 79 | + Collectors.toList(), |
| 80 | + list -> { |
| 81 | + double[] sums = new double[3]; |
| 82 | + for (ProductMetricsSummary s : list) { |
| 83 | + sums[0] += s.getLikeCount(); |
| 84 | + sums[1] += s.getStockCount(); |
| 85 | + sums[2] += s.getViewCount(); |
| 86 | + } |
| 87 | + return sums; |
| 88 | + } |
| 89 | + ) |
| 90 | + )); |
| 91 | + |
| 92 | + // 점수 계산 및 정렬 |
| 93 | + List<RankedProduct> rankedProducts = aggregatedMap.entrySet().stream() |
| 94 | + .map(entry -> { |
| 95 | + double[] sums = entry.getValue(); |
| 96 | + double score = rankingWeightReader.calculateTotalScore( |
| 97 | + (int) sums[0], (int) sums[1], (int) sums[2] |
| 98 | + ); |
| 99 | + return new RankedProduct(entry.getKey(), score); |
| 100 | + }) |
| 101 | + .sorted(Comparator.comparingDouble(RankedProduct::score).reversed()) |
| 102 | + .limit(TOP_N) |
| 103 | + .toList(); |
| 104 | + |
| 105 | + log.info("주간 랭킹 Reader 완료: 집계 상품 수={}, TOP-N={}", aggregatedMap.size(), rankedProducts.size()); |
| 106 | + |
| 107 | + iterator = rankedProducts.iterator(); |
| 108 | + } |
| 109 | + |
| 110 | + private LocalDate parseStartDate() { |
| 111 | + if (startDateStr != null && !startDateStr.isBlank()) { |
| 112 | + return LocalDate.parse(startDateStr, DATE_FORMATTER); |
| 113 | + } |
| 114 | + // 기본값: 이번 주 월요일 |
| 115 | + return LocalDate.now().with(DayOfWeek.MONDAY); |
| 116 | + } |
| 117 | + |
| 118 | + private LocalDate parseEndDate() { |
| 119 | + if (endDateStr != null && !endDateStr.isBlank()) { |
| 120 | + return LocalDate.parse(endDateStr, DATE_FORMATTER); |
| 121 | + } |
| 122 | + // 기본값: 이번 주 일요일 |
| 123 | + return LocalDate.now().with(DayOfWeek.SUNDAY); |
| 124 | + } |
| 125 | +} |
0 commit comments