Browse Source

晚上7点后仅发送冷启内容池内容

wangyunpeng 9 months ago
parent
commit
ea3643c644

+ 1 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/common/enums/RankStrategyEnum.java

@@ -18,6 +18,7 @@ public enum RankStrategyEnum {
     ArticleRankV11("ArticleRankV11", "ArticleRankV11", "rankV11Strategy"),
     ArticleRankV12("ArticleRankV12", "ArticleRankV12", "rankV12Strategy"),
 
+    late_Strategy("ArticleRankLate", "晚间策略", "lateRankStrategy"),
     default_strategy("ArticleRankV1", "默认策略", "defaultRankStrategy"),
     ;
 

+ 2 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/mapper/aigc/AigcBaseMapper.java

@@ -23,4 +23,6 @@ public interface AigcBaseMapper {
     List<CrawlerPlan> getCrawlerPlanByPlanIds(List<String> planIds);
 
     List<NotPublishPlan> getNotPublishPlan();
+
+    Integer getPublishPlanPushType(String planId);
 }

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

@@ -2,7 +2,13 @@ package com.tzld.longarticle.recommend.server.service;
 
 import com.alibaba.fastjson.JSONObject;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.tzld.longarticle.recommend.server.common.enums.RankStrategyEnum;
+import com.tzld.longarticle.recommend.server.mapper.aigc.AigcBaseMapper;
+import com.tzld.longarticle.recommend.server.mapper.crawler.ArticleUserGroupMapper;
 import com.tzld.longarticle.recommend.server.model.dto.Content;
+import com.tzld.longarticle.recommend.server.model.entity.crawler.AccountAvgInfo;
+import com.tzld.longarticle.recommend.server.model.entity.crawler.PublishContentSortLog;
+import com.tzld.longarticle.recommend.server.model.entity.crawler.PublishSortLog;
 import com.tzld.longarticle.recommend.server.model.param.RecommendParam;
 import com.tzld.longarticle.recommend.server.model.param.RecommendRequest;
 import com.tzld.longarticle.recommend.server.model.vo.ArticleSortResponseData;
@@ -12,10 +18,6 @@ import com.tzld.longarticle.recommend.server.model.vo.RecommendWithUserGroupResp
 import com.tzld.longarticle.recommend.server.repository.crawler.AccountAvgInfoRepository;
 import com.tzld.longarticle.recommend.server.repository.crawler.PublishContentSortLogRepository;
 import com.tzld.longarticle.recommend.server.repository.crawler.PublishSortLogRepository;
-import com.tzld.longarticle.recommend.server.model.entity.crawler.AccountAvgInfo;
-import com.tzld.longarticle.recommend.server.model.entity.crawler.PublishContentSortLog;
-import com.tzld.longarticle.recommend.server.model.entity.crawler.PublishSortLog;
-import com.tzld.longarticle.recommend.server.mapper.crawler.ArticleUserGroupMapper;
 import com.tzld.longarticle.recommend.server.service.rank.RankParam;
 import com.tzld.longarticle.recommend.server.service.rank.RankResult;
 import com.tzld.longarticle.recommend.server.service.rank.RankService;
@@ -63,6 +65,8 @@ public class RecommendService {
     AccountAvgInfoRepository accountAvgInfoRepository;
     @Autowired
     private ArticleUserGroupMapper articleUserGroupMapper;
+    @Autowired
+    private AigcBaseMapper aigcBaseMapper;
 
     @ApolloJsonValue("${accountStrategyConfig:{}}")
     private Map<String, String> accountStrategyConfigMap;
@@ -75,20 +79,30 @@ public class RecommendService {
         RecommendParam param = genRecommendParam(request, SceneConstants.DEFAULT);
         log.info("genRecommendParam {}", JSONUtils.toJson(param));
         // 获取账号排序设置
-        String strategyConfig = accountStrategyConfigMap.get(request.getAccountName());
-        if (StringUtils.hasText(strategyConfig) && !request.isParamStrategy()) {
-            param.setStrategy(strategyConfig);
-        }
+        setStrategy(request, param);
+
         RecallResult recallResult = recallService.recall(convertToRecallParam(param));
 
         RankResult rankResult = rankService.rank(convertToRankParam(param, recallResult));
-        //log.info("rankResult {}", JSONUtils.toJson(rankResult));
 
         saveSortLog(param, rankResult);
 
         return buildRecommendResponse(recallResult, rankResult, param.getPublishNum());
     }
 
+    private void setStrategy(RecommendRequest request, RecommendParam param) {
+        String strategyConfig = accountStrategyConfigMap.get(request.getAccountName());
+        if (StringUtils.hasText(strategyConfig) && !request.isParamStrategy()) {
+            param.setStrategy(strategyConfig);
+        }
+        if (DateUtils.getCurrentHour() >= 19) {
+            Integer pushType = aigcBaseMapper.getPublishPlanPushType(request.getPlanId());
+            if (Objects.equals(2, pushType)) {
+                param.setStrategy(RankStrategyEnum.late_Strategy.getStrategy());
+            }
+        }
+    }
+
     public RecommendWithUserGroupResponse recommend4FwhColdStart(RecommendRequest request) {
 
         RecommendParam param = genRecommendParam(request, FWH_COLD_START);
@@ -99,10 +113,8 @@ public class RecommendService {
             param.setStrategy(strategyConfig);
         }
         RecallResult recallResult = recallService.recall(convertToRecallParam(param));
-        //log.info("recallResult {}", JSONUtils.toJson(recallResult));
 
         RankResult rankResult = rankService.rank(convertToRankParam(param, recallResult));
-        //log.info("rankResult {}", JSONUtils.toJson(rankResult));
 
         saveSortLog(param, rankResult);
 

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

@@ -0,0 +1,80 @@
+package com.tzld.longarticle.recommend.server.service.rank.strategy;
+
+import com.tzld.longarticle.recommend.server.model.dto.Content;
+import com.tzld.longarticle.recommend.server.service.AccountContentPoolConfigService;
+import com.tzld.longarticle.recommend.server.service.rank.RankItem;
+import com.tzld.longarticle.recommend.server.service.rank.RankParam;
+import com.tzld.longarticle.recommend.server.service.rank.RankResult;
+import com.tzld.longarticle.recommend.server.service.rank.RankStrategy;
+import com.tzld.longarticle.recommend.server.service.score.ScoreParam;
+import com.tzld.longarticle.recommend.server.service.score.ScoreResult;
+import com.tzld.longarticle.recommend.server.service.score.ScoreService;
+import com.tzld.longarticle.recommend.server.service.score.strategy.*;
+import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Service
+@Slf4j
+public class LateRankStrategy implements RankStrategy {
+
+    @Autowired
+    private ScoreService scoreService;
+    @Autowired
+    private AccountContentPoolConfigService accountContentPoolConfigService;
+
+    public RankResult rank(RankParam param) {
+        List<Content> result = new ArrayList<>();
+        String[] contentPools = accountContentPoolConfigService.getContentPools(param.getAccountName());
+        ScoreParam scoreParam = RankStrategy.convertToScoreParam(param);
+        scoreParam.setContents(scoreParam.getContents().stream().filter(
+                c -> contentPools[2].equals(c.getContentPoolType())).collect(Collectors.toList()));
+        ScoreResult scoreResult = scoreService.score(RankStrategy.convertToScoreParam(param));
+
+        Map<String, Map<String, Double>> scoreMap = scoreResult.getScoreMap();
+
+        List<RankItem> items = CommonCollectionUtils.toList(scoreParam.getContents(), c -> {
+            RankItem item = new RankItem();
+            item.setContent(c);
+            c.setScoreMap(scoreMap.get(c.getId()));
+            item.setScoreMap(scoreMap.get(c.getId()));
+            double score = 0.0;
+            if (contentPools[2].equals(item.getContent().getContentPoolType())) {
+                score = item.getScore(SimilarityStrategy.class.getSimpleName())
+                        + item.getScore(CategoryStrategy.class.getSimpleName())
+                        + item.getScore(AccountPreDistributeStrategy.class.getSimpleName())
+                        + item.getScore(PublishTimesStrategy.class.getSimpleName())
+                        + item.getScore(FlowCtlDecreaseStrategy.class.getSimpleName());
+            }
+            c.setScore(score);
+            item.setScore(score);
+            return item;
+        });
+
+        // 1 排序
+        Collections.sort(items, (o1, o2) -> -Double.compare(o1.getScore(), o2.getScore()));
+        // 2 相似去重
+        List<Content> contents = CommonCollectionUtils.toList(items, RankItem::getContent);
+
+        // 3 文章按照内容池分组
+        Map<String, List<Content>> contentMap = new HashMap<>();
+        for (Content c : contents) {
+            List<Content> data = contentMap.computeIfAbsent(c.getContentPoolType(), k -> new ArrayList<>());
+            data.add(c);
+        }
+
+        // 全部使用3-8内容池
+        List<Content> pool = contentMap.get(contentPools[2]);
+        if (CollectionUtils.isNotEmpty(pool)) {
+            result.addAll(pool.subList(0, Math.min(pool.size(), param.getSize())));
+        }
+
+        return new RankResult(result);
+    }
+
+}

+ 7 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/ScoreService.java

@@ -10,7 +10,6 @@ import com.tzld.longarticle.recommend.server.util.JSONUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.stereotype.Service;
@@ -95,6 +94,13 @@ public class ScoreService implements ApplicationContextAware {
         if (!similarityStopStrategies.contains(param.getStrategy())) {
             strategies.add(strategyMap.get(SimilarityStrategy.class.getSimpleName()));
         }
+        if (StringUtils.equals(param.getStrategy(), RankStrategyEnum.late_Strategy.getStrategy())) {
+            strategies.add(strategyMap.get(CategoryStrategy.class.getSimpleName()));
+            strategies.add(strategyMap.get(AccountPreDistributeStrategy.class.getSimpleName()));
+            strategies.add(strategyMap.get(PublishTimesStrategy.class.getSimpleName()));
+            strategies.add(strategyMap.get(FlowCtlDecreaseStrategy.class.getSimpleName()));
+            return strategies;
+        }
         strategies.add(strategyMap.get(ViewCountStrategy.class.getSimpleName()));
         if (StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV3.getStrategy())
                 || StringUtils.equals(param.getStrategy(), RankStrategyEnum.ArticleRankV4.getStrategy())

+ 1 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/strategy/ViewCountRateCorrelationStrategy.java

@@ -71,7 +71,7 @@ public class ViewCountRateCorrelationStrategy implements ScoreStrategy {
                 for (ContentHisPublishArticle hisItem : content.getHisPublishArticleList()) {
                     // 过滤掉发布时间晚于20点数据
                     int publishHour = DateUtils.getHourByTimestamp(hisItem.getUpdateTime());
-                    if (publishHour >= 20) {
+                    if (publishHour >= 19) {
                         continue;
                     }
                     if (hisItem.isInnerAccount() && Objects.nonNull(hisItem.getViewCount())

+ 1 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/score/strategy/ViewCountRateStrategy.java

@@ -71,7 +71,7 @@ public class ViewCountRateStrategy implements ScoreStrategy {
                 for (ContentHisPublishArticle hisItem : content.getHisPublishArticleList()) {
                     // 过滤掉发布时间晚于20点数据
                     int publishHour = DateUtils.getHourByTimestamp(hisItem.getUpdateTime());
-                    if (publishHour >= 20) {
+                    if (publishHour >= 19) {
                         continue;
                     }
                     if (hisItem.isInnerAccount() && Objects.nonNull(hisItem.getViewCount())

+ 4 - 0
long-article-recommend-service/src/main/resources/mapper/aigc/AigcBaseMapper.xml

@@ -79,5 +79,9 @@
           AND setting.push_type = 2
     </select>
 
+    <select id="getPublishPlanPushType" resultType="java.lang.Integer">
+        select push_type from publish_plan_setting where plan_id = #{planId}
+    </select>
+
 
 </mapper>