Browse Source

增加top1视频缓存

gufengshou1 1 year ago
parent
commit
d431d7d0c2

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

@@ -1,27 +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.tzld.piaoquan.ad.engine.service.predict.odps.ODPSClient;
-import org.springframework.beans.factory.annotation.Autowired;
+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};";
 
-    @Autowired
-    ODPSClient client;
 
-    Map<Integer,List<Integer>> videoMap=new HashMap<>();
 
+    @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(){
-        List<Record> recordList=client.getCurrentTopVideoRecords();
+        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(record.get("apptype"),new ArrayList<>());
+            List<Long> idList=tempMap.getOrDefault(Long.parseLong(record.get("apptype").toString()),new ArrayList<>());
             if(idList.size()==0){
-                tempMap.put((Long)record.get("apptype"),idList);
+                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;
     }
 }

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

@@ -11,8 +11,8 @@ import com.tzld.piaoquan.ad.engine.service.predict.PredictModelService;
 import com.tzld.piaoquan.ad.engine.service.predict.config.AbConfig;
 import com.tzld.piaoquan.ad.engine.service.predict.container.AbTestConfigContainer;
 import com.tzld.piaoquan.ad.engine.service.predict.container.ThresholdModelContainer;
+import com.tzld.piaoquan.ad.engine.service.predict.container.TopOneVideoContainer;
 import com.tzld.piaoquan.ad.engine.service.predict.param.RoiThresholdPredictModelParam;
-import com.tzld.piaoquan.ad.engine.service.predict.param.RuleParamHelper;
 import com.tzld.piaoquan.ad.engine.service.predict.param.ThresholdPredictModelParam;
 import com.tzld.piaoquan.ad.engine.service.predict.param.request.RoiPredictModelRequestParam;
 import com.tzld.piaoquan.ad.engine.service.predict.param.request.ThresholdPredictModelRequestParam;
@@ -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;
@@ -47,13 +50,13 @@ public class PredictModelServiceImpl implements PredictModelService {
         //先判断是否开启实验 和是否不出广告时间 而后判断默认0-8
         Map<String,Object> result=new HashMap<>();
         int hourOfDay= DateUtils.getCurrentHour();
-        if(container.containsCode(requestParam.getAbTestCode())
+        if(abTestConfigContainer.containsCode(requestParam.getAbTestCode())
                 &&
-                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;
-        }else  if(!container.containsCode(requestParam.getAbTestCode())&&(hourOfDay<8&&hourOfDay>=0)){
+        }else  if(!abTestConfigContainer.containsCode(requestParam.getAbTestCode())&&(hourOfDay<8&&hourOfDay>=0)){
             result.put("ad_predict", 1);
             result.put("no_ad_strategy","no_ad_time");
             return result;
@@ -108,8 +111,16 @@ public class PredictModelServiceImpl implements PredictModelService {
         }
 
 
-        Map<String,List<String>> noAdGroupWithVideoMapping=(Map) abtestParam.get("no_ad_group_with_video_mapping");
-
+        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 - 67
ad-engine-service/src/main/java/com/tzld/piaoquan/ad/engine/service/predict/odps/ODPSClient.java

@@ -1,67 +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 com.tzld.piaoquan.ad.engine.commons.util.DateUtils;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Service;
-
-import javax.annotation.PostConstruct;
-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;
-    public static final String sqlExp="select * from top_return_videolist_hh where dt={dt};";
-
-    private Odps odps;
-
-    @PostConstruct
-    public void initClient(){
-        Account account = new AliyunAccount(odpsAccessId, odpsAAccessKey);
-        odps = new Odps(account);
-        odps.setEndpoint(odpsEndpoint);
-        odps.setDefaultProject("loghubods");
-    }
-    public List<Record> getCurrentTopVideoRecords(){
-        List<Record> records=new ArrayList<>();
-        Instance i;
-        try {
-            i = SQLTask.run(odps, sqlExp.replace("{dt}",DateUtils.getCurrentDateStr("yyyyMMddHH")));
-            i.waitForSuccess();
-            records = SQLTask.getResult(i);
-        } catch (OdpsException e) {
-            e.printStackTrace();
-        }
-        return records;
-    }
-
-
-    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;
-        try {
-            i = SQLTask.run(odps, sqlExp.replace("{dt}",DateUtils.getCurrentDateStr("yyyyMMddHH")));
-            i.waitForSuccess();
-            List<Record> records = SQLTask.getResult(i);
-            for(Record r:records){
-                System.out.println(r.get("apptype").toString()+"----"+r.get("videoid").toString()+"----"+r.get("rank").toString());
-                System.out.println("-0-----------------");
-            }
-        } catch (OdpsException e) {
-            e.printStackTrace();
-        }
-        System.out.println();
-    }
-}