Browse Source

feat:添加广告打分排序日志

zhaohaipeng 11 months ago
parent
commit
b215a9f819

+ 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.19</version>
+            <version>1.1.20</version>
         </dependency>
         <dependency>
             <groupId>com.tzld.piaoquan</groupId>

+ 1 - 0
ad-engine-server/src/main/resources/application.yml

@@ -91,6 +91,7 @@ aliyun:
       request: request-log
       info: info-log
       error: error-log
+      statistics: statistics-log
       landingTypeFilter: landingpagetype-filter-error-log
       adPackageData: ad-package-data
     topic:

+ 13 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/log/LogHubService.java

@@ -0,0 +1,13 @@
+package com.tzld.piaoquan.ad.engine.service.log;
+
+import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
+import com.tzld.piaoquan.ad.engine.service.score.dto.AdPlatformCreativeDTO;
+import com.tzld.piaoquan.ad.engine.service.score.param.RecommendRequestParam;
+import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
+
+import java.util.List;
+
+public interface LogHubService {
+
+    void scoreLogUpload(ScoreParam param, List<AdPlatformCreativeDTO> adIdList, List<AdRankItem> rankItems, RecommendRequestParam requestParam, String scoreStrategy);
+}

+ 65 - 0
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/log/impl/LogHubServiceImpl.java

@@ -0,0 +1,65 @@
+package com.tzld.piaoquan.ad.engine.service.log.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.tzld.commons.aliyun.log.AliyunLogManager;
+import com.tzld.piaoquan.ad.engine.commons.score.ScoreParam;
+import com.tzld.piaoquan.ad.engine.service.log.LogHubService;
+import com.tzld.piaoquan.ad.engine.service.score.dto.AdPlatformCreativeDTO;
+import com.tzld.piaoquan.ad.engine.service.score.param.RecommendRequestParam;
+import com.tzld.piaoquan.recommend.feature.domain.ad.base.AdRankItem;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.LinkedBlockingDeque;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+public class LogHubServiceImpl implements LogHubService {
+
+    private static final ThreadPoolExecutor logUploadThreadPool = new ThreadPoolExecutor(20, 40, 30,
+            TimeUnit.MINUTES, new LinkedBlockingDeque<>(), new ThreadPoolExecutor.CallerRunsPolicy());
+
+    @Value("${aliyun.log.logstore.statistics}")
+    private String logStore;
+    @Value("${aliyun.log.project}")
+    private String project;
+
+    @Autowired
+    private AliyunLogManager aliyunLogManager;
+
+    @Override
+    public void scoreLogUpload(ScoreParam param, List<AdPlatformCreativeDTO> adIdList, List<AdRankItem> rankItems, RecommendRequestParam requestParam, String scoreStrategy) {
+        logUploadThreadPool.execute(new Runnable() {
+            @Override
+            public void run() {
+                JSONObject logMap = new JSONObject();
+                logMap.put("pqtId", param.getPqtId());
+                logMap.put("mid", param.getMid());
+                logMap.put("videoId", param.getVideoId());
+                // 获取AB实验列表
+                Set<String> abExpCode = new HashSet<>();
+                if (CollectionUtils.isNotEmpty(requestParam.getAdAbExpArr())) {
+                    for (Map<String, Object> map : requestParam.getAdAbExpArr()) {
+                        if (map.containsKey("abExpCode")) {
+                            abExpCode.add(map.get("abExpCode").toString());
+                        }
+                    }
+                }
+                logMap.put("abExpCode", abExpCode);
+                logMap.put("adAbGroup", requestParam.getAdAbGroup());
+                logMap.put("adIdList", adIdList);
+                logMap.put("scoreResult", rankItems);
+                logMap.put("scoreStrategy", scoreStrategy);
+                logMap.put("appType", requestParam.getAppType());
+                aliyunLogManager.sendLog(project, logStore, "", logMap);
+            }
+        });
+    }
+}

+ 35 - 5
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/VideoAdThompsonScorerV2.java

