浏览代码

Merge branch 'feature_20240614_zhaohaipeng_676' into pre-master

zhaohaipeng 11 月之前
父节点
当前提交
16e7825e30

+ 1 - 1
ad-engine-commons/pom.xml

@@ -25,7 +25,7 @@
         <dependency>
             <groupId>com.tzld.piaoquan</groupId>
             <artifactId>recommend-feature-client</artifactId>
-            <version>1.1.20</version>
+            <version>1.1.30</version>
         </dependency>
         <dependency>
             <groupId>com.tzld.piaoquan</groupId>

+ 7 - 6
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/container/ThresholdModelContainer.java

@@ -26,24 +26,25 @@ public class ThresholdModelContainer {
     @Value("${ad.predict.t-digest.position:0.52}")
     private double position;
 
-    public static Map<String,ThresholdPredictModel> modelMap=new HashMap<>();
+    public static Map<String, ThresholdPredictModel> modelMap = new HashMap<>();
 
 
     private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
+
     @PostConstruct
     public void init() {
-        Map<String,ThresholdPredictModel> beanMap= applicationContext.getBeansOfType(ThresholdPredictModel.class);
-        beanMap.forEach((s,model)->{
+        Map<String, ThresholdPredictModel> beanMap = applicationContext.getBeansOfType(ThresholdPredictModel.class);
+        beanMap.forEach((s, model) -> {
             modelMap.put(model.getName(), model);
         });
 
     }
 
-    public static ThresholdPredictModel getThresholdPredictModel(String modelName){
-        return modelMap.getOrDefault(modelName,getBasicPredictModel());
+    public static ThresholdPredictModel getThresholdPredictModel(String modelName) {
+        return modelMap.getOrDefault(modelName, getBasicPredictModel());
     }
 
-    public static ThresholdPredictModel getBasicPredictModel(){
+    public static ThresholdPredictModel getBasicPredictModel() {
         return modelMap.get("basic");
     }
 

+ 16 - 9
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/helper/NewExpInfoHelper.java

@@ -51,22 +51,29 @@ public class NewExpInfoHelper {
         }
     }
 
-    public static boolean checkInNewExpGroupAndSetParamIfIn(String appId, int groupNumber, String expId, ThresholdPredictModelParam modelParam){
+    public static boolean checkInNewExpGroupAndSetParamIfIn(Collection<String> abCodes, String appId, int groupNumber, String expId, ThresholdPredictModelParam modelParam) {
+        if (abCodes.contains(NewExpInfoHelper.flagId)) {
+            return checkInNewExpGroupAndSetParamIfIn(appId, groupNumber, expId, modelParam);
+        }
+        return false;
+    }
+
+    public static boolean checkInNewExpGroupAndSetParamIfIn(String appId, int groupNumber, String expId, ThresholdPredictModelParam modelParam) {
         try {
-            if(appExpIdCache.get(appId)==null||!appExpIdCache.get(appId).contains(expId)){
+            if (appExpIdCache.get(appId) == null || !appExpIdCache.get(appId).contains(expId)) {
                 return false;
             }
-            if((expIdAndRangeCache.get(expId).getAllEnter()&&groupNumber<0)
-                    ||(
-                    expIdAndRangeCache.get(expId).getRange()[0]<=groupNumber&&expIdAndRangeCache.get(expId).getRange()[1]>=groupNumber)
-            ){
-                for(Map.Entry<String,Object> entry:expIdAndRangeCache.get(expId).getParam().entrySet()){
-                    modelParam.getExtraParam().put(entry.getKey(),entry.getValue());
+            if ((expIdAndRangeCache.get(expId).getAllEnter() && groupNumber < 0)
+                    || (
+                    expIdAndRangeCache.get(expId).getRange()[0] <= groupNumber && expIdAndRangeCache.get(expId).getRange()[1] >= groupNumber)
+            ) {
+                for (Map.Entry<String, Object> entry : expIdAndRangeCache.get(expId).getParam().entrySet()) {
+                    modelParam.getExtraParam().put(entry.getKey(), entry.getValue());
                 }
                 return true;
             }
             return false;
-        }catch (Exception e){
+        } catch (Exception e) {
             return false;
         }
     }

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

@@ -6,6 +6,7 @@ import com.alibaba.fastjson.TypeReference;
 import com.tzld.piaoquan.ad.engine.commons.enums.AppTypeEnum;
 import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
 import com.tzld.piaoquan.ad.engine.commons.util.DateUtils;
+import com.tzld.piaoquan.ad.engine.commons.util.JSONUtils;
 import com.tzld.piaoquan.ad.engine.service.predict.config.NewExpUserGroupConfig;
 import com.tzld.piaoquan.ad.engine.service.predict.config.RoiModelConfig;
 import com.tzld.piaoquan.ad.engine.service.predict.constant.RuleRedisKeyConst;
@@ -27,6 +28,7 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import java.util.*;
+
 //
 @Service
 public class PredictModelServiceImpl implements PredictModelService {
@@ -63,23 +65,23 @@ public class PredictModelServiceImpl implements PredictModelService {
     @Value("${ad.predict.without.ad.video_ids:0}")
     private String withoutAdVideoIds;
     @Value("${ad.predict.old.exp.appId:0,3,4,5,6,17,18,19,21,22}")
-    private String oldExpGroupAppId="";
-    List<Integer> appIdArr=Arrays.asList(new Integer[]{0,3,4,5,6,17,18,19,21,22});
+    private String oldExpGroupAppId = "";
+    List<Integer> appIdArr = Arrays.asList(new Integer[]{0, 3, 4, 5, 6, 17, 18, 19, 21, 22});
 
 
     public Map<String, Object> adPredict(ThresholdPredictModelRequestParam requestParam) {
-        Map<String,Object> result=new HashMap<>();
+        Map<String, Object> result = new HashMap<>();
         result.put("pqtId", requestParam.getPqtId());
         try {
-            String[] withoutAdVideoIdsArr=withoutAdVideoIds.split(",");
-            for(String videoId:withoutAdVideoIdsArr){
-                if(videoId.equals(requestParam.getVideoId()+"")){
-                    if(requestParam.getAppType().equals(0)
-                            ||requestParam.getAppType().equals(4)
-                            ||requestParam.getAppType().equals(5)
-                            ||requestParam.getAppType().equals(21)
-                    ){
-                         result=new HashMap<>();
+            String[] withoutAdVideoIdsArr = withoutAdVideoIds.split(",");
+            for (String videoId : withoutAdVideoIdsArr) {
+                if (videoId.equals(requestParam.getVideoId() + "")) {
+                    if (requestParam.getAppType().equals(0)
+                            || requestParam.getAppType().equals(4)
+                            || requestParam.getAppType().equals(5)
+                            || requestParam.getAppType().equals(21)
+                    ) {
+                        result = new HashMap<>();
                         result.put("ad_predict", 1);
                         result.put("no_ad_strategy", "no_ad_with_video_in_white_list");
                         return result;
@@ -88,28 +90,28 @@ public class PredictModelServiceImpl implements PredictModelService {
             }
 
 
-            List<Map<String,Object>> mapList=(List)requestParam.getAbExpInfo().get("ab_test002");
+            List<Map<String, Object>> mapList = (List) requestParam.getAbExpInfo().get("ab_test002");
 
-            Map<String,List<JSONObject>> configMap=new HashMap<>();
-            //该用户所有实验合集
-            Set<String> expCodes=new HashSet<>();
-            for(Map<String,Object> map:mapList){
-                String expCode=map.getOrDefault("abExpCode","").toString();
+            Map<String, List<JSONObject>> configMap = new HashMap<>();
+            // 该用户所有实验合集
+            Set<String> expCodes = new HashSet<>();
+            for (Map<String, Object> map : mapList) {
+                String expCode = map.getOrDefault("abExpCode", "").toString();
                 expCodes.add(expCode);
-                if("555".equals(expCode)){
-                    configMap=JSONObject.parseObject(map.get("configValue").toString(),Map.class);
+                if ("555".equals(expCode)) {
+                    configMap = JSONObject.parseObject(map.get("configValue").toString(), Map.class);
                 }
             }
-            //先判断是否开启实验 和是否不出广告时间 而后判断默认0-8
-            //不出广告时间判定
-            int hourOfDay= DateUtils.getCurrentHour();
-            Boolean condition1=abTestConfigContainer.inAdTimeTest(requestParam.getAbExpInfo());
-            Boolean condition2=abTestConfigContainer.containsCode(configMap,requestParam.getAbTestCode());
-            if(condition1
+            // 先判断是否开启实验 和是否不出广告时间 而后判断默认0-8
+            // 不出广告时间判定
+            int hourOfDay = DateUtils.getCurrentHour();
+            Boolean condition1 = abTestConfigContainer.inAdTimeTest(requestParam.getAbExpInfo());
+            Boolean condition2 = abTestConfigContainer.containsCode(configMap, requestParam.getAbTestCode());
+            if (condition1
                     &&
                     condition2
                     &&
-                    abTestConfigContainer.inWithoutAdTime(configMap,requestParam.getAbTestCode(),hourOfDay)){
+                    abTestConfigContainer.inWithoutAdTime(configMap, requestParam.getAbTestCode(), hourOfDay)) {
                 // 开启555 & 555的配置包含abcode & 命中555为这个code配置的不出广告时间
                 result.put("ad_predict", 1);
                 result.put("no_ad_strategy", "no_ad_time_with_time_plan");
@@ -123,20 +125,20 @@ public class PredictModelServiceImpl implements PredictModelService {
                 result.put("no_ad_strategy", "no_ad_time_with_fixed_time");
                 return result;
             }
-            String abtestId=null;
-            String abTestConfigTag=null;
-            Map<String, Object> abtestParam=null;
-            String midGroup =null;
-            String shareType =null;
+            String abtestId = null;
+            String abTestConfigTag = null;
+            Map<String, Object> abtestParam = null;
+            String midGroup = null;
+            String shareType = null;
 
-            String[] appIdArr=oldExpGroupAppId.split(",");
+            String[] appIdArr = oldExpGroupAppId.split(",");
             // 新老实验系统
-            List<String> appIdList=Arrays.asList(appIdArr);
-            if(appIdList.contains(requestParam.getAppType().toString())){
+            List<String> appIdList = Arrays.asList(appIdArr);
+            if (appIdList.contains(requestParam.getAppType().toString())) {
 //            if(appIdArr.contains(requestParam.getAppType())){
                 String[] abParamArr = abConfig.getAbParams(requestParam.getAbTestCode(), requestParam.getAbExpInfo());
                 if (abParamArr == null) {
-                    abParamArr= NewExpUserGroupConfig.newExpUserGroupMap.get(requestParam.getAppType().toString());
+                    abParamArr = NewExpUserGroupConfig.newExpUserGroupMap.get(requestParam.getAppType().toString());
                     if (abParamArr == null) {
                         result.put("msg", "abConfig_error");
                         return result;
@@ -159,11 +161,11 @@ public class PredictModelServiceImpl implements PredictModelService {
 //        }else {
 //            midGroup = redisHelper.getString(midGroupKeyName);
 //        }
-                //没有时为新用户或者无分享用户
-                shareType =midGroup;
+                // 没有时为新用户或者无分享用户
+                shareType = midGroup;
                 if (midGroup == null) {
                     midGroup = "mean_group";
-                    shareType="noShare";
+                    shareType = "noShare";
                 }
                 String[] noAdMidGroupList = new String[0];
                 noAdMidGroupList = ((JSONArray) abtestParam.get("no_ad_mid_group_list")).toArray(noAdMidGroupList);
@@ -175,7 +177,7 @@ public class PredictModelServiceImpl implements PredictModelService {
                         break;
                     }
                 }
-                //不出广告组
+                // 不出广告组
                 if (inNoAdGroup) {
                     // User is in the no-ad group, no ad should be shown
                     result.put("mid_group", midGroup);
@@ -183,7 +185,7 @@ public class PredictModelServiceImpl implements PredictModelService {
                     result.put("no_ad_strategy", "no_ad_mid_group_with_video");
                     return result;
                 }
-                //top1广告不出视频
+                // top1广告不出视频
                 Map<String, List<String>> noAdGroupWithVideoMapping = (Map) abtestParam.getOrDefault("no_ad_group_with_video_mapping", new HashMap<>());
                 if (noAdGroupWithVideoMapping.keySet().contains(midGroup)
                         &&
@@ -194,22 +196,21 @@ public class PredictModelServiceImpl implements PredictModelService {
                     result.put("no_ad_strategy", "no_ad_mid_group_with_video");
                     return result;
                 }
-            }else {
-                String midGroupKeyName = RuleRedisKeyConst.KEY_NAME_PREFIX_MID_GROUP +  "class1:" + requestParam.getMid();
+            } else {
+                String midGroupKeyName = RuleRedisKeyConst.KEY_NAME_PREFIX_MID_GROUP + "class1:" + requestParam.getMid();
                 midGroup = redisHelper.getString(midGroupKeyName);
-                shareType =midGroup;
+                shareType = midGroup;
                 if (midGroup == null) {
                     midGroup = "mean_group";
-                    shareType="noShare";
+                    shareType = "noShare";
                 }
             }
 
 
-
-            //市-中文
+            // 市-中文
             requestParam.setRegion(requestParam.getRegion().replace("省", ""));
             requestParam.setCity(requestParam.getCity().replace("市", ""));
-            //设置信息
+            // 设置信息
             ThresholdPredictModelParam modelParam = ThresholdPredictModelParam.builder()
                     .build();
             BeanUtils.copyProperties(requestParam, modelParam);
@@ -219,34 +220,47 @@ public class PredictModelServiceImpl implements PredictModelService {
             modelParam.setAbtestParam(abtestParam);
             modelParam.setMidGroup(midGroup);
             modelParam.setExtraParam(new HashMap<>());
-            modelParam.addUserExtraFuture("shareType",shareType);
+            modelParam.addUserExtraFuture("shareType", shareType);
             setExtraParam(modelParam);
 
+            String appTypeStr = requestParam.getAppType().toString();
+
+            boolean in676Exp = expCodes.contains("673")
+                    || NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(expCodes, appTypeStr, requestParam.getNewExpGroup(), "673", modelParam);
+            if (in676Exp) {
+                result = ThresholdModelContainer.getThresholdPredictModel("random673").predict(modelParam);
+                // 如果676实验返回结果,表示未命中规则即对应的用户来源和所属层存在配置,使用676实验的结果,否则继续走599实验
+                if (Objects.nonNull(result)) {
+                    log.info("广告跳出选择 -- 673实验结果: {}", JSONUtils.toJson(result));
+                    return result;
+                }
+            }
+
             // 新老实验系统兼容
-            if(expCodes.contains("599")||
-                    (expCodes.contains(NewExpInfoHelper.flagId)&&NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
-                            requestParam.getAppType().toString(),requestParam.getNewExpGroup(),"599",modelParam))){
+            if (expCodes.contains("599") ||
+                    (expCodes.contains(NewExpInfoHelper.flagId) && NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
+                            requestParam.getAppType().toString(), requestParam.getNewExpGroup(), "599", modelParam))) {
                 // NewExpInfoHelper.flagId   647
                 result = ThresholdModelContainer.
                         getThresholdPredictModel("random")
                         .predict(modelParam);
-            }else if(expCodes.contains("667")||
-                    (expCodes.contains(NewExpInfoHelper.flagId)&&NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
-                            requestParam.getAppType().toString(),requestParam.getNewExpGroup(),"667",modelParam))){
+                log.info("广告跳出选择 -- 599实验结果: {}", JSONUtils.toJson(result));
+            } else if (expCodes.contains("667") ||
+                    (expCodes.contains(NewExpInfoHelper.flagId) && NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
+                            requestParam.getAppType().toString(), requestParam.getNewExpGroup(), "667", modelParam))) {
                 // NewExpInfoHelper.flagId   647
                 result = ThresholdModelContainer.
                         getThresholdPredictModel("random667")
                         .predict(modelParam);
-            }else if(inExpList(expCodes,adPredictImmersionExpCode)
+            } else if (inExpList(expCodes, adPredictImmersionExpCode)
                     ||
-                    (expCodes.contains(NewExpInfoHelper.flagId)&&NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
-                            requestParam.getAppType().toString(),requestParam.getNewExpGroup(),"607",modelParam))){
+                    (expCodes.contains(NewExpInfoHelper.flagId) && NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
+                            requestParam.getAppType().toString(), requestParam.getNewExpGroup(), "607", modelParam))) {
                 // adPredictImmersionExpCode 607 631
                 result = ThresholdModelContainer.
                         getThresholdPredictModel("immersion")
                         .predict(modelParam);
-            }
-            else {
+            } else {
                 Object thresholdMixFunc = abtestParam.getOrDefault("threshold_mix_func", "basic");
                 result = ThresholdModelContainer.
                         getThresholdPredictModel(thresholdMixFunc.toString())
@@ -254,9 +268,9 @@ public class PredictModelServiceImpl implements PredictModelService {
             }
 
             return result;
-        }catch (Exception e){
+        } catch (Exception e) {
             log.error("svc=adPredict appType={} group={} newGroup={} pqtId={}"
-                    ,requestParam.getAppType(),requestParam.getAbTestCode(),requestParam.getNewExpGroup(),requestParam.getPqtId());
+                    , requestParam.getAppType(), requestParam.getAbTestCode(), requestParam.getNewExpGroup(), requestParam.getPqtId());
             result.put("ad_predict", 1);
             result.put("no_ad_strategy", "error");
             return result;
@@ -292,24 +306,25 @@ public class PredictModelServiceImpl implements PredictModelService {
                 .predict(modelParam);
     }
 
-    public void setExtraParam(ThresholdPredictModelParam modelParam){
-        String[] ids=testIds.split(",");
-        List<String> idList=Arrays.asList(ids);
-        List<Map<String,Object>> mapList=(List)modelParam.getAbExpInfo().get("ab_test002");
-        Collections.sort(mapList,new Comparator<Map<String, Object>>() {
+    public void setExtraParam(ThresholdPredictModelParam modelParam) {
+        String[] ids = testIds.split(",");
+        List<String> idList = Arrays.asList(ids);
+        List<Map<String, Object>> mapList = (List) modelParam.getAbExpInfo().get("ab_test002");
+        Collections.sort(mapList, new Comparator<Map<String, Object>>() {
             @Override
             public int compare(Map<String, Object> map1, Map<String, Object> map2) {
-                int abExpCode1 =Integer.parseInt(map1.get("abExpCode").toString()) ;
-                int abExpCode2 =Integer.parseInt(map2.get("abExpCode").toString());
+                int abExpCode1 = Integer.parseInt(map1.get("abExpCode").toString());
+                int abExpCode2 = Integer.parseInt(map2.get("abExpCode").toString());
                 return Integer.compare(abExpCode1, abExpCode2);
             }
         });
-        Map<String,Object> configMap;
-        for(Map<String,Object> map:mapList){
-            if(idList.contains(map.getOrDefault("abExpCode",""))){
-                configMap=JSONObject.parseObject(map.get("configValue").toString(),new TypeReference<Map<String,Object>>(){});
-                for(Map.Entry<String,Object> entry:configMap.entrySet()){
-                    modelParam.getExtraParam().put(entry.getKey(),entry.getValue());
+        Map<String, Object> configMap;
+        for (Map<String, Object> map : mapList) {
+            if (idList.contains(map.getOrDefault("abExpCode", ""))) {
+                configMap = JSONObject.parseObject(map.get("configValue").toString(), new TypeReference<Map<String, Object>>() {
+                });
+                for (Map.Entry<String, Object> entry : configMap.entrySet()) {
+                    modelParam.getExtraParam().put(entry.getKey(), entry.getValue());
                 }
             }
         }
@@ -318,10 +333,10 @@ public class PredictModelServiceImpl implements PredictModelService {
     }
 
 
-    boolean inExpList(Set<String> set,String expCodes){
-        String[] expArr=expCodes.split(",");
-        for(String str:expArr){
-            if(set.contains(str)){
+    boolean inExpList(Set<String> set, String expCodes) {
+        String[] expArr = expCodes.split(",");
+        for (String str : expArr) {
+            if (set.contains(str)) {
                 return true;
             }
         }

+ 67 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/model/threshold/RandomPredict673Model.java

@@ -0,0 +1,67 @@
+package com.tzld.piaoquan.ad.engine.service.predict.model.threshold;
+
+import com.tzld.piaoquan.ad.engine.service.predict.container.RandWContainer;
+import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdPredictModelParam;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 676实验
+ */
+@Component
+public class RandomPredict673Model extends ThresholdPredictModel {
+    private final static Logger log = LoggerFactory.getLogger(RandomPredict673Model.class);
+
+    @Override
+    String initName() {
+        return "random673";
+    }
+
+
+    @Override
+    public Map<String, Object> predict(ThresholdPredictModelParam modelParam) {
+        // 如果用户的外部来源和分享所属层为空,则跳过
+        if (StringUtils.isAnyBlank(modelParam.getUserExternalSource(), modelParam.getShareLayer())) {
+            return null;
+        }
+
+        //  如果配置不存在,则跳过
+        String abParamKey = String.format("%s:%s", modelParam.getUserExternalSource(), modelParam.getShareLayer());
+        if (!modelParam.getExtraParam().containsKey(abParamKey)) {
+            return null;
+        }
+
+        Map<String, Object> abParam = new HashMap<>();
+        try {
+            abParam.putAll((Map<String, ?>) modelParam.getExtraParam().get(abParamKey));
+        } catch (Exception e) {
+            log.error("get abParam error: ", e);
+        }
+        int hash = modelParam.getMid().hashCode();
+        hash = hash < 0 ? -hash : hash;
+        double score = (hash + RandWContainer.getRandW()) % 100 / 100d;
+
+        String keySuffix = modelParam.getUserExtraFuture("shareType").toString()
+                .replace("return", "")
+                .replace("mids", "");
+        Integer appType = modelParam.getAppType();
+
+        double threshold = Double.parseDouble(abParam.getOrDefault(appType + "_" + keySuffix, -1).toString());
+        if (threshold < 0d) {
+            threshold = Double.parseDouble(abParam.getOrDefault("default_" + keySuffix, "-1").toString());
+        }
+        Map<String, Object> result = new HashMap<>();
+        result.put("ad_predict", score < threshold ? 2 : 1);
+        result.put("score", score);
+        result.put("threshold", threshold);
+        result.put("model", this.initName());
+
+        return result;
+    }
+
+}

+ 12 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/param/ThresholdPredictModelParam.java

@@ -42,6 +42,18 @@ public class ThresholdPredictModelParam {
     String city = "-1";
     MachineInfoParam machineInfo = new MachineInfoParam();
 
+
+    private String userExternalSource;
+
+    /**
+     * 分享所属层
+     * <br />
+     * 1. firstLayer: 首层
+     * <br />
+     * 2. otherLayer: 裂变层
+     */
+    private String shareLayer;
+
     Map<String,Object> extraParam=new HashMap<>();
     Map<String,Object> userExtraFutureMap=new HashMap<>();
 

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

@@ -22,8 +22,19 @@ public class ThresholdPredictModelRequestParam {
     Long careModelStatus;
     Integer newExpGroup;
     String region = "-1";
-    //市-中文
+    // 市-中文
     String city = "-1";
     String pqtId;
     MachineInfoParam machineInfo = new MachineInfoParam();
+
+    private String userExternalSource;
+
+    /**
+     * 分享所属层
+     * <br />
+     * 1. firstLayer: 首层
+     * <br />
+     * 2. otherLayer: 裂变层
+     */
+    private String shareLayer;
 }

+ 28 - 19
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/VideoAdThompsonScorerV2.java

@@ -6,7 +6,6 @@ import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
 import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 import com.tzld.piaoquan.ad.engine.service.score.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
-import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.math3.distribution.BetaDistribution;
 import org.slf4j.Logger;
@@ -38,6 +37,7 @@ public class VideoAdThompsonScorerV2 {
     private Map<String, Double> exp666Param = new HashMap<>();
     private Map<String, Double> exp669Param = new HashMap<>();
     private Map<String, Object> exp670Param = new HashMap<>();
+    private Map<String, Object> exp671Param = new HashMap<>();
     private Map<String, Object> exp672Param = new HashMap<>();
 
     Random random = new Random();
@@ -406,7 +406,6 @@ public class VideoAdThompsonScorerV2 {
 
         double w1 = Double.parseDouble(exp670Param.getOrDefault("w1", "1").toString());
         double w2 = Double.parseDouble(exp670Param.getOrDefault("w2", "1").toString());
-        String scoreCalcWay = exp670Param.getOrDefault("scoreCalcWay", "statistic").toString();
 
         double alpha = Double.parseDouble(exp670Param.getOrDefault("alpha", "0").toString());
         double cidBeta = Double.parseDouble(exp670Param.getOrDefault("cidBeta", "0").toString());
@@ -430,19 +429,15 @@ public class VideoAdThompsonScorerV2 {
                 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);
-                }
+                // double cidScore = this.betaSampler(cidStatistic.parseOrderToDouble() + alpha, cidStatistic.parseExpToDouble() + cidBeta);
+                // double vidCidScore = this.betaSampler(vidCidStatistic.parseOrderToDouble() + alpha, vidCidStatistic.parseExpToDouble() + vidCidBeta);
+                double cidScore = this.orderDivideExp(cidStatistic);
+                double vidCidScore = this.orderDivideExp(vidCidStatistic);
 
 
-                if (w1 != 0 || w2 != 0) {
-                    score = ((w1 * vidCidScore * vidCidExp + w2 * cidScore * cidExp) / (w1 * vidCidExp + w2 * cidExp)) * cpa;
+                double b = w1 * vidCidExp + w2 * cidExp;
+                if (b != 0) {
+                    score = ((w1 * vidCidScore * vidCidExp + w2 * cidScore * cidExp) / b) * cpa;
                 }
 
                 ext.put("w1", w1);
@@ -459,10 +454,10 @@ public class VideoAdThompsonScorerV2 {
                 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);
             } catch (Exception e) {
                 log.error("svc=670exp, error: ", e);
+                score = 0; // 确保在异常情况下`score`有一个默认值
             }
 
             AdRankItem item = new AdRankItem();
@@ -623,9 +618,17 @@ public class VideoAdThompsonScorerV2 {
     }
 
     public List<AdRankItem> thompsonScorerByExp672(ScoreParam param, List<AdPlatformCreativeDTO> adIdList) {
-        String cidRedisKey = exp672Param.getOrDefault("cidActionRedisKey", redisCreativeStatisticsPrefix).toString();
-        String vidCidRedisKey = exp672Param.getOrDefault("vidCidActionRedisKey", redisVideoCreativeStatisticsPrefix).toString();
-        double viewThreshold = Double.parseDouble(exp672Param.getOrDefault("viewThreshold", "7000").toString());
+        return statisticsScore(param, adIdList, exp672Param, 672);
+    }
+
+    public List<AdRankItem> thompsonScorerByExp671(ScoreParam param, List<AdPlatformCreativeDTO> adIdList) {
+        return statisticsScore(param, adIdList, exp671Param, 671);
+    }
+
+    private List<AdRankItem> statisticsScore(ScoreParam param, List<AdPlatformCreativeDTO> adIdList, Map<String, Object> expParam, int scoreType){
+        String cidRedisKey = expParam.getOrDefault("cidActionRedisKey", redisCreativeStatisticsPrefix).toString();
+        String vidCidRedisKey = expParam.getOrDefault("vidCidActionRedisKey", redisVideoCreativeStatisticsPrefix).toString();
+        double viewThreshold = Double.parseDouble(expParam.getOrDefault("viewThreshold", "7000").toString());
 
         // RedisKey前缀判断
         if (!StringUtils.equals(redisCidActionKeyV1, cidRedisKey) && !StringUtils.equals(redisCreativeStatisticsPrefix, cidRedisKey)) {
@@ -672,7 +675,7 @@ public class VideoAdThompsonScorerV2 {
                 ext.put("vidCidStatistic", JSON.toJSONString(vidCidStatistic));
                 ext.put("cpa", cpa);
             } catch (Exception e) {
-                log.error("svc=672exp, error: ", e);
+                log.error("svc=statisticsScore, error: ", e);
             }
 
             AdRankItem item = new AdRankItem();
@@ -681,7 +684,7 @@ public class VideoAdThompsonScorerV2 {
             item.setScore(score);
             item.setExt(ext);
             item.setVideoId(param.getVideoId());
-            item.setScore_type(672);
+            item.setScore_type(scoreType);
             item.setWeight(dto.getWeight());
             item.setCreativeCode(dto.getCreativeCode());
             result.add(item);
@@ -691,6 +694,7 @@ public class VideoAdThompsonScorerV2 {
         return result;
     }
 
+
     class CreativeStatistic {
 
         private String exp;
@@ -812,6 +816,11 @@ public class VideoAdThompsonScorerV2 {
         this.exp670Param = gson.fromJson(str, Map.class);
     }
 
+    @Value("${ad.engine.new.thompson.exp.V2.671:{}}")
+    public void setExp671Param(String str) {
+        this.exp671Param = gson.fromJson(str, Map.class);
+    }
+
     @Value("${ad.engine.new.thompson.exp.V2.672:{}}")
     public void setExp672Param(String str) {
         this.exp672Param = gson.fromJson(str, Map.class);

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

@@ -2,25 +2,25 @@ package com.tzld.piaoquan.ad.engine.service.score.impl;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
+import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
+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;
-import com.tzld.piaoquan.ad.engine.service.log.LogHubService;
+import com.tzld.piaoquan.ad.engine.service.remote.FeatureRemoteService;
+import com.tzld.piaoquan.ad.engine.service.score.RankService;
 import com.tzld.piaoquan.ad.engine.service.score.VideoAdThompsonScorerV2;
 import com.tzld.piaoquan.ad.engine.service.score.container.AdCreativeFeatureContainer;
 import com.tzld.piaoquan.ad.engine.service.score.container.PidLambdaContainer;
 import com.tzld.piaoquan.ad.engine.service.score.container.PidLambdaForCpcContainer;
 import com.tzld.piaoquan.ad.engine.service.score.container.PidLambdaV2Container;
+import com.tzld.piaoquan.ad.engine.service.score.convert.RequestConvert;
 import com.tzld.piaoquan.ad.engine.service.score.dto.AdPlatformCreativeDTO;
 import com.tzld.piaoquan.ad.engine.service.score.param.BidRankRecommendRequestParam;
+import com.tzld.piaoquan.ad.engine.service.score.param.RankRecommendRequestParam;
 import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdItemFeature;
 import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
 import com.tzld.piaoquan.recommend.feature.domain.ad.base.UserAdFeature;
-import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
-import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
-import com.tzld.piaoquan.ad.engine.service.remote.FeatureRemoteService;
-import com.tzld.piaoquan.ad.engine.service.score.RankService;
-import com.tzld.piaoquan.ad.engine.service.score.convert.RequestConvert;
-import com.tzld.piaoquan.ad.engine.service.score.param.RankRecommendRequestParam;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
@@ -52,52 +52,52 @@ public class RankServiceImpl implements RankService {
     @Autowired
     AdCreativeFeatureContainer adCreativeFeatureContainer;
     @Value("${ad.model.cpm.max:200}")
-    Double cpmMax=200d;
+    Double cpmMax = 200d;
     @Value("${ad.model.cpm.min:30}")
-    Double cpmMin=30d;
+    Double cpmMin = 30d;
     @Value("${ad.pid.cpc.exp:30}")
     private String cpcPidExpCode;
     @Value("${ad.cvr.adjusting.exp:652}")
     private String cvrAdjustingExpCode;
 
-    public AdRankItem adItemRank(RankRecommendRequestParam request){
-        ScoreParam param= RequestConvert.requestConvert(request);
+    public AdRankItem adItemRank(RankRecommendRequestParam request) {
+        ScoreParam param = RequestConvert.requestConvert(request);
         LocalDateTime currentTime = LocalDateTime.now();
         int currentHour = currentTime.getHour();
         int dayOfWeek = currentTime.getDayOfWeek().getValue();
-        param.getRequestContext().setHour(currentHour+"");
-        param.getRequestContext().setWeek(dayOfWeek+"");
-        param.getRequestContext().setRegion(request.getRegion().replace("省",""));
-        param.getRequestContext().setCity(request.getCity().replace("市",""));
+        param.getRequestContext().setHour(currentHour + "");
+        param.getRequestContext().setWeek(dayOfWeek + "");
+        param.getRequestContext().setRegion(request.getRegion().replace("省", ""));
+        param.getRequestContext().setCity(request.getCity().replace("市", ""));
         param.getRequestContext().setDay(currentTime.format(dateFormatter));
 
-        UserAdFeature userAdFeature=featureRemoteService.getUserAdFeature(request.getMid());
-        if(userAdFeature==null){
-            userAdFeature=new UserAdFeature();
+        UserAdFeature userAdFeature = featureRemoteService.getUserAdFeature(request.getMid());
+        if (userAdFeature == null) {
+            userAdFeature = new UserAdFeature();
         }
-        Map<Long,List<AdPlatformCreativeDTO>> groupMap=request
+        Map<Long, List<AdPlatformCreativeDTO>> groupMap = request
                 .getAdIdList()
                 .stream()
                 .collect(Collectors.groupingBy(creativeDTO -> creativeDTO.getCreativeId()));
-        Map<Long, AdRankItem> cache=adCreativeFeatureContainer.getAll(new ArrayList<>(groupMap.keySet()));
-        List<AdRankItem> rankItems=Collections.emptyList();
-        if(!cache.isEmpty()){
-            rankItems=new LinkedList<>(cache.values());
+        Map<Long, AdRankItem> cache = adCreativeFeatureContainer.getAll(new ArrayList<>(groupMap.keySet()));
+        List<AdRankItem> rankItems = Collections.emptyList();
+        if (!cache.isEmpty()) {
+            rankItems = new LinkedList<>(cache.values());
         }
-        //避免recommend-feature出问题
-        if(rankItems==null|| rankItems.size()==0){
-            rankItems=new LinkedList<>();
-            for(Long adId:groupMap.keySet()){
-                AdRankItem item=new AdRankItem();
+        // 避免recommend-feature出问题
+        if (rankItems == null || rankItems.size() == 0) {
+            rankItems = new LinkedList<>();
+            for (Long adId : groupMap.keySet()) {
+                AdRankItem item = new AdRankItem();
                 item.setAdId(adId);
                 item.setItemFeature(new AdItemFeature());
                 rankItems.add(item);
             }
         }
-        boolean inCpcPidExp=false;
-        boolean inCvrAdjustingExp=false;
+        boolean inCpcPidExp = false;
+        boolean inCvrAdjustingExp = false;
         if (request.getAdAbExpArr() != null && request.getAdAbExpArr().size() != 0) {
-            for (Map<String, Object> map : request.getAdAbExpArr() ) {
+            for (Map<String, Object> map : request.getAdAbExpArr()) {
                 if (map.getOrDefault("abExpCode", "").equals(cpcPidExpCode)) {
                     inCpcPidExp = true;
                 }
@@ -106,39 +106,39 @@ public class RankServiceImpl implements RankService {
                 }
             }
         }
-        double lambda=-1d;
-        if(inCpcPidExp){
-            for(AdRankItem item:rankItems){
+        double lambda = -1d;
+        if (inCpcPidExp) {
+            for (AdRankItem item : rankItems) {
                 try {
-                    AdPlatformCreativeDTO dto=groupMap.get(item.getAdId()).get(0);
+                    AdPlatformCreativeDTO dto = groupMap.get(item.getAdId()).get(0);
                     item.setBid1(dto.getBid1());
                     item.setBid2(dto.getBid2());
-                    lambda= PidLambdaForCpcContainer.getPidLambda(item.getAdId());
-                    if(lambda<0){
+                    lambda = PidLambdaForCpcContainer.getPidLambda(item.getAdId());
+                    if (lambda < 0) {
                         item.setCpa(dto.getCpa());
                         item.setPidLambda(1);
-                    }else {
-                        if(dto.getCpa()>1&&lambda<=1){
-                            lambda=2d;
+                    } else {
+                        if (dto.getCpa() > 1 && lambda <= 1) {
+                            lambda = 2d;
                         }
                         item.setCpa(lambda);
                         item.setPidLambda(1d);
                     }
-                }catch (Exception e){
-                    log.error("rankItems info error itemId={}",item.getAdId());
+                } catch (Exception e) {
+                    log.error("rankItems info error itemId={}", item.getAdId());
                     e.printStackTrace();
                 }
             }
-        }else {
-            for(AdRankItem item:rankItems){
+        } else {
+            for (AdRankItem item : rankItems) {
                 try {
-                    AdPlatformCreativeDTO dto=groupMap.get(item.getAdId()).get(0);
+                    AdPlatformCreativeDTO dto = groupMap.get(item.getAdId()).get(0);
                     item.setBid1(dto.getBid1());
                     item.setBid2(dto.getBid2());
                     item.setCpa(dto.getCpa());
                     item.setPidLambda(1d);
-                }catch (Exception e){
-                    log.error("rankItems info error itemId={}",item.getAdId());
+                } catch (Exception e) {
+                    log.error("rankItems info error itemId={}", item.getAdId());
                     e.printStackTrace();
                 }
             }
@@ -153,28 +153,28 @@ public class RankServiceImpl implements RankService {
         }
 
         if (!CollectionUtils.isEmpty(rankResult)) {
-            JSONObject object=new JSONObject();
-            object.put("mid",request.getMid());
-            object.put("adid",rankResult.get(0).getAdId());
-            object.put("type",rankResult.get(0).getScore_type());
-            object.put("pctr",rankResult.get(0).getCtr());
-            object.put("pcvr",rankResult.get(0).getCvr());
-            object.put("score",rankResult.get(0).getScore());
-            object.put("pidLambda",rankResult.get(0).getPidLambda());
-            object.put("lrsamples",rankResult.get(0).getLrSampleString());
-            object.put("dataTime",currentTime.format(timeFormatter));
-            object.put("creativeId",rankResult.get(0).getAdId());
+            JSONObject object = new JSONObject();
+            object.put("mid", request.getMid());
+            object.put("adid", rankResult.get(0).getAdId());
+            object.put("type", rankResult.get(0).getScore_type());
+            object.put("pctr", rankResult.get(0).getCtr());
+            object.put("pcvr", rankResult.get(0).getCvr());
+            object.put("score", rankResult.get(0).getScore());
+            object.put("pidLambda", rankResult.get(0).getPidLambda());
+            object.put("lrsamples", rankResult.get(0).getLrSampleString());
+            object.put("dataTime", currentTime.format(timeFormatter));
+            object.put("creativeId", rankResult.get(0).getAdId());
             object.put("videoId", request.getVideoId());
             object.put("pqtId", request.getPqtId());
             log.info("svc=adItemRank {}", JSONObject.toJSONString(object));
             object.remove("lrsamples");
-            if(inCpcPidExp){
-                AdPlatformCreativeDTO dto=groupMap.get(rankResult.get(0).getAdId()).get(0);
-                object.put("cpa",dto.getCpa()*dto.getBid1());
-                object.put("oCpa",dto.getCpa());
-                object.put("realECpm",rankResult.get(0).getEcpm1());
+            if (inCpcPidExp) {
+                AdPlatformCreativeDTO dto = groupMap.get(rankResult.get(0).getAdId()).get(0);
+                object.put("cpa", dto.getCpa() * dto.getBid1());
+                object.put("oCpa", dto.getCpa());
+                object.put("realECpm", rankResult.get(0).getEcpm1());
                 log.info("svc=cpc_pid obj={}", JSONObject.toJSONString(object));
-            }else {
+            } else {
                 log.info("svc=pid_log obj={}", JSONObject.toJSONString(object));
             }
 
@@ -186,67 +186,27 @@ public class RankServiceImpl implements RankService {
                     inCvrAdjustingExp ? "cvr_adjusting" : "base_line", "531");
 
             return rankResult.get(0);
-        }else {
-            //空返回值
+        } else {
+            // 空返回值
             return new AdRankItem();
         }
     }
 
-    public AdRankItem adItemRankWithVideoAdThompson(RankRecommendRequestParam request){
-        ScoreParam param= RequestConvert.requestConvert(request);
+    public AdRankItem adItemRankWithVideoAdThompson(RankRecommendRequestParam request) {
+        ScoreParam param = RequestConvert.requestConvert(request);
         LocalDateTime currentTime = LocalDateTime.now();
         int currentHour = currentTime.getHour();
         int dayOfWeek = currentTime.getDayOfWeek().getValue();
-        param.getRequestContext().setHour(currentHour+"");
-        param.getRequestContext().setWeek(dayOfWeek+"");
-        param.getRequestContext().setRegion(request.getRegion().replace("省",""));
-        param.getRequestContext().setCity(request.getCity().replace("市",""));
+        param.getRequestContext().setHour(currentHour + "");
+        param.getRequestContext().setWeek(dayOfWeek + "");
+        param.getRequestContext().setRegion(request.getRegion().replace("省", ""));
+        param.getRequestContext().setCity(request.getCity().replace("市", ""));
         param.getRequestContext().setDay(currentTime.format(dateFormatter));
 
-//        UserAdFeature userAdFeature=featureRemoteService.getUserAdFeature(request.getMid());
-//        if(userAdFeature==null){
-//            userAdFeature=new UserAdFeature();
-//        }
-//        Map<Long,List<AdPlatformCreativeDTO>> groupMap=request
-//                .getAdIdList()
-//                .stream()
-//                .collect(Collectors.groupingBy(creativeDTO -> creativeDTO.getCreativeId()));
-//        Map<Long, AdRankItem> cache=adCreativeFeatureContainer.getAll(new ArrayList<>(groupMap.keySet()));
-//        List<AdRankItem> rankItems=Collections.emptyList();
-//        if(!cache.isEmpty()){
-//            rankItems=new LinkedList<>();
-//            for (AdRankItem value : cache.values()) {
-//                value.setVideoId(request.getVideoId());
-//                rankItems.add(value);
-//            }
-//        }
-//        //避免recommend-feature出问题
-//        if(rankItems==null|| rankItems.size()==0){
-//            rankItems=new LinkedList<>();
-//            for(Long adId:groupMap.keySet()){
-//                AdRankItem item=new AdRankItem();
-//                item.setAdId(adId);
-//                item.setItemFeature(new AdItemFeature());
-//                item.setVideoId(request.getVideoId());
-//                rankItems.add(item);
-//            }
-//        }
-//        for(AdRankItem item:rankItems){
-//            try {
-//                AdPlatformCreativeDTO dto=groupMap.get(item.getAdId()).get(0);
-//                item.setBid1(dto.getBid1());
-//                item.setBid2(dto.getBid2());
-//                item.setCpa(dto.getCpa());
-//                item.setPidLambda(1d);
-//            }catch (Exception e){
-//                log.error("rankItems info error itemId={}",item.getAdId());
-//                e.printStackTrace();
-//            }
-//        }
-        Set<String> expCodes=new HashSet<>();
+        Set<String> expCodes = new HashSet<>();
         if (request.getAdAbExpArr() != null && request.getAdAbExpArr().size() != 0) {
-            for (Map<String, Object> map : request.getAdAbExpArr() ) {
-                String expCode=map.getOrDefault("abExpCode","").toString();
+            for (Map<String, Object> map : request.getAdAbExpArr()) {
+                String expCode = map.getOrDefault("abExpCode", "").toString();
                 expCodes.add(expCode);
             }
         }
@@ -254,25 +214,25 @@ public class RankServiceImpl implements RankService {
                 .build();
         // 兜底方案
         String abCode = "";
-        List<AdRankItem> rankResult=null;
-        if(expCodes.contains("663")||
-                (expCodes.contains(NewExpInfoHelper.flagId)&&NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
-                        request.getAppType().toString(),request.getNewExpGroup(),"663",modelParam))){
+        List<AdRankItem> rankResult = null;
+        if (expCodes.contains("663") ||
+                (expCodes.contains(NewExpInfoHelper.flagId) && NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
+                        request.getAppType().toString(), request.getNewExpGroup(), "663", modelParam))) {
             rankResult = videoAdThompsonScorerV2.thompsonScorerByExp663(param, request.getAdIdList());
             abCode = "663";
-        }else if(expCodes.contains("664")||
-                (expCodes.contains(NewExpInfoHelper.flagId)&&NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
-                        request.getAppType().toString(),request.getNewExpGroup(),"664",modelParam))){
+        } else if (expCodes.contains("664") ||
+                (expCodes.contains(NewExpInfoHelper.flagId) && NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
+                        request.getAppType().toString(), request.getNewExpGroup(), "664", modelParam))) {
             rankResult = videoAdThompsonScorerV2.thompsonScorerByExp664(param, request.getAdIdList());
             abCode = "664";
-        }else if(expCodes.contains("665")||
-                (expCodes.contains(NewExpInfoHelper.flagId)&&NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
-                        request.getAppType().toString(),request.getNewExpGroup(),"665",modelParam))){
+        } else if (expCodes.contains("665") ||
+                (expCodes.contains(NewExpInfoHelper.flagId) && NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
+                        request.getAppType().toString(), request.getNewExpGroup(), "665", modelParam))) {
             rankResult = videoAdThompsonScorerV2.thompsonScorerByExp665(param, request.getAdIdList());
             abCode = "665";
-        }else if(expCodes.contains("666")||
-                (expCodes.contains(NewExpInfoHelper.flagId)&&NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
-                        request.getAppType().toString(),request.getNewExpGroup(),"666",modelParam))){
+        } else if (expCodes.contains("666") ||
+                (expCodes.contains(NewExpInfoHelper.flagId) && NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
+                        request.getAppType().toString(), request.getNewExpGroup(), "666", modelParam))) {
             rankResult = videoAdThompsonScorerV2.thompsonScorerByExp666(param, request.getAdIdList());
             abCode = "666";
         } else if (expCodes.contains("669") ||
@@ -285,9 +245,14 @@ public class RankServiceImpl implements RankService {
                         request.getAppType().toString(), request.getNewExpGroup(), "670", modelParam))) {
             rankResult = videoAdThompsonScorerV2.thompsonScorerByExp670(param, request.getAdIdList());
             abCode = "670";
-        }else if (expCodes.contains("672") ||
+        } else if (expCodes.contains("671") ||
                 (expCodes.contains(NewExpInfoHelper.flagId) && NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
-                        request.getAppType().toString(), request.getNewExpGroup(), "672", modelParam))){
+                        request.getAppType().toString(), request.getNewExpGroup(), "671", modelParam))) {
+            rankResult = videoAdThompsonScorerV2.thompsonScorerByExp671(param, request.getAdIdList());
+            abCode = "671";
+        } else if (expCodes.contains("672") ||
+                (expCodes.contains(NewExpInfoHelper.flagId) && NewExpInfoHelper.checkInNewExpGroupAndSetParamIfIn(
+                        request.getAppType().toString(), request.getNewExpGroup(), "672", modelParam))) {
             rankResult = videoAdThompsonScorerV2.thompsonScorerByExp672(param, request.getAdIdList());
             abCode = "672";
         }
@@ -297,26 +262,26 @@ public class RankServiceImpl implements RankService {
         logHubService.scoreLogUpload(param, request.getAdIdList(), rankResult, request, "video_ad_thompson", abCode);
 
         if (!CollectionUtils.isEmpty(rankResult)) {
-            JSONObject object=new JSONObject();
-            object.put("mid",request.getMid());
-            object.put("adid",rankResult.get(0).getAdId());
-            object.put("type",rankResult.get(0).getScore_type());
-            object.put("pctr",rankResult.get(0).getCtr());
-            object.put("pcvr",rankResult.get(0).getCvr());
-            object.put("score",rankResult.get(0).getScore());
-            object.put("pidLambda",rankResult.get(0).getPidLambda());
-            object.put("lrsamples",rankResult.get(0).getLrSampleString());
-            object.put("dataTime",currentTime.format(timeFormatter));
-            object.put("creativeId",rankResult.get(0).getAdId());
+            JSONObject object = new JSONObject();
+            object.put("mid", request.getMid());
+            object.put("adid", rankResult.get(0).getAdId());
+            object.put("type", rankResult.get(0).getScore_type());
+            object.put("pctr", rankResult.get(0).getCtr());
+            object.put("pcvr", rankResult.get(0).getCvr());
+            object.put("score", rankResult.get(0).getScore());
+            object.put("pidLambda", rankResult.get(0).getPidLambda());
+            object.put("lrsamples", rankResult.get(0).getLrSampleString());
+            object.put("dataTime", currentTime.format(timeFormatter));
+            object.put("creativeId", rankResult.get(0).getAdId());
             object.put("videoId", rankResult.get(0).getVideoId());
             object.put("pqtId", request.getPqtId());
             log.info("svc=videoAdThompsonRank_lr_log {}", JSONObject.toJSONString(object));
             object.remove("lrsamples");
-            object.put("score_500",rankResult.get(0).getTf_ctr());
-            object.put("score_1000",rankResult.get(0).getTf_cvr());
+            object.put("score_500", rankResult.get(0).getTf_ctr());
+            object.put("score_1000", rankResult.get(0).getTf_cvr());
             log.info("svc=videoAdThompsonRank_log obj={}", JSONObject.toJSONString(object));
             return rankResult.get(0);
-        }else {
+        } else {
             return new AdRankItem();
         }
     }
@@ -324,124 +289,124 @@ public class RankServiceImpl implements RankService {
     @Override
     public AdPlatformCreativeDTO adBidRank(BidRankRecommendRequestParam request) {
 
-        ScoreParam param= RequestConvert.requestConvert(request);
+        ScoreParam param = RequestConvert.requestConvert(request);
 
         LocalDateTime currentTime = LocalDateTime.now();
         int currentHour = currentTime.getHour();
         int dayOfWeek = currentTime.getDayOfWeek().getValue();
-        param.getRequestContext().setHour(currentHour+"");
-        param.getRequestContext().setWeek(dayOfWeek+"");
-        param.getRequestContext().setRegion(request.getRegion().replace("省",""));
-        param.getRequestContext().setCity(request.getCity().replace("市",""));
+        param.getRequestContext().setHour(currentHour + "");
+        param.getRequestContext().setWeek(dayOfWeek + "");
+        param.getRequestContext().setRegion(request.getRegion().replace("省", ""));
+        param.getRequestContext().setCity(request.getCity().replace("市", ""));
         param.getRequestContext().setDay(currentTime.format(dateFormatter));
 
-        UserAdFeature userAdFeature=featureRemoteService.getUserAdFeature(request.getMid());
-        if(userAdFeature==null){
-            userAdFeature=new UserAdFeature();
+        UserAdFeature userAdFeature = featureRemoteService.getUserAdFeature(request.getMid());
+        if (userAdFeature == null) {
+            userAdFeature = new UserAdFeature();
         }
-        Map<Long,List<AdPlatformCreativeDTO>> groupMap=request
+        Map<Long, List<AdPlatformCreativeDTO>> groupMap = request
                 .getCreativeList()
                 .stream()
                 .collect(Collectors.groupingBy(creativeDTO -> creativeDTO.getCreativeId()));
 
 
-        Map<Long, AdRankItem> cache=adCreativeFeatureContainer.getAll(new ArrayList<>(groupMap.keySet()));
-        List<AdRankItem> rankItems=Collections.emptyList();
-        if(!cache.isEmpty()){
-            rankItems=new LinkedList<>(cache.values());
+        Map<Long, AdRankItem> cache = adCreativeFeatureContainer.getAll(new ArrayList<>(groupMap.keySet()));
+        List<AdRankItem> rankItems = Collections.emptyList();
+        if (!cache.isEmpty()) {
+            rankItems = new LinkedList<>(cache.values());
         }
-        double lambda=-1d;
-        for(AdRankItem item:rankItems){
+        double lambda = -1d;
+        for (AdRankItem item : rankItems) {
             try {
-                AdPlatformCreativeDTO dto=groupMap.get(item.getAdId()).get(0);
+                AdPlatformCreativeDTO dto = groupMap.get(item.getAdId()).get(0);
                 item.setBid1(dto.getBid1());
                 item.setBid2(dto.getBid2());
-                lambda=PidLambdaContainer.getPidLambda(item.getAdId());
-                if(lambda<0){
+                lambda = PidLambdaContainer.getPidLambda(item.getAdId());
+                if (lambda < 0) {
                     item.setCpa(dto.getCpa());
                     item.setPidLambda(0.6);
-                }else {
-                    if(dto.getCpa()>1&&lambda<=1){
-                        lambda=2d;
+                } else {
+                    if (dto.getCpa() > 1 && lambda <= 1) {
+                        lambda = 2d;
                     }
                     item.setCpa(lambda);
                     item.setPidLambda(1d);
                 }
 
-            }catch (Exception e){
-                log.error("rankItems info error itemId={}",item.getAdId());
+            } catch (Exception e) {
+                log.error("rankItems info error itemId={}", item.getAdId());
                 e.printStackTrace();
             }
         }
         List<AdRankItem> rankResult;
-        if(rankItems==null|| rankItems.size()==0){
-            rankItems=new LinkedList<>();
-            for(AdPlatformCreativeDTO dto:request.getCreativeList()){
-                AdRankItem item=new AdRankItem();
+        if (rankItems == null || rankItems.size() == 0) {
+            rankItems = new LinkedList<>();
+            for (AdPlatformCreativeDTO dto : request.getCreativeList()) {
+                AdRankItem item = new AdRankItem();
                 item.setBid1(dto.getBid1());
                 item.setBid2(dto.getBid2());
                 item.setAdId(dto.getCreativeId());
                 item.setItemFeature(new AdItemFeature());
-                lambda=PidLambdaContainer.getPidLambda(item.getAdId());
-                if(lambda<0){
+                lambda = PidLambdaContainer.getPidLambda(item.getAdId());
+                if (lambda < 0) {
                     item.setCpa(dto.getCpa());
                     item.setPidLambda(0.6);
-                }else {
-                    if(dto.getCpa()>1&&lambda<=1){
-                        lambda=2d;
+                } else {
+                    if (dto.getCpa() > 1 && lambda <= 1) {
+                        lambda = 2d;
                     }
                     item.setCpa(lambda);
                     item.setPidLambda(1d);
                 }
                 rankItems.add(item);
             }
-            rankResult=rankServiceThompson.rank(param, userAdFeature, rankItems,null);
-        }else {
-            rankResult=rank(param, userAdFeature, rankItems,ScorerUtils.BASE_CONF);
+            rankResult = rankServiceThompson.rank(param, userAdFeature, rankItems, null);
+        } else {
+            rankResult = rank(param, userAdFeature, rankItems, ScorerUtils.BASE_CONF);
         }
 
-        AdRankItem topItem=rankResult.get(0);
+        AdRankItem topItem = rankResult.get(0);
 
-        AdPlatformCreativeDTO result= AdPlatformCreativeDTO
+        AdPlatformCreativeDTO result = AdPlatformCreativeDTO
                 .builder()
                 .build();
-        BeanUtils.copyProperties(topItem,result);
+        BeanUtils.copyProperties(topItem, result);
         result.setCreativeId(topItem.getAdId());
         result.setPctr(topItem.getCtr());
         result.setPcvr(topItem.getCvr());
 //        result.setCreativeCode(groupMap.get(topItem.getAdId()+"").get(0).getCreativeCode());
         result.setCreativeCode(groupMap.get(topItem.getAdId()).get(0).getCreativeCode());
-        double realECpm=0d;
-            //经验值 待定
+        double realECpm = 0d;
+        // 经验值 待定
 //        realECpm=topItem.getEcpm1();
-        realECpm=topItem.getEcpm2();
+        realECpm = topItem.getEcpm2();
 
-        if(realECpm>cpmMax/1000d){
-            realECpm=cpmMax/1000d;
+        if (realECpm > cpmMax / 1000d) {
+            realECpm = cpmMax / 1000d;
         }
-        if(realECpm<cpmMin/1000d){
-            realECpm=cpmMin/1000d;
+        if (realECpm < cpmMin / 1000d) {
+            realECpm = cpmMin / 1000d;
         }
         result.setEcpm2(realECpm);
-        AdPlatformCreativeDTO dto=groupMap.get(topItem.getAdId()).get(0);
-        JSONObject object=new JSONObject();
-        object.put("mid",request.getMid());
-        object.put("adid",result.getCreativeId());
-        object.put("creativeCode",result.getCreativeCode());
-        object.put("type",topItem.getScore_type());
-        object.put("pctr",topItem.getCtr());
-        object.put("pcvr",topItem.getCvr());
-        object.put("lrsamples",topItem.getLrSampleString());
-        object.put("pidLambda",topItem.getPidLambda());
+        AdPlatformCreativeDTO dto = groupMap.get(topItem.getAdId()).get(0);
+        JSONObject object = new JSONObject();
+        object.put("mid", request.getMid());
+        object.put("adid", result.getCreativeId());
+        object.put("creativeCode", result.getCreativeCode());
+        object.put("type", topItem.getScore_type());
+        object.put("pctr", topItem.getCtr());
+        object.put("pcvr", topItem.getCvr());
+        object.put("lrsamples", topItem.getLrSampleString());
+        object.put("pidLambda", topItem.getPidLambda());
         object.put("videoId", request.getVideoId());
 
-        //临时加入供pid v2使用
-        object.put("realECpm",realECpm);
-        object.put("creativeId",result.getCreativeId());
-        //CPA还原
-        object.put("cpa",dto.getCpa()*dto.getBid1());
-        object.put("oCpa",dto.getCpa());
-        object.put("dataTime",currentTime.format(timeFormatter));
+        // 临时加入供pid v2使用
+        object.put("realECpm", realECpm);
+        object.put("creativeId", result.getCreativeId());
+        // CPA还原
+        object.put("cpa", dto.getCpa() * dto.getBid1());
+        object.put("oCpa", dto.getCpa());
+        object.put("dataTime", currentTime.format(timeFormatter));
         object.put("pqtId", request.getPqtId());
         log.info("svc=adBidRank {}", JSONObject.toJSONString(object));
         object.remove("lrsamples");
@@ -452,128 +417,128 @@ public class RankServiceImpl implements RankService {
     @Override
     public AdPlatformCreativeDTO adBidRankNewPid(BidRankRecommendRequestParam request) {
 
-        ScoreParam param= RequestConvert.requestConvert(request);
+        ScoreParam param = RequestConvert.requestConvert(request);
         LocalDateTime currentTime = LocalDateTime.now();
         int currentHour = currentTime.getHour();
         int dayOfWeek = currentTime.getDayOfWeek().getValue();
-        param.getRequestContext().setHour(currentHour+"");
-        param.getRequestContext().setWeek(dayOfWeek+"");
-        param.getRequestContext().setRegion(request.getRegion().replace("省",""));
-        param.getRequestContext().setCity(request.getCity().replace("市",""));
+        param.getRequestContext().setHour(currentHour + "");
+        param.getRequestContext().setWeek(dayOfWeek + "");
+        param.getRequestContext().setRegion(request.getRegion().replace("省", ""));
+        param.getRequestContext().setCity(request.getCity().replace("市", ""));
         param.getRequestContext().setDay(currentTime.format(dateFormatter));
 
-        UserAdFeature userAdFeature=featureRemoteService.getUserAdFeature(request.getMid());
-        if(userAdFeature==null){
-            userAdFeature=new UserAdFeature();
+        UserAdFeature userAdFeature = featureRemoteService.getUserAdFeature(request.getMid());
+        if (userAdFeature == null) {
+            userAdFeature = new UserAdFeature();
         }
-        Map<Long,List<AdPlatformCreativeDTO>> groupMap=request
+        Map<Long, List<AdPlatformCreativeDTO>> groupMap = request
                 .getCreativeList()
                 .stream()
                 .collect(Collectors.groupingBy(creativeDTO -> creativeDTO.getCreativeId()));
 
 
-        Map<Long, AdRankItem> cache=adCreativeFeatureContainer.getAll(new ArrayList<>(groupMap.keySet()));
-        List<AdRankItem> rankItems=Collections.emptyList();
-        if(!cache.isEmpty()){
-            rankItems=new LinkedList<>(cache.values());
+        Map<Long, AdRankItem> cache = adCreativeFeatureContainer.getAll(new ArrayList<>(groupMap.keySet()));
+        List<AdRankItem> rankItems = Collections.emptyList();
+        if (!cache.isEmpty()) {
+            rankItems = new LinkedList<>(cache.values());
         }
-        double lambda=-1d;
-        for(AdRankItem item:rankItems){
+        double lambda = -1d;
+        for (AdRankItem item : rankItems) {
             try {
 
-                AdPlatformCreativeDTO dto=groupMap.get(item.getAdId()).get(0);
+                AdPlatformCreativeDTO dto = groupMap.get(item.getAdId()).get(0);
                 item.setBid1(dto.getBid1());
                 item.setBid2(dto.getBid2());
-                lambda=PidLambdaV2Container.getPidLambda(item.getAdId());
-                if(lambda<0){
+                lambda = PidLambdaV2Container.getPidLambda(item.getAdId());
+                if (lambda < 0) {
                     item.setCpa(dto.getCpa());
-                    item.setPidLambda(dto.getCpa()*0.6);
-                }else {
-                    if(dto.getCpa()>1&&lambda<=1){
-                        lambda=2d;
+                    item.setPidLambda(dto.getCpa() * 0.6);
+                } else {
+                    if (dto.getCpa() > 1 && lambda <= 1) {
+                        lambda = 2d;
                     }
                     item.setCpa(dto.getCpa());
                     item.setPidLambda(lambda);
                 }
 
-            }catch (Exception e){
-                log.error("rankItems info error itemId={}",item.getAdId());
+            } catch (Exception e) {
+                log.error("rankItems info error itemId={}", item.getAdId());
                 e.printStackTrace();
             }
         }
         List<AdRankItem> rankResult;
-        if(rankItems==null|| rankItems.size()==0){
-            rankItems=new LinkedList<>();
-            for(AdPlatformCreativeDTO dto:request.getCreativeList()){
-                AdRankItem item=new AdRankItem();
+        if (rankItems == null || rankItems.size() == 0) {
+            rankItems = new LinkedList<>();
+            for (AdPlatformCreativeDTO dto : request.getCreativeList()) {
+                AdRankItem item = new AdRankItem();
                 item.setBid1(dto.getBid1());
                 item.setBid2(dto.getBid2());
                 item.setAdId(dto.getCreativeId());
                 item.setItemFeature(new AdItemFeature());
-                lambda=PidLambdaV2Container.getPidLambda(item.getAdId());
+                lambda = PidLambdaV2Container.getPidLambda(item.getAdId());
 
-                if(lambda<0){
+                if (lambda < 0) {
                     item.setCpa(dto.getCpa());
-                    item.setPidLambda(dto.getCpa()*0.6);
-                }else {
-                    if(dto.getCpa()>1&&lambda<=1){
-                        lambda=2d;
+                    item.setPidLambda(dto.getCpa() * 0.6);
+                } else {
+                    if (dto.getCpa() > 1 && lambda <= 1) {
+                        lambda = 2d;
                     }
                     item.setCpa(dto.getCpa());
                     item.setPidLambda(lambda);
                 }
                 rankItems.add(item);
             }
-            rankResult=rankServiceThompson.rank(param, userAdFeature, rankItems,null);
-        }else {
-            rankResult=rank(param, userAdFeature, rankItems,ScorerUtils.BASE_CONF);
+            rankResult = rankServiceThompson.rank(param, userAdFeature, rankItems, null);
+        } else {
+            rankResult = rank(param, userAdFeature, rankItems, ScorerUtils.BASE_CONF);
         }
 
-        AdRankItem topItem=rankResult.get(0);
+        AdRankItem topItem = rankResult.get(0);
 
-        AdPlatformCreativeDTO result= AdPlatformCreativeDTO
+        AdPlatformCreativeDTO result = AdPlatformCreativeDTO
                 .builder()
                 .build();
-        BeanUtils.copyProperties(topItem,result);
+        BeanUtils.copyProperties(topItem, result);
         result.setCreativeId(topItem.getAdId());
         result.setPctr(topItem.getCtr());
         result.setPcvr(topItem.getCvr());
         result.setCreativeCode(groupMap.get(topItem.getAdId()).get(0).getCreativeCode());
-        double realECpm=0d;
-        realECpm=topItem.getEcpm2();
+        double realECpm = 0d;
+        realECpm = topItem.getEcpm2();
 
-        if(realECpm>cpmMax/1000d){
-            realECpm=cpmMax/1000d;
+        if (realECpm > cpmMax / 1000d) {
+            realECpm = cpmMax / 1000d;
         }
-        if(realECpm<cpmMin/1000d){
-            realECpm=cpmMin/1000d;
+        if (realECpm < cpmMin / 1000d) {
+            realECpm = cpmMin / 1000d;
         }
         result.setEcpm2(realECpm);
-        AdPlatformCreativeDTO dto=groupMap.get(topItem.getAdId()).get(0);
-        JSONObject object=new JSONObject();
-        object.put("mid",request.getMid());
-        object.put("adid",result.getCreativeId());
-        object.put("creativeCode",result.getCreativeCode());
-        object.put("type",topItem.getScore_type());
-        object.put("pctr",topItem.getCtr());
-        object.put("pcvr",topItem.getCvr());
-        object.put("lrsamples",topItem.getLrSampleString());
-        object.put("pidLambda",topItem.getPidLambda());
-        object.put("realECpm",realECpm);
-        object.put("creativeId",result.getCreativeId());
+        AdPlatformCreativeDTO dto = groupMap.get(topItem.getAdId()).get(0);
+        JSONObject object = new JSONObject();
+        object.put("mid", request.getMid());
+        object.put("adid", result.getCreativeId());
+        object.put("creativeCode", result.getCreativeCode());
+        object.put("type", topItem.getScore_type());
+        object.put("pctr", topItem.getCtr());
+        object.put("pcvr", topItem.getCvr());
+        object.put("lrsamples", topItem.getLrSampleString());
+        object.put("pidLambda", topItem.getPidLambda());
+        object.put("realECpm", realECpm);
+        object.put("creativeId", result.getCreativeId());
         object.put("videoId", request.getVideoId());
-        //CPA还原
-        object.put("cpa",dto.getCpa()*dto.getBid1());
-        object.put("oCpa",dto.getCpa());
+        // CPA还原
+        object.put("cpa", dto.getCpa() * dto.getBid1());
+        object.put("oCpa", dto.getCpa());
         object.put("pqtId", request.getPqtId());
-        object.put("dataTime",currentTime.format(timeFormatter));
+        object.put("dataTime", currentTime.format(timeFormatter));
         log.info("svc=adBidRankNewPid {}", JSONObject.toJSONString(object));
         object.remove("lrsamples");
         log.info("svc=pid_v2_log obj={}", JSONObject.toJSONString(object));
         return result;
     }
 
-    public List<AdRankItem> rank (ScoreParam param, UserAdFeature userAdFeature, List<AdRankItem> rankItems,String configFile){
+    public List<AdRankItem> rank(ScoreParam param, UserAdFeature userAdFeature, List<AdRankItem> rankItems, String configFile) {
         List<AdRankItem> rankResult = ScorerUtils
                 .getScorerPipeline(configFile)
                 .scoring(param, userAdFeature, rankItems);