Procházet zdrojové kódy

抽取通用方法提到父类

xueyiming před 1 měsícem
rodič
revize
c9e1e42254

+ 28 - 4
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,7 +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) {
@@ -115,10 +116,10 @@ public abstract class RankStrategyBasic implements RankStrategy {
         try {
             if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
                 String thatDayDateString = DateUtils.getThatDayDateString();
-                String redisKey = key.replace("{date}", thatDayDateString);
+                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("{adrId}", e)).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++) {
@@ -137,7 +138,18 @@ public abstract class RankStrategyBasic implements RankStrategy {
         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;
         }
@@ -166,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);

+ 4 - 21
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<>());
@@ -304,18 +296,9 @@ public class RankStrategyBy688 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);