@@ -34,7 +34,7 @@ public class VideoAdThompsonScorerV2 {
     public List<AdRankItem> thompsonScorerByExp663(ScoreParam param, List<AdPlatformCreativeDTO> adIdList){
         List<AdRankItem> result=new LinkedList<>();
         String jsonStr;
-        CreativeStatistic statistic;
+        CreativeStatistic statistic = null;
         List<String> redisKey=new LinkedList<>();
         adIdList.forEach(creativeDTO -> {
             redisKey.add(redisCreativeStatisticsPrefix+creativeDTO.getCreativeId());
@@ -59,6 +59,8 @@ public class VideoAdThompsonScorerV2 {
                             / (exp663Param.getOrDefault("beta",10000d)+Long.parseLong(statistic.getExp())) ;
                 }
                 score=score*dto.getCpa()*dto.getBid1()*dto.getBid2();
+                Map<String, Object> ext = this.extMap(statistic, "663", dto.getCpa(), null, exp663Param.getOrDefault("alpha", 1d), exp663Param.getOrDefault("beta", 10000d), null);
+                item.setExt(ext);
                 item.setScore(score);
             }catch (Exception e){
                 log.error("svc=thompsonScorerByExp663 {}",gson.toJson(e.getStackTrace()));
@@ -72,7 +74,7 @@ public class VideoAdThompsonScorerV2 {
     public List<AdRankItem> thompsonScorerByExp664(ScoreParam param,List<AdPlatformCreativeDTO> adIdList){
         List<AdRankItem> result=new LinkedList<>();
         String jsonStr;
-        CreativeStatistic statistic;
+        CreativeStatistic statistic=null;
         List<String> redisKey=new LinkedList<>();
         adIdList.forEach(creativeDTO -> {
             redisKey.add(redisVideoCreativeStatisticsPrefix+param.getVideoId()+"_"+creativeDTO.getCreativeId());
@@ -99,6 +101,9 @@ public class VideoAdThompsonScorerV2 {
                 }
                 score=score*dto.getCpa()*dto.getBid1()*dto.getBid2();
                 item.setScore(score);
+
+                Map<String, Object> ext = this.extMap(statistic, "664", dto.getCpa(), null, exp663Param.getOrDefault("alpha", 1d), exp663Param.getOrDefault("beta", 10000d), null);
+                item.setExt(ext);
             }catch (Exception e){
                 log.error("svc=thompsonScorerByExp664 {}",gson.toJson(e.getStackTrace()));
             }
@@ -112,7 +117,7 @@ public class VideoAdThompsonScorerV2 {
     public List<AdRankItem> thompsonScorerByExp665(ScoreParam param,List<AdPlatformCreativeDTO> adIdList){
         List<AdRankItem> result=new LinkedList<>();
         String jsonStr;
-        CreativeStatistic statistic;
+        CreativeStatistic statistic = null;
         List<String> redisKey=new LinkedList<>();
         adIdList.forEach(creativeDTO -> {
             redisKey.add(redisCreativeStatisticsPrefix+creativeDTO.getCreativeId());
@@ -138,6 +143,8 @@ public class VideoAdThompsonScorerV2 {
                 score=score*dto.getCpa()*dto.getBid1()*dto.getBid2();
                 item.setScore(score);
 
+                Map<String, Object> ext = this.extMap(statistic, "665", dto.getCpa(), null, exp663Param.getOrDefault("alpha", 1d), exp663Param.getOrDefault("beta", 10000d), null);
+                item.setExt(ext);
             }catch (Exception e){
                 log.error("svc=thompsonScorerByExp665 {}",gson.toJson(e.getStackTrace()));
             }
@@ -151,7 +158,7 @@ public class VideoAdThompsonScorerV2 {
     public List<AdRankItem> thompsonScorerByExp666(ScoreParam param,List<AdPlatformCreativeDTO> adIdList){
         List<AdRankItem> result=new LinkedList<>();
         String jsonStr;
-        CreativeStatistic statistic;
+        CreativeStatistic statistic = null;
 
         List<String> redisKey=new LinkedList<>();
         adIdList.forEach(creativeDTO -> {
@@ -184,7 +191,7 @@ public class VideoAdThompsonScorerV2 {
                 }else {
                     statistic =gson.fromJson(jsonStr,CreativeStatistic.class);
                     if(Double.parseDouble(statistic.getExp())>exp666Param.getOrDefault("viewThreshold",5000d)){
-                        score = betaSampler(1d+Long.parseLong(statistic.getOrder()) ,
+                        score = betaSampler(exp666Param.getOrDefault("alpha",1d)+Long.parseLong(statistic.getOrder()) ,
                                 (exp666Param.getOrDefault("beta",100000d)+Long.parseLong(statistic.getExp()))/(1+exp666Param.getOrDefault("beta_k",9d))) ;
                     } else if( values.get(i)!=null) {
                         statistic =gson.fromJson(values.get(i),CreativeStatistic.class);
@@ -197,6 +204,10 @@ public class VideoAdThompsonScorerV2 {
                 score=score*dto.getCpa()*dto.getBid1()*dto.getBid2();
                 item.setScore(score);
 
+                Map<String, Object> ext = this.extMap(statistic, "665", dto.getCpa(), exp666Param.getOrDefault("viewThreshold",5000d), exp663Param.getOrDefault("alpha", 1d),
+                        exp663Param.getOrDefault("beta", 10000d), exp666Param.getOrDefault("beta_k",9d));
+                item.setExt(ext);
+
             }catch (Exception e){
                 log.error("svc=thompsonScorerByExp666 {}",gson.toJson(e.getStackTrace()));
             }
@@ -206,6 +217,25 @@ public class VideoAdThompsonScorerV2 {
         Collections.sort(result);
         return result;
     }
+
+    private Map<String, Object> extMap(CreativeStatistic statistic, String abCode, Double cpa, Double viewThreshold, Double alpha, Double beta, Double beta_k) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("abCode", abCode);
+        if (Objects.nonNull(viewThreshold)) {
+            map.put("viewThreshold", viewThreshold);
+        }
+        map.put("alpha", alpha);
+        map.put("beta", beta);
+        map.put("beta_k", beta_k);
+        map.put("cpa", cpa);
+        if (Objects.nonNull(statistic)) {
+            map.put("click", statistic.click);
+            map.put("order", statistic.getOrder());
+            map.put("exp", statistic.getExp());
+        }
+        return map;
+    }
+
     class CreativeStatistic{
         public CreativeStatistic() {
         }

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

@@ -1,6 +1,7 @@
 package com.tzld.piaoquan.ad.engine.service.score.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.ad.engine.service.log.LogHubService;
 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;
@@ -42,6 +43,8 @@ public class RankServiceImpl implements RankService {
     RankServiceThompsonImpl rankServiceThompson;
     @Autowired
     VideoAdThompsonScorerV2 videoAdThompsonScorerV2;
+    @Autowired
+    private LogHubService logHubService;
 
     @Autowired
     AdCreativeFeatureContainer adCreativeFeatureContainer;
@@ -249,7 +252,8 @@ public class RankServiceImpl implements RankService {
             rankResult = videoAdThompsonScorerV2.thompsonScorerByExp666(param, request.getAdIdList());
         }
 
-
+        // 日志上报
+        logHubService.scoreLogUpload(param, request.getAdIdList(), rankResult, request, "video_ad_thompson");
 
         if (!CollectionUtils.isEmpty(rankResult)) {
             JSONObject object=new JSONObject();

+ 2 - 1
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/score/param/RecommendRequestParam.java

@@ -20,5 +20,6 @@ public class RecommendRequestParam {
     String city = "-1";
     Integer newExpGroup;
     String pqtId;
-    List<Map> adAbExpArr ;
+    List<Map<String, Object>> adAbExpArr ;
+    String adAbGroup;
 }