cal_24h_score.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # coding utf-8
  2. import sys
  3. import traceback
  4. import pandas as pd
  5. from utils import send_msg_to_feishu
  6. from config import set_config
  7. from log import Log
  8. config_, _ = set_config()
  9. log_ = Log()
  10. features = [
  11. 'apptype',
  12. 'videoid',
  13. 'preview人数', # 过去24h预曝光人数
  14. 'view人数', # 过去24h曝光人数
  15. 'play人数', # 过去24h播放人数
  16. 'share人数', # 过去24h分享人数
  17. '回流人数', # 过去24h分享,过去24h回流人数
  18. 'preview次数', # 过去24h预曝光次数
  19. 'view次数', # 过去24h曝光次数
  20. 'play次数', # 过去24h播放次数
  21. 'share次数', # 过去24h分享次数
  22. 'platform_return',
  23. 'platform_preview',
  24. 'platform_preview_total',
  25. 'platform_show',
  26. 'platform_show_total',
  27. 'platform_view',
  28. 'platform_view_total',
  29. ]
  30. def data_group(data_path):
  31. """将数据按照videoid聚合(求和)"""
  32. f = open(data_path)
  33. index = 0
  34. data_dict = {}
  35. while True:
  36. line = f.readline()
  37. if not line:
  38. break
  39. if index == 0:
  40. index += 1
  41. continue
  42. index += 1
  43. items = line.strip().split(",")
  44. # print(items)
  45. if len(items) < len(features):
  46. continue
  47. video_id = items[1]
  48. if video_id not in data_dict:
  49. data_dict[video_id] = {'videoid': video_id}
  50. for i, feature in enumerate(features):
  51. if feature in ['apptype', 'videoid']:
  52. continue
  53. data_dict[video_id][feature] = int(float(items[i]))
  54. else:
  55. for i, feature in enumerate(features):
  56. if feature in ['apptype', 'videoid']:
  57. continue
  58. data_dict[video_id][feature] = data_dict[video_id][feature] + int(float(items[i]))
  59. f.close()
  60. data_list = [item for video_id, item in data_dict.items()]
  61. data_df = pd.DataFrame(data_list)
  62. return data_df
  63. def cal_score(data_df):
  64. """计算score"""
  65. df = data_df.copy()
  66. # score1 = 回流/(view+10)
  67. df['24h_score1'] = df['回流人数'] / (df['view人数'] + 10)
  68. score_df = df[['videoid', '24h_score1']]
  69. # print(score_df)
  70. return score_df
  71. if __name__ == "__main__":
  72. try:
  73. now_date = sys.argv[1]
  74. log_.info(f"now_date: {now_date}")
  75. data_path = f"./data/24h_video_data_{now_date}.csv"
  76. data_df = data_group(data_path=data_path)
  77. log_.info(f"24h data_df shape: {data_df.shape}")
  78. hour_score_path = f"./data/24h_score_{now_date}.csv"
  79. score_df = cal_score(data_df=data_df)
  80. score_df.to_csv(hour_score_path, index=False)
  81. log_.info(f"24h score_df shape: {score_df.shape}")
  82. except Exception as e:
  83. log_.error(f"rank 24h分值更新失败, exception: {e}, traceback: {traceback.format_exc()}")
  84. send_msg_to_feishu(
  85. webhook=config_.FEISHU_ROBOT['server_robot'].get('webhook'),
  86. key_word=config_.FEISHU_ROBOT['server_robot'].get('key_word'),
  87. msg_text=f"rov-offline{config_.ENV_TEXT} - rank 24h分值更新失败\n"
  88. f"exception: {e}\n"
  89. f"traceback: {traceback.format_exc()}"
  90. )