utils.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # coding:utf-8
  2. import pickle
  3. import os
  4. import requests
  5. import json
  6. import traceback
  7. from odps import ODPS
  8. from config import set_config
  9. from db_helper import HologresHelper, MysqlHelper, RedisHelper
  10. from log import Log
  11. config_, env = set_config()
  12. log_ = Log()
  13. def execute_sql_from_odps(project, sql, connect_timeout=3000, read_timeout=500000,
  14. pool_maxsize=1000, pool_connections=1000):
  15. odps = ODPS(
  16. access_id='LTAI4FtW5ZzxMvdw35aNkmcp',
  17. secret_access_key='0VKnydcaHK3ITjylbgUsLubX6rwiwc',
  18. project=project,
  19. endpoint='http://service.cn.maxcompute.aliyun.com/api',
  20. connect_timeout=connect_timeout,
  21. read_timeout=read_timeout,
  22. pool_maxsize=pool_maxsize,
  23. pool_connections=pool_connections
  24. )
  25. records = odps.execute_sql(sql=sql)
  26. return records
  27. def get_data_from_odps(date, project, table, connect_timeout=3000, read_timeout=500000,
  28. pool_maxsize=1000, pool_connections=1000):
  29. """
  30. 从odps获取数据
  31. :param date: 日期 type-string '%Y%m%d'
  32. :param project: type-string
  33. :param table: 表名 type-string
  34. :param connect_timeout: 连接超时设置
  35. :param read_timeout: 读取超时设置
  36. :param pool_maxsize:
  37. :param pool_connections:
  38. :return: records
  39. """
  40. odps = ODPS(
  41. access_id='LTAI4FtW5ZzxMvdw35aNkmcp',
  42. secret_access_key='0VKnydcaHK3ITjylbgUsLubX6rwiwc',
  43. project=project,
  44. endpoint='http://service.cn.maxcompute.aliyun.com/api',
  45. connect_timeout=connect_timeout,
  46. read_timeout=read_timeout,
  47. pool_maxsize=pool_maxsize,
  48. pool_connections=pool_connections
  49. )
  50. records = odps.read_table(name=table, partition='dt=%s' % date)
  51. return records
  52. def write_to_pickle(data, filename, filepath=config_.DATA_DIR_PATH):
  53. """
  54. 将数据写入pickle文件中
  55. :param data: 数据
  56. :param filename: 写入的文件名
  57. :param filepath: 文件存放路径,默认为config_.DATA_DIR_PATH
  58. :return: None
  59. """
  60. if not os.path.exists(filepath):
  61. os.makedirs(filepath)
  62. file = os.path.join(filepath, filename)
  63. with open(file, 'wb') as wf:
  64. pickle.dump(data, wf)
  65. def read_from_pickle(filename, filepath=config_.DATA_DIR_PATH):
  66. """
  67. 从pickle文件读取数据
  68. :param filename: 文件名
  69. :param filepath: 文件存放路径,默认为config_.DATA_DIR_PATH
  70. :return: data
  71. """
  72. file = os.path.join(filepath, filename)
  73. if not os.path.exists(file):
  74. return None
  75. with open(file, 'rb') as rf:
  76. data = pickle.load(rf)
  77. return data
  78. def send_msg_to_feishu(msg_text):
  79. """发送消息到飞书"""
  80. # webhook地址
  81. webhook = 'https://open.feishu.cn/open-apis/bot/v2/hook/8de4de35-30ed-4692-8854-7a154e89b2f2'
  82. # 自定义关键词key_word
  83. key_word = '服务报警'
  84. headers = {'Content-Type': 'application/json'}
  85. payload_message = {
  86. "msg_type": "text",
  87. "content": {
  88. "text": '{}: {}'.format(key_word, msg_text)
  89. }
  90. }
  91. response = requests.request('POST', url=webhook, headers=headers, data=json.dumps(payload_message))
  92. print(response.text)
  93. def request_post(request_url, request_data):
  94. """
  95. post 请求 HTTP接口
  96. :param request_url: 接口URL
  97. :param request_data: 请求参数
  98. :return: res_data json格式
  99. """
  100. try:
  101. response = requests.post(url=request_url, json=request_data)
  102. if response.status_code == 200:
  103. res_data = json.loads(response.text)
  104. return res_data
  105. else:
  106. return None
  107. except Exception as e:
  108. log_.error('url: {}, exception: {}, traceback: {}'.format(request_url, e, traceback.format_exc()))
  109. send_msg_to_feishu('rov-offline{} - 接口请求失败:{}, exception: {}'.format(config_.ENV_TEXT, request_url, e))
  110. return None
  111. def data_normalization(data):
  112. """
  113. 对结果做归一化处理(Min-Max Normalization),将分数控制在[0, 100]
  114. :param data: type-list
  115. :return: normal_data, type-list 归一化后的数据
  116. """
  117. x_max = max(data)
  118. x_min = min(data)
  119. normal_data = [(x-x_min)/(x_max-x_min)*100 for x in data]
  120. return normal_data
  121. def filter_video_status(video_ids):
  122. """
  123. 对视频状态进行过滤
  124. :param video_ids: 视频id列表 type-list
  125. :return: filtered_videos
  126. """
  127. if len(video_ids) == 1:
  128. sql = "set hg_experimental_enable_shard_pruning=off; " \
  129. "SELECT video_id " \
  130. "FROM {} " \
  131. "WHERE audit_status = 5 " \
  132. "AND applet_rec_status IN (1, -6) " \
  133. "AND open_status = 1 " \
  134. "AND payment_status = 0 " \
  135. "AND encryption_status != 5 " \
  136. "AND transcoding_status = 3 " \
  137. "AND video_id IN ({});".format(config_.VIDEO_STATUS, video_ids[0])
  138. else:
  139. sql = "set hg_experimental_enable_shard_pruning=off; " \
  140. "SELECT video_id " \
  141. "FROM {} " \
  142. "WHERE audit_status = 5 " \
  143. "AND applet_rec_status IN (1, -6) " \
  144. "AND open_status = 1 " \
  145. "AND payment_status = 0 " \
  146. "AND encryption_status != 5 " \
  147. "AND transcoding_status = 3 " \
  148. "AND video_id IN {};".format(config_.VIDEO_STATUS, tuple(video_ids))
  149. hologres_helper = HologresHelper()
  150. data = hologres_helper.get_data(sql=sql)
  151. filtered_videos = [int(temp[0]) for temp in data]
  152. return filtered_videos
  153. def update_video_w_h_rate(video_ids, key_name):
  154. """
  155. 获取横屏视频的宽高比,并存入redis中 (width/height>1)
  156. :param video_ids: videoId列表 type-list
  157. :param key_name: redis key
  158. :return: None
  159. """
  160. # 获取数据
  161. if len(video_ids) == 1:
  162. sql = "SELECT id, width, height, rotate FROM longvideo.wx_video WHERE id = {};".format(video_ids[0])
  163. else:
  164. sql = "SELECT id, width, height, rotate FROM longvideo.wx_video WHERE id IN {};".format(tuple(video_ids))
  165. mysql_helper = MysqlHelper()
  166. data = mysql_helper.get_data(sql=sql)
  167. # 更新到redis
  168. info_data = {}
  169. for video_id, width, height, rotate in data:
  170. if int(width) == 0 or int(height) == 0:
  171. continue
  172. # rotate 字段值为 90或270时,width和height的值相反
  173. if int(rotate) in (90, 270):
  174. w_h_rate = int(height) / int(width)
  175. else:
  176. w_h_rate = int(width) / int(height)
  177. if w_h_rate > 1:
  178. info_data[int(video_id)] = w_h_rate
  179. redis_helper = RedisHelper()
  180. # 删除旧数据
  181. redis_helper.del_keys(key_name=key_name)
  182. # 写入新数据
  183. if len(info_data) > 0:
  184. redis_helper.add_data_with_zset(key_name=key_name, data=info_data)
  185. if __name__ == '__main__':
  186. # data_test = [9.20273281e+03, 7.00795065e+03, 5.54813112e+03, 9.97402494e-01, 9.96402495e-01, 9.96402494e-01]
  187. # data_normalization(data_test)
  188. # request_post(request_url=config_.NOTIFY_BACKEND_UPDATE_ROV_SCORE_URL, request_data={'videos': []})
  189. video_ids = [110, 112, 113, 115, 116, 117, 8289883]
  190. update_video_w_h_rate(video_ids=video_ids, key_name='')