|
@@ -13,6 +13,10 @@ import com.tzld.piaoquan.recommend.server.service.rank.RankService;
|
|
|
import com.tzld.piaoquan.recommend.server.service.recall.RecallParam;
|
|
|
import com.tzld.piaoquan.recommend.server.service.recall.RecallResult;
|
|
|
import com.tzld.piaoquan.recommend.server.service.recall.RecallService;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.recall.strategy.Region24HRecallStrategy;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.recall.strategy.RegionHRecallStrategy;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.recall.strategy.RegionRelative24HDupRecallStrategy;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.recall.strategy.RegionRelative24HRecallStrategy;
|
|
|
import com.tzld.piaoquan.recommend.server.util.DateUtils;
|
|
|
import com.tzld.piaoquan.recommend.server.util.JSONUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -40,7 +44,7 @@ public class RecommendService {
|
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
|
|
|
- private Map<String, String> ab_initial_config_map = new HashMap<>();
|
|
|
+ private Map<Integer, String> ab_initial_config_map = new HashMap<>();
|
|
|
|
|
|
@ApolloJsonValue("ab_exp_code")
|
|
|
private Map<String, Map<String, String>> abExpCodeMap = new HashMap<>();
|
|
@@ -57,15 +61,17 @@ public class RecommendService {
|
|
|
private RecallService recallService;
|
|
|
@Autowired
|
|
|
private RankService rankService;
|
|
|
+ @Autowired
|
|
|
+ private PreViewedService preViewedService;
|
|
|
|
|
|
@PostConstruct
|
|
|
public void init() {
|
|
|
- ab_initial_config_map.put("VLOG", "095");
|
|
|
- ab_initial_config_map.put("LOVE_LIVE", "144");
|
|
|
- ab_initial_config_map.put("LONG_VIDEO", "121");
|
|
|
- ab_initial_config_map.put("LAO_HAO_KAN_VIDEO", "074");
|
|
|
- ab_initial_config_map.put("ZUI_JING_QI", "069");
|
|
|
- ab_initial_config_map.put("other", "095-1");
|
|
|
+ ab_initial_config_map.put(0, "095");
|
|
|
+ ab_initial_config_map.put(4, "144");
|
|
|
+ ab_initial_config_map.put(5, "121");
|
|
|
+ ab_initial_config_map.put(18, "074");
|
|
|
+ ab_initial_config_map.put(19, "069");
|
|
|
+ ab_initial_config_map.put(-1, "095-1");
|
|
|
}
|
|
|
|
|
|
public HomepageRecommendResponse homepageRecommend(HomepageRecommendRequest request) {
|
|
@@ -83,16 +89,74 @@ public class RecommendService {
|
|
|
|
|
|
|
|
|
List<Video> videos = videoRecommend(request, param);
|
|
|
- List<Video> result = new ArrayList<>();
|
|
|
- int i = 0;
|
|
|
- for (Video v : videos) {
|
|
|
- result.add(v.toBuilder().setPosition(i++).build());
|
|
|
+
|
|
|
+ updateCache(request, videos);
|
|
|
+
|
|
|
+ return HomepageRecommendResponse.newBuilder()
|
|
|
+ .setResult(Result.newBuilder().setCode(1).setMessage("success"))
|
|
|
+ .addAllVideo(videos)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateCache(HomepageRecommendRequest request, List<Video> videos) {
|
|
|
+ if (StringUtils.isNotBlank(request.getMid())
|
|
|
+ || CollectionUtils.isEmpty(videos)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1 更新预曝光数据 过期时间 30min
|
|
|
+ Long[] videoIds = new Long[videos.size()];
|
|
|
+ for (int i = 0; i < videoIds.length; i++) {
|
|
|
+ videoIds[i] = videos.get(i).getVideoId();
|
|
|
+ }
|
|
|
+
|
|
|
+ preViewedService.updateCache(request.getAppType(), request.getMid(), videoIds);
|
|
|
+ updateLastVideoCache(request, videos);
|
|
|
+
|
|
|
+ // 更新流量池:本地分发数量-1
|
|
|
+ // redis没有这个key 也没有flowPool这个push_form
|
|
|
+ // updateFlowPoolCache(request, videos);
|
|
|
+
|
|
|
+ // 更新视频限流逻辑:redis没有这个key,而且代码中无引用
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateLastVideoCache(HomepageRecommendRequest request, List<Video> videos) {
|
|
|
+ // 2 地域小时最后一个视频
|
|
|
+ for (int i = videos.size() - 1; i >= 0; i--) {
|
|
|
+ if (videos.get(i).equals(RegionHRecallStrategy.PUSH_FORM)) {
|
|
|
+ redisTemplate.opsForValue().set(String.format(RegionHRecallStrategy.LAST_VIDEO_KEY_FORMAT,
|
|
|
+ request.getAppType(), request.getMid()), String.valueOf(videos.get(i).getVideoId()),
|
|
|
+ 24, TimeUnit.HOURS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3 地域24小时最后一个视频
|
|
|
+ for (int i = videos.size() - 1; i >= 0; i--) {
|
|
|
+ if (videos.get(i).equals(Region24HRecallStrategy.PUSH_FORM)) {
|
|
|
+ redisTemplate.opsForValue().set(String.format(Region24HRecallStrategy.LAST_VIDEO_KEY_FORMAT,
|
|
|
+ request.getAppType(), request.getMid()), String.valueOf(videos.get(i).getVideoId()),
|
|
|
+ 24, TimeUnit.HOURS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4 地域相对24小时最后一个视频
|
|
|
+ for (int i = videos.size() - 1; i >= 0; i--) {
|
|
|
+ if (videos.get(i).equals(RegionRelative24HRecallStrategy.PUSH_FORM)) {
|
|
|
+ redisTemplate.opsForValue().set(String.format(RegionRelative24HRecallStrategy.LAST_VIDEO_KEY_FORMAT,
|
|
|
+ request.getAppType(), request.getMid()), String.valueOf(videos.get(i).getVideoId()),
|
|
|
+ 24, TimeUnit.HOURS);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- //updateCache()
|
|
|
- // update_redis_data(result=rank_result, app_type=app_type, mid=mid, top_K=top_K,
|
|
|
- // level_weight=level_weight, flow_pool_abtest_group=flow_pool_abtest_group)
|
|
|
- return null;
|
|
|
+ // 5 地域相对24小时最后一个视频
|
|
|
+ for (int i = videos.size() - 1; i >= 0; i--) {
|
|
|
+ if (videos.get(i).equals(RegionRelative24HDupRecallStrategy.PUSH_FORM)) {
|
|
|
+ redisTemplate.opsForValue().set(String.format(RegionRelative24HDupRecallStrategy.LAST_VIDEO_KEY_FORMAT,
|
|
|
+ request.getAppType(), request.getMid()), String.valueOf(videos.get(i).getVideoId()),
|
|
|
+ 24, TimeUnit.HOURS);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private List<Video> videoRecommend(HomepageRecommendRequest request, HomepageRecommendParam param) {
|
|
@@ -104,15 +168,28 @@ public class RecommendService {
|
|
|
rankParam.setRecallResult(recallResult);
|
|
|
RankResult result = rankService.rank(rankParam);
|
|
|
|
|
|
+ if (result == null || CollectionUtils.isEmpty(result.getVideos())) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
|
|
|
- return null;
|
|
|
- }
|
|
|
+ // 只返回size条数据
|
|
|
+ List<Video> videos = new ArrayList<>();
|
|
|
+ int size = request.getSize() < result.getVideos().size()
|
|
|
+ ? request.getSize()
|
|
|
+ : result.getVideos().size();
|
|
|
+
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ videos.add(Video.newBuilder()
|
|
|
+ .setPosition(i + 1)
|
|
|
+ .setPushFrom(videos.get(i).getPushFrom())
|
|
|
+ .setAbCode(videos.get(i).getAbCode())
|
|
|
+ .setVideoId(videos.get(i).getVideoId())
|
|
|
+ .setRovScore(videos.get(i).getRovScore())
|
|
|
+ .setSortScore(videos.get(i).getRovScore())
|
|
|
+ .build());
|
|
|
+ }
|
|
|
|
|
|
- private List<Video> videoRecommendOld(HomepageRecommendRequest request, HomepageRecommendParam param) {
|
|
|
- // TODO
|
|
|
- RecallParam recallParam = new RecallParam();
|
|
|
- recallService.recall(recallParam);
|
|
|
- return null;
|
|
|
+ return videos;
|
|
|
}
|
|
|
|
|
|
private HomepageRecommendParam genHomepageRecommendParam(HomepageRecommendRequest request, int recommendType) {
|
|
@@ -125,7 +202,7 @@ public class RecommendService {
|
|
|
|
|
|
String abInitialConfig = ab_initial_config_map.containsKey(request.getAppType())
|
|
|
? ab_initial_config_map.get(request.getAppType())
|
|
|
- : ab_initial_config_map.get("other");
|
|
|
+ : ab_initial_config_map.get(-1);
|
|
|
|
|
|
Map<String, String> abExpCode = abExpCodeMap.get(abInitialConfig);
|
|
|
param.setAb_code(abExpCode.get("ab_code"));
|