|  | @@ -2,6 +2,7 @@ package com.tzld.piaoquan.recommend.server.service.rank.strategy;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | +import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 | 
	
		
			
				|  |  |  import com.google.common.reflect.TypeToken;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.model.Video;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.service.rank.RankParam;
 | 
	
	
		
			
				|  | @@ -9,8 +10,7 @@ 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.SpecialRecallStrategy;
 | 
	
		
			
				|  |  | +import com.tzld.piaoquan.recommend.server.service.rank.processor.RankProcessorTagFilter;
 | 
	
		
			
				|  |  |  import com.tzld.piaoquan.recommend.server.util.JSONUtils;
 | 
	
		
			
				|  |  |  import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  |  import org.apache.commons.collections4.CollectionUtils;
 | 
	
	
		
			
				|  | @@ -27,20 +27,10 @@ import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  |  @Slf4j
 | 
	
		
			
				|  |  |  public class RankStrategy4Density extends RankService {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    @ApolloJsonValue("${RankStrategy4DensityFilter:}")
 | 
	
		
			
				|  |  | +    private Map<String,Map<String, Map<String, String>>> filterRules;
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  |      public RankResult mergeAndSort(RankParam param, List<Video> rovRecallRank, List<Video> flowPoolRank) {
 | 
	
		
			
				|  |  | -        // 测试,添加数据
 | 
	
		
			
				|  |  | -//        rovRecallRank.add(0, getTestVideo(1070462L, ""));
 | 
	
		
			
				|  |  | -//        rovRecallRank.add(0, getTestVideo(1085062L, ""));
 | 
	
		
			
				|  |  | -//        rovRecallRank.add(0, getTestVideo(1102552L, ""));
 | 
	
		
			
				|  |  | -//        rovRecallRank.add(0, getTestVideo(1115929L, ""));
 | 
	
		
			
				|  |  | -//
 | 
	
		
			
				|  |  | -//        flowPoolRank = new ArrayList<>();
 | 
	
		
			
				|  |  | -//        flowPoolRank.add(getTestVideo(1263931L, "flow"));
 | 
	
		
			
				|  |  | -//        flowPoolRank.add(getTestVideo(1284797L, "flow"));
 | 
	
		
			
				|  |  | -//        flowPoolRank.add(getTestVideo(1289001L, "flow"));
 | 
	
		
			
				|  |  | -//        flowPoolRank.add(getTestVideo(1331503L, "flow"));
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          if (CollectionUtils.isEmpty(rovRecallRank)) {
 | 
	
		
			
				|  |  |              if (param.getSize() < flowPoolRank.size()) {
 | 
	
	
		
			
				|  | @@ -49,31 +39,44 @@ public class RankStrategy4Density extends RankService {
 | 
	
		
			
				|  |  |                  return new RankResult(flowPoolRank);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        // 1 读取多样性密度控制规则
 | 
	
		
			
				|  |  | -        String appType = String.valueOf(param.getAppType());
 | 
	
		
			
				|  |  | -        String ruleStr = this.redisTemplate.opsForValue().get("TAGS_FILTER_RULE_V1_JSON");
 | 
	
		
			
				|  |  | +        // 1 读取多样性密度控制规则------------------
 | 
	
		
			
				|  |  | +//        String appType = String.valueOf(param.getAppType());
 | 
	
		
			
				|  |  | +//        String ruleStr = this.redisTemplate.opsForValue().get("TAGS_FILTER_RULE_V1_JSON");
 | 
	
		
			
				|  |  | +//        Map<String, Integer> densityRules = new HashMap<>();
 | 
	
		
			
				|  |  | +//        if (ruleStr != null){
 | 
	
		
			
				|  |  | +//            Map<String, Map<String, Object>> ruleOrigin = JSONUtils.fromJson(ruleStr,
 | 
	
		
			
				|  |  | +//                    new TypeToken<Map<String, Map<String, Object>>>() {},
 | 
	
		
			
				|  |  | +//                    Collections.emptyMap());
 | 
	
		
			
				|  |  | +//            for (Map.Entry<String, Map<String, Object>> entry : ruleOrigin.entrySet()){
 | 
	
		
			
				|  |  | +//                String k = entry.getKey();
 | 
	
		
			
				|  |  | +//                if (!entry.getValue().containsKey(appType)){
 | 
	
		
			
				|  |  | +//                    continue;
 | 
	
		
			
				|  |  | +//                }
 | 
	
		
			
				|  |  | +//                JSONObject jb = (JSONObject) entry.getValue().get(appType);
 | 
	
		
			
				|  |  | +//                try{
 | 
	
		
			
				|  |  | +//                    if (jb.containsKey("density") && jb.get("density") instanceof Integer){
 | 
	
		
			
				|  |  | +//                        densityRules.put(k, jb.getInteger("density"));
 | 
	
		
			
				|  |  | +//                    }
 | 
	
		
			
				|  |  | +//                }catch (Exception e){
 | 
	
		
			
				|  |  | +//                    log.error("parse densityRules is wrong:", e);
 | 
	
		
			
				|  |  | +//                }
 | 
	
		
			
				|  |  | +//            }
 | 
	
		
			
				|  |  | +//        }
 | 
	
		
			
				|  |  |          Map<String, Integer> densityRules = new HashMap<>();
 | 
	
		
			
				|  |  | -        if (ruleStr != null){
 | 
	
		
			
				|  |  | -            Map<String, Map<String, Object>> ruleOrigin = JSONUtils.fromJson(ruleStr,
 | 
	
		
			
				|  |  | -                    new TypeToken<Map<String, Map<String, Object>>>() {},
 | 
	
		
			
				|  |  | -                    Collections.emptyMap());
 | 
	
		
			
				|  |  | -            for (Map.Entry<String, Map<String, Object>> entry : ruleOrigin.entrySet()){
 | 
	
		
			
				|  |  | -                String k = entry.getKey();
 | 
	
		
			
				|  |  | -                if (!entry.getValue().containsKey(appType)){
 | 
	
		
			
				|  |  | -                    continue;
 | 
	
		
			
				|  |  | -                }
 | 
	
		
			
				|  |  | -                JSONObject jb = (JSONObject) entry.getValue().get(appType);
 | 
	
		
			
				|  |  | -                try{
 | 
	
		
			
				|  |  | -                    if (jb.containsKey("density") && jb.get("density") instanceof Integer){
 | 
	
		
			
				|  |  | -                        densityRules.put(k, jb.getInteger("density"));
 | 
	
		
			
				|  |  | -                    }
 | 
	
		
			
				|  |  | -                }catch (Exception e){
 | 
	
		
			
				|  |  | -                    log.error("parse densityRules is wrong:", e);
 | 
	
		
			
				|  |  | +        String abCode = param.getAbCode();
 | 
	
		
			
				|  |  | +        if (this.filterRules != null && this.filterRules.containsKey(abCode)){
 | 
	
		
			
				|  |  | +            Map<String, Map<String, String>> rule = this.filterRules.get(abCode);
 | 
	
		
			
				|  |  | +            for (Map.Entry<String, Map<String, String>> entry : rule.entrySet()){
 | 
	
		
			
				|  |  | +                String key = entry.getKey();
 | 
	
		
			
				|  |  | +                Map<String, String> value = entry.getValue();
 | 
	
		
			
				|  |  | +                if (value.containsKey("density")){
 | 
	
		
			
				|  |  | +                    densityRules.put(key, Integer.valueOf(value.get("density")));
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        // 2 读取video的tags
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 2 读取video的tags------------------
 | 
	
		
			
				|  |  |          List<Long> videoIds = new ArrayList<>();
 | 
	
		
			
				|  |  |          for (Video v : rovRecallRank) {
 | 
	
		
			
				|  |  |              videoIds.add(v.getVideoId());
 | 
	
	
		
			
				|  | @@ -88,9 +91,14 @@ public class RankStrategy4Density extends RankService {
 | 
	
		
			
				|  |  |          for (Video v : flowPoolRank) {
 | 
	
		
			
				|  |  |              v.setTags(videoTagDict.getOrDefault(v.getVideoId(), new ArrayList<>()));
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        // ------------------读取video的tags完成---------------------
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        // 3 读取过滤规则,根据tag和过滤规则进行过滤------------------
 | 
	
		
			
				|  |  | +        if (this.filterRules != null && this.filterRules.containsKey(abCode)){
 | 
	
		
			
				|  |  | +            Map<String, Map<String, String>> rule = this.filterRules.get(abCode);
 | 
	
		
			
				|  |  | +            RankProcessorTagFilter.tagFitlter(rovRecallRank, flowPoolRank, rule);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        // 4 流量池按比例强插---------------------
 | 
	
		
			
				|  |  |          List<Video> result = new ArrayList<>();
 | 
	
		
			
				|  |  |          for (int i = 0; i < param.getTopK() && i < rovRecallRank.size(); i++) {
 | 
	
		
			
				|  |  |              result.add(rovRecallRank.get(i));
 | 
	
	
		
			
				|  | @@ -128,15 +136,19 @@ public class RankStrategy4Density extends RankService {
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        // 3 进行密度控制
 | 
	
		
			
				|  |  | +        // 3 进行密度控制------------------
 | 
	
		
			
				|  |  |          Set<Long> videosSet = result.stream().map(r-> r.getVideoId()).collect(Collectors.toSet());
 | 
	
		
			
				|  |  |          List<Video> rovRecallRankNew = rovRecallRank.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
 | 
	
		
			
				|  |  |          List<Video> flowPoolRankNew = flowPoolRank.stream().filter(r -> !videosSet.contains(r.getVideoId())).collect(Collectors.toList());
 | 
	
		
			
				|  |  |          List<Video> resultWithDnsity = RankProcessorDensity.mergeDensityControl(result,
 | 
	
		
			
				|  |  |                  rovRecallRankNew, flowPoolRankNew, densityRules);
 | 
	
		
			
				|  |  | +//        log.info("zhangbo22 old={}, new={}", JSONUtils.toJson(result),
 | 
	
		
			
				|  |  | +//                    JSONUtils.toJson(resultWithDnsity));
 | 
	
		
			
				|  |  |          return new RankResult(resultWithDnsity);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  //    public Video getTestVideo(Long id, String s){
 | 
	
		
			
				|  |  |  //        Video a1 = new Video();
 | 
	
		
			
				|  |  |  //        a1.setVideoId(id);
 |