소스 검색

Merge branch 'feature_yxh_ad_out' of algorithm/rov-server into master

yangxiaohui 1 년 전
부모
커밋
5002f68f36
1개의 변경된 파일46개의 추가작업 그리고 17개의 파일을 삭제
  1. 46 17
      ad_recommend.py

+ 46 - 17
ad_recommend.py

@@ -303,26 +303,50 @@ def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abte
     item_key_name = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_SCORE_ITEM}{model_key}:{video_id}"
     item_key_name = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_SCORE_ITEM}{model_key}:{video_id}"
     config_key_prefix = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_CONFIG}{model_key}:{abtest_id}:{abtest_config_tag}"
     config_key_prefix = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_CONFIG}{model_key}:{abtest_id}:{abtest_config_tag}"
     threshold_key = f"{config_key_prefix}:threshold"
     threshold_key = f"{config_key_prefix}:threshold"
+    use_backup_key = f"{config_key_prefix}:use_backup"
+    item_threshold_key = f"{config_key_prefix}:item_threshold"
+    user_threshold_key = f"{config_key_prefix}:user_threshold"
+    miss_threshold_key = f"{config_key_prefix}:miss_threshold"
 
 
     user_score = redis_helper.get_data_from_redis(key_name=user_key_name)
     user_score = redis_helper.get_data_from_redis(key_name=user_key_name)
     item_score = redis_helper.get_data_from_redis(key_name=item_key_name)
     item_score = redis_helper.get_data_from_redis(key_name=item_key_name)
-
+    hit_strategy = 'model'
     # 如果离线分数为空,则走基线逻辑
     # 如果离线分数为空,则走基线逻辑
-    if user_score is None or item_score is None:
-        result = predict_mid_video_res(
-            now_date=now_date,
-            mid=mid,
-            video_id=video_id,
-            abtest_param=abtest_param,
-            abtest_id=abtest_id,
-            abtest_config_tag=abtest_config_tag,
-            ab_test_code=ab_test_code,
-            care_model_status=care_model_status,
-            app_type=app_type
-        )
-        return result
+    if user_score is not None and item_score is not None:
+        offline_score = float(user_score) + float(item_score)
+    else:
+        use_backup = redis_helper.get_data_from_redis(key_name=use_backup_key)
+        # 如果离线分数为空 & 兜底策略开启,走兜底策略
+        if use_backup == 'true':
+            result = predict_mid_video_res(
+                now_date=now_date,
+                mid=mid,
+                video_id=video_id,
+                abtest_param=abtest_param,
+                abtest_id=abtest_id,
+                abtest_config_tag=abtest_config_tag,
+                ab_test_code=ab_test_code,
+                care_model_status=care_model_status,
+                app_type=app_type
+            )
+            if result is not None:
+                hit_strategy = 'backup'
+                result['hit_strategy'] = hit_strategy
+            return result
+        else:
+            if item_score is not None:
+                offline_score = float(item_score)
+                threshold_key = item_threshold_key
+                hit_strategy = 'item'
+            elif user_score is not None:
+                offline_score = float(user_score)
+                threshold_key = user_threshold_key
+                hit_strategy = 'user'
+            else:  # item_score and user_score all None
+                offline_score = 0.0
+                threshold_key = miss_threshold_key
+                hit_strategy = 'miss'
 
 
-    offline_score = float(user_score) + float(item_score)
     online_features = {
     online_features = {
         'ctx_apptype': str(app_type),
         'ctx_apptype': str(app_type),
         'ctx_week': time.strftime('%w', time.localtime()),
         'ctx_week': time.strftime('%w', time.localtime()),
@@ -330,8 +354,12 @@ def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abte
     }
     }
 
 
     final_score, online_score = get_final_score(online_features, offline_score)
     final_score, online_score = get_final_score(online_features, offline_score)
-    threshold = float(redis_helper.get_data_from_redis(key_name=threshold_key))
-
+    threshold = redis_helper.get_data_from_redis(key_name=threshold_key)
+    if threshold is None:
+        threshold = 0.0
+        hit_strategy = 'error_' + hit_strategy
+    else:
+        threshold = float(threshold)
     # 跳出率阈值判断
     # 跳出率阈值判断
     if final_score < threshold:
     if final_score < threshold:
         # 小于阈值,出广告
         # 小于阈值,出广告
@@ -347,6 +375,7 @@ def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abte
         'threshold': threshold,
         'threshold': threshold,
         'ad_predict': ad_predict,
         'ad_predict': ad_predict,
         'online_features': online_features,
         'online_features': online_features,
+        'hit_strategy': hit_strategy
     }
     }
     return result
     return result