Browse Source

823实验

wangyunpeng 11 hours ago
parent
commit
b884a0fde4

+ 22 - 14
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/impl/PredictModelServiceImpl.java

@@ -3,6 +3,7 @@ package com.tzld.piaoquan.ad.engine.service.predict.impl;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.TypeReference;
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
 import com.tzld.piaoquan.ad.engine.commons.enums.AppTypeEnum;
 import com.tzld.piaoquan.ad.engine.commons.enums.RedisPrefixEnum;
 import com.tzld.piaoquan.ad.engine.commons.redis.AdRedisHelper;
@@ -77,6 +78,8 @@ public class PredictModelServiceImpl implements PredictModelService {
     private PredictStrategyBy819 predictStrategyBy819;
     @Autowired
     private PredictStrategyBy820 predictStrategyBy820;
+    @Autowired
+    private PredictStrategyBy823 predictStrategyBy823;
 
     @Autowired
     private UserService userService;
@@ -163,15 +166,15 @@ public class PredictModelServiceImpl implements PredictModelService {
             boolean execute817 = false;
             // 不出广告时间判定
             int hourOfDay = DateUtils.getCurrentHour();
-            if ( 0 <= hourOfDay && hourOfDay < 8 && !isAdvanceShowAd() ) {
+            if (0 <= hourOfDay && hourOfDay < 8 && !isAdvanceShowAd()) {
                 // 0点到8点 && 不是节日
                 boolean isIn817Time = false;
-                try{
+                try {
                     String[] split = experiment817WithAdHour.split(",");
-                    if ( Integer.parseInt(split[0]) <= hourOfDay && hourOfDay < Integer.parseInt(split[1]) ) {
+                    if (Integer.parseInt(split[0]) <= hourOfDay && hourOfDay < Integer.parseInt(split[1])) {
                         isIn817Time = true;
                     }
-                }catch (Exception e){
+                } catch (Exception e) {
                     log.error("experiment817WithAdHour配置异常", e);
                 }
                 // 在 817实验时间范围 && 817实验开启
@@ -186,7 +189,7 @@ public class PredictModelServiceImpl implements PredictModelService {
 
             // 新老实验和填充requestParam参数(老实验可能直接返回结果)
             Map<String, Object> result1 = getParamAndFill(requestParam, result);
-            if (result1 != null){
+            if (result1 != null) {
                 result1.put("ad_predict", 1);
                 return result1;
             }
@@ -198,6 +201,13 @@ public class PredictModelServiceImpl implements PredictModelService {
                 return predictStrategyBy817.predict(predictContext);
             }
 
+            // 823熔断
+            if (predictStrategyBy823.in823Ab(requestParam, expCodes, hourOfDay)) {
+                Map<String, Object> resultMap = predictStrategyBy823.predict(predictContext);
+                predictStrategyBy823.addConfigExt(requestParam, resultMap);
+                return resultMap;
+            }
+
             // 819只做参数填充
             Map<String, Object> predictExtInfo = null;
             if (expCodes.contains("819")) {
@@ -225,8 +235,6 @@ public class PredictModelServiceImpl implements PredictModelService {
                 }
             }
 
-
-
             if (expCodes.contains("673")) {
                 Map<String, Object> predictResult = predictStrategyBy673.predict(predictContext);
                 if (Objects.nonNull(predictResult)) {
@@ -239,11 +247,11 @@ public class PredictModelServiceImpl implements PredictModelService {
             }
 
             Map<String, Object> predictResult;
-            if (expCodes.contains("599")){
+            if (expCodes.contains("599")) {
                 predictResult = predictStrategyBy599.predict(predictContext);
-            }else if (expCodes.contains("667")){
+            } else if (expCodes.contains("667")) {
                 predictResult = predictStrategyBy667.predict(predictContext);
-            }else {
+            } else {
                 predictResult = predictStrategyBy599.predict(predictContext);
             }
             // 填充 819 参数
@@ -251,7 +259,7 @@ public class PredictModelServiceImpl implements PredictModelService {
                 predictResult.putAll(predictExtInfo);
             }
             return predictResult;
-            
+
         } catch (Exception e) {
             log.error("svc=adPredict appType={} group={} newGroup={} pqtId={}"
                     , requestParam.getAppType(), requestParam.getAbTestCode(), requestParam.getNewExpGroup(), requestParam.getPqtId(), e);
@@ -526,11 +534,11 @@ public class PredictModelServiceImpl implements PredictModelService {
                 }
             }
 
-            if (expCodes.contains("599")){
+            if (expCodes.contains("599")) {
                 return predictStrategyBy599.predict(ConvertUtil.predictParam2Context(requestParam));
-            }else if (expCodes.contains("667")){
+            } else if (expCodes.contains("667")) {
                 return predictStrategyBy667.predict(ConvertUtil.predictParam2Context(requestParam));
-            }else {
+            } else {
                 return predictStrategyBy599.predict(ConvertUtil.predictParam2Context(requestParam));
             }
 

+ 3 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/param/request/ThresholdPredictModelRequestParam.java

@@ -45,4 +45,7 @@ public class ThresholdPredictModelRequestParam {
     private String shareType;
 
     private String userLayer;
+
+    private String rootSourceId;
+    private Integer userShareDepth;
 }

+ 153 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/v2/PredictStrategyBy823.java

@@ -0,0 +1,153 @@
+package com.tzld.piaoquan.ad.engine.service.predict.v2;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
+import com.tzld.piaoquan.ad.engine.commons.util.JSONUtils;
+import com.tzld.piaoquan.ad.engine.service.predict.param.request.ThresholdPredictModelRequestParam;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+
+@Slf4j
+@Service
+public class PredictStrategyBy823 extends BasicPredict {
+
+    @Value("${experiment.823.ad.hour:0,8}")
+    private String experiment823WithAdHour;
+
+    @ApolloJsonValue("${experiment.823.config:{}}")
+    private JSONObject experiment823Config;
+
+
+    @Override
+    public String name() {
+        return "823";
+    }
+
+    @Override
+    public Map<String, Object> predict(PredictContext ctx) {
+
+        String rootSessionId = ctx.getRootSessionId();
+        String userLayer = ctx.getUserLayer();
+        String shareType = ctx.getShareType();
+
+        double score = this.calcScoreByMid(ctx.getMid());
+        String tail = rootSessionId.substring(rootSessionId.length() - 1);
+        shareType = ctx.getShareType()
+                .replace("return", "")
+                .replace("mids", "");
+
+        Map<String, Object> returnMap = new HashMap<>();
+
+
+        returnMap.putAll(rtnAdPredict(ctx));
+        returnMap.put("model", this.name());
+
+        returnMap.put("score", score);
+        returnMap.put("userLayer", userLayer);
+
+        JSONObject logJson = new JSONObject();
+        logJson.putAll(returnMap);
+        logJson.put("mid", ctx.getMid());
+        logJson.put("appType", ctx.getAppType());
+        logJson.put("rootSessionIdTail", tail);
+        logJson.put("shareType", shareType);
+
+        logJson.put("expId", "userLayerRootSessionIdTailExp");
+        logJson.put("adPlatformType", ctx.getAdPlatformType());
+        logJson.put("abCode", ctx.getAdAbCode());
+        logJson.put("rootSessionId", ctx.getRootSessionId());
+
+        log.info("广告跳出选择 -- 823实验结果: {}, 参数: {}",
+                JSONUtils.toJson(returnMap), logJson.toJSONString());
+
+        return returnMap;
+    }
+
+    public void addConfigExt(ThresholdPredictModelRequestParam params, Map<String, Object> resultMap) {
+        String channel = "*";
+        List<String> channels = new ArrayList<>(experiment823Config.keySet());
+        for (String s : channels) {
+            if (params.getRootSourceId().startsWith(s)) {
+                channel = s;
+                break;
+            }
+        }
+        JSONObject LayerConfig = experiment823Config.getJSONObject(channel);
+        JSONObject config;
+        if (params.getUserShareDepth() == 0) {
+            config = LayerConfig.getJSONObject("首层");
+        } else {
+            config = LayerConfig.getJSONObject("非首层");
+        }
+
+        resultMap.put("headVideo", config.getJSONObject("headVideo"));
+        resultMap.put("recommend", config.getJSONObject("recommend"));
+    }
+
+    public boolean in823Ab(ThresholdPredictModelRequestParam params, Set<String> expCodes, int hourOfDay) {
+        if (params.getUserShareDepth() != 0 || !expCodes.contains("823")) {
+            return false;
+        }
+        boolean isIn823Time = false;
+        try {
+            String[] split = experiment823WithAdHour.split(",");
+            if (Integer.parseInt(split[0]) <= hourOfDay && hourOfDay < Integer.parseInt(split[1])) {
+                isIn823Time = true;
+            }
+        } catch (Exception e) {
+            log.error("experiment823WithAdHour配置异常", e);
+        }
+        if (!isIn823Time) {
+            return false;
+        }
+        String channel = "*";
+        List<String> channels = new ArrayList<>(experiment823Config.keySet());
+        for (String s : channels) {
+            if (params.getRootSourceId().startsWith(s)) {
+                channel = s;
+                break;
+            }
+        }
+        JSONObject LayerConfig = experiment823Config.getJSONObject(channel);
+        JSONObject config;
+        if (params.getUserShareDepth() == 0) {
+            config = LayerConfig.getJSONObject("首层");
+        } else {
+            config = LayerConfig.getJSONObject("非首层");
+        }
+        JSONArray rootSessionIds = config.getJSONArray("rootSessionId");
+        if (Objects.isNull(rootSessionIds) || !rootSessionIds.contains(getGroup(params.getRootSessionId()))) {
+            return false;
+        }
+        JSONArray putScenes = config.getJSONArray("putScene");
+        if (Objects.isNull(putScenes)) {
+            if (channel.equals("*")) {
+                return false;
+            } else {
+                return true;
+            }
+        }
+        boolean isScene = false;
+        for (Object putScene : putScenes) {
+            if (params.getRootSourceId().startsWith((String) putScene)) {
+                isScene = true;
+                break;
+            }
+        }
+        return isScene;
+    }
+
+    private String getGroup(String rootSessionId) {
+        if (StringUtils.isBlank(rootSessionId)) {
+            return null;
+        }
+        return String.valueOf(rootSessionId.charAt(rootSessionId.length() - 1));
+    }
+}
+