liqian 2 年之前
父节点
当前提交
77a46d7056
共有 1 个文件被更改,包括 16 次插入10 次删除
  1. 16 10
      pool_predict.py

+ 16 - 10
pool_predict.py

@@ -39,7 +39,7 @@ def get_videos_from_flow_pool(app_type, size=1000):
     return videos
 
 
-def get_videos_remain_view_count(app_type, video_info_list):
+def get_videos_remain_view_count(video_info_list):
     """
     获取视频在流量池中的剩余可分发数,并存入对应的redis中
     :param app_type: 产品标识 type-int
@@ -52,10 +52,12 @@ def get_videos_remain_view_count(app_type, video_info_list):
 
     # 每次请求10个
     for i in range(len(video_info_list)//10 + 1):
+        remain_st_time = time.time()
         videos = [{'videoId': info[0], 'flowPool': info[1]} for info in video_info_list[i*10:(i+1)*10]]
-        request_data = {'appType': app_type, 'videos': videos}
+        request_data = {'videos': videos}
         result = request_post(request_url=config_.GET_REMAIN_VIEW_COUNT_URL,
                               request_data=request_data, timeout=(0.1, 3))
+        log_.info(f"i = {i}, expend time = {(time.time()-remain_st_time)*1000}")
         if result is None:
             continue
         if result['code'] != 0:
@@ -102,7 +104,7 @@ def get_score(video_ids):
     return [random.uniform(0, 100) for _ in range(len(video_ids))]
 
 
-def predict(app_type):
+def predict(app_type, video_info_list):
     """
     对流量池视频排序,并将结果上传Redis
     :param app_type: 产品标识 type-int
@@ -113,7 +115,7 @@ def predict(app_type):
         videos = get_videos_from_flow_pool(app_type=app_type)
         if len(videos) <= 0:
             log_.info('流量池中无需分发的视频')
-            return None
+            return video_info_list
         # video_id 与 flow_pool 进行mapping
         video_ids = set()
         log_.info('流量池中视频数:{}'.format(len(videos)))
@@ -134,14 +136,13 @@ def predict(app_type):
         log_.info('filter videos status finished, filtered_videos nums={}'.format(len(filtered_videos)))
         if not filtered_videos:
             log_.info('流量池中视频状态不符合分发')
-            return None
+            return video_info_list
         # 预测
         video_score = get_score(filtered_videos)
         log_.info('predict finished!')
         # 上传数据到redis
         redis_data = {}
         quick_flow_pool_redis_data = {}
-        video_info_list = []
         for i in range(len(video_score)):
             video_id = filtered_videos[i]
             score = video_score[i]
@@ -186,9 +187,6 @@ def predict(app_type):
 
         log_.info('data to redis finished!')
 
-        # 更新剩余分发数
-        get_videos_remain_view_count(app_type, video_info_list)
-
         return video_info_list
 
     except Exception as e:
@@ -199,6 +197,8 @@ def predict(app_type):
             key_word=config_.FEISHU_ROBOT['server_robot'].get('key_word'),
             msg_text='rov-offline{} - 流量池更新失败, appType: {}, exception: {}'.format(config_.ENV_TEXT, app_type, e)
         )
+        return video_info_list
+
 
 
 def get_data_from_odps(project, sql):
@@ -320,6 +320,7 @@ def predict_19(app_type):
 if __name__ == '__main__':
     app_type_list = [config_.APP_TYPE['LAO_HAO_KAN_VIDEO'], config_.APP_TYPE['ZUI_JING_QI']]
     log_.info('flow pool predict start...')
+    video_info_list = []
     for app_name, app_type in config_.APP_TYPE.items():
         log_.info('{} predict start...'.format(app_name))
         if app_type == config_.APP_TYPE['LAO_HAO_KAN_VIDEO']:
@@ -329,9 +330,14 @@ if __name__ == '__main__':
             # predict_19(app_type=app_type)
             continue
         else:
-            predict(app_type=app_type)
+            video_info_list = predict(app_type=app_type, video_info_list=video_info_list)
         log_.info('{} predict end...'.format(app_name))
 
+    # 更新剩余分发数
+    log_.info(f"video_info_list count = {len(video_info_list)}")
+    st_time = time.time()
+    get_videos_remain_view_count(video_info_list)
+    log_.info(f"expend time = {(time.time() - st_time) * 1000}")
     log_.info('flow pool predict end...')