#251 换源

Обединени
fanjinyang обедини 1 ревизии от algorithm/20260805_feature_fjy_copc във algorithm/master преди 2 дни

+ 83 - 0
ad-engine-commons/src/main/java/com/tzld/piaoquan/ad/engine/commons/helper/CreativeUserLayerDataHelperV3L6.java

@@ -0,0 +1,83 @@
+package com.tzld.piaoquan.ad.engine.commons.helper;
+
+import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+@Slf4j
+@Component
+public class CreativeUserLayerDataHelperV3L6 {
+    @Autowired
+    protected AlgorithmRedisHelper algRedisHelper;
+
+    private static final String redisKey = "ad:engine:strategy:clib:v3:l6:cid_layer";
+    private static final String keyFormat = "%s:%s:%s";
+
+    private volatile static Map<String, Double> cidMap = Collections.emptyMap();
+
+    // 服务启动时初始化数据
+    @PostConstruct
+    public void init() {
+        Map<String, Double> map = updateCidMap();
+        cidMap = Collections.unmodifiableMap(map);
+    }
+
+    // 每5分钟更新一次数据
+    @Scheduled(fixedRate = 5 * 60 * 1000)
+    public void scheduledUpdate() {
+        Map<String, Double> map = updateCidMap();
+        cidMap = Collections.unmodifiableMap(map);
+    }
+
+    public static Double getCopc(String model, String cid, String layer) {
+
+        if (null != cidMap) {
+            String key = String.format(keyFormat, model, cid, layer);
+            if (cidMap.containsKey(key)) {
+                if (cidMap.get(key) != null && cidMap.get(key) > 0) {
+                    return cidMap.get(key);
+                }
+            }
+            key = String.format(keyFormat, model, cid, "sum");
+            if (cidMap.containsKey(key)) {
+                if (cidMap.get(key) != null && cidMap.get(key) > 0) {
+                    return cidMap.get(key);
+                }
+            }
+            key = String.format(keyFormat, model,"sum",layer);
+            if (cidMap.containsKey(key)) {
+                if (cidMap.get(key) != null && cidMap.get(key) > 0) {
+                    return cidMap.get(key);
+                }
+            }
+        }
+        return null;
+    }
+
+    private Map<String, Double> updateCidMap() {
+        Map<String, Double> tmpCidSMap = new HashMap<>();
+        try {
+            String value = algRedisHelper.get(redisKey);
+            if (null != value && !value.isEmpty()) {
+                String[] cells = value.split(",");
+                for (String cell : cells) {
+                    String[] pair = cell.split("_");
+                    if (2 == pair.length) {
+                        tmpCidSMap.put(pair[0], Double.parseDouble(pair[1]));
+                    }
+                }
+            }
+            log.info("CreativeUserLayerDataHelperV2L6 update cid layer copc success size={}", tmpCidSMap.size());
+        } catch (Exception e) {
+            log.error("CreativeUserLayerDataHelperV2L6 update cid layer copc error", e);
+        }
+        return tmpCidSMap;
+    }
+}

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

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
 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.CreativeUserLayerDataHelperV2L6;
+import com.tzld.piaoquan.ad.engine.commons.helper.CreativeUserLayerDataHelperV3L6;
 import com.tzld.piaoquan.ad.engine.commons.helper.DnnCidDataHelper;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
@@ -54,6 +55,9 @@ public class RankStrategyBy892 extends RankStrategyBasic {
     @Value("${word2vec.exp:694}")
     private String word2vecExp;
 
+    @Value("${use.copc.l6.v3:0}")
+    private Integer useCopcL6V3;
+
 
     @ApolloJsonValue("${rank.score.params.892:{}}")
     private Map<String, String> paramsMap;
@@ -1145,7 +1149,12 @@ public class RankStrategyBy892 extends RankStrategyBasic {
             String layer = reqFeature.get("layer_l6");
             for (AdRankItem item : items) {
                 String cid = String.valueOf(item.getAdId());
-                Double diff = CreativeUserLayerDataHelperV2L6.getCopc(modelName, cid, layer);
+                Double diff = null;
+                if (useCopcL6V3 != null && useCopcL6V3 == 1) {
+                    diff = CreativeUserLayerDataHelperV3L6.getCopc(modelName, cid, layer);
+                }else {
+                    diff = CreativeUserLayerDataHelperV2L6.getCopc(modelName, cid, layer);
+                }
                 item.getScoreMap().put("cidCtcvrCoefficient", 1.0);
                 if (null != diff) {
                     double newDiff = Math.min(Math.max(minVal, diff), maxVal);

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

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
 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.CreativeUserLayerDataHelperV2L6;
+import com.tzld.piaoquan.ad.engine.commons.helper.CreativeUserLayerDataHelperV3L6;
 import com.tzld.piaoquan.ad.engine.commons.helper.DnnCidDataHelper;
 import com.tzld.piaoquan.ad.engine.commons.param.RankRecommendRequestParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
@@ -54,6 +55,9 @@ public class RankStrategyBy893 extends RankStrategyBasic {
     @Value("${word2vec.exp:694}")
     private String word2vecExp;
 
+    @Value("${use.copc.l6.v3:0}")
+    private Integer useCopcL6V3;
+
 
     @ApolloJsonValue("${rank.score.params.893:{}}")
     private Map<String, String> paramsMap;
@@ -1131,7 +1135,12 @@ public class RankStrategyBy893 extends RankStrategyBasic {
             String layer = reqFeature.get("layer_l6");
             for (AdRankItem item : items) {
                 String cid = String.valueOf(item.getAdId());
-                Double diff = CreativeUserLayerDataHelperV2L6.getCopc(modelName, cid, layer);
+                Double diff = null;
+                if (useCopcL6V3 != null && useCopcL6V3 == 1) {
+                    diff = CreativeUserLayerDataHelperV3L6.getCopc(modelName, cid, layer);
+                }else {
+                    diff = CreativeUserLayerDataHelperV2L6.getCopc(modelName, cid, layer);
+                }
                 item.getScoreMap().put("cidCtcvrCoefficient", 1.0);
                 if (null != diff) {
                     double newDiff = Math.min(Math.max(minVal, diff), maxVal);