UnitDailyReport.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/9/15 5:10 下午
  4. # @Site :
  5. # @File : UnitReport.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. import logging
  9. from math import ceil
  10. from Apietl.kwai_etl.report.ReplaceAccountInfo import ReplaceAccountInfo
  11. from Apietl.utlis.Logger import Logger
  12. from Apietl.utlis.Utils import Utils
  13. from Apietl.conn.TidbConn import TidbConn
  14. class UnitDailyReport:
  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 daily_report(self):
  21. try:
  22. sql = """
  23. select advertiser_id from media_api.kuaishou_unit_report_daily where
  24. stat_date between {start_time} and {end_time} group by advertiser_id""".format(
  25. start_time=self.start_time, end_time=self.end_time)
  26. account_list = TidbConn.quary(self.conn, sql)
  27. for account_id in account_list:
  28. sql = """
  29. replace into application.kuaishou_unit_report_daily_dw
  30. (account_id, stat_date, campaign_id, campaign_name, unit_id, unit_name, project_id, product_id, advertiser_id,
  31. 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,
  32. impression_1k_cost, photo_click_cost, action_cost, share, comment, `like`, follow, cancel_follow, report, block,
  33. negative, submit, download_started, download_completed, activation, event_pay_first_day,
  34. event_pay_purchase_amount_first_day, event_pay_first_day_roi, event_pay, event_pay_purchase_amount, event_pay_roi,
  35. event_register, event_register_cost, event_register_ratio, event_jin_jian_app, event_jin_jian_app_cost,
  36. event_credit_grant_app, event_credit_grant_app_cost, event_credit_grant_app_ratio, event_order_paid,
  37. event_order_paid_purchase_amount, event_order_paid_cost, event_next_day_stay, event_next_day_stay_cost,
  38. event_next_day_stay_ratio, form_count, form_cost, form_action_ratio, event_jin_jian_landing_page,
  39. event_jin_jian_landing_page_cost, event_credit_grant_landing_page, event_credit_grant_landing_page_cost,
  40. event_credit_grant_landing_page_ratio, event_valid_clues, event_valid_clues_cost, event_add_wechat,
  41. event_add_wechat_cost, event_add_wechat_ratio, event_get_through, event_get_through_cost, event_get_through_ratio,
  42. event_app_invoked, event_app_invoked_cost, event_app_invoked_ratio, event_credit_grant_landing_ratio, play_5s_ratio,
  43. play_end_ratio, event_new_user_pay, event_new_user_pay_cost, event_new_user_pay_ratio, click_1k_cost, ad_product_cnt,
  44. event_goods_view, merchant_reco_fans, event_order_amount_roi, event_goods_view_cost, merchant_reco_fans_cost,
  45. event_button_click, event_button_click_cost, event_button_click_ratio, event_order_paid_roi,
  46. event_new_user_jinjian_app, event_new_user_jinjian_app_cost, event_new_user_jinjian_app_roi,
  47. event_new_user_credit_grant_app, event_new_user_credit_grant_app_cost, event_new_user_credit_grant_app_roi,
  48. event_new_user_jinjian_page, event_new_user_jinjian_page_cost, event_new_user_jinjian_page_roi,
  49. event_new_user_credit_grant_page, event_new_user_credit_grant_page_cost, event_new_user_credit_grant_page_roi,
  50. event_appoint_form, event_appoint_form_cost, event_appoint_form_ratio, event_appoint_jump_click,
  51. event_appoint_jump_click_cost, event_appoint_jump_click_ratio, union_event_pay_purchase_amount_7d,
  52. union_event_pay_purchase_amount_7d_roi, placement_type, ad_scene, event_order_successed, ad_photo_played_10s_ratio,
  53. key_action_cost, event_add_shopping_cart_cost, event_ad_watch_times_ratio, event_outbound_call_cost,
  54. event_outbound_call, action_new_ratio, event_watch_app_ad, event_multi_conversion_cost, event_phone_card_activate,
  55. ad_photo_played_75percent_ratio, key_action_ratio, event_ad_watch_times_cost, event_add_shopping_cart, key_action,
  56. event_multi_conversion, event_wechat_connected, event_dsp_gift_form, event_intention_confirmed,
  57. event_phone_get_through, event_multi_conversion_ratio, event_measurement_house, ad_photo_played_2s_ratio,
  58. event_outbound_call_ratio, event_ad_watch_times,event_order_submit,
  59. live_event_goods_view)
  60. select report.advertiser_id as account_id,
  61. report.stat_date,
  62. report.campaign_id,
  63. report.campaign_name,
  64. report.unit_id,
  65. report.unit_name,
  66. sys.project_id,
  67. sys.product_id,
  68. sys.advertiser_id,
  69. sys.company_id,
  70. sys.account_name,
  71. sys.project_name,
  72. sys.advertiser_name,
  73. sys.company_name,
  74. sys.sale_id,
  75. sys.user_id,
  76. sys.org_code,
  77. report.charge,
  78. report.`show`,
  79. report.photo_click,
  80. report.aclick,
  81. report.bclick,
  82. report.photo_click_ratio,
  83. report.play_3s_ratio,
  84. report.action_ratio,
  85. report.impression_1k_cost,
  86. report.photo_click_cost,
  87. report.action_cost,
  88. report.share,
  89. report.comment,
  90. report.`like`,
  91. report.follow,
  92. report.cancel_follow,
  93. report.report,
  94. report.block,
  95. report.negative,
  96. report.submit,
  97. report.download_started,
  98. report.download_completed,
  99. report.activation,
  100. report.event_pay_first_day,
  101. report.event_pay_purchase_amount_first_day,
  102. report.event_pay_first_day_roi,
  103. report.event_pay,
  104. report.event_pay_purchase_amount,
  105. report.event_pay_roi,
  106. report.event_register,
  107. report.event_register_cost,
  108. report.event_register_ratio,
  109. report.event_jin_jian_app,
  110. report.event_jin_jian_app_cost,
  111. report.event_credit_grant_app,
  112. report.event_credit_grant_app_cost,
  113. report.event_credit_grant_app_ratio,
  114. report.event_order_paid,
  115. report.event_order_paid_purchase_amount,
  116. report.event_order_paid_cost,
  117. report.event_next_day_stay,
  118. report.event_next_day_stay_cost,
  119. report.event_next_day_stay_ratio,
  120. report.form_count,
  121. report.form_cost,
  122. report.form_action_ratio,
  123. report.event_jin_jian_landing_page,
  124. report.event_jin_jian_landing_page_cost,
  125. report.event_credit_grant_landing_page,
  126. report.event_credit_grant_landing_page_cost,
  127. report.event_credit_grant_landing_page_ratio,
  128. report.event_valid_clues,
  129. report.event_valid_clues_cost,
  130. report.event_add_wechat,
  131. report.event_add_wechat_cost,
  132. report.event_add_wechat_ratio,
  133. report.event_get_through,
  134. report.event_get_through_cost,
  135. report.event_get_through_ratio,
  136. report.event_app_invoked,
  137. report.event_app_invoked_cost,
  138. report.event_app_invoked_ratio,
  139. report.event_credit_grant_landing_ratio,
  140. report.play_5s_ratio,
  141. report.play_end_ratio,
  142. report.event_new_user_pay,
  143. report.event_new_user_pay_cost,
  144. report.event_new_user_pay_ratio,
  145. report.click_1k_cost,
  146. report.ad_product_cnt,
  147. report.event_goods_view,
  148. report.merchant_reco_fans,
  149. report.event_order_amount_roi,
  150. report.event_goods_view_cost,
  151. report.merchant_reco_fans_cost,
  152. report.event_button_click,
  153. report.event_button_click_cost,
  154. report.event_button_click_ratio,
  155. report.event_order_paid_roi,
  156. report.event_new_user_jinjian_app,
  157. report.event_new_user_jinjian_app_cost,
  158. report.event_new_user_jinjian_app_roi,
  159. report.event_new_user_credit_grant_app,
  160. report.event_new_user_credit_grant_app_cost,
  161. report.event_new_user_credit_grant_app_roi,
  162. report.event_new_user_jinjian_page,
  163. report.event_new_user_jinjian_page_cost,
  164. report.event_new_user_jinjian_page_roi,
  165. report.event_new_user_credit_grant_page,
  166. report.event_new_user_credit_grant_page_cost,
  167. report.event_new_user_credit_grant_page_roi,
  168. report.event_appoint_form,
  169. report.event_appoint_form_cost,
  170. report.event_appoint_form_ratio,
  171. report.event_appoint_jump_click,
  172. report.event_appoint_jump_click_cost,
  173. report.event_appoint_jump_click_ratio,
  174. report.union_event_pay_purchase_amount_7d,
  175. report.union_event_pay_purchase_amount_7d_roi,
  176. report.placement_type,
  177. report.ad_scene,
  178. report.event_order_successed,
  179. report.ad_photo_played_10s_ratio,
  180. report.key_action_cost,
  181. report.event_add_shopping_cart_cost,
  182. report.event_ad_watch_times_ratio,
  183. report.event_outbound_call_cost,
  184. report.event_outbound_call,
  185. report.action_new_ratio,
  186. report.event_watch_app_ad,
  187. report.event_multi_conversion_cost,
  188. report.event_phone_card_activate,
  189. report.ad_photo_played_75percent_ratio,
  190. report.key_action_ratio,
  191. report.event_ad_watch_times_cost,
  192. report.event_add_shopping_cart,
  193. report.key_action,
  194. report.event_multi_conversion,
  195. report.event_wechat_connected,
  196. report.event_dsp_gift_form,
  197. report.event_intention_confirmed,
  198. report.event_phone_get_through,
  199. report.event_multi_conversion_ratio,
  200. report.event_measurement_house,
  201. report.ad_photo_played_2s_ratio,
  202. report.event_outbound_call_ratio,
  203. report.event_ad_watch_times,
  204. report.event_order_submit,
  205. report.live_event_goods_view
  206. from media_api.kuaishou_unit_report_daily report
  207. left join
  208. (SELECT
  209. cua.account_id id,
  210. cua.account_id account_id,
  211. cp.id project_id,
  212. product.id product_id,
  213. ca.id advertiser_id,
  214. uc.company_id company_id,
  215. cua.auth_name account_name,
  216. cp.project_name project_name,
  217. ca.`name` advertiser_name,
  218. uc.company_name company_name,
  219. cp.sale_id sale_id,
  220. su.id user_id,
  221. su.leader_id,
  222. su.org_code org_code
  223. FROM
  224. `jeecg-boot`.ctop_user_allocation cua
  225. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  226. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  227. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  228. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  229. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  230. WHERE
  231. cua.account_id IS NOT NULL
  232. ) sys
  233. on report.advertiser_id = sys.account_id
  234. where report.stat_date between {start_time} and {end_time}
  235. and report.advertiser_id={account_id}""".format(start_time=self.start_time, end_time=self.end_time, account_id=account_id[0])
  236. print(sql)
  237. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  238. self.conn.commit()
  239. TidbConn.conn_close(conn=self.conn)
  240. return 0
  241. except Exception as e:
  242. logging.error(e)
  243. return -1
  244. if __name__ == '__main__':
  245. status = UnitDailyReport().daily_report()
  246. if status == 0:
  247. print("作业执行成功")
  248. exit(0)
  249. else:
  250. print("作业执行失败")
  251. exit(-1)