ad_recommend.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import datetime
  2. from utils import RedisHelper
  3. from config import set_config
  4. config_ = set_config()
  5. redis_helper = RedisHelper()
  6. def get_params(ab_exp_info, ab_test_code):
  7. """
  8. 根据实验分组给定对应的参数
  9. :param ab_exp_info: AB实验组参数
  10. :param ab_test_code: 用户对应的ab组
  11. :return:
  12. """
  13. abtest_id, abtest_config_tag = None, None
  14. ad_abtest_id_list = [key.split('-')[0] for key in config_.AD_ABTEST_CONFIG]
  15. # 获取广告实验配置
  16. config_value_dict = {}
  17. if ab_exp_info:
  18. ab_exp_list = ab_exp_info.get('ab_test003', None)
  19. if ab_exp_list:
  20. for ab_item in ab_exp_list:
  21. ab_exp_code = ab_item.get('abExpCode', None)
  22. if not ab_exp_code:
  23. continue
  24. if ab_exp_code in ad_abtest_id_list:
  25. config_value = ab_item.get('configValue', None)
  26. if config_value:
  27. config_value_dict[str(ab_exp_code)] = eval(str(config_value))
  28. if len(config_value_dict) > 0:
  29. for ab_exp_code, config_value in config_value_dict.items():
  30. for tag, value in config_value:
  31. if ab_test_code in value:
  32. abtest_id = ab_exp_code
  33. abtest_config_tag = tag
  34. break
  35. return abtest_id, abtest_config_tag
  36. def ad_recommend_predict(app_type, mid, video_id, ab_exp_info, ab_test_code):
  37. """
  38. 广告推荐预测
  39. :param app_type: app_type
  40. :param mid: mid
  41. :param video_id: video_id
  42. :param ab_exp_info: AB实验组参数
  43. :param ab_test_code: 用户对应的ab组
  44. :return: ad_predict, type-int, 1-不发放广告,2-发放广告
  45. """
  46. now_date = datetime.datetime.today()
  47. now_dt = datetime.datetime.strftime(now_date, '%Y%m%d')
  48. # 获取实验参数
  49. abtest_id, abtest_config_tag = get_params(ab_exp_info=ab_exp_info, ab_test_code=ab_test_code)
  50. # 判断mid所属分组
  51. mid_group_key_name = f"{config_.KEY_NAME_PREFIX_MID_GROUP}{mid}"
  52. mid_group = redis_helper.get_data_from_redis(key_name=mid_group_key_name)
  53. if mid_group is None:
  54. mid_group = 'mean_group'
  55. # 判断用户是否在免广告用户组列表中
  56. if mid_group in config_.NO_AD_MID_GROUP_LIST:
  57. # 在免广告用户组列表中,则不出广告
  58. ad_predict = 1
  59. result = {
  60. 'mid_group': mid_group,
  61. 'ad_predict': ad_predict
  62. }
  63. else:
  64. # 获取用户组分享率
  65. group_share_rate_key = f"{config_.KEY_NAME_PREFIX_AD_GROUP}{now_dt}"
  66. if not redis_helper.key_exists(group_share_rate_key):
  67. redis_dt = datetime.datetime.strftime(now_date - datetime.timedelta(days=1), '%Y%m%d')
  68. group_share_rate_key = f"{config_.KEY_NAME_PREFIX_AD_GROUP}{redis_dt}"
  69. group_share_rate = redis_helper.get_score_with_value(key_name=group_share_rate_key, value=mid_group)
  70. # 获取视频分享率
  71. video_share_rate_key = f"{config_.KEY_NAME_PREFIX_AD_VIDEO}{now_dt}"
  72. if not redis_helper.key_exists(video_share_rate_key):
  73. redis_dt = datetime.datetime.strftime(now_date - datetime.timedelta(days=1), '%Y%m%d')
  74. video_share_rate_key = f"{config_.KEY_NAME_PREFIX_AD_VIDEO}{redis_dt}"
  75. video_share_rate = redis_helper.get_score_with_value(key_name=video_share_rate_key, value=int(video_id))
  76. if video_share_rate is None:
  77. video_share_rate = redis_helper.get_score_with_value(key_name=video_share_rate_key, value=-1)
  78. # 计算 mid-video 分享率
  79. mid_video_share_rate = float(group_share_rate) * float(video_share_rate)
  80. # 获取对应的阈值
  81. threshold_key_name = f"{config_.KEY_NAME_PREFIX_AD_THRESHOLD}{app_type}:{mid_group}"
  82. threshold = redis_helper.get_data_from_redis(key_name=threshold_key_name)
  83. if threshold is None:
  84. threshold = 0
  85. else:
  86. threshold = float(threshold)
  87. # 阈值判断
  88. if mid_video_share_rate > threshold:
  89. # 大于阈值,出广告
  90. ad_predict = 2
  91. else:
  92. # 否则,不出广告
  93. ad_predict = 1
  94. result = {
  95. 'mid_group': mid_group,
  96. 'group_share_rate': group_share_rate,
  97. 'video_share_rate': video_share_rate,
  98. 'mid_video_share_rate': mid_video_share_rate,
  99. 'threshold': threshold,
  100. 'ad_predict': ad_predict}
  101. return result