|
@@ -4,6 +4,10 @@ package com.tzld.longarticle.recommend.server.service.recommend.rank;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.ctrip.framework.apollo.model.ConfigChangeEvent;
|
|
|
+import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
|
|
|
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
|
|
|
+import com.tzld.longarticle.recommend.server.common.enums.aigc.PublishPlanInputSourceTypesEnum;
|
|
|
import com.tzld.longarticle.recommend.server.common.enums.recommend.RankStrategyEnum;
|
|
|
import com.tzld.longarticle.recommend.server.model.dto.Content;
|
|
|
import com.tzld.longarticle.recommend.server.service.ServiceBeanFactory;
|
|
@@ -12,11 +16,13 @@ import com.tzld.longarticle.recommend.server.service.recommend.score.AccountInde
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.RandomUtils;
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.tzld.longarticle.recommend.server.common.constant.SceneConstants.FWH_COLD_START;
|
|
|
|
|
@@ -25,7 +31,38 @@ import static com.tzld.longarticle.recommend.server.common.constant.SceneConstan
|
|
|
*/
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
-public class RankService {
|
|
|
+public class RankService implements InitializingBean {
|
|
|
+
|
|
|
+ private static Map<String, Map<String, Integer>> staticStrategyPoolSourceTypeMap;
|
|
|
+
|
|
|
+ @ApolloJsonValue("${strategyPoolSourceTypeConfig:{}}")
|
|
|
+ private Map<String, Map<String, Integer>> strategyPoolSourceTypeMap;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
+ RankService.staticStrategyPoolSourceTypeMap = strategyPoolSourceTypeMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApolloConfigChangeListener(interestedKeys = {"strategyPoolSourceTypeConfig"})
|
|
|
+ public void configChange(ConfigChangeEvent changeEvent) {
|
|
|
+ RankService.staticStrategyPoolSourceTypeMap = JSONObject.parseObject(changeEvent.getChange(
|
|
|
+ "strategyPoolSourceTypeConfig").getNewValue(), Map.class);
|
|
|
+ log.info("strategyPoolSourceTypeConfig change updateStaticValue success newValue:{}",
|
|
|
+ JSONObject.toJSONString(staticStrategyPoolSourceTypeMap));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer getStrategyPoolSourceType(String strategy, Integer index) {
|
|
|
+ Integer sourceType = 0;
|
|
|
+ Map<String, Integer> indexSourceTypeMap = staticStrategyPoolSourceTypeMap.get(strategy);
|
|
|
+ if (Objects.nonNull(indexSourceTypeMap)) {
|
|
|
+ sourceType = indexSourceTypeMap.get(String.valueOf(index));
|
|
|
+ }
|
|
|
+ return sourceType;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map<String, Integer> getStrategyPoolSourceType(String strategy) {
|
|
|
+ return staticStrategyPoolSourceTypeMap.get(strategy);
|
|
|
+ }
|
|
|
|
|
|
public RankResult rank(RankParam param) {
|
|
|
RankStrategy strategy = getRankStrategy(param);
|
|
@@ -60,11 +97,14 @@ public class RankService {
|
|
|
log.info("{} 账号名称 {} {}评分结果 {}", strategy, accountName, position, JSONObject.toJSONString(jsonArray));
|
|
|
}
|
|
|
|
|
|
- public static void commonAddSecondContent(List<Content> result,
|
|
|
+ public static void commonAddSecondContent(RankParam param, List<Content> result,
|
|
|
String[] publishPool, String[] contentPools,
|
|
|
Map<String, List<Content>> contentMap,
|
|
|
Map<Integer, AccountIndexReplacePoolConfig> indexReplacePoolConfigMap) {
|
|
|
List<Content> pool = contentMap.get(contentPools[1]);
|
|
|
+ if (CollectionUtils.isNotEmpty(pool)) {
|
|
|
+ pool = contentSourceTypeFilter(param.getStrategy(), pool, 2);
|
|
|
+ }
|
|
|
if (CollectionUtils.isNotEmpty(pool)) {
|
|
|
int i = RandomUtils.nextInt(0, Math.min(pool.size(), 5));
|
|
|
int j = RandomUtils.nextInt(0, Math.min(pool.size(), 5));
|
|
@@ -82,20 +122,43 @@ public class RankService {
|
|
|
}
|
|
|
} else {
|
|
|
// 替补 根据设置替补内容池查找内容进行替补
|
|
|
- AccountIndexReplacePoolConfig replacePoolConfig = indexReplacePoolConfigMap.get(2);
|
|
|
- if (Objects.nonNull(replacePoolConfig)) {
|
|
|
- List<Content> poolReplace = contentMap.get(replacePoolConfig.getContentPool());
|
|
|
- if (CollectionUtils.isNotEmpty(poolReplace)) {
|
|
|
- publishPool[1] = replacePoolConfig.getContentPool();
|
|
|
- result.add(poolReplace.get(0));
|
|
|
- }
|
|
|
+ findReplacePoolContent(result, publishPool, contentMap, indexReplacePoolConfigMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void findReplacePoolContent(List<Content> result, String[] publishPool,
|
|
|
+ Map<String, List<Content>> contentMap,
|
|
|
+ Map<Integer, AccountIndexReplacePoolConfig> indexReplacePoolConfigMap) {
|
|
|
+ AccountIndexReplacePoolConfig replacePoolConfig = indexReplacePoolConfigMap.get(2);
|
|
|
+ if (Objects.nonNull(replacePoolConfig)) {
|
|
|
+ List<Content> poolReplace = contentMap.get(replacePoolConfig.getContentPool());
|
|
|
+ if (CollectionUtils.isNotEmpty(poolReplace)) {
|
|
|
+ publishPool[1] = replacePoolConfig.getContentPool();
|
|
|
+ result.add(poolReplace.get(0));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static List<Content> contentSourceTypeFilter(String strategy, List<Content> pool, Integer index) {
|
|
|
+ if (!staticStrategyPoolSourceTypeMap.containsKey(strategy)) {
|
|
|
+ return pool;
|
|
|
+ }
|
|
|
+ Integer sourceType = getStrategyPoolSourceType(strategy, index);
|
|
|
+ Integer videoSourceType = PublishPlanInputSourceTypesEnum.longArticleVideoPoolSource.getVal();
|
|
|
+ if (Objects.nonNull(sourceType) && sourceType.equals(videoSourceType)) {
|
|
|
+ pool = pool.stream().filter(o -> Objects.equals(o.getSourceType(), videoSourceType)).collect(Collectors.toList());
|
|
|
+ } else {
|
|
|
+ pool = pool.stream().filter(o -> !Objects.equals(o.getSourceType(), videoSourceType)).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return pool;
|
|
|
+ }
|
|
|
+
|
|
|
public static void commonAdd38Content(RankParam param, List<Content> result, String[] contentPools,
|
|
|
Map<String, List<Content>> contentMap, String strategy) {
|
|
|
List<Content> pool = contentMap.get(contentPools[2]);
|
|
|
+ if (CollectionUtils.isNotEmpty(pool)) {
|
|
|
+ pool = contentSourceTypeFilter(param.getStrategy(), pool, 3);
|
|
|
+ }
|
|
|
if (CollectionUtils.isNotEmpty(pool) && param.getSize() > result.size()) {
|
|
|
RankService.printSortLog(strategy, param.getAccountName(), "3-8", pool.subList(0, Math.min(pool.size(), 200)));
|
|
|
result.addAll(pool.subList(0, Math.min(pool.size(), param.getSize() - result.size())));
|