ソースを参照

ADD: 610 地域过滤实验正交

sunxy 1 年間 前
コミット
46aa2db848

+ 3 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/implement/recall/ViewedHistoryFilter.java

@@ -9,6 +9,7 @@ import com.tzld.piaoquan.recommend.server.gen.recommend.RecommendRequest;
 import com.tzld.piaoquan.recommend.server.model.Video;
 import com.tzld.piaoquan.recommend.server.service.ServiceBeanFactory;
 import com.tzld.piaoquan.recommend.server.service.SpringContextHolder;
+import com.tzld.piaoquan.recommend.server.service.filter.VideoCityFilterService;
 import com.tzld.piaoquan.recommend.server.service.filter.strategy.VideoView;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -27,6 +28,7 @@ import java.util.stream.Collectors;
 
 public class ViewedHistoryFilter extends AbstractFilter<Video> {
 
+    private VideoCityFilterService videoCityFilterService;
 
     protected Set<String> historySet;
 
@@ -46,6 +48,7 @@ public class ViewedHistoryFilter extends AbstractFilter<Video> {
         if (historySet == null) {
             historySet = new HashSet<>();
         }
+
     }
 
     @Override

+ 111 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/filter/VideoCityFilterService.java

@@ -0,0 +1,111 @@
+package com.tzld.piaoquan.recommend.server.service.filter;
+
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.google.common.base.Stopwatch;
+import com.tzld.piaoquan.recommend.server.repository.WxVideoTagRel;
+import com.tzld.piaoquan.recommend.server.repository.WxVideoTagRelRepository;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/**
+ * @author sunxy
+ */
+@Service
+public class VideoCityFilterService {
+
+//    private final Logger logger = Logger.getLogger(VideoCityFilterService.class.getName());
+
+    private final Logger logger = LoggerFactory.getLogger(VideoCityFilterService.class);
+
+//    @Value("${video.filter.city.tagid.json:}")
+//    private String videoFilterTagIdJson;
+
+    @ApolloJsonValue("${video.filter.city.tagid.json:{}}")
+    private Map<String, List<Long>> videoFilterCityTagIdMap;
+
+    @Resource
+    private WxVideoTagRelRepository wxVideoTagRelRepository;
+    private Map<String, Set<Long>> videoTagCache = new ConcurrentHashMap<>();
+
+    @PostConstruct
+    public void init() {
+        initCacheByValue();
+    }
+
+    @Scheduled(cron = "0 0 0/2 * * ? ")
+    public void cornInit() {
+        initCacheByValue();
+    }
+
+    private void initCacheByValue() {
+        Stopwatch stopwatch = Stopwatch.createStarted();
+        if (Objects.isNull(videoFilterCityTagIdMap) || videoFilterCityTagIdMap.isEmpty()) {
+            videoTagCache = new ConcurrentHashMap<>();
+            return;
+        }
+        Map<String, Set<Long>> tmp = new ConcurrentHashMap<>();
+        for (Map.Entry<String, List<Long>> entry : videoFilterCityTagIdMap.entrySet()) {
+            if (Objects.isNull(entry)) {
+                continue;
+            }
+            String cityCode = entry.getKey();
+            List<Long> tagList = entry.getValue();
+            if (Objects.isNull(cityCode) || Objects.isNull(tagList) || tagList.isEmpty()) {
+                continue;
+            }
+            for (Long tagId : tagList) {
+                if (tagId == null || tagId <= 0L) {
+                    continue;
+                }
+                Set<Long> videosByTag = wxVideoTagRelRepository.findAllByTagId(tagId).stream().map(WxVideoTagRel::getVideoId).collect(Collectors.toSet());
+                tmp.put(cityCode, videosByTag);
+            }
+        }
+        videoTagCache = tmp;
+        logger.info("initCacheByValue videoTagCache.size = {} execute time = {}", videoTagCache.size(), stopwatch.stop().elapsed(TimeUnit.MILLISECONDS));
+
+    }
+
+    public List<Long> filterVideosByCity(List<Long> videoIds, String cityCode) {
+        if (videoIds == null || videoIds.isEmpty() || StringUtils.isBlank(cityCode)) {
+            return videoIds;
+        }
+        List<Long> tagIdList = videoFilterCityTagIdMap.get(cityCode);
+        if (tagIdList == null || tagIdList.isEmpty()) {
+            return videoIds;
+        }
+        videoIds.removeIf(videoId -> {
+            if (videoId == null || videoId <= 0L) {
+                return true;
+            }
+            for (Long tagId : tagIdList) {
+                if (tagId == null || tagId <= 0L) {
+                    continue;
+                }
+                Set<Long> videosByTag = videoTagCache.get(cityCode);
+                if (videosByTag == null || videosByTag.isEmpty()) {
+                    continue;
+                }
+                if (videosByTag.contains(videoId)) {
+                    return false;
+                }
+            }
+            return true;
+        });
+        return videoIds;
+    }
+
+}