Browse Source

修改为并行打分

gufengshou1 1 year ago
parent
commit
72baeb9702

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

@@ -54,7 +54,7 @@ public class PredictModelServiceImpl implements PredictModelService {
 
     @Value("${ad.predict.break.switch:false}")
     private boolean adPredictBreakSwitch;
-    @Value("${ad.predict.break.exp.code:0}")
+    @Value("${ad.predict.break.exp.code:570}")
     private String adPredictBreakExpCode;
 
     @Value("${ad.predict.param.testIds:0}")
@@ -78,38 +78,9 @@ public class PredictModelServiceImpl implements PredictModelService {
                 }
             }
         }
-        boolean isHit = false;
-
-        try {
-            JSONObject object = requestParam.getAbExpInfo();
-            if (object != null) {
-                JSONArray array = object.getJSONArray("ab_test002");
-                if (array != null) {
-                    List<Map<String, String>> abExps = JSONUtils.fromJson(array.toJSONString(),
-                            new TypeToken<List<Map<String, String>>>() {
-                            }, Collections.emptyList());
-                    for (Map<String, String> abExpMap : abExps) {
-                        if (StringUtils.equals(abExpMap.get("abExpCode"), adPredictBreakExpCode)) {
-                            isHit = true;
-                            break;
-                        }
-                    }
-                }
-            }
-        } catch (Exception e) {
-            log.error("adPredict hit error", e);
-        }
-
-        if (adPredictBreakSwitch || isHit) {
-            return adPredictNew(requestParam);
-        } else {
-            return adPredictOld(requestParam);
-        }
-    }
-
-    public Map<String, Object> adPredictNew(ThresholdPredictModelRequestParam requestParam) {
-        JSONObject object=requestParam.getAbExpInfo();
-        //临时解决方案
+        //先判断是否开启实验 和是否不出广告时间 而后判断默认0-8
+        //不出广告时间判定
+        Map<String,Object> result=new HashMap<>();
         List<Map<String,Object>> mapList=(List)requestParam.getAbExpInfo().get("ab_test002");
         Map<String,List<JSONObject>> configMap=new HashMap<>();
         for(Map<String,Object> map:mapList){
@@ -118,8 +89,6 @@ public class PredictModelServiceImpl implements PredictModelService {
                 break;
             }
         }
-        //先判断是否开启实验 和是否不出广告时间 而后判断默认0-8
-        Map<String,Object> result=new HashMap<>();
         int hourOfDay= DateUtils.getCurrentHour();
         Boolean condition1=abTestConfigContainer.inAdTimeTest(requestParam.getAbExpInfo());
         Boolean condition2=abTestConfigContainer.containsCode(configMap,requestParam.getAbTestCode());
@@ -143,125 +112,27 @@ public class PredictModelServiceImpl implements PredictModelService {
         }
 
 
-        String[] abParamArr = abConfig.getAbParams(requestParam.getAbTestCode(), requestParam.getAbExpInfo());
-        if (abParamArr == null) {
-            result.put("msg", "abConfig_error");
-            return result;
-        }
-        String abtestId = abParamArr[0];
-        String abTestConfigTag = abParamArr[1];
-
-        if("173".equals(abtestId)&&(requestParam.getAbTestCode().equals("ab3")||requestParam.getAbTestCode().equals("ab4"))){
-            requestParam.setAbTestCode("ab5");
-        }
-        if("243".equals(abtestId)&&(requestParam.getAbTestCode().equals("ab1")||requestParam.getAbTestCode().equals("ab2")||requestParam.getAbTestCode().equals("ab3"))){
-            requestParam.setAbTestCode("ab5");
-        }
-
-        HashMap<String, Map<String, Object>> abConfigMap = abConfig.getAbConfigMap();
-        Map<String, Object> abtestParam = abConfigMap.getOrDefault(abtestId + "-" + abTestConfigTag, null);
-        if (abtestParam == null) {
-            result.put("msg", "abConfig_error");
-            return result;
-        }
-        //市-中文
-        requestParam.setRegion(requestParam.getRegion().replace("省", ""));
-        requestParam.setCity(requestParam.getCity().replace("市", ""));
-
-
-        // Determine the group to which mid belongs
-        String groupClassKey = (String) abtestParam.get("group_class_key");
-        String midGroupKeyName = RuleRedisKeyConst.KEY_NAME_PREFIX_MID_GROUP + groupClassKey + ":" + requestParam.getMid();
-        String midGroup = redisHelper.getString(midGroupKeyName);
-        if (midGroup == null) {
-            midGroup = "mean_group";
-        }
-
-        String[] noAdMidGroupList = new String[0];
-        noAdMidGroupList = ((JSONArray) abtestParam.get("no_ad_mid_group_list")).toArray(noAdMidGroupList);
-
-        boolean inNoAdGroup = false;
-        for (String group : noAdMidGroupList) {
-            if (group.equals(midGroup)) {
-                inNoAdGroup = true;
-                break;
-            }
-        }
-        //不出广告组
-        if (inNoAdGroup) {
-            // User is in the no-ad group, no ad should be shown
-            result.put("mid_group", midGroup);
-            result.put("ad_predict", 1);
-            result.put("no_ad_strategy", "no_ad_mid_group_with_video");
-            return result;
-        }
-
-        //top1广告不出视频
-        Map<String, List<String>> noAdGroupWithVideoMapping = (Map) abtestParam.getOrDefault("no_ad_group_with_video_mapping", new HashMap<>());
-        if (noAdGroupWithVideoMapping.keySet().contains(midGroup)
-                &&
-                topOneVideoContainer.inNoAdTopVideo(requestParam.getAppType().longValue(), requestParam.getVideoId())
-        ) {
-            result.put("mid_group", midGroup);
-            result.put("ad_predict", 1);
-            result.put("no_ad_strategy", "no_ad_mid_group_with_video");
-            return result;
-        }
-
-        //设置信息
-        ThresholdPredictModelParam modelParam = ThresholdPredictModelParam.builder()
-                .build();
-        BeanUtils.copyProperties(requestParam, modelParam);
-        modelParam.setDate(new Date());
-        modelParam.setAbtestId(abtestId);
-        modelParam.setAbTestConfigTag(abTestConfigTag);
-        modelParam.setAbtestParam(abtestParam);
-        modelParam.setMidGroup(midGroup);
-        modelParam.setExtraParam(new HashMap<>());
-        setExtraParam(modelParam);
-        result = ThresholdModelContainer.
-                getThresholdPredictModel("modelV2")
-                .predict(modelParam);
-
-        return result;
-    }
-
-    public Map<String, Object> adPredictOld(ThresholdPredictModelRequestParam requestParam) {
+        boolean isHit = false;
 
-        JSONObject object=requestParam.getAbExpInfo();
-        //临时解决方案
-        List<Map<String,Object>> mapList=(List)requestParam.getAbExpInfo().get("ab_test002");
-        Map<String,List<JSONObject>> configMap=new HashMap<>();
-        for(Map<String,Object> map:mapList){
-            if("555".equals(map.getOrDefault("abExpCode",""))){
-                configMap=JSONObject.parseObject(map.get("configValue").toString(),Map.class);
-                break;
+        try {
+            JSONObject object = requestParam.getAbExpInfo();
+            if (object != null) {
+                JSONArray array = object.getJSONArray("ab_test002");
+                if (array != null) {
+                    List<Map<String, String>> abExps = JSONUtils.fromJson(array.toJSONString(),
+                            new TypeToken<List<Map<String, String>>>() {
+                            }, Collections.emptyList());
+                    for (Map<String, String> abExpMap : abExps) {
+                        if (StringUtils.equals(abExpMap.get("abExpCode"), adPredictBreakExpCode)) {
+                            isHit = true;
+                            break;
+                        }
+                    }
+                }
             }
+        } catch (Exception e) {
+            log.error("adPredict hit error", e);
         }
-        //先判断是否开启实验 和是否不出广告时间 而后判断默认0-8
-        Map<String,Object> result=new HashMap<>();
-        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)){
-            result.put("ad_predict", 1);
-            result.put("no_ad_strategy", "no_ad_time_with_time_plan");
-            return result;
-        } else if (
-                (!condition1 || (condition1 && !condition2))
-                        &&
-                        (0 <= hourOfDay && hourOfDay < 8)) {
-            result.put("ad_predict", 1);
-            result.put("no_ad_strategy", "no_ad_time_with_fixed_time");
-            return result;
-        }
-
 
         String[] abParamArr = abConfig.getAbParams(requestParam.getAbTestCode(), requestParam.getAbExpInfo());
         if (abParamArr == null) {
@@ -339,11 +210,17 @@ public class PredictModelServiceImpl implements PredictModelService {
         modelParam.setMidGroup(midGroup);
         modelParam.setExtraParam(new HashMap<>());
         setExtraParam(modelParam);
-        Object thresholdMixFunc = abtestParam.getOrDefault("threshold_mix_func", "basic");
-        result = ThresholdModelContainer.
-                getThresholdPredictModel(thresholdMixFunc.toString())
-                .predict(modelParam);
 
+        if (adPredictBreakSwitch || isHit) {
+            result = ThresholdModelContainer.
+                    getThresholdPredictModel("modelV2")
+                    .predict(modelParam);
+        } else {
+            Object thresholdMixFunc = abtestParam.getOrDefault("threshold_mix_func", "basic");
+            result = ThresholdModelContainer.
+                    getThresholdPredictModel(thresholdMixFunc.toString())
+                    .predict(modelParam);
+        }
         return result;
     }
 
@@ -375,7 +252,6 @@ public class PredictModelServiceImpl implements PredictModelService {
                 .predict(modelParam);
     }
 
-
     public void setExtraParam(ThresholdPredictModelParam modelParam){
         String[] ids=testIds.split(",");
         List<String> idList=Arrays.asList(ids);