|
@@ -0,0 +1,77 @@
|
|
|
|
+import redis
|
|
|
|
+import random
|
|
|
|
+from models.model import R
|
|
|
|
+from utils import filter_utils
|
|
|
|
+from configs import config_redis, config_algo
|
|
|
|
+
|
|
|
|
+class pool_manager:
|
|
|
|
+ def __init__(self, k, p, mid, scene,counts=10):
|
|
|
|
+ self.k = k
|
|
|
|
+ self.p = p
|
|
|
|
+ self.mid = mid
|
|
|
|
+ self.scene = scene
|
|
|
|
+ self.counts = counts
|
|
|
|
+
|
|
|
|
+ #前k个按rov分值排序,后面total-k个,按p的概率从测试池取值,1-p的概率从召回池取
|
|
|
|
+ def get_rov_data(self, k,p):
|
|
|
|
+ res_all = []
|
|
|
|
+ res_recall = self._get_data_from_cache(config_redis.ROV_SERVICE_RECALL_VIDS, self.counts, self.mid)
|
|
|
|
+ res_pool = self._get_data_from_cache(config_redis.ROV_SERVICE_POOL_VIDS, self.counts, self.mid)
|
|
|
|
+ head_recall = 0
|
|
|
|
+ head_pool = 0
|
|
|
|
+ #TODO
|
|
|
|
+ #添加score及from来源
|
|
|
|
+ i = 0
|
|
|
|
+ item = None
|
|
|
|
+ while(i<k):
|
|
|
|
+ if res_recall[head_recall]['score']>res_pool[head_pool]['score']:
|
|
|
|
+ item = res_recall[head_recall]
|
|
|
|
+ head_recall += 1
|
|
|
|
+ else:
|
|
|
|
+ item = res_pool[head_pool]
|
|
|
|
+ head_pool += 1
|
|
|
|
+ res_all.append(item)
|
|
|
|
+ i += 1
|
|
|
|
+
|
|
|
|
+ i = 0
|
|
|
|
+ while(i<self.counts-k):
|
|
|
|
+ if random.randint(1,10)<p:
|
|
|
|
+ item = res_recall[head_recall]
|
|
|
|
+ head_recall += 1
|
|
|
|
+ else:
|
|
|
|
+ item = res_pool[head_pool]
|
|
|
|
+ head_pool += 1
|
|
|
|
+ res_all.append(item)
|
|
|
|
+ i+= 1
|
|
|
|
+
|
|
|
|
+ return res_all
|
|
|
|
+
|
|
|
|
+ #获取兜底数据
|
|
|
|
+ def get_backup_data(self, vid, start, counts):
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+ #检查vid是否已超量分发(只对测试池有效)
|
|
|
|
+ def check_over_distribute(self, vid):
|
|
|
|
+ pass
|
|
|
|
+
|
|
|
|
+ #从缓存中key取counts个数据
|
|
|
|
+ def _get_data_from_cache(self, key, counts, mid):
|
|
|
|
+ start = 0
|
|
|
|
+ end = counts
|
|
|
|
+ res = []
|
|
|
|
+ while(len(res)<counts):
|
|
|
|
+ _res = self._sub_get_data_from_cache(self, start, end, key, counts)
|
|
|
|
+ res.extend(_res)
|
|
|
|
+ start = end
|
|
|
|
+ end = start + counts
|
|
|
|
+ #TODO
|
|
|
|
+ #数量不足用backup数据补齐
|
|
|
|
+ return res
|
|
|
|
+
|
|
|
|
+ #从缓存中key取counts个数据
|
|
|
|
+ def _sub_get_data_from_cache(self, start, end, key, counts):
|
|
|
|
+ res = R.zrange(key, start, end, withscores=True)
|
|
|
|
+ res = filter_utils.filter_res_unavailable(res, self.scene)
|
|
|
|
+ res = filter_utils.filter_mid_viewed_videos(res, self.scene, self.mid)
|
|
|
|
+ res = filter_utils.filter_mid_viewed_videos(res, self.scene, self.mid)
|
|
|
|
+ return res
|