utils.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # coding:utf-8
  2. import pickle
  3. import os
  4. import requests
  5. import json
  6. from odps import ODPS
  7. from config import set_config
  8. from db_helper import HologresHelper
  9. config_ = set_config()
  10. def execute_sql_from_odps(project, sql, connect_timeout=3000, read_timeout=500000,
  11. pool_maxsize=1000, pool_connections=1000):
  12. odps = ODPS(
  13. access_id='LTAI4FtW5ZzxMvdw35aNkmcp',
  14. secret_access_key='0VKnydcaHK3ITjylbgUsLubX6rwiwc',
  15. project=project,
  16. endpoint='http://service.cn.maxcompute.aliyun.com/api',
  17. connect_timeout=connect_timeout,
  18. read_timeout=read_timeout,
  19. pool_maxsize=pool_maxsize,
  20. pool_connections=pool_connections
  21. )
  22. records = odps.execute_sql(sql=sql)
  23. return records
  24. def get_data_from_odps(date, project, table, connect_timeout=3000, read_timeout=500000,
  25. pool_maxsize=1000, pool_connections=1000):
  26. """
  27. 从odps获取数据
  28. :param date: 日期 type-string '%Y%m%d'
  29. :param project: type-string
  30. :param table: 表名 type-string
  31. :param connect_timeout: 连接超时设置
  32. :param read_timeout: 读取超时设置
  33. :param pool_maxsize:
  34. :param pool_connections:
  35. :return: records
  36. """
  37. odps = ODPS(
  38. access_id='LTAI4FtW5ZzxMvdw35aNkmcp',
  39. secret_access_key='0VKnydcaHK3ITjylbgUsLubX6rwiwc',
  40. project=project,
  41. endpoint='http://service.cn.maxcompute.aliyun.com/api',
  42. connect_timeout=connect_timeout,
  43. read_timeout=read_timeout,
  44. pool_maxsize=pool_maxsize,
  45. pool_connections=pool_connections
  46. )
  47. records = odps.read_table(name=table, partition='dt=%s' % date)
  48. return records
  49. def write_to_pickle(data, filename, filepath=config_.DATA_DIR_PATH):
  50. """
  51. 将数据写入pickle文件中
  52. :param data: 数据
  53. :param filename: 写入的文件名
  54. :param filepath: 文件存放路径,默认为config_.DATA_DIR_PATH
  55. :return: None
  56. """
  57. if not os.path.exists(filepath):
  58. os.makedirs(filepath)
  59. file = os.path.join(filepath, filename)
  60. with open(file, 'wb') as wf:
  61. pickle.dump(data, wf)
  62. def read_from_pickle(filename, filepath=config_.DATA_DIR_PATH):
  63. """
  64. 从pickle文件读取数据
  65. :param filename: 文件名
  66. :param filepath: 文件存放路径,默认为config_.DATA_DIR_PATH
  67. :return: data
  68. """
  69. file = os.path.join(filepath, filename)
  70. if not os.path.exists(file):
  71. return None
  72. with open(file, 'rb') as rf:
  73. data = pickle.load(rf)
  74. return data
  75. def request_post(request_url, request_data):
  76. """
  77. post 请求 HTTP接口
  78. :param request_url: 接口URL
  79. :param request_data: 请求参数
  80. :return: res_data json格式
  81. """
  82. response = requests.post(url=request_url, json=request_data)
  83. if response.status_code == 200:
  84. res_data = json.loads(response.text)
  85. return res_data
  86. def data_normalization(data):
  87. """
  88. 对结果做归一化处理(Min-Max Normalization),将分数控制在[0, 100]
  89. :param data: type-list
  90. :return: normal_data, type-list 归一化后的数据
  91. """
  92. x_max = max(data)
  93. x_min = min(data)
  94. normal_data = [(x-x_min)/(x_max-x_min)*100 for x in data]
  95. return normal_data
  96. def filter_video_status(video_ids):
  97. """
  98. 对视频状态进行过滤
  99. :param video_ids: 视频id列表 type-list
  100. :return: filtered_videos
  101. """
  102. if len(video_ids) == 1:
  103. sql = "set hg_experimental_enable_shard_pruning=off; " \
  104. "SELECT video_id " \
  105. "FROM {} " \
  106. "WHERE audit_status = 5 " \
  107. "AND applet_rec_status IN (1, -6) " \
  108. "AND open_status = 1 " \
  109. "AND payment_status = 0 " \
  110. "AND encryption_status IS NULL " \
  111. "AND transcoding_status = 3 " \
  112. "AND video_id IN ({});".format(config_.VIDEO_STATUS, video_ids[0])
  113. else:
  114. sql = "set hg_experimental_enable_shard_pruning=off; " \
  115. "SELECT video_id " \
  116. "FROM {} " \
  117. "WHERE audit_status = 5 " \
  118. "AND applet_rec_status IN (1, -6) " \
  119. "AND open_status = 1 " \
  120. "AND payment_status = 0 " \
  121. "AND encryption_status IS NULL " \
  122. "AND transcoding_status = 3 " \
  123. "AND video_id IN {};".format(config_.VIDEO_STATUS, tuple(video_ids))
  124. hologres_helper = HologresHelper()
  125. data = hologres_helper.get_data(sql=sql)
  126. filtered_videos = [temp[0] for temp in data]
  127. return filtered_videos
  128. if __name__ == '__main__':
  129. # data_test = [9.20273281e+03, 7.00795065e+03, 5.54813112e+03, 9.97402494e-01, 9.96402495e-01, 9.96402494e-01]
  130. # data_normalization(data_test)
  131. request_post(request_url=config_.NOTIFY_BACKEND_UPDATE_ROV_SCORE_URL, request_data={'videos': []})