AccountDailyReport.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/9/27 10:50 上午
  4. # @Site :
  5. # @File : AccountDailyReport.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import logging
  10. from math import ceil
  11. from Apietl.kwai_etl.report.ReplaceAccountInfo import ReplaceAccountInfo
  12. from Apietl.utlis.Logger import Logger
  13. from Apietl.utlis.Utils import Utils
  14. from Apietl.conn.TidbConn import TidbConn
  15. class AccountDailyReport:
  16. def __init__(self):
  17. self.conn = TidbConn.get_connection()
  18. self.parm = Utils.get_args()
  19. self.start_time = self.parm[0]
  20. self.end_time = self.parm[1]
  21. def daily_report(self):
  22. try:
  23. sql = """
  24. replace into application.kuaishou_account_report_daily_dw
  25. (account_id, stat_date, project_id, product_id, advertiser_id, company_id, account_name, project_name, advertiser_name,
  26. company_name, sale_id, user_id, org_code, charge, `show`, photo_click, aclick, bclick, photo_click_ratio,
  27. play_3s_ratio, action_ratio, impression_1k_cost, photo_click_cost, action_cost, share, comment, `like`, follow,
  28. cancel_follow, report, block, negative, submit, download_started, download_completed, activation, event_pay_first_day,
  29. event_pay_purchase_amount_first_day, event_pay_first_day_roi, event_pay, event_pay_purchase_amount, event_pay_roi,
  30. event_register, event_register_cost, event_register_ratio, event_jin_jian_app, event_jin_jian_app_cost,
  31. event_credit_grant_app, event_credit_grant_app_cost, event_credit_grant_app_ratio, event_order_paid,
  32. event_order_paid_purchase_amount, event_order_paid_cost, event_next_day_stay, event_next_day_stay_cost,
  33. event_next_day_stay_ratio, form_count, form_cost, form_action_ratio, event_jin_jian_landing_page,
  34. event_jin_jian_landing_page_cost, event_credit_grant_landing_page, event_credit_grant_landing_page_cost,
  35. event_credit_grant_landing_page_ratio, event_valid_clues, event_valid_clues_cost, event_add_wechat,
  36. event_add_wechat_cost, event_add_wechat_ratio, event_get_through, event_get_through_cost, event_get_through_ratio,
  37. event_app_invoked, event_app_invoked_cost, event_app_invoked_ratio, event_credit_grant_landing_ratio, play_5s_ratio,
  38. play_end_ratio, event_new_user_pay, event_new_user_pay_cost, event_new_user_pay_ratio, click_1k_cost, ad_product_cnt,
  39. event_goods_view, merchant_reco_fans, event_order_amount_roi, event_goods_view_cost, merchant_reco_fans_cost,
  40. event_button_click, event_button_click_cost, event_button_click_ratio, event_order_paid_roi,
  41. event_new_user_jinjian_app, event_new_user_jinjian_app_cost, event_new_user_jinjian_app_roi,
  42. event_new_user_credit_grant_app, event_new_user_credit_grant_app_cost, event_new_user_credit_grant_app_roi,
  43. event_new_user_jinjian_page, event_new_user_jinjian_page_cost, event_new_user_jinjian_page_roi,
  44. event_new_user_credit_grant_page, event_new_user_credit_grant_page_cost, event_new_user_credit_grant_page_roi,
  45. event_appoint_form, event_appoint_form_cost, event_appoint_form_ratio, event_appoint_jump_click,
  46. event_appoint_jump_click_cost, event_appoint_jump_click_ratio, union_event_pay_purchase_amount_7d,
  47. union_event_pay_purchase_amount_7d_roi, placement_type, ad_scene, event_order_successed, ad_photo_played_10s_ratio,
  48. key_action_cost, event_add_shopping_cart_cost, event_ad_watch_times_ratio, event_outbound_call_cost,
  49. event_outbound_call, action_new_ratio, event_watch_app_ad, event_multi_conversion_cost, event_phone_card_activate,
  50. ad_photo_played_75percent_ratio, key_action_ratio, event_ad_watch_times_cost, event_add_shopping_cart, key_action,
  51. event_multi_conversion, event_wechat_connected, event_dsp_gift_form, event_intention_confirmed,
  52. event_phone_get_through, event_multi_conversion_ratio, event_measurement_house, ad_photo_played_2s_ratio,
  53. event_outbound_call_ratio, event_ad_watch_times,event_pay_purchase_amount_one_day_by_conversion,
  54. ad_photo_played_10s,
  55. ad_photo_played_2s,
  56. ad_photo_played_75percent,
  57. approx_pay_cost,
  58. approx_pay_count,
  59. approx_pay_ratio,cancel_like,
  60. click_conversion_ratio,
  61. conversion_cost,conversion_cost_by_impression_7d,conversion_num,conversion_num_by_impression_7d,conversion_num_cost,conversion_ratio,conversion_ratio_by_impression_7d,deep_conversion_cost,deep_conversion_cost_by_impression_7d,deep_conversion_num,deep_conversion_num_by_impression_7d,deep_conversion_ratio,deep_conversion_ratio_by_impression_7d,download_completed_cost,download_completed_ratio,download_conversion_ratio,download_installed,download_started_cost,download_started_ratio,event_24h_stay,event_24h_stay_by_conversion,event_24h_stay_by_conversion_cost,event_24h_stay_by_conversion_ratio,event_24h_stay_cost,event_24h_stay_ratio,event_ad_watch_10_times,event_ad_watch_10_times_cost,event_ad_watch_10_times_ratio,event_ad_watch_20_times,event_ad_watch_20_times_cost,event_ad_watch_20_times_ratio,event_ad_watch_5_times,event_ad_watch_5_times_cost,event_ad_watch_5_times_ratio,event_audition,event_consultation_valid_retained,event_consultation_valid_retained_cost,event_consultation_valid_retained_ratio,event_conversion_click_cost,event_conversion_click_ratio,event_credit_grant_first_day_app,event_credit_grant_first_day_app_cost,event_credit_grant_first_day_app_ratio,event_credit_grant_first_day_landing_page,event_credit_grant_first_day_landing_page_cost,event_credit_grant_first_day_landing_page_ratio,event_making_calls,event_making_calls_cost,event_making_calls_ratio,event_order_submit,event_pay_purchase_amount_one_day,event_pay_purchase_amount_one_day_by_conversion_roi,event_pay_purchase_amount_one_day_roi,event_pay_purchase_amount_three_day_by_conversion,event_pay_purchase_amount_three_day_by_conversion_roi,event_pay_purchase_amount_week_by_conversion,event_pay_purchase_amount_week_by_conversion_roi,event_pay_week_by_conversion,event_pay_week_by_conversion_cost,event_pay_weighted_purchase_amount,event_pay_weighted_purchase_amount_first_day,event_pre_component_consultation_valid_retained,event_three_day_stay_by_conversion,event_three_day_stay_by_conversion_cost,event_three_day_stay_by_conversion_ratio,event_wechat_qr_code_link_click,event_week_stay_by_conversion,event_week_stay_by_conversion_cost,event_week_stay_by_conversion_ratio,live_event_goods_view,live_played_3s,played_end,played_five_seconds,played_three_seconds,event_next_day_stay_new,event_next_day_stay_new_cost,event_next_day_stay_new_ratio,ad_show,event_credit_card_recheck,event_credit_card_recheck_first_day,event_no_intention,event_multi_pay_seven_day_by_conversion,event_multi_pay_seven_day_by_conversion_cost,mcb_left_time_perf_cpa_bid,mcb_left_time_perf_roi_ratio,mcb_all_day_perf_cpa_bid,mcb_all_day_perf_roi_ratio,live_room_avg_played_seconds,ad_live_share,ad_live_comment,live_played_started,live_played_started_cost,ad_live_follow,ad_live_follow_cost,simple_live_played_started,standard_live_played_started,conversion_component_impression,conversion_component_click,conversion_component_rate,ad_landing_page_impression,ad_app_download_half_impression
  62. )
  63. select report.advertiser_id as account_id,
  64. stat_date,
  65. project_id,
  66. product_id,
  67. sys.advertiser_id,
  68. company_id,
  69. account_name,
  70. project_name,
  71. advertiser_name,
  72. company_name,
  73. sale_id,
  74. user_id,
  75. org_code,
  76. charge,
  77. `show`,
  78. photo_click,
  79. aclick,
  80. bclick,
  81. photo_click_ratio,
  82. play_3s_ratio,
  83. action_ratio,
  84. impression_1k_cost,
  85. photo_click_cost,
  86. action_cost,
  87. share,
  88. comment,
  89. `like`,
  90. follow,
  91. cancel_follow,
  92. report,
  93. block,
  94. negative,
  95. submit,
  96. download_started,
  97. download_completed,
  98. activation,
  99. event_pay_first_day,
  100. event_pay_purchase_amount_first_day,
  101. event_pay_first_day_roi,
  102. event_pay,
  103. event_pay_purchase_amount,
  104. event_pay_roi,
  105. event_register,
  106. event_register_cost,
  107. event_register_ratio,
  108. event_jin_jian_app,
  109. event_jin_jian_app_cost,
  110. event_credit_grant_app,
  111. event_credit_grant_app_cost,
  112. event_credit_grant_app_ratio,
  113. event_order_paid,
  114. event_order_paid_purchase_amount,
  115. event_order_paid_cost,
  116. event_next_day_stay,
  117. event_next_day_stay_cost,
  118. event_next_day_stay_ratio,
  119. form_count,
  120. form_cost,
  121. form_action_ratio,
  122. event_jin_jian_landing_page,
  123. event_jin_jian_landing_page_cost,
  124. event_credit_grant_landing_page,
  125. event_credit_grant_landing_page_cost,
  126. event_credit_grant_landing_page_ratio,
  127. event_valid_clues,
  128. event_valid_clues_cost,
  129. event_add_wechat,
  130. event_add_wechat_cost,
  131. event_add_wechat_ratio,
  132. event_get_through,
  133. event_get_through_cost,
  134. event_get_through_ratio,
  135. event_app_invoked,
  136. event_app_invoked_cost,
  137. event_app_invoked_ratio,
  138. event_credit_grant_landing_ratio,
  139. play_5s_ratio,
  140. play_end_ratio,
  141. event_new_user_pay,
  142. event_new_user_pay_cost,
  143. event_new_user_pay_ratio,
  144. click_1k_cost,
  145. ad_product_cnt,
  146. event_goods_view,
  147. merchant_reco_fans,
  148. event_order_amount_roi,
  149. event_goods_view_cost,
  150. merchant_reco_fans_cost,
  151. event_button_click,
  152. event_button_click_cost,
  153. event_button_click_ratio,
  154. event_order_paid_roi,
  155. event_new_user_jinjian_app,
  156. event_new_user_jinjian_app_cost,
  157. event_new_user_jinjian_app_roi,
  158. event_new_user_credit_grant_app,
  159. event_new_user_credit_grant_app_cost,
  160. event_new_user_credit_grant_app_roi,
  161. event_new_user_jinjian_page,
  162. event_new_user_jinjian_page_cost,
  163. event_new_user_jinjian_page_roi,
  164. event_new_user_credit_grant_page,
  165. event_new_user_credit_grant_page_cost,
  166. event_new_user_credit_grant_page_roi,
  167. event_appoint_form,
  168. event_appoint_form_cost,
  169. event_appoint_form_ratio,
  170. event_appoint_jump_click,
  171. event_appoint_jump_click_cost,
  172. event_appoint_jump_click_ratio,
  173. union_event_pay_purchase_amount_7d,
  174. union_event_pay_purchase_amount_7d_roi,
  175. placement_type,
  176. ad_scene,
  177. event_order_successed,
  178. ad_photo_played_10s_ratio,
  179. key_action_cost,
  180. event_add_shopping_cart_cost,
  181. event_ad_watch_times_ratio,
  182. event_outbound_call_cost,
  183. event_outbound_call,
  184. action_new_ratio,
  185. event_watch_app_ad,
  186. event_multi_conversion_cost,
  187. event_phone_card_activate,
  188. ad_photo_played_75percent_ratio,
  189. key_action_ratio,
  190. event_ad_watch_times_cost,
  191. event_add_shopping_cart,
  192. key_action,
  193. event_multi_conversion,
  194. event_wechat_connected,
  195. event_dsp_gift_form,
  196. event_intention_confirmed,
  197. event_phone_get_through,
  198. event_multi_conversion_ratio,
  199. event_measurement_house,
  200. ad_photo_played_2s_ratio,
  201. event_outbound_call_ratio,
  202. event_ad_watch_times,
  203. event_pay_purchase_amount_one_day_by_conversion,
  204. ad_photo_played_10s
  205. ,ad_photo_played_2s
  206. ,ad_photo_played_75percent
  207. ,approx_pay_cost
  208. ,approx_pay_count
  209. ,approx_pay_ratio
  210. ,cancel_like
  211. ,click_conversion_ratio
  212. ,conversion_cost
  213. ,conversion_cost_by_impression_7d
  214. ,conversion_num
  215. ,conversion_num_by_impression_7d
  216. ,conversion_num_cost
  217. ,conversion_ratio
  218. ,conversion_ratio_by_impression_7d
  219. ,deep_conversion_cost
  220. ,deep_conversion_cost_by_impression_7d
  221. ,deep_conversion_num
  222. ,deep_conversion_num_by_impression_7d
  223. ,deep_conversion_ratio
  224. ,deep_conversion_ratio_by_impression_7d
  225. ,download_completed_cost
  226. ,download_completed_ratio
  227. ,download_conversion_ratio
  228. ,download_installed
  229. ,download_started_cost
  230. ,download_started_ratio
  231. ,event_24h_stay
  232. ,event_24h_stay_by_conversion
  233. ,event_24h_stay_by_conversion_cost
  234. ,event_24h_stay_by_conversion_ratio
  235. ,event_24h_stay_cost
  236. ,event_24h_stay_ratio
  237. ,event_ad_watch_10_times
  238. ,event_ad_watch_10_times_cost
  239. ,event_ad_watch_10_times_ratio
  240. ,event_ad_watch_20_times
  241. ,event_ad_watch_20_times_cost
  242. ,event_ad_watch_20_times_ratio
  243. ,event_ad_watch_5_times
  244. ,event_ad_watch_5_times_cost
  245. ,event_ad_watch_5_times_ratio
  246. ,event_audition
  247. ,event_consultation_valid_retained
  248. ,event_consultation_valid_retained_cost
  249. ,event_consultation_valid_retained_ratio
  250. ,event_conversion_click_cost
  251. ,event_conversion_click_ratio
  252. ,event_credit_grant_first_day_app
  253. ,event_credit_grant_first_day_app_cost
  254. ,event_credit_grant_first_day_app_ratio
  255. ,event_credit_grant_first_day_landing_page
  256. ,event_credit_grant_first_day_landing_page_cost
  257. ,event_credit_grant_first_day_landing_page_ratio
  258. ,event_making_calls
  259. ,event_making_calls_cost
  260. ,event_making_calls_ratio
  261. ,event_order_submit
  262. ,event_pay_purchase_amount_one_day
  263. ,event_pay_purchase_amount_one_day_by_conversion_roi
  264. ,event_pay_purchase_amount_one_day_roi
  265. ,event_pay_purchase_amount_three_day_by_conversion
  266. ,event_pay_purchase_amount_three_day_by_conversion_roi
  267. ,event_pay_purchase_amount_week_by_conversion
  268. ,event_pay_purchase_amount_week_by_conversion_roi
  269. ,event_pay_week_by_conversion
  270. ,event_pay_week_by_conversion_cost
  271. ,event_pay_weighted_purchase_amount
  272. ,event_pay_weighted_purchase_amount_first_day
  273. ,event_pre_component_consultation_valid_retained
  274. ,event_three_day_stay_by_conversion
  275. ,event_three_day_stay_by_conversion_cost
  276. ,event_three_day_stay_by_conversion_ratio
  277. ,event_wechat_qr_code_link_click
  278. ,event_week_stay_by_conversion
  279. ,event_week_stay_by_conversion_cost
  280. ,event_week_stay_by_conversion_ratio
  281. ,live_event_goods_view
  282. ,live_played_3s
  283. ,played_end
  284. ,played_five_seconds
  285. ,played_three_seconds
  286. ,event_next_day_stay_new
  287. ,event_next_day_stay_new_cost
  288. ,event_next_day_stay_new_ratio
  289. ,ad_show
  290. ,event_credit_card_recheck
  291. ,event_credit_card_recheck_first_day
  292. ,event_no_intention
  293. ,event_multi_pay_seven_day_by_conversion
  294. ,event_multi_pay_seven_day_by_conversion_cost
  295. ,mcb_left_time_perf_cpa_bid
  296. ,mcb_left_time_perf_roi_ratio
  297. ,mcb_all_day_perf_cpa_bid
  298. ,mcb_all_day_perf_roi_ratio
  299. ,live_room_avg_played_seconds
  300. ,ad_live_share
  301. ,ad_live_comment
  302. ,live_played_started
  303. ,live_played_started_cost
  304. ,ad_live_follow
  305. ,ad_live_follow_cost
  306. ,simple_live_played_started
  307. ,standard_live_played_started
  308. ,conversion_component_impression
  309. ,conversion_component_click
  310. ,conversion_component_rate
  311. ,ad_landing_page_impression
  312. ,ad_app_download_half_impression
  313. from media_api.kuaishou_account_report_daily report
  314. left join
  315. (SELECT
  316. cua.account_id id,
  317. cua.account_id account_id,
  318. cp.id project_id,
  319. product.id product_id,
  320. ca.id advertiser_id,
  321. uc.company_id company_id,
  322. cua.auth_name account_name,
  323. cp.project_name project_name,
  324. ca.`name` advertiser_name,
  325. uc.company_name company_name,
  326. cp.sale_id sale_id,
  327. su.id user_id,
  328. su.leader_id,
  329. su.org_code org_code
  330. FROM
  331. `jeecg-boot`.ctop_user_allocation cua
  332. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  333. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  334. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  335. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  336. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  337. WHERE
  338. cua.account_id IS NOT NULL
  339. ) sys
  340. on report.advertiser_id = sys.account_id
  341. where report.stat_date between {start_time} and {end_time}
  342. """.format(start_time=self.start_time, end_time=self.end_time)
  343. print(sql)
  344. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  345. self.conn.commit()
  346. TidbConn.conn_close(conn=self.conn)
  347. return 0
  348. except Exception as e:
  349. logging.error(e)
  350. return -1
  351. if __name__ == '__main__':
  352. status = AccountDailyReport().daily_report()
  353. if status == 0:
  354. print("作业执行成功")
  355. exit(0)
  356. else:
  357. print("作业执行失败")
  358. exit(-1)