ソースを参照

阅读均值表查询修改

wangyunpeng 10 ヶ月 前
コミット
785b9f13b2

+ 5 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/crawler/AccountAvgInfoRepository.java

@@ -10,5 +10,9 @@ import java.util.Set;
 @Repository
 public interface AccountAvgInfoRepository extends JpaRepository<AccountAvgInfo, AccountAvgInfo.PK> {
 
-    List<AccountAvgInfo> getAllByGhIdIn(Set<String> ghIds);
+    List<AccountAvgInfo> getAllByGhIdInAndStatusEquals(Set<String> ghIds, Integer status);
+
+    List<AccountAvgInfo> getAllByGhIdEqualsAndStatusEquals(String ghId, Integer status);
+
+    List<AccountAvgInfo> getAllByStatusEquals(Integer status);
 }

+ 3 - 1
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/repository/entity/crawler/AccountAvgInfo.java

@@ -11,7 +11,7 @@ import java.io.Serializable;
 @AllArgsConstructor
 @NoArgsConstructor
 @Entity
-@Table(name = "account_avg_info")
+@Table(name = "account_avg_info_v2")
 @IdClass(AccountAvgInfo.PK.class)
 public class AccountAvgInfo implements Serializable {
 
@@ -29,6 +29,8 @@ public class AccountAvgInfo implements Serializable {
     private Double readAvg;
     @Column(name = "like_avg")
     private Double likeAvg;
+    @Column(name = "status")
+    private Integer status;
 
     @Data
     public static class PK implements Serializable {

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

@@ -2,17 +2,28 @@ package com.tzld.longarticle.recommend.server.service;
 
 import cn.hutool.core.io.resource.ResourceUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.longarticle.recommend.server.repository.crawler.AccountAvgInfoRepository;
+import com.tzld.longarticle.recommend.server.repository.entity.crawler.AccountAvgInfo;
 import com.tzld.longarticle.recommend.server.service.score.AvgReadDTO;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
+import java.util.List;
+import java.util.Map;
 import java.util.Objects;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 @Component
 @Slf4j
 public class AccountIndexAvgViewCountService {
 
+    @Autowired
+    AccountAvgInfoRepository accountAvgInfoRepository;
+
     private JSONObject accountInfo;
 
     @PostConstruct
@@ -37,4 +48,20 @@ public class AccountIndexAvgViewCountService {
         return dto == null ? 1.0 : dto.getReadAvg();
     }
 
+    public double getAvgReadCountByDB(String ghId, Integer index) {
+        List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdEqualsAndStatusEquals(ghId, 1);
+        if (CollectionUtils.isEmpty(accountAvgInfoList)) {
+            return 1.0;
+        }
+        Map<String, AccountAvgInfo> accountAvgInfoMap = accountAvgInfoList.stream().collect(Collectors.toMap(AccountAvgInfo::getPosition, Function.identity()));
+        AccountAvgInfo info = accountAvgInfoMap.get(index + "");
+        if (Objects.isNull(info)) {
+            info =  accountAvgInfoMap.get("4");
+            if (Objects.isNull(info)) {
+                info =  accountAvgInfoMap.get("1");
+            }
+        }
+        return info == null ? 1.0 : info.getReadAvg();
+    }
+
 }

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

@@ -251,7 +251,7 @@ public class RecommendService {
             sortLog.setCrawlerChannelContentId(content.getCrawlerChannelContentId());
             sortLog.setTitle(content.getTitle());
             sortLog.setIndex(i);
-            sortLog.setIndexAvgCount(accountIndexAvgViewCountService.getAvgReadCount(param.getGhId(), i));
+            sortLog.setIndexAvgCount(accountIndexAvgViewCountService.getAvgReadCountByDB(param.getGhId(), i));
             sortLog.setCategory(JSONObject.toJSONString(content.getCategory()));
             sortLog.setStrategy(param.getStrategy());
             sortLog.setCreateTimestamp(System.currentTimeMillis());

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

@@ -205,7 +205,7 @@ public class RecallService implements ApplicationContextAware {
         Map<String, Map<String, Article>> firstIndexHisArticleMap = firstIndexHisArticleList.stream().collect(
                 Collectors.groupingBy(Article::getGhId, Collectors.toMap(Article::getAppMsgId, o -> o)));
         // 获取发布账号 位置历史均值
-        List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdIn(ghIds);
+        List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdInAndStatusEquals(ghIds, 1);
         Map<String, Map<String, AccountAvgInfo>> accountAvgInfoIndexMap = accountAvgInfoList.stream().collect(
                 Collectors.groupingBy(AccountAvgInfo::getGhId, Collectors.toMap(AccountAvgInfo::getPosition, o -> o)));
         Map<String, AccountAvgInfo> firstIndexAvgInfoMap = accountAvgInfoList.stream().filter(o -> "1".equals(o.getPosition()))

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

@@ -6,7 +6,7 @@ import lombok.Data;
 public class AvgReadDTO {
 
     private String accountName;
-    private String ghID;
+    private String ghId;
     private long fans;
     private String position;
     private double readAvg;

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

@@ -32,8 +32,8 @@ public class ViewCountRateStrategy implements ScoreStrategy {
                 if (!contentPools[i].equals(content.getContentPoolType())) {
                     continue;
                 }
-                double avgViewCountPos = accountIndexAvgViewCountService.getAvgReadCount(param.getGhId(), i + 1);
-                double avgViewCountFirst = accountIndexAvgViewCountService.getAvgReadCount(param.getGhId(), 1);
+                double avgViewCountPos = accountIndexAvgViewCountService.getAvgReadCountByDB(param.getGhId(), i + 1);
+                double avgViewCountFirst = accountIndexAvgViewCountService.getAvgReadCountByDB(param.getGhId(), 1);
                 double showViewCountSum = 0D;
                 double avgViewCountSum = 0D;
                 double showViewCountSumFirst = 0D;

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

@@ -48,7 +48,7 @@ public class ViewMultiplierStrategy implements ScoreStrategy {
             ChangwenArticleDatastat datastat = changwenArticleDatastatDTOMap.get(c.getCrawlerChannelContentId());
 
             if (Objects.nonNull(article) && Objects.nonNull(datastat)) {
-                double avgReadCount = accountIndexAvgViewCountService.getAvgReadCount(article.getAccountId(), article.getItemIndex());
+                double avgReadCount = accountIndexAvgViewCountService.getAvgReadCountByDB(article.getAccountId(), article.getItemIndex());
                 Integer readCount = datastat.getReadCount();
                 score.setScore(NormalizationUtils.min(Math.max(readCount, 0) / avgReadCount - 1));
             } else {

+ 4 - 4
long-article-recommend-service/src/test/java/com/tzld/longarticle/recommend/server/RecommendTest.java

@@ -56,7 +56,7 @@ public class RecommendTest {
         Map<String, List<ArticleDetailInfo>> articleDetailInfoMap = articleDetailInfoList.stream()
                 .collect(Collectors.groupingBy(ArticleDetailInfo::getWxSn));
 
-        List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdIn(ghIds);
+        List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdInAndStatusEquals(ghIds, 1);
         Map<String, Map<String, AccountAvgInfo>> accountAvgInfoIndexMap = accountAvgInfoList.stream().collect(
                 Collectors.groupingBy(AccountAvgInfo::getGhId, Collectors.toMap(AccountAvgInfo::getPosition, o -> o)));
         JSONArray jsonArray = new JSONArray();
@@ -66,13 +66,13 @@ public class RecommendTest {
                 continue;
             }
             Date minDate = articleDetailInfos.stream().map(ArticleDetailInfo::getRecallDt).min(Date::compareTo).orElse(new Date());
-            int firstLevel = 0;
+            int sumfirstLevel = 0;
             int sumFission0 = 0;
             int sumFission1 = 0;
             int sumFission2 = 0;
             for (ArticleDetailInfo articleDetailInfo : articleDetailInfos) {
                 if (articleDetailInfo.getRecallDt().equals(minDate)) {
-                    firstLevel += Optional.ofNullable(articleDetailInfo.getFirstLevel()).orElse(0);
+                    sumfirstLevel += Optional.ofNullable(articleDetailInfo.getFirstLevel()).orElse(0);
                     sumFission0 += Optional.ofNullable(articleDetailInfo.getFission0()).orElse(0);
                     sumFission1 += Optional.ofNullable(articleDetailInfo.getFission1()).orElse(0);
                     sumFission2 += Optional.ofNullable(articleDetailInfo.getFission2()).orElse(0);
@@ -94,7 +94,7 @@ public class RecommendTest {
                 obj.put("avgViewCount", avgInfo.getReadAvg());
                 obj.put("viewCountRate", (article.getShowViewCount() * 1.0) / avgInfo.getReadAvg());
             }
-            obj.put("firstLevel", sumFission0);
+            obj.put("firstLevel", sumfirstLevel);
             obj.put("fission0", sumFission0);
             obj.put("fission1", sumFission1);
             obj.put("fission2", sumFission2);