Browse Source

Merge branch 'refs/heads/dev-xym-update-guarantee' into pre-master

xueyiming 4 days ago
parent
commit
907a2bda67

+ 8 - 3
ad-engine-server/src/main/java/com/tzld/piaoquan/ad/engine/server/controller/AdRecommendController.java

@@ -1,6 +1,7 @@
 package com.tzld.piaoquan.ad.engine.server.controller;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.TypeReference;
 import com.tzld.piaoquan.ad.engine.commons.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.ad.engine.commons.enums.GuaranteedTypeEnum;
@@ -20,9 +21,7 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
 
 @RestController
 @RequestMapping("/recommend")
@@ -70,6 +69,12 @@ public class AdRecommendController {
                 } else {
                     contentMap.put("type", GuaranteedTypeEnum.NOT_GUARANTEED.getType());
                 }
+                List<String> participateCompetitionType = new ArrayList<>();
+                participateCompetitionType.add("engine");
+                if (rankResult.getExt().containsKey("isGuaranteeType") && (boolean) rankResult.getExt().get("isGuaranteeType")) {
+                    participateCompetitionType.add("guarantee");
+                }
+                contentMap.put("participateCompetitionType", participateCompetitionType);
                 map.put("content", contentMap);
                 return map;
             }

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

@@ -82,6 +82,12 @@ public abstract class RankStrategyBasic implements RankStrategy {
     @Value("${calibration.alpha:0.5}")
     protected Double calibrationAlpha;
 
+    @Value("${guarantee.weight:30}")
+    protected Integer guaranteeWeight;
+
+    @Value("${guarantee.switching.time:1753286400000}")
+    protected Long guaranteeSwitchingTime;
+
     @Autowired
     private FeatureService featureService;
     @Autowired
@@ -265,10 +271,19 @@ public abstract class RankStrategyBasic implements RankStrategy {
     }
 
 
-    protected Map<String, GuaranteeView> getGuaranteeViewMap(RankRecommendRequestParam request, ScoreParam scoreParam) {
+    protected boolean getIsGuaranteedFlow(ScoreParam scoreParam) {
+        if (System.currentTimeMillis() < guaranteeSwitchingTime) {
+            return scoreParam.getExpCodeSet().contains(guaranteeExp);
+        }
+        Random random = new Random();
+        int i = random.nextInt(100);
+        return i < guaranteeWeight;
+    }
+
+    protected Map<String, GuaranteeView> getGuaranteeViewMap(RankRecommendRequestParam request, boolean isGuaranteedFlow) {
         Map<String, GuaranteeView> map = new HashMap<>();
         try {
-            if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+            if (isGuaranteedFlow) {
                 String thatDayDateString = DateUtils.getThatDayDateString();
                 String redisKey = adPlatformGuaranteeKey.replace("{date}", thatDayDateString);
                 List<String> adVerIds = request.getAdIdList().stream().map(AdPlatformCreativeDTO::getAdVerId).distinct()
@@ -292,8 +307,8 @@ public abstract class RankStrategyBasic implements RankStrategy {
         return map;
     }
 
-    protected void setGuaranteeWeight(Map<String, GuaranteeView> map, String adVerId, Map<String, Object> ext) {
-        if (MapUtils.isNotEmpty(map)) {
+    protected void setGuaranteeWeight(Map<String, GuaranteeView> map, String adVerId, Map<String, Object> ext, boolean isGuaranteedFlow) {
+        if (isGuaranteedFlow && MapUtils.isNotEmpty(map)) {
             GuaranteeView guaranteeView = map.get(adVerId);
             if (guaranteeView != null) {
                 double guaranteeWeight = calculateGuaranteedWeight(guaranteeView);
@@ -301,6 +316,7 @@ public abstract class RankStrategyBasic implements RankStrategy {
                 ext.put("guaranteeView", guaranteeView.toString());
                 ext.put("guaranteeWeight", guaranteeWeight);
                 ext.put("isGuaranteed", isGuaranteed);
+                ext.put("isGuaranteedFlow", isGuaranteedFlow);
             }
         }
     }
@@ -343,8 +359,8 @@ public abstract class RankStrategyBasic implements RankStrategy {
                 && guaranteeView.getGuaranteeRate() != null && guaranteeView.getGuaranteeRate() != 0.0;
     }
 
-    protected double getGuaranteeScoreCoefficient(ScoreParam scoreParam, Map<String, Object> ext) {
-        if (scoreParam.getExpCodeSet().contains(guaranteeExp)) {
+    protected double getGuaranteeScoreCoefficient(boolean isGuaranteedFlow, Map<String, Object> ext) {
+        if (isGuaranteedFlow) {
             if (ext.get("guaranteeWeight") == null) {
                 return 1.0;
             } else {

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

@@ -97,7 +97,8 @@ public class RankStrategyBy679 extends RankStrategyBasic {
 
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
-        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, scoreParam);
+        boolean isGuaranteedFlow = getIsGuaranteedFlow(scoreParam);
+        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, isGuaranteedFlow);
         Map<Long, CorrectCpaParam> correctCpaMap = getCorrectCpaParamMap(request, scoreParam);
         List<AdRankItem> adRankItems = new ArrayList<>();
         Random random = new Random();
@@ -126,7 +127,7 @@ public class RankStrategyBy679 extends RankStrategyBasic {
                     adRankItem.getExt().put("recallsources", dto.getRecallSources());
                     adRankItem.getExt().put("correctCpaMap", JSONObject.toJSONString(correctCpaMap.get(dto.getCreativeId())));
                     adRankItem.getExt().put("correctionFactor", correctCpaMap.get(dto.getCreativeId()).getCorrectionFactor());
-                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt());
+                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt(), isGuaranteedFlow);
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -224,6 +225,7 @@ public class RankStrategyBy679 extends RankStrategyBasic {
         // loop
         double cpmCoefficient = weightParam.getOrDefault("cpmCoefficient", 0.9);
 
+        boolean isGuaranteeType = false;
         for (AdRankItem item : result) {
             double bid = item.getCpa();
             if (scoreParam.getExpCodeSet().contains(correctCpaExp1) || scoreParam.getExpCodeSet().contains(correctCpaExp2)) {
@@ -232,8 +234,11 @@ public class RankStrategyBy679 extends RankStrategyBasic {
                 bid = bid * correctionFactor;
             }
             item.getScoreMap().put("ecpm", item.getLrScore() * bid * 1000);
+            if (isGuaranteedFlow && item.getExt().get("isGuaranteed") != null && (boolean) item.getExt().get("isGuaranteed")) {
+                isGuaranteeType = true;
+            }
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(scoreParam, item.getExt());
+            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(isGuaranteedFlow, item.getExt());
             double score = item.getLrScore() * bid * scoreCoefficient * guaranteeScoreCoefficient;
             item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
@@ -259,6 +264,7 @@ public class RankStrategyBy679 extends RankStrategyBasic {
 
         if (CollectionUtils.isNotEmpty(result)) {
             AdRankItem top1Item = result.get(0);
+            top1Item.getExt().put("isGuaranteeType", isGuaranteeType);
             putMetaFeature(top1Item, feature, reqFeature, sceneFeatureMap, request);
         }
         return result;

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

@@ -109,7 +109,8 @@ public class RankStrategyBy680 extends RankStrategyBasic {
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
 
-        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, scoreParam);
+        boolean isGuaranteedFlow = getIsGuaranteedFlow(scoreParam);
+        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, isGuaranteedFlow);
         Map<Long, CorrectCpaParam> correctCpaMap = getCorrectCpaParamMap(request, scoreParam);
         List<AdRankItem> adRankItems = new ArrayList<>();
         Random random = new Random();
@@ -139,7 +140,7 @@ public class RankStrategyBy680 extends RankStrategyBasic {
                     adRankItem.getExt().put("recallsources", dto.getRecallSources());
                     adRankItem.getExt().put("correctCpaMap", JSONObject.toJSONString(correctCpaMap.get(dto.getCreativeId())));
                     adRankItem.getExt().put("correctionFactor", correctCpaMap.get(dto.getCreativeId()).getCorrectionFactor());
-                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt());
+                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt(), isGuaranteedFlow);
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -245,6 +246,7 @@ public class RankStrategyBy680 extends RankStrategyBasic {
         // loop
         double cpmCoefficient = weightParam.getOrDefault("cpmCoefficient", 0.9);
 
+        boolean isGuaranteeType = false;
         for (AdRankItem item : result) {
             double bid = item.getCpa();
             if (scoreParam.getExpCodeSet().contains(correctCpaExp1) || scoreParam.getExpCodeSet().contains(correctCpaExp2)) {
@@ -253,8 +255,11 @@ public class RankStrategyBy680 extends RankStrategyBasic {
                 bid = bid * correctionFactor;
             }
             item.getScoreMap().put("ecpm", item.getLrScore() * bid * 1000);
+            if (isGuaranteedFlow && item.getExt().get("isGuaranteed") != null && (boolean) item.getExt().get("isGuaranteed")) {
+                isGuaranteeType = true;
+            }
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(scoreParam, item.getExt());
+            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(isGuaranteedFlow, item.getExt());
             double score = item.getLrScore() * bid * scoreCoefficient * guaranteeScoreCoefficient;
             item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
@@ -274,6 +279,7 @@ public class RankStrategyBy680 extends RankStrategyBasic {
 
         if (CollectionUtils.isNotEmpty(result)) {
             AdRankItem top1Item = result.get(0);
+            top1Item.getExt().put("isGuaranteeType", isGuaranteeType);
             putMetaFeature(top1Item, feature, reqFeature, sceneFeatureMap, request);
         }
         long time6 = System.currentTimeMillis();

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

@@ -106,7 +106,8 @@ public class RankStrategyBy683 extends RankStrategyBasic {
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
 
-        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, scoreParam);
+        boolean isGuaranteedFlow = getIsGuaranteedFlow(scoreParam);
+        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, isGuaranteedFlow);
         Map<Long, CorrectCpaParam> correctCpaMap = getCorrectCpaParamMap(request, scoreParam);
         List<AdRankItem> adRankItems = new ArrayList<>();
         Random random = new Random();
@@ -136,7 +137,7 @@ public class RankStrategyBy683 extends RankStrategyBasic {
                     adRankItem.getExt().put("recallsources", dto.getRecallSources());
                     adRankItem.getExt().put("correctCpaMap", JSONObject.toJSONString(correctCpaMap.get(dto.getCreativeId())));
                     adRankItem.getExt().put("correctionFactor", correctCpaMap.get(dto.getCreativeId()).getCorrectionFactor());
-                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt());
+                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt(), isGuaranteedFlow);
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -241,8 +242,11 @@ public class RankStrategyBy683 extends RankStrategyBasic {
         calculateCtcvrScore(result, request, scoreParam);
         // loop
         double cpmCoefficient = weightParam.getOrDefault("cpmCoefficient", 0.9);
-
+        boolean isGuaranteeType = false;
         for (AdRankItem item : result) {
+            if (isGuaranteedFlow && item.getExt().get("isGuaranteed") != null && (boolean) item.getExt().get("isGuaranteed")) {
+                isGuaranteeType = true;
+            }
             double bid = item.getCpa();
             if (scoreParam.getExpCodeSet().contains(correctCpaExp1) || scoreParam.getExpCodeSet().contains(correctCpaExp2)) {
                 Double correctionFactor = (Double) item.getExt().get("correctionFactor");
@@ -251,7 +255,7 @@ public class RankStrategyBy683 extends RankStrategyBasic {
             }
             item.getScoreMap().put("ecpm", item.getLrScore() * bid * 1000);
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(scoreParam, item.getExt());
+            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(isGuaranteedFlow, item.getExt());
             double score = item.getLrScore() * bid * scoreCoefficient * guaranteeScoreCoefficient;
             item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
@@ -273,6 +277,7 @@ public class RankStrategyBy683 extends RankStrategyBasic {
 
         if (CollectionUtils.isNotEmpty(result)) {
             AdRankItem top1Item = result.get(0);
+            top1Item.getExt().put("isGuaranteeType", isGuaranteeType);
             putMetaFeature(top1Item, feature, reqFeature, sceneFeatureMap, request);
         }
         long time6 = System.currentTimeMillis();

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

@@ -142,7 +142,8 @@ public class RankStrategyBy688 extends RankStrategyBasic {
         Map<String, String> sceneFeatureMap = this.handleSceneFeature(ts);
         long time1 = System.currentTimeMillis();
 
-        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, scoreParam);
+        boolean isGuaranteedFlow = getIsGuaranteedFlow(scoreParam);
+        Map<String, GuaranteeView> map = getGuaranteeViewMap(request, isGuaranteedFlow);
         Map<Long, CorrectCpaParam> correctCpaMap = getCorrectCpaParamMap(request, scoreParam);
         List<AdRankItem> adRankItems = new ArrayList<>();
         Random random = new Random();
@@ -171,7 +172,7 @@ public class RankStrategyBy688 extends RankStrategyBasic {
                     adRankItem.getExt().put("recallsources", dto.getRecallSources());
                     adRankItem.getExt().put("correctCpaMap", JSONObject.toJSONString(correctCpaMap.get(dto.getCreativeId())));
                     adRankItem.getExt().put("correctionFactor", correctCpaMap.get(dto.getCreativeId()).getCorrectionFactor());
-                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt());
+                    setGuaranteeWeight(map, dto.getAdVerId(), adRankItem.getExt(), isGuaranteedFlow);
                     String cidStr = dto.getCreativeId().toString();
                     Map<String, String> cidFeatureMap = adRankItem.getFeatureMap();
                     Map<String, Map<String, String>> cidFeature = allCidFeature.getOrDefault(cidStr, new HashMap<>());
@@ -296,7 +297,7 @@ public class RankStrategyBy688 extends RankStrategyBasic {
         calculateCtcvrScore(result, request, scoreParam);
         // loop
         double cpmCoefficient = weightParam.getOrDefault("cpmCoefficient", 0.9);
-
+        boolean isGuaranteeType = false;
         for (AdRankItem item : result) {
             double bid = item.getCpa();
             if (scoreParam.getExpCodeSet().contains(correctCpaExp1) || scoreParam.getExpCodeSet().contains(correctCpaExp2)) {
@@ -305,21 +306,29 @@ public class RankStrategyBy688 extends RankStrategyBasic {
                 bid = bid * correctionFactor;
             }
             item.getScoreMap().put("ecpm", item.getLrScore() * bid * 1000);
+            if (isGuaranteedFlow && item.getExt().get("isGuaranteed") != null && (boolean) item.getExt().get("isGuaranteed")) {
+                isGuaranteeType = true;
+            }
             double scoreCoefficient = creativeScoreCoefficient.getOrDefault(item.getAdId(), 1d);
-            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(scoreParam, item.getExt());
+            double guaranteeScoreCoefficient = getGuaranteeScoreCoefficient(isGuaranteedFlow, item.getExt());
             double score = item.getLrScore() * bid * scoreCoefficient * guaranteeScoreCoefficient;
             item.getScoreMap().put("guaranteeScoreCoefficient", guaranteeScoreCoefficient);
             item.getScoreMap().put("cpa", item.getCpa());
             item.getScoreMap().put("cpm", item.getCpm());
-            item.getScoreMap().put("bid", bid);
             item.getScoreMap().put("cpmCoefficient", cpmCoefficient);
             item.getScoreMap().put("scoreCoefficient", scoreCoefficient);
             item.getFeatureMap().putAll(userFeatureMap);
             item.getFeatureMap().putAll(sceneFeatureMap);
+
             // 没有转化回传的广告主,使用后台配置的CPM
             if (noApiAdVerIds.contains(item.getAdVerId())) {
                 score = item.getCpm() * cpmCoefficient / 1000;
             }
+            if (scoreParam.getExpCodeSet().contains(correctCpaExp1) || scoreParam.getExpCodeSet().contains(correctCpaExp2)) {
+                Double correctionFactor = (Double) item.getExt().get("correctionFactor");
+                item.getScoreMap().put("correctionFactor", correctionFactor);
+                score = score * correctionFactor;
+            }
             item.setScore(score);
         }
 
@@ -328,6 +337,7 @@ public class RankStrategyBy688 extends RankStrategyBasic {
 
         if (CollectionUtils.isNotEmpty(result)) {
             AdRankItem top1Item = result.get(0);
+            top1Item.getExt().put("isGuaranteeType", isGuaranteeType);
             putMetaFeature(top1Item, feature, reqFeature, sceneFeatureMap, request);
         }
         long time6 = System.currentTimeMillis();