Browse Source

保量通用方法抽取

xueyiming 1 day ago
parent
commit
8eddac315c

+ 33 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBasic.java

@@ -1,7 +1,9 @@
 package com.tzld.piaoquan.ad.engine.service.score.strategy;
 
+import com.alibaba.fastjson.JSONObject;
 import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
+import com.tzld.piaoquan.ad.engine.commons.util.DateUtils;
 import com.tzld.piaoquan.ad.engine.service.entity.GuaranteeView;
 import com.tzld.piaoquan.ad.engine.commons.enums.RedisPrefixEnum;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
@@ -13,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 
 import java.util.*;
 import java.util.stream.Collectors;
@@ -26,11 +29,19 @@ public abstract class RankStrategyBasic implements RankStrategy {
     @ApolloJsonValue("${creative.model.score.coefficient:{}}")
     private Map<Long, Double> creativeScoreCoefficient;
 
+    @Value("${guarantee.exp:742}")
+    protected String guaranteeExp;
+    @ApolloJsonValue("${alpha:1.0}")
+    protected Double alpha;
     @Autowired
     private FeatureService featureService;
     @Autowired
     protected AdRedisHelper adRedisHelper;
 
+    String key = "ad:platform:guarantee:data:{date}:{adrId}";
+
+
+
     protected Feature getFeature(ScoreParam param, RankRecommendRequestParam request) {
         List<AdPlatformCreativeDTO> adIdList = request.getAdIdList();
         List<String> cidList = adIdList.stream()
@@ -100,6 +111,28 @@ public abstract class RankStrategyBasic implements RankStrategy {
 
     }
 
+    protected Map<String, GuaranteeView> getGuaranteeViewMap(RankRecommendRequestParam request, ScoreParam scoreParam) {
+        Map<String, GuaranteeView> map = new HashMap<>();
+        if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+            String thatDayDateString = DateUtils.getThatDayDateString();
+            String redisKey = key.replace("{date}", thatDayDateString);
+            List<String> adVerIds = request.getAdIdList().stream().map(AdPlatformCreativeDTO::getAdVerId).distinct()
+                    .filter(Objects::nonNull).collect(Collectors.toList());
+            List<String> redisKeys = adVerIds.stream().map(e -> redisKey.replace("{adrId}", e)).collect(Collectors.toList());
+            List<String> values = adRedisHelper.mget(redisKeys);
+            if (CollectionUtils.isNotEmpty(values)) {
+                for (int i = 0; i < redisKeys.size(); i++) {
+                    String value = values.get(i);
+                    if (value != null) {
+                        GuaranteeView guaranteeView = JSONObject.parseObject(value, GuaranteeView.class);
+                        map.put(adVerIds.get(i), guaranteeView);
+                    }
+                }
+            }
+        }
+        return map;
+    }
+
     protected double getGuaranteeWeight(GuaranteeView guaranteeView) {
         double guaranteeWeight;
         if (guaranteeView.getGuaranteeNum() == null || guaranteeView.getGuaranteeNum() == 0

+ 22 - 3
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBy679.java

@@ -7,6 +7,7 @@ import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
 import com.tzld.piaoquan.ad.engine.commons.thread.ThreadPoolFactory;
 import com.tzld.piaoquan.ad.engine.commons.util.*;
+import com.tzld.piaoquan.ad.engine.service.entity.GuaranteeView;
 import com.tzld.piaoquan.ad.engine.service.feature.Feature;
 import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
 import lombok.extern.slf4j.Slf4j;
@@ -88,7 +89,7 @@ public class RankStrategyBy679 extends RankStrategyBasic {
 
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
-
+        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, scoreParam);
         List<AdRankItem> adRankItems = new ArrayList<>();
         Random random = new Random();
         List<Future<AdRankItem>> futures = new ArrayList<>();
@@ -112,7 +113,14 @@ public class RankStrategyBy679 extends RankStrategyBasic {
                         adRankItem.getExt().put("isApi", "1");
                     }
                     adRankItem.getExt().put("recallsources", dto.getRecallSources());
-
+                    if (MapUtils.isNotEmpty(map)) {
+                        GuaranteeView guaranteeView = map.get(dto.getAdVerId());
+                        if (guaranteeView != null) {
+                            double guaranteeWeight = getGuaranteeWeight(guaranteeView);
+                            adRankItem.getExt().put("guaranteeView", guaranteeView.toString());
+                            adRankItem.getExt().put("guaranteeWeight", guaranteeWeight);
+                        }
+                    }
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -211,7 +219,18 @@ public class RankStrategyBy679 extends RankStrategyBasic {
         for (AdRankItem item : result) {
 
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa());
+            if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+                double guaranteeScoreCoefficient;
+                if (item.getExt().get("guaranteeWeight") == null) {
+                    guaranteeScoreCoefficient = 1.0;
+                } else {
+                    guaranteeScoreCoefficient = Math.pow((double) item.getExt().get("guaranteeWeight"), alpha);
+                }
+                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa() * guaranteeScoreCoefficient);
+                item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
+            } else {
+                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa());
+            }
 
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());

