丁云鹏 11 месяцев назад
Родитель
Сommit
8001d8f810
12 измененных файлов с 244 добавлено и 52 удалено
  1. 0 1
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/Application.java
  2. 35 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/ArticlePreDistributeAccount.java
  3. 13 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/ArticlePreDistributeAccountRepository.java
  4. 1 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/FilterParam.java
  5. 57 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/AccountPreDistributeStrategy.java
  6. 47 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/AccountStrategy.java
  7. 0 26
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/CategoryStrategy.java
  8. 0 25
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/filter/strategy/DuplicateStrategy.java
  9. 3 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/rank/RankService.java
  10. 83 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/rank/strategy/RankV3Strategy.java
  11. 1 0
      long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/service/recall/FilterParamFactory.java
  12. 4 0
      long-article-recommend-service/src/main/resources/application-dev.yml

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

@@ -1,6 +1,5 @@
 package com.tzld.longarticle.recommend.server;
 
-// import com.tzld.piaoquan.recommend.feature.client.FeatureClient;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;

+ 35 - 0
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/ArticlePreDistributeAccount.java

@@ -0,0 +1,35 @@
+package com.tzld.longarticle.recommend.server.repository;
+
+import lombok.Data;
+
+import javax.persistence.*;
+import java.io.Serializable;
+
+@Data
+@Entity
+@Table(name = "article_pre_distribute_account")
+@IdClass(ArticlePreDistributeAccount.PK.class)
+public class ArticlePreDistributeAccount implements Serializable {
+
+    @Id
+    private String ghId;
+    // YYYY-mm-dd
+    @Id
+    private String date;
+
+    @Column(name = "article_list", columnDefinition = "TEXT")
+    private String articleList;
+
+
+    @Data
+    public static class PK implements Serializable {
+        private String ghId;
+        // YYYY-mm-dd
+        private String date;
+
+        public PK() {
+        }
+
+
+    }
+}

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

@@ -0,0 +1,13 @@
+package com.tzld.longarticle.recommend.server.repository;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface ArticlePreDistributeAccountRepository extends JpaRepository<ArticlePreDistributeAccount,
+        ArticlePreDistributeAccount.PK> {
+
+    @Query("SELECT t FROM ArticlePreDistributeAccount t WHERE t.ghId = :ghId ORDER BY t.date DESC")
+    ArticlePreDistributeAccount findByGhIdOrderByDate(String ghId);
+}

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

@@ -12,4 +12,5 @@ import java.util.List;
 public class FilterParam {
     private String accountName;
     private List<Content> contents;
+    private String accountId;
 }

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

@@ -0,0 +1,57 @@
+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.repository.ArticlePreDistributeAccount;
+import com.tzld.longarticle.recommend.server.repository.ArticlePreDistributeAccountRepository;
+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;
+import java.util.Set;
+
+/**
+ * @author dyp
+ */
+@Component
+@Slf4j
+public class AccountPreDistributeStrategy implements FilterStrategy {
+    @Autowired
+    private AccountContentPoolConfigService accountContentPoolConfigService;
+    @Autowired
+    private ArticlePreDistributeAccountRepository articlePreDistributeAccountRepository;
+
+    @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());
+        if (pools.length >= 3) {
+            ArticlePreDistributeAccount articlePreDistributeAccount =
+                    articlePreDistributeAccountRepository.findByGhIdOrderByDate(param.getAccountId());
+            if (StringUtils.isNotEmpty(articlePreDistributeAccount.getArticleList())) {
+                Set<String> articles = Sets.newHashSet(StringUtils.split(","));
+                for (Content content : contents) {
+                    if (StringUtils.equals(pools[2], content.getContentPoolType())) {
+                        if (articles.contains(content.getId())) {
+                            result.add(content.getId());
+                        }
+                    }
+                }
+            }
+        }
+        filterResult.setContentIds(result);
+        filterResult.setFilterContent(filterContents);
+        return filterResult;
+    }
+
+}

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

@@ -0,0 +1,47 @@
+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;
+    }
+
+}

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

@@ -1,26 +0,0 @@
-package com.tzld.longarticle.recommend.server.service.filter.strategy;
-
-import com.tzld.longarticle.recommend.server.model.Content;
-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 com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Component;
-
-/**
- * @author dyp
- */
-@Component
-@Slf4j
-public class CategoryStrategy implements FilterStrategy {
-
-    @Override
-    public FilterResult filter(FilterParam param) {
-        FilterResult filterResult = new FilterResult();
-        filterResult.setContentIds(CommonCollectionUtils.toList(param.getContents(), Content::getId));
-        return filterResult;
-    }
-
-}

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

