Browse Source

cold start

丁云鹏 11 months ago
parent
commit
3b19cc7537

+ 10 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/RecommendService.java

@@ -93,10 +93,19 @@ public class RecommendService {
     public RankParam convertToRankParam(RecommendParam param, RecallResult recallResult) {
         RankParam rankParam = new RankParam();
         List<Content> contentList = new ArrayList<>();
+        List<Content> backup = new ArrayList<>();
         if (Objects.nonNull(recallResult) && CollectionUtils.isNotEmpty(recallResult.getData())) {
-            recallResult.getData().forEach(item -> contentList.addAll(item.getContents()));
+            for (RecallResult.RecallData item : recallResult.getData()) {
+                if (item.isBackup()) {
+                    backup.addAll(item.getContents());
+                } else {
+                    contentList.addAll(item.getContents());
+                }
+            }
+
         }
         rankParam.setContents(contentList);
+        rankParam.setBackup(backup);
         rankParam.setStrategy(param.getStrategy());
         rankParam.setAccountName(param.getAccountName());
         rankParam.setSize(param.getPublishNum());

+ 1 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/FilterParam.java

@@ -15,4 +15,5 @@ public class FilterParam {
     private String accountId;
     private String strategy;
     private String ghId;
+    private boolean backup;
 }

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

@@ -3,10 +3,7 @@ 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.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.service.filter.strategy.*;
 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
 import com.tzld.longarticle.recommend.server.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
@@ -90,6 +87,14 @@ public class FilterService {
     }
 
     private List<FilterStrategy> getStrategies(FilterParam param) {
+        if (param.isBackup()) {
+            List<FilterStrategy> strategies = new ArrayList<>();
+            strategies.add(ServiceBeanFactory.getBean(HistoryTitleStrategy.class));
+            strategies.add(ServiceBeanFactory.getBean(BadStrategy.class));
+            strategies.add(ServiceBeanFactory.getBean(SensitiveStrategy.class));
+            strategies.add(ServiceBeanFactory.getBean(ColdStartBackupFilterStrategy.class));
+            return strategies;
+        }
         List<FilterStrategy> strategies = new ArrayList<>();
         strategies.add(ServiceBeanFactory.getBean(HistoryTitleStrategy.class));
         strategies.add(ServiceBeanFactory.getBean(BadStrategy.class));

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

@@ -0,0 +1,40 @@
+package com.tzld.longarticle.recommend.server.service.filter.strategy;
+
+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.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author dyp
+ */
+@Component
+@Slf4j
+public class ColdStartBackupFilterStrategy 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();
+        String[] pools = accountContentPoolConfigService.getContentPools(param.getAccountName());
+        for (Content content : contents) {
+            if (pools.length > 2 && StringUtils.equals(pools[2], content.getContentPoolType())) {
+                result.add(content.getId());
+            }
+        }
+        filterResult.setContentIds(result);
+        return filterResult;
+    }
+
+}

+ 1 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/rank/RankParam.java

@@ -12,6 +12,7 @@ import java.util.List;
 public class RankParam {
     private String accountName;
     private List<Content> contents;
+    private List<Content> backup;
     private int size;
     private String strategy;
 }

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

@@ -125,6 +125,11 @@ public class RankV3Strategy implements RankStrategy {
         if (CollectionUtils.isNotEmpty(pool)) {
             Collections.shuffle(pool);
             result.addAll(pool.subList(0, Math.min(pool.size(), param.getSize() - result.size())));
+        } else {
+            // 兜底
+            pool = param.getBackup();
+            Collections.shuffle(pool);
+            result.addAll(pool.subList(0, Math.min(pool.size(), param.getSize() - result.size())));
         }
 
         return new RankResult(result);

+ 1 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recall/RecallResult.java

@@ -20,6 +20,7 @@ public class RecallResult {
     @AllArgsConstructor
     @NoArgsConstructor
     public static class RecallData {
+        private boolean backup;
         private List<Content> contents;
         private List<Content> filterContents;
     }

+ 2 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recall/RecallService.java

@@ -1,6 +1,7 @@
 package com.tzld.longarticle.recommend.server.service.recall;
 
 import com.tzld.longarticle.recommend.server.common.ThreadPoolFactory;
+import com.tzld.longarticle.recommend.server.service.recall.strategy.ColdStartBackupRecallStrategy;
 import com.tzld.longarticle.recommend.server.service.recall.strategy.DefaultRecallStrategy;
 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
 import com.tzld.longarticle.recommend.server.util.JSONUtils;
@@ -80,7 +81,7 @@ public class RecallService implements ApplicationContextAware {
     private List<RecallStrategy> getRecallStrategy(RecallParam param) {
         List<RecallStrategy> strategies = new ArrayList<>();
         strategies.add(strategyMap.get(DefaultRecallStrategy.class.getSimpleName()));
-
+        strategies.add(strategyMap.get(ColdStartBackupRecallStrategy.class.getSimpleName()));
         return strategies;
     }
 

+ 71 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recall/strategy/ColdStartBackupRecallStrategy.java

@@ -0,0 +1,71 @@
+package com.tzld.longarticle.recommend.server.service.recall.strategy;
+
+import com.tzld.longarticle.recommend.server.model.Content;
+import com.tzld.longarticle.recommend.server.remote.AIGCRemoteService;
+import com.tzld.longarticle.recommend.server.repository.mapper.crawler.CrawlerBaseMapper;
+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.FilterService;
+import com.tzld.longarticle.recommend.server.service.recall.*;
+import org.apache.commons.collections4.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * @author dyp
+ */
+@Component
+public class ColdStartBackupRecallStrategy implements RecallStrategy {
+
+    @Autowired
+    private FilterService filterService;
+    @Autowired
+    private AIGCRemoteService aigcRemoteService;
+    @Autowired
+    CrawlerBaseMapper crawlerBaseMapper;
+
+    @Override
+    public RecallResult.RecallData recall(RecallParam param) {
+
+        List<Content> content = aigcRemoteService.getAllContent(param);
+        // category 查询
+        setContentCategory(content);
+        // 处理 content
+        FilterParam filterParam = FilterParamFactory.create(param, content);
+        filterParam.setBackup(true);
+        FilterResult filterResult = filterService.filter(filterParam);
+        // 处理 content
+        RecallResult.RecallData result = new RecallResult.RecallData();
+        result.setContents(content.stream().filter(o -> filterResult.getContentIds().contains(o.getId()))
+                .collect(Collectors.toList()));
+        result.setFilterContents(filterResult.getFilterContent());
+        result.setBackup(true);
+        return result;
+    }
+
+    private void setContentCategory(List<Content> contentList) {
+        List<String> channelContentIds = contentList.stream().map(Content::getCrawlerChannelContentId).collect(Collectors.toList());
+        List<ContentCategory> categoryList = getContentCategoryByChannelContentId(channelContentIds);
+        if (CollectionUtils.isEmpty(categoryList)) {
+            return;
+        }
+        Map<String, List<String>> categoryMap = categoryList.stream().collect(Collectors.groupingBy(ContentCategory::getContentChannelId,
+                Collectors.mapping(ContentCategory::getCategory, Collectors.toList())));
+        for (Content content : contentList) {
+            content.setCategory(categoryMap.get(content.getCrawlerChannelContentId()));
+        }
+    }
+
+    private List<ContentCategory> getContentCategoryByChannelContentId(List<String> channelContentIds) {
+        if (CollectionUtils.isEmpty(channelContentIds)) {
+            return new ArrayList<>();
+        }
+        return crawlerBaseMapper.getContentCategory(channelContentIds);
+    }
+
+}