+ 22 - 2
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBy680.java

@@ -5,6 +5,7 @@ import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
 import com.tzld.piaoquan.ad.engine.commons.thread.ThreadPoolFactory;
 import com.tzld.piaoquan.ad.engine.commons.util.*;
+import com.tzld.piaoquan.ad.engine.service.entity.GuaranteeView;
 import com.tzld.piaoquan.ad.engine.service.feature.Feature;
 import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
@@ -97,6 +98,7 @@ public class RankStrategyBy680 extends RankStrategyBasic {
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
 
+        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, scoreParam);
         List<AdRankItem> adRankItems = new ArrayList<>();
         Random random = new Random();
         List<Future<AdRankItem>> futures = new ArrayList<>();
@@ -121,7 +123,14 @@ public class RankStrategyBy680 extends RankStrategyBasic {
                     }
 
                     adRankItem.getExt().put("recallsources", dto.getRecallSources());
-
+                    if (MapUtils.isNotEmpty(map)) {
+                        GuaranteeView guaranteeView = map.get(dto.getAdVerId());
+                        if (guaranteeView != null) {
+                            double guaranteeWeight = getGuaranteeWeight(guaranteeView);
+                            adRankItem.getExt().put("guaranteeView", guaranteeView.toString());
+                            adRankItem.getExt().put("guaranteeWeight", guaranteeWeight);
+                        }
+                    }
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -228,7 +237,18 @@ public class RankStrategyBy680 extends RankStrategyBasic {
         for (AdRankItem item : result) {
 
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa());
+            if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+                double guaranteeScoreCoefficient;
+                if (item.getExt().get("guaranteeWeight") == null) {
+                    guaranteeScoreCoefficient = 1.0;
+                } else {
+                    guaranteeScoreCoefficient = Math.pow((double) item.getExt().get("guaranteeWeight"), alpha);
+                }
+                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa() * guaranteeScoreCoefficient);
+                item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
+            } else {
+                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa());
+            }
 
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());

+ 22 - 2
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBy683.java

@@ -5,6 +5,7 @@ import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
 import com.tzld.piaoquan.ad.engine.commons.thread.ThreadPoolFactory;
 import com.tzld.piaoquan.ad.engine.commons.util.*;
+import com.tzld.piaoquan.ad.engine.service.entity.GuaranteeView;
 import com.tzld.piaoquan.ad.engine.service.feature.Feature;
 import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
@@ -98,6 +99,7 @@ public class RankStrategyBy683 extends RankStrategyBasic {
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
 
+        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, scoreParam);
         List<AdRankItem> adRankItems = new ArrayList<>();
         Random random = new Random();
         List<Future<AdRankItem>> futures = new ArrayList<>();
@@ -122,7 +124,14 @@ public class RankStrategyBy683 extends RankStrategyBasic {
                     }
 
                     adRankItem.getExt().put("recallsources", dto.getRecallSources());
-
+                    if (MapUtils.isNotEmpty(map)) {
+                        GuaranteeView guaranteeView = map.get(dto.getAdVerId());
+                        if (guaranteeView != null) {
+                            double guaranteeWeight = getGuaranteeWeight(guaranteeView);
+                            adRankItem.getExt().put("guaranteeView", guaranteeView.toString());
+                            adRankItem.getExt().put("guaranteeWeight", guaranteeWeight);
+                        }
+                    }
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -229,7 +238,18 @@ public class RankStrategyBy683 extends RankStrategyBasic {
         for (AdRankItem item : result) {
 
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa());
+            if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+                double guaranteeScoreCoefficient;
+                if (item.getExt().get("guaranteeWeight") == null) {
+                    guaranteeScoreCoefficient = 1.0;
+                } else {
+                    guaranteeScoreCoefficient = Math.pow((double) item.getExt().get("guaranteeWeight"), alpha);
+                }
+                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa() * guaranteeScoreCoefficient);
+                item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
+            } else {
+                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa());
+            }
 
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());

+ 22 - 2
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBy687.java

@@ -4,6 +4,7 @@ import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
 import com.tzld.piaoquan.ad.engine.commons.thread.ThreadPoolFactory;
 import com.tzld.piaoquan.ad.engine.commons.util.*;
