|  | @@ -1,34 +1,23 @@
 | 
	
		
			
				|  |  |  package com.tzld.piaoquan.recommend.server.service.rank.strategy;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  |  import com.google.common.reflect.TypeToken;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.feature.domain.video.base.ItemFeature;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.feature.domain.video.base.RequestContext;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.feature.domain.video.base.UserFeature;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.common.base.RankItem;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.server.common.enums.AppTypeEnum;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.server.model.MachineInfo;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.model.Video;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.server.remote.FeatureRemoteService;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.server.service.flowpool.FlowPoolConstants;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.server.service.rank.RankResult;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.service.rank.RankService;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.server.service.rank.extractor.RankExtractorFeature;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorDensity;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.server.service.recall.RecallResult;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.service.recall.strategy.*;
 | 
	
		
			
				|  |  | -import com.tzld.piaoquan.recommend.server.service.score.ScoreParam;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.service.score.ScorerUtils;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.util.CommonCollectionUtils;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.util.JSONUtils;
 | 
	
		
			
				|  |  |  import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  |  import org.apache.commons.collections4.CollectionUtils;
 | 
	
		
			
				|  |  | -import org.apache.commons.lang3.RandomUtils;
 | 
	
		
			
				|  |  |  import org.apache.commons.lang3.math.NumberUtils;
 | 
	
		
			
				|  |  | -import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | +import org.springframework.data.redis.connection.RedisConnectionFactory;
 | 
	
		
			
				|  |  | +import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
 | 
	
		
			
				|  |  | +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
 | 
	
		
			
				|  |  | +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
 | 
	
		
			
				|  |  |  import org.springframework.data.redis.core.RedisTemplate;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -43,6 +32,8 @@ import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  @Slf4j
 | 
	
		
			
				|  |  |  public class RankStrategy4RankModel extends RankService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Value("${video.model.weight:}")
 | 
	
		
			
				|  |  | +    private Double mergeWeight;
 | 
	
		
			
				|  |  |      final private String CLASS_NAME = this.getClass().getSimpleName();
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public List<Video> mergeAndRankRovRecall(RankParam param) {
 | 
	
	
		
			
				|  | @@ -83,11 +74,14 @@ public class RankStrategy4RankModel extends RankService {
 | 
	
		
			
				|  |  |              Collections.sort(rovRecallRank, Comparator.comparingDouble(o -> -o.getSortScore()));
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        // todo zhangbo 增加排序str模型逻辑
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        return rovRecallRank;
 | 
	
		
			
				|  |  | +        //------------------- todo zhangbo 增加排序str模型逻辑 合并二者得分-------------------
 | 
	
		
			
				|  |  | +        List<Video> videosWithModel = model(rovRecallRank, param);
 | 
	
		
			
				|  |  | +        for (Video v : videosWithModel){
 | 
	
		
			
				|  |  | +            double mergeWeightIn = this.mergeWeight == null? 0.0D: this.mergeWeight;
 | 
	
		
			
				|  |  | +            v.setSortScore(v.getSortScore() + mergeWeightIn * v.getModelScore());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        Collections.sort(videosWithModel, Comparator.comparingDouble(o -> -o.getSortScore()));
 | 
	
		
			
				|  |  | +        return videosWithModel;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      public List<Video> model(List<Video> videos, RankParam param){
 | 
	
	
		
			
				|  | @@ -95,10 +89,20 @@ public class RankStrategy4RankModel extends RankService {
 | 
	
		
			
				|  |  |              return videos;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        RedisStandaloneConfiguration redisSC = new RedisStandaloneConfiguration();
 | 
	
		
			
				|  |  | +        redisSC.setPort(6379);
 | 
	
		
			
				|  |  | +        redisSC.setPassword("Wqsd@2019");
 | 
	
		
			
				|  |  | +        redisSC.setHostName("r-bp1pi8wyv6lzvgjy5z.redis.rds.aliyuncs.com");
 | 
	
		
			
				|  |  | +        RedisConnectionFactory connectionFactory = new JedisConnectionFactory(redisSC);
 | 
	
		
			
				|  |  | +        ((LettuceConnectionFactory) connectionFactory).setTimeout(1000);
 | 
	
		
			
				|  |  | +        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
 | 
	
		
			
				|  |  | +        redisTemplate.setConnectionFactory(connectionFactory);
 | 
	
		
			
				|  |  | +        redisTemplate.afterPropertiesSet();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          Map<String, String> userFeatureMap = new HashMap<>();
 | 
	
		
			
				|  |  |          if (param.getMid() != null && !param.getMid().isEmpty()){
 | 
	
		
			
				|  |  |              String midKey = "user_info_4video_" + param.getMid();
 | 
	
		
			
				|  |  | -            String userFeatureStr = this.redisTemplate.opsForValue().get(midKey);
 | 
	
		
			
				|  |  | +            String userFeatureStr = redisTemplate.opsForValue().get(midKey);
 | 
	
		
			
				|  |  |              if (userFeatureStr != null){
 | 
	
		
			
				|  |  |                  try{
 | 
	
		
			
				|  |  |                      userFeatureMap = JSONUtils.fromJson(userFeatureStr,
 | 
	
	
		
			
				|  | @@ -140,7 +144,7 @@ public class RankStrategy4RankModel extends RankService {
 | 
	
		
			
				|  |  |          List<Long> videoIds = CommonCollectionUtils.toListDistinct(videos, Video::getVideoId);
 | 
	
		
			
				|  |  |          List<String> videoFeatureKeys = videoIds.stream().map(r-> "video_info_" + r)
 | 
	
		
			
				|  |  |                  .collect(Collectors.toList());
 | 
	
		
			
				|  |  | -        List<String> videoFeatures = this.redisTemplate.opsForValue().multiGet(videoFeatureKeys);
 | 
	
		
			
				|  |  | +        List<String> videoFeatures = redisTemplate.opsForValue().multiGet(videoFeatureKeys);
 | 
	
		
			
				|  |  |          if (videoFeatures != null){
 | 
	
		
			
				|  |  |              for (int i=0; i<videoFeatures.size(); ++i){
 | 
	
		
			
				|  |  |                  String vF = videoFeatures.get(i);
 | 
	
	
		
			
				|  | @@ -167,16 +171,17 @@ public class RankStrategy4RankModel extends RankService {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          log.info("ItemFeature = {}", JSONUtils.toJson(videoFeatures));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        ((JedisConnectionFactory) connectionFactory).destroy();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          Map<String, String> sceneFeatureMap =  this.getSceneFeature(param);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          List<RankItem> rovRecallScore = ScorerUtils.getScorerPipeline(ScorerUtils.BASE_CONF)
 | 
	
		
			
				|  |  |                  .scoring(sceneFeatureMap, userFeatureMap, rankItems);
 | 
	
		
			
				|  |  |          log.info("mergeAndRankRovRecallNew rovRecallScore={}", JSONUtils.toJson(rovRecallScore));
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          return CommonCollectionUtils.toList(rovRecallScore, i -> {
 | 
	
		
			
				|  |  |              // hard code 将排序分数 赋值给video的sortScore
 | 
	
		
			
				|  |  |              Video v = i.getVideo();
 | 
	
		
			
				|  |  | -            v.setSortScore(i.getScore());
 | 
	
		
			
				|  |  | +            v.setModelScore(i.getScore());
 | 
	
		
			
				|  |  |              return v;
 | 
	
		
			
				|  |  |          });
 | 
	
		
			
				|  |  |      }
 |