jch 2 недель назад
Родитель
Сommit
3b1a2539c3

+ 1 - 1
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/entity/RootSessionIdExpConfig.java → ad-engine-commons/src/main/java/com/tzld/piaoquan/ad/engine/commons/util/RootSessionIdExpConfig.java

@@ -1,4 +1,4 @@
-package com.tzld.piaoquan.ad.engine.service.entity;
+package com.tzld.piaoquan.ad.engine.commons.util;
 
 import lombok.Data;
 

+ 48 - 0
ad-engine-commons/src/main/java/com/tzld/piaoquan/ad/engine/commons/util/RootSessionIdTailUtil.java

@@ -1,8 +1,56 @@
 package com.tzld.piaoquan.ad.engine.commons.util;
 
+import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+@Slf4j
 public class RootSessionIdTailUtil {
+    public static final int rankTailN = 6;
+    public static final int reRankTailN = 7;
+    public static final long allApp = 999;
+
+    // appType: 999 表示全量
+    public static List<RootSessionIdExpConfig> getRootSessionIdTailConfig(ScoreParam scoreParam, int nFromEnd, List<RootSessionIdExpConfig> rootSessionIdConfig) {
+        try {
+            List<RootSessionIdExpConfig> hitConfigs = new ArrayList<>();
+            if (CollectionUtils.isNotEmpty(rootSessionIdConfig)) {
+                Long appType = scoreParam.getAppType();
+                String tail = getNthCharFromEnd(scoreParam.getRootSessionId(), nFromEnd);
+                for (RootSessionIdExpConfig item : rootSessionIdConfig) {
+                    Set<Long> appTypeSet = item.getAppType();
+                    if (null == appTypeSet) {
+                        continue;
+                    }
+
+                    // match all
+                    if (appTypeSet.contains(allApp)) {
+                        hitConfigs.add(item);
+                        continue;
+                    }
+                    // match app & tail
+                    Set<String> tailSet = item.getTail();
+                    if (null != appType && null != tail && null != tailSet) {
+                        if (appTypeSet.contains(appType) && tailSet.contains(tail)) {
+                            hitConfigs.add(item);
+                            continue;
+                        }
+                    }
+                }
+            }
+            return hitConfigs;
+        } catch (Exception e) {
+            log.error("rootSessionId尾号配置异常", e);
+        }
+        return Collections.emptyList();
+    }
+
     public static String getNthCharFromEnd(String rootSessionId, int nFromEnd) {
         if (StringUtils.isBlank(rootSessionId) || nFromEnd < 1) {
             return null;

+ 8 - 16
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/impl/RankServiceImpl.java

@@ -6,8 +6,8 @@ import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
 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.util.RootSessionIdExpConfig;
 import com.tzld.piaoquan.ad.engine.commons.util.RootSessionIdTailUtil;
-import com.tzld.piaoquan.ad.engine.service.entity.RootSessionIdExpConfig;
 import com.tzld.piaoquan.ad.engine.service.log.LogHubService;
 import com.tzld.piaoquan.ad.engine.service.predict.helper.NewExpInfoHelper;
 import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdPredictModelParam;
@@ -137,21 +137,13 @@ public class RankServiceImpl implements RankService {
 
     private String getRankStrategyCode(ScoreParam scoreParam) {
         try {
-            if (CollectionUtils.isNotEmpty(rankRootSessionIdConfig)) {
-                Long appType = scoreParam.getAppType();
-                String rootSessionId = scoreParam.getRootSessionId();
-                if (null != appType && null != rootSessionId) {
-                    String tail = RootSessionIdTailUtil.getNthCharFromEnd(rootSessionId, 6);
-                    if (null != tail) {
-                        for (RootSessionIdExpConfig item : rankRootSessionIdConfig) {
-                            if (null != item.getAppType() && null != item.getTail()) {
-                                if (item.getAppType().contains(appType) && item.getTail().contains(tail)) {
-                                    if (null != item.getConfig() && item.getConfig().containsKey("rank")) {
-                                        return item.getConfig().get("rank");
-                                    }
-                                }
-                            }
-                        }
+            List<RootSessionIdExpConfig> expConfigs = RootSessionIdTailUtil.getRootSessionIdTailConfig(scoreParam, RootSessionIdTailUtil.rankTailN, rankRootSessionIdConfig);
+            if (!expConfigs.isEmpty()) {
+                RootSessionIdExpConfig expConfig = expConfigs.get(0);
+                if (null != expConfig && null != expConfig.getConfig()) {
+                    String expCode = expConfig.getConfig().get("rank");
+                    if (null != expCode) {
+                        return expCode;
                     }
                 }
             }

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

@@ -7,6 +7,7 @@ import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.ad.engine.commons.enums.CrowdLayerEnum;
 import com.tzld.piaoquan.ad.engine.commons.enums.FilterTypeEnum;
 import com.tzld.piaoquan.ad.engine.commons.enums.RedisPrefixEnum;
+import com.tzld.piaoquan.ad.engine.commons.helper.LayerCustomerFlowControlDataHelper;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
 import com.tzld.piaoquan.ad.engine.commons.redis.AdRedisHelper;
 import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
@@ -14,6 +15,8 @@ import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 import com.tzld.piaoquan.ad.engine.commons.thread.ThreadPoolFactory;
 import com.tzld.piaoquan.ad.engine.commons.util.DateUtils;
 import com.tzld.piaoquan.ad.engine.commons.util.ObjUtil;
+import com.tzld.piaoquan.ad.engine.commons.util.RootSessionIdExpConfig;
+import com.tzld.piaoquan.ad.engine.commons.util.RootSessionIdTailUtil;
 import com.tzld.piaoquan.ad.engine.service.entity.*;
 import com.tzld.piaoquan.ad.engine.service.feature.Feature;
 import com.tzld.piaoquan.ad.engine.service.feature.FeatureService;
@@ -46,6 +49,12 @@ public abstract class RankStrategyBasic implements RankStrategy {
     @ApolloJsonValue("${h5.score.suppress:{}}")
     private Map<String, Double> h5ScoreSuppress;
 
+    @ApolloJsonValue("${rerank.strategy.flow.control:{}}")
+    private Map<String, Double> flowControlParams;
+
+    @ApolloJsonValue("${rerank.root.session.id.tail.config:[]}")
+    private List<RootSessionIdExpConfig> rerankRootSessionIdConfig;
+
     @ApolloJsonValue("${h5.score.suppress.customer.white:[]}")
     private Set<Long> customerIdWhiteSet;
 
@@ -1329,4 +1338,93 @@ public abstract class RankStrategyBasic implements RankStrategy {
             return false;
         }
     }
+
+    // rerank
+    protected void calRerankWeight(ScoreParam scoreParam, String layer, List<AdRankItem> items) {
+        try {
+            List<RootSessionIdExpConfig> expConfigs = RootSessionIdTailUtil.getRootSessionIdTailConfig(scoreParam, RootSessionIdTailUtil.reRankTailN, rerankRootSessionIdConfig);
+            if (expConfigs.isEmpty()) {
+                return;
+            }
+            Set<String> hit = new HashSet<>();
+            for (RootSessionIdExpConfig expConfig : expConfigs) {
+                if (null != expConfig && null != expConfig.getConfig()) {
+                    String rerankStrategy = expConfig.getConfig().get("rerank");
+                    if (null == rerankStrategy || hit.contains(rerankStrategy)) {
+                        continue;
+                    }
+                    hit.add(rerankStrategy);
+                    switch (rerankStrategy) {
+                        case "flowControl":
+                            calFlowControlWeight(layer, items);
+                            break;
+                        default:
+                            break;
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("cal rerank weight error", e);
+        }
+    }
+
+    // 控制流量
+    protected void calFlowControlWeight(String layer, List<AdRankItem> items) {
+        try {
+            if (org.springframework.util.CollectionUtils.isEmpty(flowControlParams)) {
+                return;
+            }
+
+            double cpmPow = flowControlParams.getOrDefault("cpmPow", 0.3);
+            double lowerCpmWeight = flowControlParams.getOrDefault("lowerCpmWeight", 1.0);
+            double upperCpmWeight = flowControlParams.getOrDefault("upperCpmWeight", 1.2);
+            double flowPow = flowControlParams.getOrDefault("flowPow", 1.2);
+            double lowerFlowWeight = flowControlParams.getOrDefault("lowerFlowWeight", 0.77);
+            double upperFlowWeight = flowControlParams.getOrDefault("upperFlowWeight", 1.0);
+
+            double hitViewRate = flowControlParams.getOrDefault("hitViewRate", 0.05);
+            double plusViewRate = flowControlParams.getOrDefault("plusViewRate", 20.0);
+            double viewRatePow = flowControlParams.getOrDefault("viewRatePow", 0.2);
+            double viewRateScale = flowControlParams.getOrDefault("viewRateScale", 0.5491);
+            for (AdRankItem item : items) {
+                double flowWeight = getFlowControlWeight(layer, item,
+                        cpmPow, lowerCpmWeight, upperCpmWeight,
+                        flowPow, lowerFlowWeight, upperFlowWeight,
+                        hitViewRate, plusViewRate, viewRatePow, viewRateScale);
+                item.getScoreMap().put("flow", flowWeight);
+            }
+        } catch (Exception e) {
+            log.error("cal flow control weight error", e);
+        }
+    }
+
+    private double getFlowControlWeight(String layer, AdRankItem item,
+                                        double cpmPow, double lowerCpmWeight, double upperCpmWeight,
+                                        double flowPow, double lowerFlowWeight, double upperFlowWeight,
+                                        double hitViewRate, double plusViewRate, double viewRatePow, double viewRateScale) {
+        try {
+            String profession = item.getProfession();
+            String customerId = String.valueOf(item.getCustomerId());
+            Pair<Double, Double> pair = LayerCustomerFlowControlDataHelper.getWeight(layer, profession, customerId);
+            if (null != pair) {
+                double cpmWeight = 1;
+                double flowWeight = 1;
+                double cpmDiff = pair.getLeft();
+                double viewRate = pair.getRight();
+                if (cpmDiff > 0) {
+                    cpmWeight = Math.pow(cpmDiff, cpmPow);
+                    cpmWeight = Math.min(Math.max(lowerCpmWeight, cpmWeight), upperCpmWeight);
+                }
+                if (viewRate > hitViewRate) {
+                    flowWeight = 1 / Math.pow(viewRate * 100 + plusViewRate, viewRatePow) / viewRateScale;
+                    flowWeight = Math.pow(flowWeight, flowPow);
+                    flowWeight = Math.min(Math.max(lowerFlowWeight, flowWeight), upperFlowWeight);
+                }
+                return cpmWeight * flowWeight;
+            }
+        } catch (Exception e) {
+            log.error("flowWeight error", e);
+        }
+        return 1;
+    }
 }

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

@@ -5,7 +5,6 @@ import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.ad.engine.commons.helper.CreativeUserLayerDataHelper;
 import com.tzld.piaoquan.ad.engine.commons.helper.DnnCidDataHelper;
-import com.tzld.piaoquan.ad.engine.commons.helper.LayerCustomerFlowControlDataHelper;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
@@ -20,7 +19,6 @@ import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang.math.NumberUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.tuple.Pair;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 import org.xm.Similarity;
@@ -373,19 +371,10 @@ public class RankStrategyBy847 extends RankStrategyBasic {
         double expScale = NumberUtils.toDouble(paramsMap.getOrDefault("expScale", "10.0"));
         int openH5 = NumberUtils.toInt(paramsMap.getOrDefault("openH5", "0"));
 
-        // 控制流量参数
+        // 计算rerank权重
         String layer = reqFeature.get("layer_l4");
-        double cpmPow = NumberUtils.toDouble(paramsMap.getOrDefault("cpmPow", "0.3"));
-        double lowerCpmWeight = NumberUtils.toDouble(paramsMap.getOrDefault("lowerCpmWeight", "1.0"));
-        double upperCpmWeight = NumberUtils.toDouble(paramsMap.getOrDefault("upperCpmWeight", "1.2"));
-        double flowPow = NumberUtils.toDouble(paramsMap.getOrDefault("flowPow", "1.2"));
-        double lowerFlowWeight = NumberUtils.toDouble(paramsMap.getOrDefault("lowerFlowWeight", "0.77"));
-        double upperFlowWeight = NumberUtils.toDouble(paramsMap.getOrDefault("upperFlowWeight", "1.0"));
-
-        double hitViewRate = NumberUtils.toDouble(paramsMap.getOrDefault("hitViewRate", "0.05"));
-        double plusViewRate = NumberUtils.toDouble(paramsMap.getOrDefault("plusViewRate", "20"));
-        double viewRatePow = NumberUtils.toDouble(paramsMap.getOrDefault("viewRatePow", "0.2"));
-        double viewRateScale = NumberUtils.toDouble(paramsMap.getOrDefault("viewRateScale", "0.5491"));
+        calRerankWeight(scoreParam, layer, result);
+
         for (AdRankItem item : result) {
             double bid = item.getCpa();
             if (scoreParam.getExpCodeSet().contains(correctCpaExp1) || scoreParam.getExpCodeSet().contains(correctCpaExp2)) {
@@ -412,11 +401,8 @@ public class RankStrategyBy847 extends RankStrategyBasic {
                     expNewKey, expNewThreshold,
                     expLowerWeight, expUpperWeight, expScale);
 
-            // 分层控制流量
-            double flowWeight = getFlowControlWeight(layer, item,
-                    cpmPow, lowerCpmWeight, upperCpmWeight,
-                    flowPow, lowerFlowWeight, upperFlowWeight,
-                    hitViewRate, plusViewRate, viewRatePow, viewRateScale);
+            // 控制流量权重
+            double flowWeight = item.getScoreMap().getOrDefault("flow", 1.0);
 
             String layerAndCreativeWeightMapKey = getLayerAndCreativeWeightMapKey(peopleLayer, String.valueOf(item.getAdId()));
             // 人群分层&创意的权重
@@ -431,7 +417,6 @@ public class RankStrategyBy847 extends RankStrategyBasic {
             item.getScoreMap().put("cpmCoefficient", cpmCoefficient);
             item.getScoreMap().put("scoreCoefficient", scoreCoefficient);
             item.getScoreMap().put("h5", h5Weight);
-            item.getScoreMap().put("flow", flowWeight);
             item.getFeatureMap().putAll(userFeatureMap);
             item.getFeatureMap().putAll(sceneFeatureMap);
 
@@ -1059,34 +1044,4 @@ public class RankStrategyBy847 extends RankStrategyBasic {
             log.error("calibCidScore error", e);
         }
     }
-
-    private double getFlowControlWeight(String layer, AdRankItem item,
-                                        double cpmPow, double lowerCpmWeight, double upperCpmWeight,
-                                        double flowPow, double lowerFlowWeight, double upperFlowWeight,
-                                        double hitViewRate, double plusViewRate, double viewRatePow, double viewRateScale) {
-        try {
-            String profession = item.getProfession();
-            String customerId = String.valueOf(item.getCustomerId());
-            Pair<Double, Double> pair = LayerCustomerFlowControlDataHelper.getWeight(layer, profession, customerId);
-            if (null != pair) {
-                double cpmWeight = 1;
-                double flowWeight = 1;
-                double cpmDiff = pair.getLeft();
-                double viewRate = pair.getRight();
-                if (cpmDiff > 0) {
-                    cpmWeight = Math.pow(cpmDiff, cpmPow);
-                    cpmWeight = Math.min(Math.max(lowerCpmWeight, cpmWeight), upperCpmWeight);
-                }
-                if (viewRate > hitViewRate) {
-                    flowWeight = 1 / Math.pow(viewRate * 100 + plusViewRate, viewRatePow) / viewRateScale;
-                    flowWeight = Math.pow(flowWeight, flowPow);
-                    flowWeight = Math.min(Math.max(lowerFlowWeight, flowWeight), upperFlowWeight);
-                }
-                return cpmWeight * flowWeight;
-            }
-        } catch (Exception e) {
-            log.error("flowWeight error", e);
-        }
-        return 1;
-    }
 }

+ 8 - 1
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBy849.java

@@ -370,6 +370,10 @@ public class RankStrategyBy849 extends RankStrategyBasic {
         double expUpperWeight = NumberUtils.toDouble(paramsMap.getOrDefault("expUpperWeight", "1.0"));
         double expScale = NumberUtils.toDouble(paramsMap.getOrDefault("expScale", "10.0"));
         int openH5 = NumberUtils.toInt(paramsMap.getOrDefault("openH5", "0"));
+
+        // 计算rerank权重
+        String layer = reqFeature.get("layer_l4");
+        calRerankWeight(scoreParam, layer, result);
         for (AdRankItem item : result) {
             double bid = item.getCpa();
             if (scoreParam.getExpCodeSet().contains(correctCpaExp1) || scoreParam.getExpCodeSet().contains(correctCpaExp2)) {
@@ -396,12 +400,15 @@ public class RankStrategyBy849 extends RankStrategyBasic {
                     expNewKey, expNewThreshold,
                     expLowerWeight, expUpperWeight, expScale);
 
+            // 控制流量权重
+            double flowWeight = item.getScoreMap().getOrDefault("flow", 1.0);
+
             String layerAndCreativeWeightMapKey = getLayerAndCreativeWeightMapKey(peopleLayer, String.valueOf(item.getAdId()));
             // 人群分层&创意的权重
             double layerAndCreativeWeight = getLayerAndCreativeWeight(layerAndCreativeWeightMapKey);
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
             double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(isGuaranteedFlow, item.getExt());
-            double score = h5Weight * expWeight * item.getLrScore() * bid * scoreCoefficient * guaranteeScoreCoefficient * layerAndCreativeWeight;
+            double score = flowWeight * h5Weight * expWeight * item.getLrScore() * bid * scoreCoefficient * guaranteeScoreCoefficient * layerAndCreativeWeight;
             item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());

+ 7 - 1
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/strategy/RankStrategyBy851.java

@@ -379,6 +379,9 @@ public class RankStrategyBy851 extends RankStrategyBasic {
         double expUpperWeight = NumberUtils.toDouble(paramsMap.getOrDefault("expUpperWeight", "1.0"));
         double expScale = NumberUtils.toDouble(paramsMap.getOrDefault("expScale", "10.0"));
         int openH5 = NumberUtils.toInt(paramsMap.getOrDefault("openH5", "0"));
+
+        // 计算rerank权重
+        calRerankWeight(scoreParam, userLayer, result);
         for (AdRankItem item : result) {
             double bid = item.getCpa();
             if (scoreParam.getExpCodeSet().contains(correctCpaExp1) || scoreParam.getExpCodeSet().contains(correctCpaExp2)) {
@@ -405,12 +408,15 @@ public class RankStrategyBy851 extends RankStrategyBasic {
                     expNewKey, expNewThreshold,
                     expLowerWeight, expUpperWeight, expScale);
 
+            // 控制流量权重
+            double flowWeight = item.getScoreMap().getOrDefault("flow", 1.0);
+
             String layerAndCreativeWeightMapKey = getLayerAndCreativeWeightMapKey(peopleLayer, String.valueOf(item.getAdId()));
             // 人群分层&创意的权重
             double layerAndCreativeWeight = getLayerAndCreativeWeight(layerAndCreativeWeightMapKey);
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
             double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(isGuaranteedFlow, item.getExt());
-            double score = h5Weight * expWeight * item.getLrScore() * bid * scoreCoefficient * guaranteeScoreCoefficient * layerAndCreativeWeight;
+            double score = flowWeight * h5Weight * expWeight * item.getLrScore() * bid * scoreCoefficient * guaranteeScoreCoefficient * layerAndCreativeWeight;
             item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());