+import com.tzld.piaoquan.ad.engine.service.entity.GuaranteeView;
 import com.tzld.piaoquan.ad.engine.service.feature.Feature;
 import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
@@ -74,6 +75,7 @@ public class RankStrategyBy687 extends RankStrategyBasic {
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
 
+        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, scoreParam);
         List<AdRankItem> adRankItems = new ArrayList<>(request.getAdIdList().size());
         Random random = new Random();
         if (similarityConcurrent) {
@@ -94,7 +96,14 @@ public class RankStrategyBy687 extends RankStrategyBasic {
                     adRankItem.getExt().put("isApi", "1");
                 }
                 adRankItem.getExt().put("recallsources", dto.getRecallSources());
-
+                if (MapUtils.isNotEmpty(map)) {
+                    GuaranteeView guaranteeView = map.get(dto.getAdVerId());
+                    if (guaranteeView != null) {
+                        double guaranteeWeight = getGuaranteeWeight(guaranteeView);
+                        adRankItem.getExt().put("guaranteeView", guaranteeView.toString());
+                        adRankItem.getExt().put("guaranteeWeight", guaranteeWeight);
+                    }
+                }
 
                 String cidStr = dto.getCreativeId().toString();
                 Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
@@ -218,7 +227,18 @@ public class RankStrategyBy687 extends RankStrategyBasic {
 
         long time3 = System.currentTimeMillis();
         for (AdRankItem item : result) {
-            item.setScore(item.getLrScore() * item.getCpa());
+            if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+                double guaranteeScoreCoefficient;
+                if (item.getExt().get("guaranteeWeight") == null) {
+                    guaranteeScoreCoefficient = 1.0;
+                } else {
+                    guaranteeScoreCoefficient = Math.pow((double) item.getExt().get("guaranteeWeight"), alpha);
+                }
+                item.setScore(item.getLrScore() * item.getCpa() * guaranteeScoreCoefficient);
+                item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
+            } else {
+                item.setScore(item.getLrScore() * item.getCpa());
+            }
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());
             item.getFeatureMap().putAll(userFeatureMap);

+ 2 - 27
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBy688.java

@@ -46,8 +46,7 @@ public class RankStrategyBy688 extends RankStrategyBasic {
     @Value("${word2vec.exp:694}")
     private String word2vecExp;
 
-    @Value("${guarantee.exp:742}")
-    private String guaranteeExp;
+
 
     // FIXME(zhoutian): 可能需要独立配置
     @ApolloJsonValue("${rank.score.weight.680:{}}")
@@ -56,15 +55,8 @@ public class RankStrategyBy688 extends RankStrategyBasic {
     @ApolloJsonValue("${rank.score.neg_sample_rate:0.01}")
     Double negSampleRate;
 
-    @ApolloJsonValue("${alpha:1.0}")
-    Double alpha;
-
-    String key = "ad:platform:guarantee:data:{date}:{adrId}";
-
     Set<String> sparseFeatureSet;
 
-    @Autowired
-    private AdRedisHelper adRedisHelper;
 
     @PostConstruct
     public void afterInit() {
@@ -165,28 +157,11 @@ public class RankStrategyBy688 extends RankStrategyBasic {
                 userFeatureMap.put("root_source_channel", rootSourceChannel);
             }
         }
-        Map<String, GuaranteeView> map = new HashMap<>();
-        if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
-            String thatDayDateString = DateUtils.getThatDayDateString();
-            String redisKey = key.replace("{date}", thatDayDateString);
-            List<String> adVerIds = request.getAdIdList().stream().map(AdPlatformCreativeDTO::getAdVerId).distinct()
-                    .filter(Objects::nonNull).collect(Collectors.toList());
-            List<String> redisKeys = adVerIds.stream().map(e -> redisKey.replace("{adrId}", e)).collect(Collectors.toList());
-            List<String> values = adRedisHelper.mget(redisKeys);
-            if (CollectionUtils.isNotEmpty(values)) {
-                for (int i = 0; i < redisKeys.size(); i++) {
-                    String value = values.get(i);
-                    if (value != null) {
-                        GuaranteeView guaranteeView = JSONObject.parseObject(value, GuaranteeView.class);
-                        map.put(adVerIds.get(i), guaranteeView);
-                    }
-                }
-            }
-        }
 
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
 
+        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, scoreParam);
         List<AdRankItem> adRankItems = new ArrayList<>();
         Random random = new Random();
         List<Future<AdRankItem>> futures = new ArrayList<>();