Kaynağa Gözat

调整epcm lambda计算公式

gufengshou1 1 yıl önce
ebeveyn
işleme
b4f85c537a

+ 0 - 57
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/container/AbTestConfigContainer.java

@@ -25,68 +25,11 @@ public class AbTestConfigContainer {
     @Value("${ad.abtest.time.plan.code:555}")
     String adTestCode;
 
-    //List<Tuple<Integer>>
-    Map<String,List<JSONObject>> adTimePlanMap=new HashMap<>();
-//    Map<Integer,Map<String,List<JSONObject>>> appAdTimePlanMap=new ConcurrentHashMap<>() ;
-//    @PostConstruct
-//    public void initContainer(){
-//        Timer timer = new Timer();
-//        timer.schedule(new TimerTask() {
-//            @Override
-//            public void run() {
-//                try {
-////                    for (Integer appType:appAdTimePlanMap.keySet()){
-////                        initMap(appType,"ab_test002");
-////                    }
-//                    initMap(3,"ab_test002");
-////                    log.info("svc=refresh_adTimePlanMap appAdTimePlanMap={}",JSONObject.toJSONString(appAdTimePlanMap));
-//                    log.info("svc=refresh_adTimePlanMap adTimePlanMap={}",JSONObject.toJSONString(adTimePlanMap));
-//                }catch (Exception e){
-//                    e.printStackTrace();
-//                }
-//            }
-//        }, 0, 1000*30);
-//    }
-
-//    public void initMap(Integer appType,String typeValue){
-//        AbTestConfigRequest request=new  AbTestConfigRequest();
-//        if(appType==null){
-//            request.setAppType(0);
-//        }else{
-//            request.setAppType(appType);
-//        }
-//        request.setTypeValue(typeValue);
-//        CommonResponse<JSONObject> response=longVideoFeign.getAbTestConfig(request);
-//        log.info("svc=initAdTimePlanMap_getConfigFromLongVideoApi response={}",JSONObject.toJSONString(response));
-//        JSONObject object=response.getData();
-//        for(Map.Entry<String,Object> entry:object.getInnerMap().entrySet()){
-//            for(Map<String,Object> map:(List<Map>)entry.getValue()){
-//                if(adTestCode.equals(map.get("abExpCode").toString())){
-//                    adTimePlanMap=JSONObject.parseObject(map.get("configValue").toString(),Map.class);
-//                    break;
-//                }
-//            }
-//        }
-//    }
-
-//    public Boolean containsCode(String code){
-//        return adTimePlanMap.keySet().contains(code);
-//    }
 
     public Boolean containsCode(Map<String,List<JSONObject>> configMap,String code){
         return configMap.keySet().contains(code);
     }
 
-//    public Boolean inWithoutAdTime(String code,Integer hourOfDay){
-//        Boolean flag=false;
-//        for(JSONObject object:adTimePlanMap.getOrDefault(code,new ArrayList<>())){
-//            if(object.getInteger("x")<=hourOfDay&&hourOfDay<object.getInteger("y")){
-//                return true;
-//            }
-//        }
-//        return flag;
-//    }
-
     public Boolean inWithoutAdTime(Map<String,List<JSONObject>> abConfigMap,String code,Integer hourOfDay){
         Boolean flag=false;
         for(JSONObject object:abConfigMap.getOrDefault(code,new ArrayList<>())){

+ 101 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/container/RandWContainer.java

@@ -0,0 +1,101 @@
+package com.tzld.piaoquan.ad.engine.service.predict.container;
+
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.TypeReference;
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.common.auth.CredentialsProvider;
+import com.aliyun.oss.common.auth.DefaultCredentialProvider;
+import com.aliyun.oss.model.CopyObjectResult;
+import com.aliyun.oss.model.OSSObject;
+import com.aliyun.oss.model.PutObjectResult;
+import com.tzld.piaoquan.ad.engine.commons.redis.AlgorithmRedisHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.Random;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+@Component
+public class RandWContainer {
+    private final static Logger log = LoggerFactory.getLogger(RandWContainer.class);
+
+    @Autowired
+    private AlgorithmRedisHelper redisHelper;
+
+    private static final int SCHEDULE_PERIOD = 10;
+    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
+
+    String randWKey = "ad:engine:random:w";
+
+    OSS client;
+
+    public static Integer randW=0;
+
+    private static Random random=new Random();
+    private Date cacheDate;
+
+    @PostConstruct
+    private void init(){
+        final Runnable task = new Runnable() {
+            public void run() {
+                try {
+                    loadAndCalIfNeed();
+                }catch (Exception e){
+                    e.printStackTrace();
+                }
+            }
+        };
+        scheduler.scheduleAtFixedRate(task, 0, SCHEDULE_PERIOD, TimeUnit.SECONDS); // 60分钟
+    }
+
+
+    private void loadAndCalIfNeed(){
+        loadRandW();
+        Date now=new Date(System.currentTimeMillis()-60*60*1000);
+        if(cacheDate==null||now.after(cacheDate)){
+            calNewRandomW();
+            writeToRedis();
+        }
+    }
+
+    private void calNewRandomW() {
+        randW=random.nextInt(100);
+    }
+
+    private void writeToRedis(){
+        //先不考虑各种更新失败及重复更新问题。
+        redisHelper.set(randWKey,randW.toString()+"_"+System.currentTimeMillis(),0);
+    }
+
+    private void loadRandW(){
+        try {
+            String str=redisHelper.getString(randWKey);
+            if(str==null) return;
+            String[] arr=str.split("_");
+            randW=Integer.parseInt(arr[0]);
+            this.cacheDate=new Date(Long.parseLong(arr[1]));
+            System.out.println("randW="+randW);
+        }catch (Exception e){
+            log.error("svc=load_randomW status=failed error={}", Arrays.toString(e.getStackTrace()));
+            e.printStackTrace();
+        }
+    }
+
+    public static Integer getRandW(){
+        return randW;
+    }
+
+}

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

@@ -65,7 +65,6 @@ public class PredictModelServiceImpl implements PredictModelService {
     private String withoutAdVideoIds;
 
     public Map<String, Object> adPredict(ThresholdPredictModelRequestParam requestParam) {
-
         String[] withoutAdVideoIdsArr=withoutAdVideoIds.split(",");
         for(String videoId:withoutAdVideoIdsArr){
             if(videoId.equals(requestParam.getVideoId()+"")){
@@ -150,12 +149,12 @@ public class PredictModelServiceImpl implements PredictModelService {
         }
         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");
-        }
+//        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);
@@ -221,10 +220,11 @@ public class PredictModelServiceImpl implements PredictModelService {
         modelParam.setExtraParam(new HashMap<>());
         modelParam.addUserExtraFuture("shareType",shareType);
         setExtraParam(modelParam);
-        long startTime=System.currentTimeMillis();
-        String methodName="adPredictNew";
-        int step=1;
-        if (adPredictBreakSwitch || isHit) {
+        if(expCodes.contains("599")){
+            result = ThresholdModelContainer.
+                    getThresholdPredictModel("random")
+                    .predict(modelParam);
+        } else if (adPredictBreakSwitch || isHit) {
             if(expCodes.contains(adPredictNoShareUserExpCode)&&"noShare".equals(shareType)){
                 result = ThresholdModelContainer.
                         getThresholdPredictModel("share0")

+ 8 - 79
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/model/threshold/NoShareUserThresholdPredictModel.java

@@ -6,6 +6,7 @@ import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
 import com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils;
 import com.tzld.piaoquan.ad.engine.commons.util.CommonCollectionUtils;
 import com.tzld.piaoquan.ad.engine.service.predict.container.PredictPidContainer;
+import com.tzld.piaoquan.ad.engine.service.predict.container.RandWContainer;
 import com.tzld.piaoquan.ad.engine.service.predict.container.ThresholdModelContainer;
 import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdPredictModelParam;
 import com.tzld.piaoquan.ad.engine.service.remote.FeatureRemoteService;
@@ -18,12 +19,16 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
+import javax.annotation.PostConstruct;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 import static com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils.BREAK_CONFIG;
 import static com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils.SHARE0_CONFIG;
@@ -31,16 +36,13 @@ import static com.tzld.piaoquan.ad.engine.commons.score.ScorerUtils.SHARE0_CONFI
 @Component
 public class NoShareUserThresholdPredictModel extends ThresholdPredictModel {
     private final static Logger log = LoggerFactory.getLogger(NoShareUserThresholdPredictModel.class);
-    @Autowired
-    private FeatureRemoteService featureRemoteService;
-
-    Random random=new Random();
 
     @Value("${ad.predict.threshold.share0:0.4}")
     private double threshold;
     @Value("${ad.model.pid.type.share0:-1}")
     private double pidType;
 
+
     @Override
     String initName() {
         return "share0";
@@ -49,86 +51,13 @@ public class NoShareUserThresholdPredictModel extends ThresholdPredictModel {
     @Override
     public Map<String, Object> predict(ThresholdPredictModelParam modelParam) {
 
-//        UserAdFeature userAdFeature = featureRemoteService.getUserAdFeature(modelParam.getMid());
-//        if (userAdFeature == null) {
-//            userAdFeature = new UserAdFeature();
-//        }
-//        List<AdRankItem> rankItems = featureRemoteService.getAllAdFeatureList(CommonCollectionUtils.toList(AdConfig.getAdIds(), id -> id.toString()));
-//
-//        // scoreParam
-//        AdRequestContext context = new AdRequestContext();
-//        context.setApptype(modelParam.getAppType().toString());
-//        context.setMachineinfoBrand(modelParam.getMachineInfo().getBrand());
-//        context.setMachineinfoModel(modelParam.getMachineInfo().getModel());
-//        context.setMachineinfoSdkversion(modelParam.getMachineInfo().getSdkVersion());
-//        context.setMachineinfoWchatversion(modelParam.getMachineInfo().getWeChatVersion());
-//        LocalDateTime date = LocalDateTime.now();
-//        context.setHour(date.getHour() + "");
-//        context.setDay(date.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
-//        context.setWeek(date.getDayOfWeek().getValue() + "");
-//
-//        ScoreParam scoreParam = new ScoreParam();
-//        scoreParam.setRequestContext(context);
-//        scoreParam.getRequestContext().setRegion(modelParam.getRegion().replace("省", ""));
-//        scoreParam.getRequestContext().setCity(modelParam.getCity().replace("市", ""));
-//        scoreParam.setVideoId(modelParam.getVideoId());
-//        scoreParam.setMid(modelParam.getMid());
-//        scoreParam.setUid("");
-//        scoreParam.setProvince(modelParam.getRegion());
-//        scoreParam.setCity(modelParam.getCity());
-//        scoreParam.setExtraParam(modelParam.getExtraParam());
-//
-//        List<AdRankItem> scoreResult = ScorerUtils
-//                .getScorerPipeline(SHARE0_CONFIG)
-//                .scoring(scoreParam, userAdFeature, rankItems);
-//
-//        // 找出ctr*cvr最大的
-////        double max = -1;
-//        AdRankItem maxItem = scoreResult.get(0);
-////        for (int i = 0; i < scoreResult.size(); i++) {
-////            AdRankItem item = scoreResult.get(i);
-////            double ctrCvr = item.getCtr() * item.getCvr();
-////            if (ctrCvr > max) {
-////                max = ctrCvr;
-////                maxItem = item;
-////            }
-////        }
-//        double realThreshold=Double.parseDouble(
-//                scoreParam.getExtraParam().getOrDefault("Share0Predict_"+modelParam.getAppType(),threshold).toString()
-//        );
-//        int adPredict;
-//        //加入pid逻辑
-//        if(pidType>1){
-//            realThreshold=realThreshold+ PredictPidContainer.getPidLambda(
-//                    scoreParam.getExtraParam().getOrDefault("predict_test_id","default")+"_"+modelParam.getAppType());
-//        }else if(pidType>=0){
-//            realThreshold=PredictPidContainer.getLatestThreshold(
-//                    scoreParam.getExtraParam().getOrDefault("predict_test_id","default")+"_"+modelParam.getAppType());
-//        }
-//
-//
-//        if (maxItem != null && maxItem.getScore() < realThreshold) {
-//            // If final score is below threshold, do not show the ad
-//            adPredict = 1;
-//        } else {
-//            // Otherwise, show the ad
-//            adPredict = 2;
-//        }
-//        if(maxItem != null){
-//            //删除多余打印
-//            maxItem.setItemFeature(null);
-//            maxItem.setLrSampleString(null);
-//            maxItem.setLrSampleStringOrgin(null);
-//            log.info("svc=Share0ThresholdPredictModel_predict modelName=ScoreV2ThresholdPredictModel maxItem={} extraParam={} app_type={} realThreshold={}",
-//                    JSONObject.toJSONString(maxItem), JSONObject.toJSONString(scoreParam.getExtraParam()),modelParam.getAppType(),realThreshold);
-//        }
-        double score=modelParam.getMid().hashCode()%100/100d;
+        double score=(modelParam.getMid().hashCode()+ RandWContainer.getRandW())%100/100d;
         score=score<0?-score:score;
 
         Map<String, Object> result = new HashMap<>();
 //        result.put("threshold", realThreshold);
 //        result.put("score", maxItem == null ? -1 : maxItem.getScore());
-        result.put("ad_predict", score>threshold?2:1);
+        result.put("ad_predict", score<threshold?2:1);
         result.put("score", score);
 
         return result;

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

@@ -0,0 +1,50 @@
+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 com.tzld.piaoquan.ad.engine.service.remote.FeatureRemoteService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+@Component
+public class RandomPredictModel extends ThresholdPredictModel {
+    private final static Logger log = LoggerFactory.getLogger(RandomPredictModel.class);
+
+    @Override
+    String initName() {
+        return "random";
+    }
+
+
+    @Override
+    public Map<String, Object> predict(ThresholdPredictModelParam modelParam) {
+        int hash=modelParam.getMid().hashCode();
+        hash=hash<0?-hash:hash;
+        double score=(hash+ RandWContainer.getRandW())%100/100d;
+        double threshold=Double.parseDouble(
+                modelParam.getExtraParam().getOrDefault(modelParam.getAppType()+"_"+modelParam.getUserExtraFuture("shareType"),-1
+                ).toString());
+        if(threshold<0d){
+            threshold=Double.parseDouble(
+                    modelParam.getExtraParam().getOrDefault("default_threshold","0.5")
+                    .toString());
+        }
+        Map<String, Object> result = new HashMap<>();
+        result.put("ad_predict", score<threshold?2:1);
+        result.put("score", score);
+
+        return result;
+    }
+
+}