UnitHourlyReport.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/9/15 5:10 下午
  4. # @Site :
  5. # @File : UnitHourlyReport.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. import logging
  9. from math import ceil
  10. import pandas as pd
  11. from sqlalchemy import create_engine
  12. from Apietl.utlis.Utils import Utils
  13. from TidbConn import TidbConn
  14. class UnitHourlyReport:
  15. def __init__(self):
  16. self.conn = TidbConn.get_connection()
  17. self.parm = Utils.get_args()
  18. self.start_time = self.parm[0]
  19. self.end_time = self.parm[1]
  20. def hourly_report(self):
  21. try:
  22. sql = """select count(1) from media_api.kuaishou_unit_report_hourly where
  23. stat_date between {start_time} and {end_time}""".format(
  24. start_time=self.start_time, end_time=self.end_time)
  25. totalNum = TidbConn.quary(sql)[0][0]
  26. pageCount = ceil(totalNum / 5000)
  27. for i in range(pageCount):
  28. sql = """replace into application.kuaishou_unit_report_hourly_dw
  29. (account_id, stat_date,`hour`, campaign_id, campaign_name, unit_id, unit_name, project_id, product_id, advertiser_id,
  30. company_id, account_name, project_name, advertiser_name, company_name, sale_id, user_id, org_code, charge, `show`, photo_click, aclick, bclick, photo_click_ratio, play_3s_ratio, action_ratio,
  31. impression_1k_cost, photo_click_cost, action_cost, share, comment, `like`, follow, cancel_follow, report, block,
  32. negative, submit, download_started, download_completed, activation, event_pay_first_day,
  33. event_pay_purchase_amount_first_day, event_pay_first_day_roi, event_pay, event_pay_purchase_amount, event_pay_roi,
  34. event_register, event_register_cost, event_register_ratio, event_jin_jian_app, event_jin_jian_app_cost,
  35. event_credit_grant_app, event_credit_grant_app_cost, event_credit_grant_app_ratio, event_order_paid,
  36. event_order_paid_purchase_amount, event_order_paid_cost, event_next_day_stay, event_next_day_stay_cost,
  37. event_next_day_stay_ratio, form_count, form_cost, form_action_ratio, event_jin_jian_landing_page,
  38. event_jin_jian_landing_page_cost, event_credit_grant_landing_page, event_credit_grant_landing_page_cost,
  39. event_credit_grant_landing_page_ratio, event_valid_clues, event_valid_clues_cost, event_add_wechat,
  40. event_add_wechat_cost, event_add_wechat_ratio, event_get_through, event_get_through_cost, event_get_through_ratio,
  41. event_app_invoked, event_app_invoked_cost, event_app_invoked_ratio, event_credit_grant_landing_ratio, play_5s_ratio,
  42. play_end_ratio, event_new_user_pay, event_new_user_pay_cost, event_new_user_pay_ratio, click_1k_cost, ad_product_cnt,
  43. event_goods_view, merchant_reco_fans, event_order_amount_roi, event_goods_view_cost, merchant_reco_fans_cost,
  44. event_button_click, event_button_click_cost, event_button_click_ratio, event_order_paid_roi,
  45. event_new_user_jinjian_app, event_new_user_jinjian_app_cost, event_new_user_jinjian_app_roi,
  46. event_new_user_credit_grant_app, event_new_user_credit_grant_app_cost, event_new_user_credit_grant_app_roi,
  47. event_new_user_jinjian_page, event_new_user_jinjian_page_cost, event_new_user_jinjian_page_roi,
  48. event_new_user_credit_grant_page, event_new_user_credit_grant_page_cost, event_new_user_credit_grant_page_roi,
  49. event_appoint_form, event_appoint_form_cost, event_appoint_form_ratio, event_appoint_jump_click,
  50. event_appoint_jump_click_cost, event_appoint_jump_click_ratio, union_event_pay_purchase_amount_7d,
  51. union_event_pay_purchase_amount_7d_roi, placement_type, ad_scene, event_order_successed, ad_photo_played_10s_ratio,
  52. key_action_cost, event_add_shopping_cart_cost, event_ad_watch_times_ratio, event_outbound_call_cost,
  53. event_outbound_call, action_new_ratio, event_watch_app_ad, event_multi_conversion_cost, event_phone_card_activate,
  54. ad_photo_played_75percent_ratio, key_action_ratio, event_ad_watch_times_cost, event_add_shopping_cart, key_action,
  55. event_multi_conversion, event_wechat_connected, event_dsp_gift_form, event_intention_confirmed,
  56. event_phone_get_through, event_multi_conversion_ratio, event_measurement_house, ad_photo_played_2s_ratio,
  57. event_outbound_call_ratio, event_ad_watch_times)
  58. select report.advertiser_id as account_id,
  59. report.stat_date,
  60. report.hour,
  61. report.campaign_id,
  62. report.campaign_name,
  63. report.unit_id,
  64. report.unit_name,
  65. sys.project_id,
  66. sys.product_id,
  67. sys.advertiser_id,
  68. sys.company_id,
  69. sys.account_name,
  70. sys.project_name,
  71. sys.advertiser_name,
  72. sys.company_name,
  73. sys.sale_id,
  74. sys.user_id,
  75. sys.org_code,
  76. report.charge,
  77. report.`show`,
  78. report.photo_click,
  79. report.aclick,
  80. report.bclick,
  81. report.photo_click_ratio,
  82. report.play_3s_ratio,
  83. report.action_ratio,
  84. report.impression_1k_cost,
  85. report.photo_click_cost,
  86. report.action_cost,
  87. report.share,
  88. report.comment,
  89. report.`like`,
  90. report.follow,
  91. report.cancel_follow,
  92. report.report,
  93. report.block,
  94. report.negative,
  95. report.submit,
  96. report.download_started,
  97. report.download_completed,
  98. report.activation,
  99. report.event_pay_first_day,
  100. report.event_pay_purchase_amount_first_day,
  101. report.event_pay_first_day_roi,
  102. report.event_pay,
  103. report.event_pay_purchase_amount,
  104. report.event_pay_roi,
  105. report.event_register,
  106. report.event_register_cost,
  107. report.event_register_ratio,
  108. report.event_jin_jian_app,
  109. report.event_jin_jian_app_cost,
  110. report.event_credit_grant_app,
  111. report.event_credit_grant_app_cost,
  112. report.event_credit_grant_app_ratio,
  113. report.event_order_paid,
  114. report.event_order_paid_purchase_amount,
  115. report.event_order_paid_cost,
  116. report.event_next_day_stay,
  117. report.event_next_day_stay_cost,
  118. report.event_next_day_stay_ratio,
  119. report.form_count,
  120. report.form_cost,
  121. report.form_action_ratio,
  122. report.event_jin_jian_landing_page,
  123. report.event_jin_jian_landing_page_cost,
  124. report.event_credit_grant_landing_page,
  125. report.event_credit_grant_landing_page_cost,
  126. report.event_credit_grant_landing_page_ratio,
  127. report.event_valid_clues,
  128. report.event_valid_clues_cost,
  129. report.event_add_wechat,
  130. report.event_add_wechat_cost,
  131. report.event_add_wechat_ratio,
  132. report.event_get_through,
  133. report.event_get_through_cost,
  134. report.event_get_through_ratio,
  135. report.event_app_invoked,
  136. report.event_app_invoked_cost,
  137. report.event_app_invoked_ratio,
  138. report.event_credit_grant_landing_ratio,
  139. report.play_5s_ratio,
  140. report.play_end_ratio,
  141. report.event_new_user_pay,
  142. report.event_new_user_pay_cost,
  143. report.event_new_user_pay_ratio,
  144. report.click_1k_cost,
  145. report.ad_product_cnt,
  146. report.event_goods_view,
  147. report.merchant_reco_fans,
  148. report.event_order_amount_roi,
  149. report.event_goods_view_cost,
  150. report.merchant_reco_fans_cost,
  151. report.event_button_click,
  152. report.event_button_click_cost,
  153. report.event_button_click_ratio,
  154. report.event_order_paid_roi,
  155. report.event_new_user_jinjian_app,
  156. report.event_new_user_jinjian_app_cost,
  157. report.event_new_user_jinjian_app_roi,
  158. report.event_new_user_credit_grant_app,
  159. report.event_new_user_credit_grant_app_cost,
  160. report.event_new_user_credit_grant_app_roi,
  161. report.event_new_user_jinjian_page,
  162. report.event_new_user_jinjian_page_cost,
  163. report.event_new_user_jinjian_page_roi,
  164. report.event_new_user_credit_grant_page,
  165. report.event_new_user_credit_grant_page_cost,
  166. report.event_new_user_credit_grant_page_roi,
  167. report.event_appoint_form,
  168. report.event_appoint_form_cost,
  169. report.event_appoint_form_ratio,
  170. report.event_appoint_jump_click,
  171. report.event_appoint_jump_click_cost,
  172. report.event_appoint_jump_click_ratio,
  173. report.union_event_pay_purchase_amount_7d,
  174. report.union_event_pay_purchase_amount_7d_roi,
  175. report.placement_type,
  176. report.ad_scene,
  177. report.event_order_successed,
  178. report.ad_photo_played_10s_ratio,
  179. report.key_action_cost,
  180. report.event_add_shopping_cart_cost,
  181. report.event_ad_watch_times_ratio,
  182. report.event_outbound_call_cost,
  183. report.event_outbound_call,
  184. report.action_new_ratio,
  185. report.event_watch_app_ad,
  186. report.event_multi_conversion_cost,
  187. report.event_phone_card_activate,
  188. report.ad_photo_played_75percent_ratio,
  189. report.key_action_ratio,
  190. report.event_ad_watch_times_cost,
  191. report.event_add_shopping_cart,
  192. report.key_action,
  193. report.event_multi_conversion,
  194. report.event_wechat_connected,
  195. report.event_dsp_gift_form,
  196. report.event_intention_confirmed,
  197. report.event_phone_get_through,
  198. report.event_multi_conversion_ratio,
  199. report.event_measurement_house,
  200. report.ad_photo_played_2s_ratio,
  201. report.event_outbound_call_ratio,
  202. report.event_ad_watch_times
  203. from media_api.kuaishou_unit_report_hourly report
  204. left join
  205. hcst_system.ctop_account_attribution_info sys
  206. on report.advertiser_id = sys.account_id
  207. where report.stat_date between {start_time} and {end_time} order by unit_id,hour,stat_date limit {num},5000""".\
  208. format(start_time=self.start_time, end_time=self.end_time,num=i * 5000)
  209. TidbConn.exec_sql(self.conn, sql)
  210. self.conn.commit()
  211. return 0
  212. except Exception as e:
  213. logging.error(e)
  214. return -1
  215. if __name__ == '__main__':
  216. status = UnitHourlyReport().hourly_report()
  217. if status == 0:
  218. logging.info("作业执行成功")
  219. else:
  220. logging.info("作业执行失败")