Browse Source

Merge branch 'feature_gufengshou_20240117_fix530bug' into test

# Conflicts:
#	ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/container/AbTestConfigContainer.java
#	ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/impl/PredictModelServiceImpl.java
gufengshou1 1 year ago
parent
commit
2ab4d2ee32

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

@@ -1,4 +1,95 @@
 package com.tzld.piaoquan.ad.engine.service.predict.container;
 
+import com.aliyun.odps.Instance;
+import com.aliyun.odps.Odps;
+import com.aliyun.odps.OdpsException;
+import com.aliyun.odps.account.Account;
+import com.aliyun.odps.account.AliyunAccount;
+import com.aliyun.odps.data.Record;
+import com.aliyun.odps.task.SQLTask;
+import com.tzld.piaoquan.ad.engine.commons.util.DateUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.*;
+
+@Component
 public class TopOneVideoContainer {
+    private final static Logger log = LoggerFactory.getLogger(TopOneVideoContainer.class);
+    Map<Long,List<Long>> videoMap=new HashMap<>();
+
+    @Value("${ad.odps.access.id:LTAIWYUujJAm7CbH}")
+    public String odpsAccessId;
+    @Value("${ad.odps.access.key:RfSjdiWwED1sGFlsjXv0DlfTnZTG1P}")
+    public String odpsAAccessKey;
+    @Value("${ad.odps.endpoint:http://service.cn.maxcompute.aliyun.com/api}")
+    public String odpsEndpoint;
+    public static final String sqlExp="select * from top_return_videolist_hh where dt={dt};";
+
+
+
+    @PostConstruct
+    public void init(){
+        refreshTopVideoIdCache();
+        Timer timer = new Timer();
+        // 计算距离下一个小时的第15分钟还有多少毫秒
+        long delay = calculateDelay();
+        // 定时任务在每小时的第15分钟执行
+        timer.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                try {
+                    refreshTopVideoIdCache();
+                }catch (Exception e){
+                    log.error("svc=timerTask taskName=refreshTopVideoIdCache e={}",Arrays.toString(e.getStackTrace()));
+                }
+            }
+        }, delay, 60 * 60 * 1000);
+    }
+    public void refreshTopVideoIdCache(){
+        Account account = new AliyunAccount(odpsAccessId, odpsAAccessKey);
+        Odps odps = new Odps(account);
+        odps.setEndpoint(odpsEndpoint);
+        odps.setDefaultProject("loghubods");
+        List<Record> recordList=new ArrayList<>();
+        Instance i;
+        try {
+            i = SQLTask.run(odps, sqlExp.replace("{dt}", DateUtils.getCurrentDateStr("yyyyMMddHH")));
+            i.waitForSuccess();
+            recordList = SQLTask.getResult(i);
+        } catch (OdpsException e) {
+            e.printStackTrace();
+        }
+        Map<Long,List<Long>> tempMap=new HashMap<>();
+        for(Record record:recordList){
+            List<Long> idList=tempMap.getOrDefault(Long.parseLong(record.get("apptype").toString()),new ArrayList<>());
+            if(idList.size()==0){
+                tempMap.put(Long.parseLong(record.get("apptype").toString()),idList);
+            }
+            idList.add(Long.parseLong(record.get("videoid").toString()));
+        }
+        videoMap=tempMap;
+        System.out.println("刷新top1视频缓存成功");
+    }
+
+    public boolean inNoAdTopVideo(Long appType,Long videoId){
+
+        if(videoMap.getOrDefault(appType,new ArrayList<>()).contains(videoId)){
+            return true;
+        }
+        return false;
+    }
+
+    private static long calculateDelay() {
+        long currentTime = System.currentTimeMillis();
+        int currentMinute = (int) ((currentTime / 60000) % 60);
+        int secondsUntilNextHour = (60 - currentMinute) * 60;
+        int secondsUntilNextQuarterHour = (15 - (currentMinute % 15)) * 60;
+        long delay = (secondsUntilNextHour + secondsUntilNextQuarterHour) * 1000;
+        return delay;
+    }
 }

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

@@ -30,7 +30,10 @@ public class PredictModelServiceImpl implements PredictModelService {
     private String adOwnExperimentTier;
 
     @Autowired
-    private AbTestConfigContainer container;
+    private AbTestConfigContainer abTestConfigContainer;
+
+    @Autowired
+    private TopOneVideoContainer topOneVideoContainer;
 
     @Autowired
     AlgorithmRedisHelper redisHelper;
@@ -53,7 +56,7 @@ public class PredictModelServiceImpl implements PredictModelService {
                 &&
                 condition2
                 &&
-                container.inWithoutAdTime(requestParam.getAbTestCode(),hourOfDay)){
+                abTestConfigContainer.inWithoutAdTime(requestParam.getAbTestCode(),hourOfDay)){
             result.put("ad_predict", 1);
             result.put("no_ad_strategy","no_ad_time");
             return result;
@@ -111,9 +114,17 @@ public class PredictModelServiceImpl implements PredictModelService {
             return result;
         }
 
-        String[] noAdGroupWithVideoMapping=new String[0];
-        noAdGroupWithVideoMapping=((JSONArray) abtestParam.get("no_ad_mid_group_list")).toArray(noAdMidGroupList);
-
+        //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()

+ 0 - 52
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/odps/ODPSClient.java

@@ -1,52 +0,0 @@
-package com.tzld.piaoquan.ad.engine.service.predict.odps;
-
-import com.aliyun.odps.*;
-import com.aliyun.odps.account.Account;
-import com.aliyun.odps.account.AliyunAccount;
-import com.aliyun.odps.data.Record;
-import com.aliyun.odps.task.SQLTask;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Service;
-import java.util.*;
-@Service
-public class ODPSClient {
-
-    @Value("${ad.odps.access.id:LTAIWYUujJAm7CbH}")
-    public String odpsAccessId;
-    @Value("${ad.odps.access.key:RfSjdiWwED1sGFlsjXv0DlfTnZTG1P}")
-    public String odpsAAccessKey;
-    @Value("${ad.odps.endpoint:http://service.cn.maxcompute.aliyun.com/api}")
-    public String odpsEndpoint;
-
-    private Odps odps;
-
-    public void initClient(){
-        Account account = new AliyunAccount(odpsAccessId, odpsAAccessKey);
-        odps = new Odps(account);
-        odps.setEndpoint(odpsEndpoint);
-        odps.setDefaultProject("loghubods");
-    }
-
-
-    public static void main(String[] args) throws Exception{
-        Account account = new AliyunAccount("LTAIWYUujJAm7CbH", "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P");
-        Odps odps = new Odps(account);
-        odps.setEndpoint("http://service.cn.maxcompute.aliyun.com/api");
-        odps.setDefaultProject("loghubods");
-        Instance i;
-        String sql="select * from top_return_videolist_hh where dt=2024011710;";
-        try {
-            i = SQLTask.run(odps, sql);
-            i.waitForSuccess();
-            List<Record> records = SQLTask.getResult(i);
-            for(Record r:records){
-                System.out.println(r.get("apptype").toString());
-                System.out.println(r.get("videoid").toString());
-                System.out.println(r.get("rank").toString());
-                System.out.println("-0-----------------");
-            }
-        } catch (OdpsException e) {
-            e.printStackTrace();
-        }
-    }
-}