check_video_limit_distribute_new.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import gevent
  2. import datetime
  3. import numpy as np
  4. from my_config import set_config
  5. from log import Log
  6. from my_utils import RedisHelper
  7. config_, _ = set_config()
  8. log_ = Log()
  9. redis_helper = RedisHelper()
  10. def update_limit_video_score(initial_videos):
  11. """
  12. 调整限流视频的分数: 将视频移至所在列表的中位数之后,多个视频时,按照原本的顺序进行排列
  13. :param initial_videos: 视频列表及score type-dict, {videoId: score, ...}
  14. :return:
  15. """
  16. if not initial_videos:
  17. return
  18. # 获取当前限流视频
  19. data = redis_helper.get_data_from_redis(key_name=config_.KEY_NAME_PREFIX_LIMIT_VIDEOS)
  20. if data is None:
  21. return initial_videos
  22. # 获取限流视频对应的score
  23. limit_video_initial_score = []
  24. for video in eval(data):
  25. video_id = int(video[0])
  26. initial_score = initial_videos.get(video_id, None)
  27. if initial_score is not None:
  28. limit_video_initial_score.append((video_id, initial_score))
  29. log_.info(f"limit_video_initial_score = {limit_video_initial_score}")
  30. if len(limit_video_initial_score) == 0:
  31. return initial_videos
  32. # 获取原始列表的分数的中位数
  33. initial_video_score_list = sorted([val for key, val in initial_videos.items()], reverse=False)
  34. media_score = np.median(initial_video_score_list)
  35. # 取中位数后一位
  36. if len(initial_video_score_list) % 2 == 0:
  37. temp_index = len(initial_video_score_list)//2
  38. else:
  39. temp_index = len(initial_video_score_list) // 2 + 1
  40. if len(initial_video_score_list) > 1:
  41. temp_score = initial_video_score_list[temp_index]
  42. else:
  43. temp_score = 0
  44. # 对限流视频score进行调整
  45. limit_video_final_score = {}
  46. limit_video_initial_score.sort(key=lambda x: x[1], reverse=True)
  47. limit_video_id_list = []
  48. for video_id, initial_score in limit_video_initial_score:
  49. if initial_score > media_score:
  50. limit_video_id_list.append(video_id)
  51. if len(limit_video_id_list) > 0:
  52. limit_score_step = (temp_score - media_score) / (len(limit_video_id_list) + 1)
  53. for i, video_id in enumerate(limit_video_id_list):
  54. final_score = media_score - limit_score_step * (i + 1)
  55. limit_video_final_score[int(video_id)] = final_score
  56. initial_videos[int(video_id)] = final_score
  57. log_.info(f"media_score = {media_score}, temp_score = {temp_score}, "
  58. f"limit_video_final_score = {limit_video_final_score}")
  59. return initial_videos