|
|
@@ -1,17 +1,18 @@
|
|
|
package com.tzld.piaoquan.recommend.server.service.rerank.strategy;
|
|
|
|
|
|
-import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
|
|
|
import com.tzld.piaoquan.recommend.server.model.Video;
|
|
|
import com.tzld.piaoquan.recommend.server.service.rerank.RerankParam;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
-import lombok.Data;
|
|
|
-import lombok.NoArgsConstructor;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.rerank.RerankStrategy;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.rerank.config.RerankConfigManager;
|
|
|
+import com.tzld.piaoquan.recommend.server.service.rerank.config.model.VideoAttrWeightConfigItem;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -19,22 +20,25 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
-public class VideoAttrWeightRerankStrategy extends BasicRerankStrategy {
|
|
|
-
|
|
|
- @ApolloJsonValue("${video.attr.weight.rerank.config:[]}")
|
|
|
- private List<VideoAttributeConfigItem> configItems;
|
|
|
+public class VideoAttrWeightRerankStrategy implements RerankStrategy {
|
|
|
|
|
|
@Override
|
|
|
public List<Video> rerank(RerankParam param) {
|
|
|
|
|
|
List<Video> rovVideos = param.getRovVideos();
|
|
|
- if (CollectionUtils.isEmpty(rovVideos) || CollectionUtils.isEmpty(configItems)) {
|
|
|
+
|
|
|
+ RerankConfigManager configManager = param.getRerankConfigManager();
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(rovVideos) || Objects.isNull(configManager) || CollectionUtils.isEmpty(configManager.getVideoAttrWeightConfigItems())) {
|
|
|
return rovVideos;
|
|
|
}
|
|
|
|
|
|
+ List<VideoAttrWeightConfigItem> configItems = configManager.getVideoAttrWeightConfigItems();
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+
|
|
|
// 重新计算分数,并重新排序
|
|
|
for (Video video : rovVideos) {
|
|
|
- this.reCalcVideoScore(video);
|
|
|
+ this.reCalcVideoScore(video, configItems, now);
|
|
|
}
|
|
|
|
|
|
rovVideos.sort(Comparator.comparingDouble(o -> -o.getSortScore()));
|
|
|
@@ -42,20 +46,21 @@ public class VideoAttrWeightRerankStrategy extends BasicRerankStrategy {
|
|
|
return rovVideos;
|
|
|
}
|
|
|
|
|
|
- private void reCalcVideoScore(Video video) {
|
|
|
+ private void reCalcVideoScore(Video video, List<VideoAttrWeightConfigItem> configItems, LocalDateTime now) {
|
|
|
double score = video.getScore();
|
|
|
video.getScoresMap().put("rerankBeforeScore", score);
|
|
|
|
|
|
Map<String, String> basicInfo = video.getMetaFeatureMap().getOrDefault("alg_vid_feature_basic_info", new HashMap<>());
|
|
|
|
|
|
- for (VideoAttributeConfigItem configItem : configItems) {
|
|
|
|
|
|
- String time = configItem.getTime();
|
|
|
+ for (VideoAttrWeightConfigItem configItem : configItems) {
|
|
|
+
|
|
|
+ Function<LocalDateTime, Boolean> timeFunc = configItem.getTimeFunc();
|
|
|
String key = configItem.getKey();
|
|
|
String value = configItem.getValue();
|
|
|
|
|
|
// 当前时间不需要加权,直接跳过
|
|
|
- if (!this.isInTimeRangeCondition(time)) {
|
|
|
+ if (Objects.nonNull(timeFunc) && !timeFunc.apply(now)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
@@ -75,7 +80,7 @@ public class VideoAttrWeightRerankStrategy extends BasicRerankStrategy {
|
|
|
double weight = configItem.getWeight();
|
|
|
|
|
|
score *= weight;
|
|
|
- video.getScoresMap().put(String.format("%s_%s_%s", key, value, time), weight);
|
|
|
+ video.getScoresMap().put(String.format("%s_%s_%s", key, value, configItem.getTime()), weight);
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -84,13 +89,4 @@ public class VideoAttrWeightRerankStrategy extends BasicRerankStrategy {
|
|
|
video.setSortScore(score);
|
|
|
}
|
|
|
|
|
|
- @Data
|
|
|
- @NoArgsConstructor
|
|
|
- @AllArgsConstructor
|
|
|
- private static class VideoAttributeConfigItem {
|
|
|
- private String key;
|
|
|
- private String value;
|
|
|
- private String time;
|
|
|
- private Double weight;
|
|
|
- }
|
|
|
}
|