浏览代码

cold start

丁云鹏 11 月之前
父节点
当前提交
37a8f1ae7e

+ 13 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/AccountContentPoolConfigService.java

@@ -1,6 +1,7 @@
 package com.tzld.longarticle.recommend.server.service;
 
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.google.common.collect.BiMap;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 
@@ -16,10 +17,22 @@ public class AccountContentPoolConfigService {
     @ApolloJsonValue("${accountContentPoolConfig:{}}")
     private Map<String, String[]> accountContentPoolMap;
 
+    @ApolloJsonValue("${contentPoolLevelConfig:{}}")
+    private BiMap<String, Integer> contentPoolLevelMap;
+
+
     public String[] getContentPools(String accountName) {
         if (accountContentPoolMap.containsKey(accountName)) {
             return accountContentPoolMap.get(accountName);
         }
         return accountContentPoolMap.get("default");
     }
+
+    public String getContentPoolByLevel(int level) {
+        return contentPoolLevelMap.inverse().getOrDefault(level, "");
+    }
+
+    public Integer getLevelByContentPool(String contentPool) {
+        return contentPoolLevelMap.get(contentPool);
+    }
 }

+ 4 - 2
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/FilterService.java

@@ -3,7 +3,10 @@ package com.tzld.longarticle.recommend.server.service.filter;
 import com.tzld.longarticle.recommend.server.common.ThreadPoolFactory;
 import com.tzld.longarticle.recommend.server.model.Content;
 import com.tzld.longarticle.recommend.server.service.ServiceBeanFactory;
-import com.tzld.longarticle.recommend.server.service.filter.strategy.*;
+import com.tzld.longarticle.recommend.server.service.filter.strategy.AccountPreDistributeStrategy;
+import com.tzld.longarticle.recommend.server.service.filter.strategy.BadStrategy;
+import com.tzld.longarticle.recommend.server.service.filter.strategy.HistoryTitleStrategy;
+import com.tzld.longarticle.recommend.server.service.filter.strategy.SensitiveStrategy;
 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
 import com.tzld.longarticle.recommend.server.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
@@ -91,7 +94,6 @@ public class FilterService {
         strategies.add(ServiceBeanFactory.getBean(HistoryTitleStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(BadStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(SensitiveStrategy.class));
-        strategies.add(ServiceBeanFactory.getBean(AccountStrategy.class));
         if (StringUtils.equals(param.getStrategy(), "ArticleRankV3")) {
             strategies.add(ServiceBeanFactory.getBean(AccountPreDistributeStrategy.class));
         }

+ 0 - 47
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/AccountStrategy.java

@@ -1,47 +0,0 @@
-package com.tzld.longarticle.recommend.server.service.filter.strategy;
-
-import com.google.common.collect.Sets;
-import com.tzld.longarticle.recommend.server.model.Content;
-import com.tzld.longarticle.recommend.server.service.AccountContentPoolConfigService;
-import com.tzld.longarticle.recommend.server.service.filter.FilterParam;
-import com.tzld.longarticle.recommend.server.service.filter.FilterResult;
-import com.tzld.longarticle.recommend.server.service.filter.FilterStrategy;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-
-/**
- * @author dyp
- */
-@Component
-@Slf4j
-public class AccountStrategy implements FilterStrategy {
-    @Autowired
-    private AccountContentPoolConfigService accountContentPoolConfigService;
-
-    @Override
-    public FilterResult filter(FilterParam param) {
-        FilterResult filterResult = new FilterResult();
-        List<String> result = new ArrayList<>();
-        List<Content> contents = param.getContents();
-        List<Content> filterContents = new ArrayList<>();
-        String[] pools = accountContentPoolConfigService.getContentPools(param.getAccountName());
-        Set<String> poolSet = Sets.newHashSet(pools);
-        for (Content content : contents) {
-            if (poolSet.contains(content.getContentPoolType())) {
-                result.add(content.getId());
-            } else {
-                content.setFilterReason("账号不支持该内容池");
-                filterContents.add(content);
-            }
-        }
-        filterResult.setContentIds(result);
-        filterResult.setFilterContent(filterContents);
-        return filterResult;
-    }
-
-}

+ 47 - 15
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/rank/strategy/RankV3Strategy.java

@@ -81,27 +81,59 @@ public class RankV3Strategy implements RankStrategy {
         // 4 选文章
         List<Content> result = new ArrayList<>();
         String[] contentPools = accountContentPoolConfigService.getContentPools(param.getAccountName());
-        // 头
-        List<Content> pool = contentMap.get(contentPools[0]);
-        if (CollectionUtils.isNotEmpty(pool)) {
-            result.add(pool.get(0));
-        }
-        // 次
-        pool = contentMap.get(contentPools[1]);
-        if (CollectionUtils.isNotEmpty(pool)) {
-            if (StringUtils.equals(contentPools[0], contentPools[1])) {
-                if (pool.size() > 2) {
-                    result.add(pool.get(2));
+
+
+        // 头、次
+        if (StringUtils.equals(contentPools[0], contentPools[1])) {
+            List<Content> pool = contentMap.get(contentPools[0]);
+            Integer level = accountContentPoolConfigService.getLevelByContentPool(contentPools[0]);
+            if (level == 1) {
+                if (CollectionUtils.isNotEmpty(pool)) {
+                    result.add(pool.get(0));
+                    if (pool.size() > 2) {
+                        result.add(pool.get(2));
+                    }
+                } else {
+                    // level2 兜底
+                    pool = contentMap.get(accountContentPoolConfigService.getContentPoolByLevel(2));
+                    if (CollectionUtils.isNotEmpty(pool)) {
+                        result.add(pool.get(0));
+                        if (pool.size() > 1) {
+                            result.add(pool.get(1));
+                        }
+                    }
+                }
+            } else if (level == 2) {
+                if (CollectionUtils.isNotEmpty(pool)) {
+                    pool = pool.subList(0, Math.max(10, pool.size()));
+                    Collections.shuffle(pool);
+                    result.add(pool.get(0));
+                    if (pool.size() > 1) {
+                        result.add(pool.get(1));
+                    }
+                }
+            }
+        } else {
+            // 配置错误 兜底
+            List<Content> pool1 = contentMap.get(contentPools[0]);
+            List<Content> pool2 = contentMap.get(contentPools[1]);
+            if (CollectionUtils.isNotEmpty(pool1)) {
+                result.add(pool1.get(0));
+                if (CollectionUtils.isNotEmpty(pool2)) {
+                    result.add(pool2.get(0));
+                }
+            } else if (CollectionUtils.isNotEmpty(pool2)) {
+                result.add(pool2.get(0));
+                if (pool2.size() > 1) {
+                    result.add(pool2.get(1));
                 }
-            } else {
-                result.add(pool.get(0));
             }
         }
 
-
         // 3-8
-        pool = contentMap.get(contentPools[2]);
+        List<Content> pool = contentMap.get(contentPools[2]);
         if (CollectionUtils.isNotEmpty(pool)) {
+            Collections.shuffle(pool);
             result.addAll(pool.subList(0, Math.min(pool.size(), param.getSize() - result.size())));
         }