浏览代码

Merge branch 'feature_20240605_zhaohaipeng_ad_filter_exp' into pre-master

zhaohaipeng 1 年之前
父节点
当前提交
2f0dde0dbc

+ 116 - 47
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/VideoAdThompsonScorerV2.java

@@ -37,7 +37,7 @@ public class VideoAdThompsonScorerV2 {
     private Map<String, Double> exp665Param = new HashMap<>();
     private Map<String, Double> exp666Param = new HashMap<>();
     private Map<String, Double> exp669Param = new HashMap<>();
-    private Map<String, Double> exp670Param = new HashMap<>();
+    private Map<String, String> exp670Param = new HashMap<>();
     private Map<String, String> exp672Param = new HashMap<>();
 
     Random random = new Random();
@@ -101,8 +101,10 @@ public class VideoAdThompsonScorerV2 {
             // 日志上报扩展参数
             Map<String, Object> ext = new HashMap<>();
             double score = 0.0;
+            double cpa = 0.0;
             try {
                 CreativeStatistic cidStatistics = creativeStatisticsMap.getOrDefault(dto.getCreativeId(), new CreativeStatistic());
+                cpa = cidStatistics.parseCpaToDouble();
 
                 double order;
                 double exp;
@@ -116,31 +118,27 @@ public class VideoAdThompsonScorerV2 {
                     s1 = a / b;
                 }
 
-                score = s1 * dto.getCpa() * dto.getBid1() * dto.getBid2();
+                score = s1 * cpa;
 
                 ext.put("alpha", alpha);
                 ext.put("beta", beta);
                 ext.put("order", order);
                 ext.put("exp", exp);
                 ext.put("s1", s1);
-                ext.put("bid1", dto.getBid1());
-                ext.put("bid2", dto.getBid2());
                 ext.put("pairOrSingle", "single");
                 ext.put("cidStatistic", JSON.toJSONString(cidStatistics));
-                ext.put("cpa", dto.getCpa());
+                ext.put("cpa", cpa);
                 ext.put("abCode", 663);
             } catch (Exception e) {
                 log.error("svc=663exp, error: ", e);
             }
             AdRankItem item = new AdRankItem();
-            item.setCpa(dto.getCpa());
+            item.setCpa(cpa);
             item.setAdId(dto.getCreativeId());
             item.setScore(score);
             item.setExt(ext);
             item.setVideoId(param.getVideoId());
             item.setScore_type(663);
-            item.setBid1(dto.getBid1());
-            item.setBid2(dto.getBid2());
             item.setWeight(dto.getWeight());
             item.setCreativeCode(dto.getCreativeCode());
             result.add(item);
@@ -337,6 +335,7 @@ public class VideoAdThompsonScorerV2 {
             // 日志上报扩展参数
             Map<String, Object> ext = new HashMap<>();
             double score = 0.0;
+            double cpa = cidStatistics.parseCpaToDouble();
             try {
 
 
@@ -351,7 +350,7 @@ public class VideoAdThompsonScorerV2 {
                 }
 
                 double s1 = this.betaSampler((alpha + order), ((beta + exp) / betaK));
-                score = s1 * dto.getCpa() * dto.getBid1() * dto.getBid2();
+                score = s1 * cpa;
 
 
                 ext.put("alpha", alpha);
@@ -360,26 +359,22 @@ public class VideoAdThompsonScorerV2 {
                 ext.put("order", order);
                 ext.put("exp", exp);
                 ext.put("s1", s1);
-                ext.put("bid1", dto.getBid1());
-                ext.put("bid2", dto.getBid2());
                 ext.put("viewThreshold", viewThreshold);
                 ext.put("pairOrSingle", pairOrSingle);
                 ext.put("cidStatistic", JSON.toJSONString(cidStatistics));
                 ext.put("vidCidStatistic", JSON.toJSONString(vidCidStatistics));
-                ext.put("cpa", dto.getCpa());
+                ext.put("cpa", cpa);
                 ext.put("abCode", 666);
             } catch (Exception e) {
                 log.error("svc=666exp, error: ", e);
             }
             AdRankItem item = new AdRankItem();
-            item.setCpa(dto.getCpa());
+            item.setCpa(cpa);
             item.setAdId(dto.getCreativeId());
             item.setScore(score);
             item.setExt(ext);
             item.setVideoId(param.getVideoId());
             item.setScore_type(666);
-            item.setBid2(dto.getBid2());
-            item.setBid1(dto.getBid1());
             item.setWeight(dto.getWeight());
             item.setCreativeCode(dto.getCreativeCode());
             result.add(item);
@@ -411,21 +406,87 @@ public class VideoAdThompsonScorerV2 {
     }
 
     public List<AdRankItem> thompsonScorerByExp670(ScoreParam param, List<AdPlatformCreativeDTO> adIdList) {
-        Map<Long, CreativeStatistic> creativeStatisticsMap = this.batchFindCreativeRedisCache(redisCidActionKeyV1, adIdList);
-        Map<Long, CreativeStatistic> videoCreativeStatisticsMap = this.batchFindCreativeRedisCache(redisCidVidActionKeyV1 + param.getVideoId() + "_", adIdList);
+        Map<Long, CreativeStatistic> cidMap = this.batchFindCreativeRedisCache(redisCidActionKeyV1, adIdList);
+        Map<Long, CreativeStatistic> vidCidMap = this.batchFindCreativeRedisCache(redisCidVidActionKeyV1 + param.getVideoId() + "_", adIdList);
+
+        double w1 = Double.parseDouble(exp670Param.getOrDefault("w1", "1"));
+        double w2 = Double.parseDouble(exp670Param.getOrDefault("w2", "1"));
+        String scoreCalcWay = exp670Param.getOrDefault("scoreCalcWay", "statistic");
+
+        double alpha = Double.parseDouble(exp670Param.getOrDefault("alpha", "0"));
+        double cidBeta = Double.parseDouble(exp670Param.getOrDefault("cidBeta", "0"));
+        double vidCidBeta = Double.parseDouble(exp670Param.getOrDefault("vidCidBeta", "0"));
+
 
         List<AdRankItem> result = new ArrayList<>(adIdList.size());
-        this.calcScore(result, adIdList, 0.0, 0.0, 0.0, creativeStatisticsMap, videoCreativeStatisticsMap, exp670Param);
-        result.sort(equalsRandomComparator());
 
-        for (AdRankItem adRankItem : result) {
-            adRankItem.setVideoId(param.getVideoId());
-            adRankItem.setScore_type(670);
-            if (MapUtils.isNotEmpty(adRankItem.getExt())) {
-                adRankItem.getExt().put("abCode", 670);
+        for (AdPlatformCreativeDTO dto : adIdList) {
+
+            // 日志上报扩展参数
+            Map<String, Object> ext = new HashMap<>();
+            double score = 0;
+            double cpa = 0.0;
+            try {
+
+                CreativeStatistic cidStatistic = cidMap.getOrDefault(dto.getCreativeId(), new CreativeStatistic());
+                CreativeStatistic vidCidStatistic = vidCidMap.getOrDefault(dto.getCreativeId(), new CreativeStatistic());
+
+                cpa = cidStatistic.parseCpaToDouble();
+                double cidExp = cidStatistic.parseExpToDouble();
+                double vidCidExp = vidCidStatistic.parseExpToDouble();
+
+                double cidScore = 0.0, vidCidScore = 0.0;
+
+                if ("thompson".equals(scoreCalcWay)) {
+                    cidScore = this.betaSampler(cidStatistic.parseOrderToDouble() + alpha, cidStatistic.parseExpToDouble() + cidBeta);
+                    vidCidScore = this.betaSampler(vidCidStatistic.parseOrderToDouble() + alpha, vidCidStatistic.parseExpToDouble() + vidCidBeta);
+                } else {
+                    cidScore = this.orderDivideExp(cidStatistic);
+                    vidCidScore = this.orderDivideExp(vidCidStatistic);
+                }
+
+
+                if (w1 != 0 || w2 != 0) {
+                    score = ((w1 * vidCidScore * vidCidExp + w2 * cidScore * cidExp) / (w1 * vidCidExp + w2 * cidExp)) * cpa;
+                }
+
+                ext.put("w1", w1);
+                ext.put("w2", w2);
+                ext.put("cidScore", cidScore);
+                ext.put("vidCidScore", vidCidScore);
+                ext.put("cidA", alpha);
+                ext.put("vidCidA", alpha);
+                ext.put("cidB", cidBeta);
+                ext.put("vidCidB", vidCidBeta);
+                ext.put("cidExp", cidExp);
+                ext.put("vidCidExp", vidCidExp);
+                ext.put("bid1", dto.getBid1());
+                ext.put("bid2", dto.getBid2());
+                ext.put("cidStatistic", JSON.toJSONString(cidStatistic));
+                ext.put("vidCidStatistic", JSON.toJSONString(vidCidStatistic));
+                ext.put("scoreCalcWay", scoreCalcWay);
+                ext.put("cpa", cpa);
+                ext.put("abCode", 670);
+            } catch (Exception e) {
+                log.error("svc=670exp, error: ", e);
             }
+
+            AdRankItem item = new AdRankItem();
+            item.setCpa(cpa);
+            item.setAdId(dto.getCreativeId());
+            item.setWeight(dto.getWeight());
+            item.setScore(score);
+            item.setExt(ext);
+            item.setBid1(dto.getBid1());
+            item.setBid2(dto.getBid2());
+            item.setCreativeCode(dto.getCreativeCode());
+            item.setScore_type(670);
+            item.setVideoId(param.getVideoId());
+            result.add(item);
+
         }
 
+        result.sort(equalsRandomComparator());
         return result;
     }
 
@@ -444,9 +505,11 @@ public class VideoAdThompsonScorerV2 {
             double w1 = expParam.getOrDefault("w1", 1d);
             double w2 = expParam.getOrDefault("w2", 1d);
 
+            double cpa = cidStatistic.parseCpaToDouble();
+
             double score = 0;
             if (w1 != 0 || w2 != 0) {
-                score = ((w1 * vidCidScore * vidCidExp + w2 * cidScore * cidExp) / (w1 + w2)) * dto.getCpa();
+                score = ((w1 * vidCidScore * vidCidExp + w2 * cidScore * cidExp) / (w1 * vidCidExp + w2 * cidExp)) * cpa;
             }
 
 
@@ -466,13 +529,13 @@ public class VideoAdThompsonScorerV2 {
             ext.put("bid2", dto.getBid2());
             ext.put("cidStatistic", JSON.toJSONString(cidStatistic));
             ext.put("vidCidStatistic", JSON.toJSONString(vidCidStatistic));
-            ext.put("cpa", dto.getCpa());
+            ext.put("cpa", cpa);
 
             log.info("svc=699And670Score, score: {}, param: {}", score, JSON.toJSONString(ext));
 
 
             AdRankItem item = new AdRankItem();
-            item.setCpa(dto.getCpa());
+            item.setCpa(cpa);
             item.setAdId(dto.getCreativeId());
             item.setWeight(dto.getWeight());
             item.setScore(score);
@@ -482,6 +545,7 @@ public class VideoAdThompsonScorerV2 {
             item.setCreativeCode(dto.getCreativeCode());
 
             result.add(item);
+
         }
     }
 
@@ -504,6 +568,15 @@ public class VideoAdThompsonScorerV2 {
         return resultMap;
     }
 
+    private double orderDivideExp(CreativeStatistic statistic) {
+        double order = statistic.parseOrderToDouble();
+        double exp = statistic.parseExpToDouble();
+        if (exp != 0) {
+            return order / exp;
+        }
+        return 0.0;
+    }
+
     private double calcThompsonScore(Map<String, Double> expParam, CreativeStatistic creativeStatistic, Double defaultAlpha, Double defaultBeta) {
         Double alpha = expParam.getOrDefault("alpha", defaultAlpha);
         Double beta = expParam.getOrDefault("beta", defaultBeta);
@@ -557,38 +630,34 @@ public class VideoAdThompsonScorerV2 {
     }
 
     public List<AdRankItem> thompsonScorerByExp672(ScoreParam param, List<AdPlatformCreativeDTO> adIdList) {
-        String cidRedisKey = exp672Param.getOrDefault("cidActionRedisKey", redisCidActionKeyV1);
-        String vidCidRedisKey = exp672Param.getOrDefault("vidCidActionRedisKey", redisCidVidActionKeyV1);
+        String cidRedisKey = exp672Param.getOrDefault("cidActionRedisKey", redisCreativeStatisticsPrefix);
+        String vidCidRedisKey = exp672Param.getOrDefault("vidCidActionRedisKey", redisVideoCreativeStatisticsPrefix);
+        double viewThreshold = Double.parseDouble(exp672Param.getOrDefault("viewThreshold", "7000"));
 
+        // RedisKey前缀判断
+        if (!StringUtils.equals(redisCidActionKeyV1, cidRedisKey) && !StringUtils.equals(redisCreativeStatisticsPrefix, cidRedisKey)) {
+            cidRedisKey = redisCreativeStatisticsPrefix;
+        }
+        if (!StringUtils.equals(redisCidVidActionKeyV1, vidCidRedisKey) && !StringUtils.equals(redisVideoCreativeStatisticsPrefix, vidCidRedisKey)) {
+            vidCidRedisKey = redisVideoCreativeStatisticsPrefix;
+        }
 
         Map<Long, CreativeStatistic> paramCidRedisCache = this.batchFindCreativeRedisCache(cidRedisKey, adIdList);
-        Map<Long, CreativeStatistic> paramVidCidRedisCache = this.batchFindCreativeRedisCache(vidCidRedisKey, adIdList);
-
-        // 兜底逻辑
-        if (MapUtils.isEmpty(paramCidRedisCache) && MapUtils.isEmpty(paramVidCidRedisCache)) {
-
-            Map<Long, CreativeStatistic> defaultCidCache = this.batchFindCreativeRedisCache(redisCidActionKeyV1, adIdList);
-            Map<Long, CreativeStatistic> defaultVidCidCache = this.batchFindCreativeRedisCache(redisCidVidActionKeyV1, adIdList);
+        Map<Long, CreativeStatistic> paramVidCidRedisCache = this.batchFindCreativeRedisCache(vidCidRedisKey + param.getVideoId() + "_", adIdList);
 
-            for (AdPlatformCreativeDTO dto : adIdList) {
-                Long cid = dto.getCreativeId();
-                paramCidRedisCache.putIfAbsent(cid, defaultCidCache.getOrDefault(cid, new CreativeStatistic()));
-                paramVidCidRedisCache.putIfAbsent(cid, defaultVidCidCache.getOrDefault(cid, new CreativeStatistic()));
-            }
-        }
-
-        double viewThreshold = Double.parseDouble(exp672Param.getOrDefault("viewThreshold", "7000"));
         List<AdRankItem> result = new ArrayList<>(adIdList.size());
         for (AdPlatformCreativeDTO dto : adIdList) {
             Map<String, Object> ext = new HashMap<>();
             double score = 0.0;
-            double cpa = dto.getCpa();
+            double cpa = 0.0;
             Long cid = dto.getCreativeId();
             try {
                 String pairOrSingle = "pair";
                 CreativeStatistic vidCidStatistic = paramVidCidRedisCache.getOrDefault(cid, new CreativeStatistic());
                 CreativeStatistic cidStatistic = paramCidRedisCache.getOrDefault(cid, new CreativeStatistic());
 
+                cpa = cidStatistic.parseCpaToDouble();
+
                 double order = vidCidStatistic.parseOrderToDouble();
                 double exp = vidCidStatistic.parseExpToDouble();
 
@@ -598,7 +667,7 @@ public class VideoAdThompsonScorerV2 {
                     exp = cidStatistic.parseExpToDouble();
                 }
 
-                if (exp > 0 && order > 0 && cpa > 0) {
+                if (exp != 0) {
                     score = (order / exp) * cpa;
                 }
 
@@ -611,7 +680,7 @@ public class VideoAdThompsonScorerV2 {
                 ext.put("cpa", cpa);
                 ext.put("abCode", 672);
             } catch (Exception e) {
-                log.error("svc=666exp, error: ", e);
+                log.error("svc=672exp, error: ", e);
             }
 
             AdRankItem item = new AdRankItem();