|
| 1 | +package com.loopers.batch.domain; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.springframework.data.redis.core.RedisTemplate; |
| 6 | +import org.springframework.stereotype.Component; |
| 7 | + |
| 8 | +/** |
| 9 | + * Redis에서 랭킹 가중치를 읽어오는 컴포넌트 |
| 10 | + * commerce-streamer의 RankingWeight와 동일한 Redis Key를 사용 |
| 11 | + */ |
| 12 | +@Slf4j |
| 13 | +@Component |
| 14 | +@RequiredArgsConstructor |
| 15 | +public class RankingWeightReader { |
| 16 | + |
| 17 | + private final RedisTemplate<String, String> redisTemplate; |
| 18 | + |
| 19 | + // commerce-streamer의 RankingWeight와 동일한 Key |
| 20 | + private static final String WEIGHT_KEY = "ranking:config:weights"; |
| 21 | + |
| 22 | + // 기본 가중치 (commerce-streamer와 동일) |
| 23 | + private static final double DEFAULT_VIEW_WEIGHT = 0.1; |
| 24 | + private static final double DEFAULT_LIKE_WEIGHT = 0.2; |
| 25 | + private static final double DEFAULT_ORDER_WEIGHT = 0.7; |
| 26 | + |
| 27 | + public double getViewWeight() { |
| 28 | + return getWeight("view", DEFAULT_VIEW_WEIGHT); |
| 29 | + } |
| 30 | + |
| 31 | + public double getLikeWeight() { |
| 32 | + return getWeight("like", DEFAULT_LIKE_WEIGHT); |
| 33 | + } |
| 34 | + |
| 35 | + public double getOrderWeight() { |
| 36 | + return getWeight("order", DEFAULT_ORDER_WEIGHT); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * 총점 계산 |
| 41 | + */ |
| 42 | + public double calculateTotalScore(int totalLikes, int totalSales, int totalViews) { |
| 43 | + return (totalViews * getViewWeight()) + (totalLikes * getLikeWeight()) + (totalSales * getOrderWeight()); |
| 44 | + } |
| 45 | + |
| 46 | + private double getWeight(String field, double defaultValue) { |
| 47 | + try { |
| 48 | + Object value = redisTemplate.opsForHash().get(WEIGHT_KEY, field); |
| 49 | + if (value != null) { |
| 50 | + return Double.parseDouble(value.toString()); |
| 51 | + } |
| 52 | + } catch (Exception e) { |
| 53 | + log.warn("가중치 조회 실패, 기본값 사용: field={}, default={}", field, defaultValue, e); |
| 54 | + } |
| 55 | + return defaultValue; |
| 56 | + } |
| 57 | +} |
0 commit comments