|
@@ -0,0 +1,162 @@
|
|
|
|
|
+package com.tzld.longarticle.recommend.server.service.recommend;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.google.common.collect.Lists;
|
|
|
|
|
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
|
|
|
|
+import com.tzld.longarticle.recommend.server.common.CommonThreadPoolExecutor;
|
|
|
|
|
+import com.tzld.longarticle.recommend.server.common.enums.recommend.ContentPoolEnum;
|
|
|
|
|
+import com.tzld.longarticle.recommend.server.mapper.longArticle.RankContentScoreMapper;
|
|
|
|
|
+import com.tzld.longarticle.recommend.server.model.dto.Content;
|
|
|
|
|
+import com.tzld.longarticle.recommend.server.model.entity.longArticle.RankContentScore;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
|
|
+import org.apache.commons.collections4.MapUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
|
+import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 排序内容分数记录服务
|
|
|
|
|
+ * 异步保存rankStrategy计算后的content分数
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class RankContentScoreService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private RankContentScoreMapper rankContentScoreMapper;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量插入批次大小
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final int BATCH_SIZE = 500;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 异步执行线程池
|
|
|
|
|
+ */
|
|
|
|
|
+ private final ExecutorService pool = new CommonThreadPoolExecutor(
|
|
|
|
|
+ 4,
|
|
|
|
|
+ 8,
|
|
|
|
|
+ 0L, TimeUnit.SECONDS,
|
|
|
|
|
+ new LinkedBlockingQueue<>(1000),
|
|
|
|
|
+ new ThreadFactoryBuilder().setNameFormat("RankContentScore-%d").build(),
|
|
|
|
|
+ new ThreadPoolExecutor.AbortPolicy());
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 异步保存排序内容分数
|
|
|
|
|
+ * 每个计划账号只存在一批次,先删除历史再插入新的
|
|
|
|
|
+ * 只保留头次内容,冷启池内容不需要保存
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param planId 计划ID
|
|
|
|
|
+ * @param ghId 公众号ID
|
|
|
|
|
+ * @param accountName 账号名称
|
|
|
|
|
+ * @param strategy 排序策略
|
|
|
|
|
+ * @param contents 内容列表
|
|
|
|
|
+ */
|
|
|
|
|
+ public void asyncSaveRankContentScore(String planId, String ghId, String accountName, String strategy, List<Content> contents) {
|
|
|
|
|
+ if (CollectionUtils.isEmpty(contents)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ pool.submit(() -> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ saveRankContentScore(planId, ghId, accountName, strategy, contents);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("asyncSaveRankContentScore error, planId:{}, ghId:{}, strategy:{}", planId, ghId, strategy, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 保存排序内容分数
|
|
|
|
|
+ * 先删除历史记录,再插入新记录
|
|
|
|
|
+ * 只保留头次内容(autoArticlePoolLevel1和autoArticlePoolLevel2),冷启池内容不保存
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param planId 计划ID
|
|
|
|
|
+ * @param ghId 公众号ID
|
|
|
|
|
+ * @param accountName 账号名称
|
|
|
|
|
+ * @param strategy 排序策略
|
|
|
|
|
+ * @param contents 内容列表
|
|
|
|
|
+ */
|
|
|
|
|
+ public void saveRankContentScore(String planId, String ghId, String accountName, String strategy, List<Content> contents) {
|
|
|
|
|
+ if (CollectionUtils.isEmpty(contents)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 过滤出头次内容(非冷启池内容)
|
|
|
|
|
+ List<Content> filteredContents = filterHeadContents(contents);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(filteredContents)) {
|
|
|
|
|
+ log.info("saveRankContentScore no head contents to save, planId:{}, ghId:{}, strategy:{}", planId, ghId, strategy);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
|
+
|
|
|
|
|
+ // 先删除该planId和ghId的历史记录
|
|
|
|
|
+ rankContentScoreMapper.deleteByPlanIdAndGhId(planId, ghId);
|
|
|
|
|
+
|
|
|
|
|
+ // 构建保存列表
|
|
|
|
|
+ List<RankContentScore> saveList = new ArrayList<>();
|
|
|
|
|
+ long createTimestamp = System.currentTimeMillis();
|
|
|
|
|
+
|
|
|
|
|
+ for (Content content : filteredContents) {
|
|
|
|
|
+ RankContentScore score = new RankContentScore();
|
|
|
|
|
+ score.setPlanId(planId);
|
|
|
|
|
+ score.setGhId(ghId);
|
|
|
|
|
+ score.setAccountName(accountName);
|
|
|
|
|
+ score.setContentId(content.getId());
|
|
|
|
|
+ score.setCrawlerChannelContentId(content.getCrawlerChannelContentId());
|
|
|
|
|
+ score.setSourceType(content.getSourceType());
|
|
|
|
|
+ score.setSourceId(content.getSourceId());
|
|
|
|
|
+ score.setTitle(content.getTitle());
|
|
|
|
|
+ score.setContentPoolType(content.getContentPoolType());
|
|
|
|
|
+ score.setStrategy(strategy);
|
|
|
|
|
+ score.setScore(content.getScore());
|
|
|
|
|
+ if (MapUtils.isNotEmpty(content.getScoreMap())) {
|
|
|
|
|
+ score.setScoreMap(JSONObject.toJSONString(content.getScoreMap()));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (CollectionUtils.isNotEmpty(content.getCategory())) {
|
|
|
|
|
+ score.setCategory(JSONObject.toJSONString(content.getCategory()));
|
|
|
|
|
+ }
|
|
|
|
|
+ score.setCreateTimestamp(createTimestamp);
|
|
|
|
|
+ saveList.add(score);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 分批批量保存
|
|
|
|
|
+ List<List<RankContentScore>> partitions = Lists.partition(saveList, BATCH_SIZE);
|
|
|
|
|
+ for (List<RankContentScore> partition : partitions) {
|
|
|
|
|
+ rankContentScoreMapper.batchInsert(partition);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.info("saveRankContentScore success, planId:{}, ghId:{}, strategy:{}, count:{}, batchCount:{}, cost:{}ms",
|
|
|
|
|
+ planId, ghId, strategy, saveList.size(), partitions.size(), System.currentTimeMillis() - start);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 过滤出头次内容(非冷启池内容)
|
|
|
|
|
+ * 冷启池:autoArticlePoolLevel4
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param contents 内容列表
|
|
|
|
|
+ * @return 过滤后的头次内容列表
|
|
|
|
|
+ */
|
|
|
|
|
+ private List<Content> filterHeadContents(List<Content> contents) {
|
|
|
|
|
+ List<Content> result = new ArrayList<>();
|
|
|
|
|
+ String coldStartPool = ContentPoolEnum.autoArticlePoolLevel4.getContentPool();
|
|
|
|
|
+
|
|
|
|
|
+ for (Content content : contents) {
|
|
|
|
|
+ // 跳过冷启池内容
|
|
|
|
|
+ if (coldStartPool.equals(content.getContentPoolType())) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ result.add(content);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|