|
@@ -0,0 +1,111 @@
|
|
|
+package com.tzld.piaoquan.ad.engine.service.predict.v2;
|
|
|
+
|
|
|
+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
|
|
|
+import com.tzld.piaoquan.ad.engine.commons.util.NumUtil;
|
|
|
+import com.tzld.piaoquan.ad.engine.service.feature.Feature;
|
|
|
+import com.tzld.piaoquan.ad.engine.service.feature.FeatureService;
|
|
|
+import com.tzld.piaoquan.ad.engine.service.log.LogHubService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.MapUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class PredictServiceV2 {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FeatureService featureService;
|
|
|
+ @Autowired
|
|
|
+ private LogHubService logHubService;
|
|
|
+
|
|
|
+ @ApolloJsonValue("${exp.713.config:{}}")
|
|
|
+ private Map<String, Double> exp713Config;
|
|
|
+
|
|
|
+ public Map<String, Object> adPredict(PredictContext context) {
|
|
|
+
|
|
|
+ Feature feature = featureService.getPredictFeature(context.getMid());
|
|
|
+ Map<String, Map<String, String>> userFeature = feature.getUserFeature();
|
|
|
+
|
|
|
+ Map<String, String> featureMap = userFeature.getOrDefault("alg_ad_crowd_choose_feature", new HashMap<>());
|
|
|
+
|
|
|
+ boolean isShowAd = false;
|
|
|
+
|
|
|
+ context.getLogParam().setExpId("713");
|
|
|
+
|
|
|
+ // 没有特征为新用户,随机出广告
|
|
|
+ if (MapUtils.isEmpty(featureMap)) {
|
|
|
+ double newUserShowAdRate = exp713Config.getOrDefault("newUserShowAdRate", 1d);
|
|
|
+ double randomRate = Math.random();
|
|
|
+ if (randomRate < newUserShowAdRate) {
|
|
|
+ isShowAd = true;
|
|
|
+ context.getLogParam().getScoreMap().put("newUserShowAdRate", newUserShowAdRate);
|
|
|
+ context.getLogParam().getScoreMap().put("randomRate", randomRate);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ context.getLogParam().getMetaFeature().putAll(userFeature);
|
|
|
+ for (Map.Entry<String, String> entry : featureMap.entrySet()) {
|
|
|
+ context.getLogParam().getAllFeature().put(entry.getKey(), Double.parseDouble(entry.getValue()));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取需要的特征值
|
|
|
+ double showAdClickPv = Double.parseDouble(featureMap.getOrDefault("show_ad_click_pv", "0"));
|
|
|
+ double noShowAdClickPv = Double.parseDouble(featureMap.getOrDefault("no_show_ad_click_pv", "0"));
|
|
|
+ double showAdIncome = Double.parseDouble(featureMap.getOrDefault("show_ad_income", "0"));
|
|
|
+ double showAdSharePv = Double.parseDouble(featureMap.getOrDefault("show_ad_share_pv", "0"));
|
|
|
+ double noShowAdSharePv = Double.parseDouble(featureMap.getOrDefault("no_show_ad_share_pv", "0"));
|
|
|
+ double showAdNewReturnPv = Double.parseDouble(featureMap.getOrDefault("show_ad_new_return_pv", "0"));
|
|
|
+ double noShowAdNewReturnPv = Double.parseDouble(featureMap.getOrDefault("no_show_ad_new_return_pv", "0"));
|
|
|
+
|
|
|
+ // 计算中间过程值
|
|
|
+ double singleReturnAdIncome = NumUtil.div(showAdIncome, (showAdClickPv + noShowAdClickPv));
|
|
|
+ double showAdShareRate = NumUtil.div((showAdSharePv + 1), (showAdClickPv + 1));
|
|
|
+ double noShowAdShareRate = NumUtil.div((noShowAdSharePv + 1), (noShowAdClickPv + 1));
|
|
|
+ double returnDivShare = NumUtil.div((showAdNewReturnPv + noShowAdNewReturnPv + 1), (showAdSharePv + noShowAdSharePv + 1));
|
|
|
+
|
|
|
+ double alpha = exp713Config.getOrDefault("alpha", 0.1d);
|
|
|
+ double score = singleReturnAdIncome + ((showAdShareRate - noShowAdShareRate) * returnDivShare * alpha);
|
|
|
+
|
|
|
+ double showAdScoreThreshold = exp713Config.getOrDefault("showAdScoreThreshold", 0d);
|
|
|
+ if (score >= showAdScoreThreshold) {
|
|
|
+ isShowAd = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ context.getLogParam().getScoreMap().put("singleReturnAdIncome", singleReturnAdIncome);
|
|
|
+ context.getLogParam().getScoreMap().put("showAdShareRate", showAdShareRate);
|
|
|
+ context.getLogParam().getScoreMap().put("noShowAdShareRate", noShowAdShareRate);
|
|
|
+ context.getLogParam().getScoreMap().put("returnDivShare", returnDivShare);
|
|
|
+ context.getLogParam().getScoreMap().put("score", score);
|
|
|
+ context.getLogParam().getScoreMap().put("alpha", alpha);
|
|
|
+ context.getLogParam().getScoreMap().put("showAdScoreThreshold", showAdScoreThreshold);
|
|
|
+ context.getLogParam().setScore(score);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ logHubService.crowdChooseLogUpload(context);
|
|
|
+
|
|
|
+ if (isShowAd) {
|
|
|
+ Map<String, Object> rtnMap = rtnAdPredict();
|
|
|
+ rtnMap.putAll(context.getLogParam().getScoreMap());
|
|
|
+ return rtnMap;
|
|
|
+ } else {
|
|
|
+ return rtnNoAdPredict("713_exp");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> rtnNoAdPredict(String noAdStrategy) {
|
|
|
+ Map<String, Object> rtnMap = new HashMap<>();
|
|
|
+ rtnMap.put("ad_predict", 1);
|
|
|
+ rtnMap.put("no_ad_strategy", noAdStrategy);
|
|
|
+ return rtnMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> rtnAdPredict() {
|
|
|
+ Map<String, Object> rtnMap = new HashMap<>();
|
|
|
+ rtnMap.put("ad_predict", 1);
|
|
|
+ return rtnMap;
|
|
|
+ }
|
|
|
+}
|