浏览代码

feat:热点宝召回修改

zhaohaipeng 1 周之前
父节点
当前提交
9330f9739b

+ 9 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/flowpool/FlowPoolConfigService.java

@@ -58,4 +58,13 @@ public class FlowPoolConfigService {
             return Collections.emptyMap();
         }
     }
+
+    public Map<String, Double> getDouHotFLowPoolWithLevel() {
+        try {
+            return levelWeightConfigCache.get("dou:hot:flow:pool:level:weight");
+        } catch (Exception e) {
+            log.error("getDouHotFLowConfig error. \n", e);
+            return Collections.emptyMap();
+        }
+    }
 }

+ 1 - 1
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/flowpool/FlowPoolService.java

@@ -372,7 +372,7 @@ public class FlowPoolService {
             if (MapUtils.isEmpty(flowPoolVideoInfo.getAttribute())) {
                 continue;
             }
-            String province = flowPoolVideoInfo.getAttribute().getString("province");
+            String province = RecallUtils.douHotProvinceConvert(flowPoolVideoInfo.getAttribute().getString("province"));
             resultMap.computeIfAbsent(province, s -> new HashMap<>()).put(flowPoolVideoInfo.getVideoId(), 1d);
         }
         return resultMap;

+ 48 - 5
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/recall/strategy/DouHotFlowPoolRecallStrategy.java

@@ -5,13 +5,15 @@ import com.tzld.piaoquan.recommend.server.model.Video;
 import com.tzld.piaoquan.recommend.server.service.filter.FilterParam;
 import com.tzld.piaoquan.recommend.server.service.filter.FilterResult;
 import com.tzld.piaoquan.recommend.server.service.filter.FilterService;
+import com.tzld.piaoquan.recommend.server.service.flowpool.FlowPoolConfigService;
 import com.tzld.piaoquan.recommend.server.service.recall.FilterParamFactory;
 import com.tzld.piaoquan.recommend.server.service.recall.RecallParam;
-import com.tzld.piaoquan.recommend.server.service.recall.RecallStrategy;
 import com.tzld.piaoquan.recommend.server.util.RecallUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.annotation.Value;
@@ -23,7 +25,7 @@ import java.util.*;
 
 @Slf4j
 @Component
-public class DouHotFlowPoolRecallStrategy implements RecallStrategy {
+public class DouHotFlowPoolRecallStrategy extends AbstractFlowPoolWithLevelRecallStrategy {
 
     @Autowired
     @Qualifier("redisTemplate")
@@ -32,18 +34,60 @@ public class DouHotFlowPoolRecallStrategy implements RecallStrategy {
     @Autowired
     private FilterService filterService;
 
+    @Autowired
+    private FlowPoolConfigService flowPoolConfigService;
+
     @Value("${dou.hot.recall.video.count:20}")
     private Integer douHotRecallVideoCnt;
 
     public static final String PUSH_FROM = "recall_strategy_hotspot";
 
+    @Override
+    Pair<String, String> flowPoolKeyAndLevel(RecallParam param) {
+
+        Map<String, Double> douHotFLowPoolWithLevel = flowPoolConfigService.getDouHotFLowPoolWithLevel();
+        if (MapUtils.isEmpty(douHotFLowPoolWithLevel)) {
+            return Pair.of("", "");
+        }
+
+        double totalWeight = 0;
+        NavigableMap<Double, String> levelMap = new TreeMap<>();
+        for (Map.Entry<String, Double> entry : douHotFLowPoolWithLevel.entrySet()) {
+            String redisKey = String.format(RedisKeyConstants.DouHot.ITEM_REDIS_KEY_FORMAT, RecallUtils.douHotProvinceConvert(param.getProvince()), entry.getKey());
+            if (redisTemplate.hasKey(redisKey)) {
+                totalWeight += entry.getValue();
+                levelMap.put(totalWeight, entry.getKey());
+            }
+        }
+
+        if (MapUtils.isEmpty(levelMap)) {
+            return Pair.of("", "");
+        }
+
+        double randomValue = Math.random() * totalWeight;
+        Map.Entry<Double, String> chooseEntry = levelMap.higherEntry(randomValue);
+        if (Objects.isNull(chooseEntry)) {
+            return Pair.of("", "");
+        }
+
+        String redisKey = String.format(RedisKeyConstants.DouHot.ITEM_REDIS_KEY_FORMAT, RecallUtils.douHotProvinceConvert(param.getProvince()), chooseEntry.getValue());
+
+        return Pair.of(redisKey, chooseEntry.getValue());
+    }
+
     @Override
     public List<Video> recall(RecallParam param) {
         if (StringUtils.isBlank(param.getProvince())) {
             return Collections.emptyList();
         }
 
-        String redisKey = String.format(RedisKeyConstants.DouHot.ITEM_REDIS_KEY_FORMAT, RecallUtils.douHotProvinceConvert(param.getProvince()), "1");
+        Pair<String, String> weightPair = this.flowPoolKeyAndLevel(param);
+        if (Objects.isNull(weightPair)) {
+            return Collections.emptyList();
+        }
+
+        String redisKey = weightPair.getLeft();
+        String level = weightPair.getRight();
         // log.info("[DouHot recall redisKey] {}", redisKey);
         Set<ZSetOperations.TypedTuple<String>> redisValues = redisTemplate.opsForZSet().rangeWithScores(redisKey, 0, 10000000);
 
@@ -82,8 +126,7 @@ public class DouHotFlowPoolRecallStrategy implements RecallStrategy {
             video.setVideoId(videoId);
             video.setFlowPool(videoIdAndFlowPoolMap.get(videoId));
             video.setPushFrom(pushFrom());
-            // 热点宝供给目前只有一层,所以写死
-            video.setLevel("1");
+            video.setLevel(level);
             videos.add(video);
         }