|
@@ -0,0 +1,96 @@
|
|
|
+package com.tzld.longarticle.recommend.server.service.score.strategy;
|
|
|
+
|
|
|
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
|
|
|
+import com.tzld.longarticle.recommend.server.model.Content;
|
|
|
+import com.tzld.longarticle.recommend.server.repository.crawler.PublishSortLogRepository;
|
|
|
+import com.tzld.longarticle.recommend.server.repository.entity.crawler.PublishSortLog;
|
|
|
+import com.tzld.longarticle.recommend.server.service.AccountContentPoolConfigService;
|
|
|
+import com.tzld.longarticle.recommend.server.service.score.Score;
|
|
|
+import com.tzld.longarticle.recommend.server.service.score.ScoreParam;
|
|
|
+import com.tzld.longarticle.recommend.server.service.score.ScoreStrategy;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class ColdStartDecreaseStrategy implements ScoreStrategy {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AccountContentPoolConfigService accountContentPoolConfigService;
|
|
|
+ @Autowired
|
|
|
+ private PublishSortLogRepository publishSortLogRepository;
|
|
|
+
|
|
|
+ // 账号score减少
|
|
|
+ @ApolloJsonValue("${accountColdStartScoreDecreaseConfig:{\"default\":-100}}")
|
|
|
+ private Map<String, Integer> accountColdStartScoreDecreaseMap;
|
|
|
+
|
|
|
+ // 计算阅读均值base
|
|
|
+ @ApolloJsonValue("${totalAvgReadCountBase:{\"default\":100}}")
|
|
|
+ private Map<String, Integer> totalAvgReadCountBaseMap;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Score> score(ScoreParam param) {
|
|
|
+ List<Score> scores = new ArrayList<>();
|
|
|
+ if (CollectionUtils.isEmpty(param.getContents())) {
|
|
|
+ return scores;
|
|
|
+ }
|
|
|
+ String[] contentPools = accountContentPoolConfigService.getContentPools(param.getAccountName());
|
|
|
+ List<String> crawlerChannelContentIds = param.getContents().stream().map(Content::getCrawlerChannelContentId).collect(Collectors.toList());
|
|
|
+ // 获取历史已发布内容
|
|
|
+ List<PublishSortLog> hisPublishContentList = publishSortLogRepository.findByCrawlerChannelContentIdIn(crawlerChannelContentIds);
|
|
|
+ Map<String, List<PublishSortLog>> hisPublishedContentMap = hisPublishContentList.stream().collect(Collectors.groupingBy(PublishSortLog::getCrawlerChannelContentId));
|
|
|
+ for (Content content : param.getContents()) {
|
|
|
+ // 仅判断3-8条 冷启层
|
|
|
+ if (!contentPools[2].equals(content.getContentPoolType())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Score score = new Score();
|
|
|
+ score.setStrategy(this);
|
|
|
+ score.setContentId(content.getId());
|
|
|
+ Integer scoreVal = getContentScore(param.getAccountName(), hisPublishedContentMap, content.getCrawlerChannelContentId());
|
|
|
+ if (scoreVal != 0) {
|
|
|
+ score.setScore(scoreVal);
|
|
|
+ scores.add(score);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return scores;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer getContentScore(String accountName,
|
|
|
+ Map<String, List<PublishSortLog>> hisPublishedContentMap,
|
|
|
+ String crawlerChannelContentId) {
|
|
|
+ Integer weight = getColdStartScoreDecreaseWeight(accountName);
|
|
|
+ Integer totalAvgReadCountBase = getAvgReadCountBase(accountName);
|
|
|
+ if (hisPublishedContentMap.containsKey(crawlerChannelContentId)) {
|
|
|
+ List<PublishSortLog> publishContents = hisPublishedContentMap.get(crawlerChannelContentId);
|
|
|
+ double sumViewCount = publishContents.stream().mapToDouble(PublishSortLog::getIndexAvgCount).sum();
|
|
|
+ if (sumViewCount > totalAvgReadCountBase) {
|
|
|
+ return weight;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer getColdStartScoreDecreaseWeight(String accountName) {
|
|
|
+ if (accountColdStartScoreDecreaseMap.containsKey(accountName)) {
|
|
|
+ return accountColdStartScoreDecreaseMap.get(accountName);
|
|
|
+ }
|
|
|
+ return accountColdStartScoreDecreaseMap.get("default");
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer getAvgReadCountBase(String accountName) {
|
|
|
+ if (totalAvgReadCountBaseMap.containsKey(accountName)) {
|
|
|
+ return totalAvgReadCountBaseMap.get(accountName);
|
|
|
+ }
|
|
|
+ return totalAvgReadCountBaseMap.get("default");
|
|
|
+ }
|
|
|
+}
|