|
@@ -477,3 +477,112 @@ def ad_recommend_predict(app_type, mid, video_id, ab_exp_info, ab_test_code, car
|
|
|
except Exception as e:
|
|
|
log_.error(traceback.format_exc())
|
|
|
return None
|
|
|
+
|
|
|
+
|
|
|
+def ad_recommend_predict_with_roi(app_type, mid, video_id, ads, arpu, roi_param):
|
|
|
+ """
|
|
|
+ 广告推荐预测
|
|
|
+ :param app_type: app_type
|
|
|
+ :param mid: mid
|
|
|
+ :param video_id: video_id
|
|
|
+ :param ads: 需要发放广告列表 list
|
|
|
+ :param arpu: 上一周期arpu值
|
|
|
+ :param roi_param: 计算roi使用参数
|
|
|
+ :return: ad_predict, type-int, 1-不发放广告,2-发放广告
|
|
|
+ """
|
|
|
+ try:
|
|
|
+ now_date = datetime.datetime.today()
|
|
|
+ now_dt = datetime.datetime.strftime(now_date, '%Y%m%d')
|
|
|
+ ad_info = ads[0]
|
|
|
+ ad_id = ad_info['adId']
|
|
|
+ ad_type = ad_info['adType']
|
|
|
+ ecpm = float(ad_info['ecpm'])
|
|
|
+
|
|
|
+ # 获取参数
|
|
|
+ params = config_.PARAMS_NEW_STRATEGY[int(app_type)]
|
|
|
+ # 判断mid所属分组
|
|
|
+ group_class_key = params.get('group_class_key')
|
|
|
+ mid_group_key_name = f"{config_.KEY_NAME_PREFIX_MID_GROUP}{group_class_key}:{mid}"
|
|
|
+ mid_group = redis_helper.get_data_from_redis(key_name=mid_group_key_name)
|
|
|
+ if mid_group is None:
|
|
|
+ mid_group = 'mean_group'
|
|
|
+
|
|
|
+ # 获取用户组出广告后分享的概率
|
|
|
+ share_user_data_key = params['user'].get('data')
|
|
|
+ share_user_rule_key = params['user'].get('rule')
|
|
|
+ group_share_rate_key_with_ad = \
|
|
|
+ f"{config_.KEY_NAME_PREFIX_GROUP_WITH_AD}{share_user_data_key}:{share_user_rule_key}:{now_dt}"
|
|
|
+ if not redis_helper.key_exists(group_share_rate_key_with_ad):
|
|
|
+ redis_dt = datetime.datetime.strftime(now_date - datetime.timedelta(days=1), '%Y%m%d')
|
|
|
+ group_share_rate_key_with_ad = \
|
|
|
+ f"{config_.KEY_NAME_PREFIX_GROUP_WITH_AD}{share_user_data_key}:{share_user_rule_key}:{redis_dt}"
|
|
|
+ group_share_rate_with_ad = redis_helper.get_score_with_value(key_name=group_share_rate_key_with_ad,
|
|
|
+ value=mid_group)
|
|
|
+
|
|
|
+ # 获取视频出广告后分享的概率
|
|
|
+ share_video_data_key = params['video'].get('data')
|
|
|
+ video_share_rate_key_with_ad = f"{config_.KEY_NAME_PREFIX_VIDEO_WITH_AD}{share_video_data_key}:{now_dt}"
|
|
|
+ if not redis_helper.key_exists(video_share_rate_key_with_ad):
|
|
|
+ redis_dt = datetime.datetime.strftime(now_date - datetime.timedelta(days=1), '%Y%m%d')
|
|
|
+ video_share_rate_key_with_ad = f"{config_.KEY_NAME_PREFIX_VIDEO_WITH_AD}{share_video_data_key}:{redis_dt}"
|
|
|
+ video_share_rate_with_ad = redis_helper.get_score_with_value(key_name=video_share_rate_key_with_ad,
|
|
|
+ value=int(video_id))
|
|
|
+ if video_share_rate_with_ad is None:
|
|
|
+ video_share_rate_with_ad = redis_helper.get_score_with_value(key_name=video_share_rate_key_with_ad,
|
|
|
+ value=-1)
|
|
|
+
|
|
|
+ # 获取用户组不出广告后分享的概率
|
|
|
+ group_share_rate_key_no_ad = \
|
|
|
+ f"{config_.KEY_NAME_PREFIX_GROUP_NO_AD}{share_user_data_key}:{share_user_rule_key}:{now_dt}"
|
|
|
+ if not redis_helper.key_exists(group_share_rate_key_no_ad):
|
|
|
+ redis_dt = datetime.datetime.strftime(now_date - datetime.timedelta(days=1), '%Y%m%d')
|
|
|
+ group_share_rate_key_no_ad = \
|
|
|
+ f"{config_.KEY_NAME_PREFIX_GROUP_NO_AD}{share_user_data_key}:{share_user_rule_key}:{redis_dt}"
|
|
|
+ group_share_rate_no_ad = redis_helper.get_score_with_value(key_name=group_share_rate_key_no_ad, value=mid_group)
|
|
|
+
|
|
|
+ # 获取视频不出广告后分享的概率
|
|
|
+ video_share_rate_key_no_ad = f"{config_.KEY_NAME_PREFIX_VIDEO_NO_AD}{share_video_data_key}:{now_dt}"
|
|
|
+ if not redis_helper.key_exists(video_share_rate_key_with_ad):
|
|
|
+ redis_dt = datetime.datetime.strftime(now_date - datetime.timedelta(days=1), '%Y%m%d')
|
|
|
+ video_share_rate_key_no_ad = f"{config_.KEY_NAME_PREFIX_VIDEO_NO_AD}{share_video_data_key}:{redis_dt}"
|
|
|
+ video_share_rate_no_ad = redis_helper.get_score_with_value(key_name=video_share_rate_key_no_ad,
|
|
|
+ value=int(video_id))
|
|
|
+ if video_share_rate_no_ad is None:
|
|
|
+ video_share_rate_no_ad = redis_helper.get_score_with_value(key_name=video_share_rate_key_no_ad, value=-1)
|
|
|
+
|
|
|
+ if group_share_rate_with_ad is None or video_share_rate_with_ad is None \
|
|
|
+ or group_share_rate_no_ad is None or video_share_rate_no_ad is None:
|
|
|
+ return None
|
|
|
+ # 计算此次请求出广告后分享的概率
|
|
|
+ share_rate_with_ad = float(group_share_rate_with_ad) * float(video_share_rate_with_ad)
|
|
|
+ # 计算此次请求不出广告分享的概率
|
|
|
+ share_rate_no_ad = float(group_share_rate_no_ad) * float(video_share_rate_no_ad)
|
|
|
+ # 计算此次请求出广告的收入增益
|
|
|
+ roi_ad = ecpm / 1000 - float(roi_param) * float(arpu) * (share_rate_no_ad - share_rate_with_ad)
|
|
|
+ # 收入增益判断
|
|
|
+ if roi_ad > 0:
|
|
|
+ # 大于0,出广告
|
|
|
+ ad_predict = 2
|
|
|
+ else:
|
|
|
+ # 否则,不出广告
|
|
|
+ ad_predict = 1
|
|
|
+ result = {
|
|
|
+ 'arpu': arpu,
|
|
|
+ 'roi_param': roi_param,
|
|
|
+ 'ad_id': ad_id,
|
|
|
+ 'ad_type': ad_type,
|
|
|
+ 'mid_group': mid_group,
|
|
|
+ 'group_share_rate_with_ad': group_share_rate_with_ad,
|
|
|
+ 'video_share_rate_with_ad': video_share_rate_with_ad,
|
|
|
+ 'group_share_rate_no_ad': group_share_rate_no_ad,
|
|
|
+ 'video_share_rate_no_ad': video_share_rate_no_ad,
|
|
|
+ 'share_rate_with_ad': share_rate_with_ad,
|
|
|
+ 'share_rate_no_ad': share_rate_no_ad,
|
|
|
+ 'roi_ad': roi_ad,
|
|
|
+ 'ad_predict': ad_predict
|
|
|
+ }
|
|
|
+ return result
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ log_.error(traceback.format_exc())
|
|
|
+ return None
|