pool_manager.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import redis
  2. import random
  3. from models.model import R
  4. from utils import filter_utils
  5. from configs import config_redis, config_algo
  6. class pool_manager:
  7. def __init__(self, k, p, mid, scene,counts=10):
  8. self.k = k
  9. self.p = p
  10. self.mid = mid
  11. self.scene = scene
  12. self.counts = counts
  13. #前k个按rov分值排序,后面total-k个,按p的概率从测试池取值,1-p的概率从召回池取
  14. def get_rov_data(self, k,p):
  15. res_all = []
  16. res_recall = self._get_data_from_cache(config_redis.ROV_SERVICE_RECALL_VIDS, self.counts, self.mid)
  17. res_pool = self._get_data_from_cache(config_redis.ROV_SERVICE_POOL_VIDS, self.counts, self.mid)
  18. head_recall = 0
  19. head_pool = 0
  20. #TODO
  21. #添加score及from来源
  22. i = 0
  23. item = None
  24. while(i<k):
  25. if res_recall[head_recall]['score']>res_pool[head_pool]['score']:
  26. item = res_recall[head_recall]
  27. head_recall += 1
  28. else:
  29. item = res_pool[head_pool]
  30. head_pool += 1
  31. res_all.append(item)
  32. i += 1
  33. i = 0
  34. while(i<self.counts-k):
  35. if random.randint(1,10)<p:
  36. item = res_recall[head_recall]
  37. head_recall += 1
  38. else:
  39. item = res_pool[head_pool]
  40. head_pool += 1
  41. res_all.append(item)
  42. i+= 1
  43. return res_all
  44. #获取兜底数据
  45. def get_backup_data(self, vid, start, counts):
  46. pass
  47. #检查vid是否已超量分发(只对测试池有效)
  48. def check_over_distribute(self, vid):
  49. pass
  50. #从缓存中key取counts个数据
  51. def _get_data_from_cache(self, key, counts, mid):
  52. start = 0
  53. end = counts
  54. res = []
  55. while(len(res)<counts):
  56. _res = self._sub_get_data_from_cache(self, start, end, key, counts)
  57. res.extend(_res)
  58. start = end
  59. end = start + counts
  60. #TODO
  61. #数量不足用backup数据补齐
  62. return res
  63. #从缓存中key取counts个数据
  64. def _sub_get_data_from_cache(self, start, end, key, counts):
  65. res = R.zrange(key, start, end, withscores=True)
  66. res = filter_utils.filter_res_unavailable(res, self.scene)
  67. res = filter_utils.filter_mid_viewed_videos(res, self.scene, self.mid)
  68. res = filter_utils.filter_mid_viewed_videos(res, self.scene, self.mid)
  69. return res