|
@@ -1,25 +1,32 @@
|
|
|
package com.tzld.longarticle.recommend.server.service.recall;
|
|
|
|
|
|
import com.tzld.longarticle.recommend.server.common.ThreadPoolFactory;
|
|
|
+import com.tzld.longarticle.recommend.server.model.Content;
|
|
|
+import com.tzld.longarticle.recommend.server.model.ContentHisPublishArticle;
|
|
|
+import com.tzld.longarticle.recommend.server.repository.aigc.CrawlerMetaArticleRepository;
|
|
|
+import com.tzld.longarticle.recommend.server.repository.crawler.ArticleRepository;
|
|
|
+import com.tzld.longarticle.recommend.server.repository.entity.aigc.CrawlerMetaArticle;
|
|
|
+import com.tzld.longarticle.recommend.server.repository.entity.crawler.Article;
|
|
|
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;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import java.util.concurrent.Future;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author dyp
|
|
@@ -28,6 +35,11 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Slf4j
|
|
|
public class RecallService implements ApplicationContextAware {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ ArticleRepository articleRepository;
|
|
|
+ @Autowired
|
|
|
+ CrawlerMetaArticleRepository crawlerMetaArticleRepository;
|
|
|
+
|
|
|
private final Map<String, RecallStrategy> strategyMap = new HashMap<>();
|
|
|
private ApplicationContext applicationContext;
|
|
|
private final ExecutorService pool = ThreadPoolFactory.recallPool();
|
|
@@ -90,4 +102,58 @@ public class RecallService implements ApplicationContextAware {
|
|
|
this.applicationContext = applicationContext;
|
|
|
}
|
|
|
|
|
|
+ public void setContentCategory(List<Content> contentList) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ List<String> channelContentIds = contentList.stream().map(Content::getCrawlerChannelContentId).collect(Collectors.toList());
|
|
|
+ List<CrawlerMetaArticle> categoryList = getContentCategoryByChannelContentId(channelContentIds);
|
|
|
+ if (CollectionUtils.isEmpty(categoryList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Map<String, List<String>> categoryMap = categoryList.stream().collect(Collectors.groupingBy(CrawlerMetaArticle::getChannelContentId,
|
|
|
+ Collectors.mapping(CrawlerMetaArticle::getCategory, Collectors.toList())));
|
|
|
+ for (Content content : contentList) {
|
|
|
+ content.setCategory(categoryMap.get(content.getCrawlerChannelContentId()));
|
|
|
+ }
|
|
|
+ log.info("setContentCategory cost:{}", System.currentTimeMillis() - start);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<CrawlerMetaArticle> getContentCategoryByChannelContentId(List<String> channelContentIds) {
|
|
|
+ if (CollectionUtils.isEmpty(channelContentIds)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ return crawlerMetaArticleRepository.getByChannelContentIdIn(channelContentIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setTitleAvgViewCount(List<Content> contentList) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ List<String> titleList = contentList.stream().map(Content::getTitle).collect(Collectors.toList());
|
|
|
+ List<Article> hisArticleList = articleRepository.getByTitleIn(titleList);
|
|
|
+ Map<String, Map<Integer, List<Article>>> map = hisArticleList.stream()
|
|
|
+ .collect(Collectors.groupingBy(Article::getTitle, Collectors.groupingBy(Article::getItemIndex)));
|
|
|
+ for (Content content : contentList) {
|
|
|
+ List<Article> hisArticles = new ArrayList<>();
|
|
|
+ Map<Integer, Integer> indexAvgMap = new HashMap<>();
|
|
|
+ Map<Integer, List<Article>> indexArticleMap = map.get(content.getTitle());
|
|
|
+ if (Objects.nonNull(indexArticleMap) && !indexArticleMap.isEmpty()) {
|
|
|
+ for (Map.Entry<Integer, List<Article>> indexArticleEntry : indexArticleMap.entrySet()) {
|
|
|
+ List<Article> indexArticleList = indexArticleEntry.getValue();
|
|
|
+ int sumViewCount = indexArticleList.stream().mapToInt(Article::getShowViewCount).sum();
|
|
|
+ indexAvgMap.put(indexArticleEntry.getKey(), sumViewCount/ indexArticleList.size());
|
|
|
+ hisArticles.addAll(indexArticleList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ content.setHisPublishArticleList(new ArrayList<>());
|
|
|
+ for (Article hisArticle : hisArticles) {
|
|
|
+ ContentHisPublishArticle article = new ContentHisPublishArticle();
|
|
|
+ BeanUtils.copyProperties(hisArticle, article);
|
|
|
+ article.setAvgViewCount(indexAvgMap.get(article.getItemIndex()));
|
|
|
+ if (Objects.nonNull(article.getAvgViewCount()) && article.getAvgViewCount() > 0) {
|
|
|
+ article.setViewCountRate((article.getShowViewCount() * 1.0) / article.getAvgViewCount());
|
|
|
+ }
|
|
|
+ content.getHisPublishArticleList().add(article);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("setTitleAvgViewCount cost:{}", System.currentTimeMillis() - start);
|
|
|
+ }
|
|
|
+
|
|
|
}
|