@@ -1,25 +0,0 @@
-package com.tzld.longarticle.recommend.server.service.filter.strategy;
-
-import com.tzld.longarticle.recommend.server.model.Content;
-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 com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Component;
-
-/**
- * @author dyp
- */
-@Component
-@Slf4j
-public class DuplicateStrategy implements FilterStrategy {
-
-    @Override
-    public FilterResult filter(FilterParam param) {
-        FilterResult filterResult = new FilterResult();
-        filterResult.setContentIds(CommonCollectionUtils.toList(param.getContents(), Content::getId));
-        return filterResult;
-    }
-
-}

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

@@ -4,6 +4,7 @@ package com.tzld.longarticle.recommend.server.service.rank;
 import com.tzld.longarticle.recommend.server.service.ServiceBeanFactory;
 import com.tzld.longarticle.recommend.server.service.rank.strategy.DefaultRankStrategy;
 import com.tzld.longarticle.recommend.server.service.rank.strategy.RankV2Strategy;
+import com.tzld.longarticle.recommend.server.service.rank.strategy.RankV3Strategy;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
@@ -23,6 +24,8 @@ public class RankService {
         switch (param.getStrategy()) {
             case "ArticleRankV2":
                 return ServiceBeanFactory.getBean(RankV2Strategy.class);
+            case "ArticleRankV3":
+                return ServiceBeanFactory.getBean(RankV3Strategy.class);
             default:
                 return ServiceBeanFactory.getBean(DefaultRankStrategy.class);
         }

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

@@ -0,0 +1,83 @@
+package com.tzld.longarticle.recommend.server.service.rank.strategy;
+
+
+import com.tzld.longarticle.recommend.server.model.Content;
+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.SimilarityStrategy;
+import com.tzld.longarticle.recommend.server.service.score.strategy.ViewCountStrategy;
+import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
+import com.tzld.longarticle.recommend.server.util.JSONUtils;
+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.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author dyp
+ */
+@Service
+@Slf4j
+public class RankV3Strategy implements RankStrategy {
+
+    @Autowired
+    private ScoreService scoreService;
+
+    public RankResult rank(RankParam param) {
+
+        log.info("RankParam {}", JSONUtils.toJson(param));
+        ScoreResult scoreResult = scoreService.score(convertToScoreParam(param));
+        log.info("ScoreResult {}", JSONUtils.toJson(scoreResult));
+
+        Map<String, Map<String, Double>> scoreMap = scoreResult.getScoreMap();
+
+        List<RankItem> items = CommonCollectionUtils.toList(param.getContents(), c -> {
+            RankItem item = new RankItem();
+            item.setContent(c);
+            item.setScoreMap(scoreMap.get(c.getId()));
+            return item;
+
+        });
+
+        // 1 排序
+        Collections.sort(items, (o1, o2) -> {
+            int result = -Double.compare(
+                    o1.getScore(SimilarityStrategy.class.getSimpleName()),
+                    o2.getScore(SimilarityStrategy.class.getSimpleName()));
+            if (result != 0) {
+                return result;
+            }
+
+            return -Double.compare(
+                    o1.getScore(ViewCountStrategy.class.getSimpleName()),
+                    o2.getScore(ViewCountStrategy.class.getSimpleName()));
+        });
+        // 2 选文章
+        List<Content> result = new ArrayList<>();
+        if (CollectionUtils.isNotEmpty(items)) {
+            for (int i = 0; i < Math.min(items.size(), param.getSize()); i++) {
+                result.add(items.get(i).getContent());
+            }
+        }
+
+        return new RankResult(result);
+    }
+
+    private ScoreParam convertToScoreParam(RankParam param) {
+        ScoreParam scoreParam = new ScoreParam();
+        scoreParam.setAccountName(param.getAccountName());
+        scoreParam.setContents(param.getContents());
+        return scoreParam;
+    }
+
+}

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

@@ -13,6 +13,7 @@ public class FilterParamFactory {
         FilterParam filterParam = new FilterParam();
         filterParam.setAccountName(param.getAccountName());
         filterParam.setContents(contents);
+        filterParam.setAccountId(param.getAccountId());
         return filterParam;
     }
 }

+ 4 - 0
long-article-recommend-service/src/main/resources/application-dev.yml

@@ -24,6 +24,10 @@ spring:
       maximum-pool-size: 10
       auto-commit: true
       idle-timeout: 30000
+  jpa:
+    hibernate:
+      ddl-auto: validate
+    database: mysql
 
 apollo:
   meta: http://devapolloconfig-internal.piaoquantv.com