Browse Source

Merge branch 'dev-xym-guarantee' into pre-master

# Conflicts:
#	ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBy688.java
xueyiming 3 weeks ago
parent
commit
ba1e8c513f

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

@@ -13,6 +13,7 @@ import com.tzld.piaoquan.ad.engine.service.feature.Feature;
 import com.tzld.piaoquan.ad.engine.service.feature.FeatureService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -38,8 +39,7 @@ public abstract class RankStrategyBasic implements RankStrategy {
     @Autowired
     protected AdRedisHelper adRedisHelper;
 
-    String key = "ad:platform:guarantee:data:{date}:{adrId}";
-
+    String adPlatformGuaranteeKey = "ad:platform:guarantee:data:{date}:{adverId}";
 
 
     protected Feature getFeature(ScoreParam param, RankRecommendRequestParam request) {
@@ -113,27 +113,46 @@ 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);
+        try {
+            if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+                String thatDayDateString = DateUtils.getThatDayDateString();
+                String redisKey = adPlatformGuaranteeKey.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("{adverId}", 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);
+                        }
                     }
                 }
             }
+        } catch (Exception e) {
+            log.error("getGuaranteeViewMap error", e);
         }
+
         return map;
     }
 
-    protected double getGuaranteeWeight(GuaranteeView guaranteeView) {
+    protected void setGuaranteeWeight(Map<String, GuaranteeView> map, String adVerId, Map<String, Object> ext) {
+        if (MapUtils.isNotEmpty(map)) {
+            GuaranteeView guaranteeView = map.get(adVerId);
+            if (guaranteeView != null) {
+                double guaranteeWeight = calculateGuaranteedWeight(guaranteeView);
+                ext.put("guaranteeView", guaranteeView.toString());
+                ext.put("guaranteeWeight", guaranteeWeight);
+            }
+        }
+    }
+
+    protected double calculateGuaranteedWeight(GuaranteeView guaranteeView) {
+        if (guaranteeView == null) {
+            return 1.0;
+        }
         double guaranteeWeight;
         if (guaranteeView.getGuaranteeNum() == null || guaranteeView.getGuaranteeNum() == 0
                 || guaranteeView.getGuaranteeRate() == null || guaranteeView.getGuaranteeRate() == 0.0) {
@@ -143,9 +162,11 @@ public abstract class RankStrategyBasic implements RankStrategy {
                     guaranteeView.getGuaranteeNum() <= guaranteeView.getAdrAlgoViewNum()) {
                 guaranteeWeight = 0.0;
             } else {
-                int allViewNum = guaranteeView.getAllAlgoViewNum() == null || guaranteeView.getAllAlgoViewNum() == 0 ? 1 : guaranteeView.getAllAlgoViewNum();
-                int adrAlogViewNum = guaranteeView.getAdrAlgoViewNum() == null || guaranteeView.getAdrAlgoViewNum() == 0 ? 1 : guaranteeView.getAdrAlgoViewNum();
-                //guaranteeView.getGuaranteeRate() 是百分之几  要成0.01
+                int allViewNum = guaranteeView.getAllAlgoViewNum() == null || guaranteeView.getAllAlgoViewNum() == 0 ?
+                        1 : guaranteeView.getAllAlgoViewNum();
+                int adrAlogViewNum = guaranteeView.getAdrAlgoViewNum() == null || guaranteeView.getAdrAlgoViewNum() == 0 ?
+                        1 : guaranteeView.getAdrAlgoViewNum();
+                //guaranteeView.getGuaranteeRate() 是百分之几  要乘0.01
                 guaranteeWeight = guaranteeView.getGuaranteeRate() * 0.01 * allViewNum / adrAlogViewNum;
                 if (guaranteeWeight < 0.5) {
                     guaranteeWeight = 0.5;
@@ -157,4 +178,16 @@ public abstract class RankStrategyBasic implements RankStrategy {
         }
         return guaranteeWeight;
     }
+
+    protected double getGuaranteeScoreCoefficient(ScoreParam scoreParam, Map<String, Object> ext) {
+        if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+            if (ext.get("guaranteeWeight") == null) {
+                return 1.0;
+            } else {
+                return Math.pow((double) ext.get("guaranteeWeight"), alpha);
+            }
+        } else {
+            return 1.0;
+        }
+    }
 }

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

@@ -113,14 +113,7 @@ 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);
-                        }
-                    }
+                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt());
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -219,19 +212,9 @@ public class RankStrategyBy679 extends RankStrategyBasic {
         for (AdRankItem item : result) {
 
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            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());
-            }
-
+            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(scoreParam, item.getExt());
+            item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa() * guaranteeScoreCoefficient);
+            item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());
             item.getScoreMap().put("cpmCoefficient", cpmCoefficient);

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

@@ -123,14 +123,7 @@ 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);
-                        }
-                    }
+                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt());
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -237,19 +230,9 @@ public class RankStrategyBy680 extends RankStrategyBasic {
         for (AdRankItem item : result) {
 
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            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());
-            }
-
+            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(scoreParam, item.getExt());
+            item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa() * guaranteeScoreCoefficient);
+            item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());
             item.getScoreMap().put("cpmCoefficient", cpmCoefficient);

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

@@ -124,14 +124,7 @@ 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);
-                        }
-                    }
+                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt());
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -238,19 +231,9 @@ public class RankStrategyBy683 extends RankStrategyBasic {
         for (AdRankItem item : result) {
 
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            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());
-            }
-
+            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(scoreParam, item.getExt());
+            item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa() * guaranteeScoreCoefficient);
+            item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());
             item.getScoreMap().put("cpmCoefficient", cpmCoefficient);

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

@@ -96,14 +96,7 @@ 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);
-                    }
-                }
+                setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt());
 
                 String cidStr = dto.getCreativeId().toString();
                 Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
@@ -227,18 +220,9 @@ public class RankStrategyBy687 extends RankStrategyBasic {
 
         long time3 = System.currentTimeMillis();
         for (AdRankItem item : result) {
-            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());
-            }
+            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(scoreParam, item.getExt());
+            item.setScore(item.getLrScore() * item.getCpa() * guaranteeScoreCoefficient);
+            item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());
             item.getFeatureMap().putAll(userFeatureMap);

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

@@ -47,7 +47,6 @@ public class RankStrategyBy688 extends RankStrategyBasic {
     private String word2vecExp;
 
 
-
     // FIXME(zhoutian): 可能需要独立配置
     @ApolloJsonValue("${rank.score.weight.680:{}}")
     private Map<String, Double> weightMap;
@@ -185,14 +184,7 @@ public class RankStrategyBy688 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);
-                        }
-                    }
+                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt());
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -302,21 +294,11 @@ public class RankStrategyBy688 extends RankStrategyBasic {
         double cpmCoefficient = weightParam.getOrDefault("cpmCoefficient", 0.9);
 
         for (AdRankItem item : result) {
-            log.info("item ext={}", item.getExt());
+
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            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);
-                }
-                log.info("guaranteeScoreCoefficient={}", guaranteeScoreCoefficient);
-                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa() * guaranteeScoreCoefficient);
-                item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
-            } else {
-                item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa());
-            }
+            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(scoreParam, item.getExt());
+            item.setScore(item.getLrScore() * scoreCoefficient * item.getCpa() * guaranteeScoreCoefficient);
+            item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());
             item.getScoreMap().put("cpmCoefficient", cpmCoefficient);