AccountHourlyReport.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/9/27 10:50 上午
  4. # @Site :
  5. # @File : AccountHourlyReport.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 AccountHourlyReport:
  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. ReplaceAccountInfo.handler()
  23. try:
  24. sql = """select count(1) from media_api.kuaishou_account_report_hourly where
  25. stat_date between {start_time} and {end_time}""".format(
  26. start_time=self.start_time, end_time=self.end_time)
  27. totalNum = TidbConn.quary(self.conn, sql)[0][0]
  28. pageCount = ceil(totalNum / 20000)
  29. for i in range(pageCount):
  30. sql = """
  31. replace into application.kuaishou_account_report_hourly_dw
  32. (account_id, stat_date, `hour`, project_id, product_id, advertiser_id, company_id, account_name, project_name, advertiser_name,
  33. company_name, sale_id, user_id, org_code, charge, `show`, photo_click, aclick, bclick, photo_click_ratio,
  34. play_3s_ratio, action_ratio, impression_1k_cost, photo_click_cost, action_cost, share, comment, `like`, follow,
  35. cancel_follow, report, block, negative, submit, download_started, download_completed, activation, event_pay_first_day,
  36. event_pay_purchase_amount_first_day, event_pay_first_day_roi, event_pay, event_pay_purchase_amount, event_pay_roi,
  37. event_register, event_register_cost, event_register_ratio, event_jin_jian_app, event_jin_jian_app_cost,
  38. event_credit_grant_app, event_credit_grant_app_cost, event_credit_grant_app_ratio, event_order_paid,
  39. event_order_paid_purchase_amount, event_order_paid_cost, event_next_day_stay, event_next_day_stay_cost,
  40. event_next_day_stay_ratio, form_count, form_cost, form_action_ratio, event_jin_jian_landing_page,
  41. event_jin_jian_landing_page_cost, event_credit_grant_landing_page, event_credit_grant_landing_page_cost,
  42. event_credit_grant_landing_page_ratio, event_valid_clues, event_valid_clues_cost, event_add_wechat,
  43. event_add_wechat_cost, event_add_wechat_ratio, event_get_through, event_get_through_cost, event_get_through_ratio,
  44. event_app_invoked, event_app_invoked_cost, event_app_invoked_ratio, event_credit_grant_landing_ratio, play_5s_ratio,
  45. play_end_ratio, event_new_user_pay, event_new_user_pay_cost, event_new_user_pay_ratio, click_1k_cost, ad_product_cnt,
  46. event_goods_view, merchant_reco_fans, event_order_amount_roi, event_goods_view_cost, merchant_reco_fans_cost,
  47. event_button_click, event_button_click_cost, event_button_click_ratio, event_order_paid_roi,
  48. event_new_user_jinjian_app, event_new_user_jinjian_app_cost, event_new_user_jinjian_app_roi,
  49. event_new_user_credit_grant_app, event_new_user_credit_grant_app_cost, event_new_user_credit_grant_app_roi,
  50. event_new_user_jinjian_page, event_new_user_jinjian_page_cost, event_new_user_jinjian_page_roi,
  51. event_new_user_credit_grant_page, event_new_user_credit_grant_page_cost, event_new_user_credit_grant_page_roi,
  52. event_appoint_form, event_appoint_form_cost, event_appoint_form_ratio, event_appoint_jump_click,
  53. event_appoint_jump_click_cost, event_appoint_jump_click_ratio, union_event_pay_purchase_amount_7d,
  54. union_event_pay_purchase_amount_7d_roi, placement_type, ad_scene, event_order_successed, ad_photo_played_10s_ratio,
  55. key_action_cost, event_add_shopping_cart_cost, event_ad_watch_times_ratio, event_outbound_call_cost,
  56. event_outbound_call, action_new_ratio, event_watch_app_ad, event_multi_conversion_cost, event_phone_card_activate,
  57. ad_photo_played_75percent_ratio, key_action_ratio, event_ad_watch_times_cost, event_add_shopping_cart, key_action,
  58. event_multi_conversion, event_wechat_connected, event_dsp_gift_form, event_intention_confirmed,
  59. event_phone_get_through, event_multi_conversion_ratio, event_measurement_house, ad_photo_played_2s_ratio,
  60. event_outbound_call_ratio, event_ad_watch_times,event_pay_purchase_amount_one_day_by_conversion, ad_photo_played_10s
  61. ,ad_photo_played_2s
  62. ,ad_photo_played_75percent
  63. ,approx_pay_cost
  64. ,approx_pay_count
  65. ,approx_pay_ratio
  66. ,cancel_like
  67. ,click_conversion_ratio
  68. ,conversion_cost
  69. ,conversion_cost_by_impression_7d
  70. ,conversion_num
  71. ,conversion_num_by_impression_7d
  72. ,conversion_num_cost
  73. ,conversion_ratio
  74. ,conversion_ratio_by_impression_7d
  75. ,deep_conversion_cost
  76. ,deep_conversion_cost_by_impression_7d
  77. ,deep_conversion_num
  78. ,deep_conversion_num_by_impression_7d
  79. ,deep_conversion_ratio
  80. ,deep_conversion_ratio_by_impression_7d
  81. ,download_completed_cost
  82. ,download_completed_ratio
  83. ,download_conversion_ratio
  84. ,download_installed
  85. ,download_started_cost
  86. ,download_started_ratio
  87. ,event_24h_stay
  88. ,event_24h_stay_by_conversion
  89. ,event_24h_stay_by_conversion_cost
  90. ,event_24h_stay_by_conversion_ratio
  91. ,event_24h_stay_cost
  92. ,event_24h_stay_ratio
  93. ,event_ad_watch_10_times
  94. ,event_ad_watch_10_times_cost
  95. ,event_ad_watch_10_times_ratio
  96. ,event_ad_watch_20_times
  97. ,event_ad_watch_20_times_cost
  98. ,event_ad_watch_20_times_ratio
  99. ,event_ad_watch_5_times
  100. ,event_ad_watch_5_times_cost
  101. ,event_ad_watch_5_times_ratio
  102. ,event_audition
  103. ,event_consultation_valid_retained
  104. ,event_consultation_valid_retained_cost
  105. ,event_consultation_valid_retained_ratio
  106. ,event_conversion_click_cost
  107. ,event_conversion_click_ratio
  108. ,event_credit_grant_first_day_app
  109. ,event_credit_grant_first_day_app_cost
  110. ,event_credit_grant_first_day_app_ratio
  111. ,event_credit_grant_first_day_landing_page
  112. ,event_credit_grant_first_day_landing_page_cost
  113. ,event_credit_grant_first_day_landing_page_ratio
  114. ,event_making_calls
  115. ,event_making_calls_cost
  116. ,event_making_calls_ratio
  117. ,event_order_submit
  118. ,event_pay_purchase_amount_one_day
  119. ,event_pay_purchase_amount_one_day_by_conversion_roi
  120. ,event_pay_purchase_amount_one_day_roi
  121. ,event_pay_purchase_amount_three_day_by_conversion
  122. ,event_pay_purchase_amount_three_day_by_conversion_roi
  123. ,event_pay_purchase_amount_week_by_conversion
  124. ,event_pay_purchase_amount_week_by_conversion_roi
  125. ,event_pay_week_by_conversion
  126. ,event_pay_week_by_conversion_cost
  127. ,event_pay_weighted_purchase_amount
  128. ,event_pay_weighted_purchase_amount_first_day
  129. ,event_pre_component_consultation_valid_retained
  130. ,event_three_day_stay_by_conversion
  131. ,event_three_day_stay_by_conversion_cost
  132. ,event_three_day_stay_by_conversion_ratio
  133. ,event_wechat_qr_code_link_click
  134. ,event_week_stay_by_conversion
  135. ,event_week_stay_by_conversion_cost
  136. ,event_week_stay_by_conversion_ratio
  137. ,live_event_goods_view
  138. ,live_played_3s
  139. ,played_end
  140. ,played_five_seconds
  141. ,played_three_seconds
  142. ,event_next_day_stay_new
  143. ,event_next_day_stay_new_cost
  144. ,event_next_day_stay_new_ratio
  145. ,ad_show
  146. ,event_credit_card_recheck
  147. ,event_credit_card_recheck_first_day
  148. ,event_no_intention
  149. ,event_multi_pay_seven_day_by_conversion
  150. ,event_multi_pay_seven_day_by_conversion_cost
  151. ,mcb_left_time_perf_cpa_bid
  152. ,mcb_left_time_perf_roi_ratio
  153. ,mcb_all_day_perf_cpa_bid
  154. ,mcb_all_day_perf_roi_ratio
  155. ,live_room_avg_played_seconds
  156. ,ad_live_share
  157. ,ad_live_comment
  158. ,live_played_started
  159. ,live_played_started_cost
  160. ,ad_live_follow
  161. ,ad_live_follow_cost
  162. ,simple_live_played_started
  163. ,standard_live_played_started
  164. ,conversion_component_impression
  165. ,conversion_component_click
  166. ,conversion_component_rate
  167. ,ad_landing_page_impression
  168. ,ad_app_download_half_impression
  169. )
  170. select report.advertiser_id as account_id,
  171. stat_date,
  172. `hour`,
  173. project_id,
  174. product_id,
  175. sys.advertiser_id,
  176. company_id,
  177. account_name,
  178. project_name,
  179. advertiser_name,
  180. company_name,
  181. sale_id,
  182. user_id,
  183. org_code,
  184. charge,
  185. `show`,
  186. photo_click,
  187. aclick,
  188. bclick,
  189. photo_click_ratio,
  190. play_3s_ratio,
  191. action_ratio,
  192. impression_1k_cost,
  193. photo_click_cost,
  194. action_cost,
  195. share,
  196. comment,
  197. `like`,
  198. follow,
  199. cancel_follow,
  200. report,
  201. block,
  202. negative,
  203. submit,
  204. download_started,
  205. download_completed,
  206. activation,
  207. event_pay_first_day,
  208. event_pay_purchase_amount_first_day,
  209. event_pay_first_day_roi,
  210. event_pay,
  211. event_pay_purchase_amount,
  212. event_pay_roi,
  213. event_register,
  214. event_register_cost,
  215. event_register_ratio,
  216. event_jin_jian_app,
  217. event_jin_jian_app_cost,
  218. event_credit_grant_app,
  219. event_credit_grant_app_cost,
  220. event_credit_grant_app_ratio,
  221. event_order_paid,
  222. event_order_paid_purchase_amount,
  223. event_order_paid_cost,
  224. event_next_day_stay,
  225. event_next_day_stay_cost,
  226. event_next_day_stay_ratio,
  227. form_count,
  228. form_cost,
  229. form_action_ratio,
  230. event_jin_jian_landing_page,
  231. event_jin_jian_landing_page_cost,
  232. event_credit_grant_landing_page,
  233. event_credit_grant_landing_page_cost,
  234. event_credit_grant_landing_page_ratio,
  235. event_valid_clues,
  236. event_valid_clues_cost,
  237. event_add_wechat,
  238. event_add_wechat_cost,
  239. event_add_wechat_ratio,
  240. event_get_through,
  241. event_get_through_cost,
  242. event_get_through_ratio,
  243. event_app_invoked,
  244. event_app_invoked_cost,
  245. event_app_invoked_ratio,
  246. event_credit_grant_landing_ratio,
  247. play_5s_ratio,
  248. play_end_ratio,
  249. event_new_user_pay,
  250. event_new_user_pay_cost,
  251. event_new_user_pay_ratio,
  252. click_1k_cost,
  253. ad_product_cnt,
  254. event_goods_view,
  255. merchant_reco_fans,
  256. event_order_amount_roi,
  257. event_goods_view_cost,
  258. merchant_reco_fans_cost,
  259. event_button_click,
  260. event_button_click_cost,
  261. event_button_click_ratio,
  262. event_order_paid_roi,
  263. event_new_user_jinjian_app,
  264. event_new_user_jinjian_app_cost,
  265. event_new_user_jinjian_app_roi,
  266. event_new_user_credit_grant_app,
  267. event_new_user_credit_grant_app_cost,
  268. event_new_user_credit_grant_app_roi,
  269. event_new_user_jinjian_page,
  270. event_new_user_jinjian_page_cost,
  271. event_new_user_jinjian_page_roi,
  272. event_new_user_credit_grant_page,
  273. event_new_user_credit_grant_page_cost,
  274. event_new_user_credit_grant_page_roi,
  275. event_appoint_form,
  276. event_appoint_form_cost,
  277. event_appoint_form_ratio,
  278. event_appoint_jump_click,
  279. event_appoint_jump_click_cost,
  280. event_appoint_jump_click_ratio,
  281. union_event_pay_purchase_amount_7d,
  282. union_event_pay_purchase_amount_7d_roi,
  283. placement_type,
  284. ad_scene,
  285. event_order_successed,
  286. ad_photo_played_10s_ratio,
  287. key_action_cost,
  288. event_add_shopping_cart_cost,
  289. event_ad_watch_times_ratio,
  290. event_outbound_call_cost,
  291. event_outbound_call,
  292. action_new_ratio,
  293. event_watch_app_ad,
  294. event_multi_conversion_cost,
  295. event_phone_card_activate,
  296. ad_photo_played_75percent_ratio,
  297. key_action_ratio,
  298. event_ad_watch_times_cost,
  299. event_add_shopping_cart,
  300. key_action,
  301. event_multi_conversion,
  302. event_wechat_connected,
  303. event_dsp_gift_form,
  304. event_intention_confirmed,
  305. event_phone_get_through,
  306. event_multi_conversion_ratio,
  307. event_measurement_house,
  308. ad_photo_played_2s_ratio,
  309. event_outbound_call_ratio,
  310. event_ad_watch_times,
  311. event_pay_purchase_amount_one_day_by_conversion,
  312. ad_photo_played_10s
  313. ,ad_photo_played_2s
  314. ,ad_photo_played_75percent
  315. ,approx_pay_cost
  316. ,approx_pay_count
  317. ,approx_pay_ratio
  318. ,cancel_like
  319. ,click_conversion_ratio
  320. ,conversion_cost
  321. ,conversion_cost_by_impression_7d
  322. ,conversion_num
  323. ,conversion_num_by_impression_7d
  324. ,conversion_num_cost
  325. ,conversion_ratio
  326. ,conversion_ratio_by_impression_7d
  327. ,deep_conversion_cost
  328. ,deep_conversion_cost_by_impression_7d
  329. ,deep_conversion_num
  330. ,deep_conversion_num_by_impression_7d
  331. ,deep_conversion_ratio
  332. ,deep_conversion_ratio_by_impression_7d
  333. ,download_completed_cost
  334. ,download_completed_ratio
  335. ,download_conversion_ratio
  336. ,download_installed
  337. ,download_started_cost
  338. ,download_started_ratio
  339. ,event_24h_stay
  340. ,event_24h_stay_by_conversion
  341. ,event_24h_stay_by_conversion_cost
  342. ,event_24h_stay_by_conversion_ratio
  343. ,event_24h_stay_cost
  344. ,event_24h_stay_ratio
  345. ,event_ad_watch_10_times
  346. ,event_ad_watch_10_times_cost
  347. ,event_ad_watch_10_times_ratio
  348. ,event_ad_watch_20_times
  349. ,event_ad_watch_20_times_cost
  350. ,event_ad_watch_20_times_ratio
  351. ,event_ad_watch_5_times
  352. ,event_ad_watch_5_times_cost
  353. ,event_ad_watch_5_times_ratio
  354. ,event_audition
  355. ,event_consultation_valid_retained
  356. ,event_consultation_valid_retained_cost
  357. ,event_consultation_valid_retained_ratio
  358. ,event_conversion_click_cost
  359. ,event_conversion_click_ratio
  360. ,event_credit_grant_first_day_app
  361. ,event_credit_grant_first_day_app_cost
  362. ,event_credit_grant_first_day_app_ratio
  363. ,event_credit_grant_first_day_landing_page
  364. ,event_credit_grant_first_day_landing_page_cost
  365. ,event_credit_grant_first_day_landing_page_ratio
  366. ,event_making_calls
  367. ,event_making_calls_cost
  368. ,event_making_calls_ratio
  369. ,event_order_submit
  370. ,event_pay_purchase_amount_one_day
  371. ,event_pay_purchase_amount_one_day_by_conversion_roi
  372. ,event_pay_purchase_amount_one_day_roi
  373. ,event_pay_purchase_amount_three_day_by_conversion
  374. ,event_pay_purchase_amount_three_day_by_conversion_roi
  375. ,event_pay_purchase_amount_week_by_conversion
  376. ,event_pay_purchase_amount_week_by_conversion_roi
  377. ,event_pay_week_by_conversion
  378. ,event_pay_week_by_conversion_cost
  379. ,event_pay_weighted_purchase_amount
  380. ,event_pay_weighted_purchase_amount_first_day
  381. ,event_pre_component_consultation_valid_retained
  382. ,event_three_day_stay_by_conversion
  383. ,event_three_day_stay_by_conversion_cost
  384. ,event_three_day_stay_by_conversion_ratio
  385. ,event_wechat_qr_code_link_click
  386. ,event_week_stay_by_conversion
  387. ,event_week_stay_by_conversion_cost
  388. ,event_week_stay_by_conversion_ratio
  389. ,live_event_goods_view
  390. ,live_played_3s
  391. ,played_end
  392. ,played_five_seconds
  393. ,played_three_seconds
  394. ,event_next_day_stay_new
  395. ,event_next_day_stay_new_cost
  396. ,event_next_day_stay_new_ratio
  397. ,ad_show
  398. ,event_credit_card_recheck
  399. ,event_credit_card_recheck_first_day
  400. ,event_no_intention
  401. ,event_multi_pay_seven_day_by_conversion
  402. ,event_multi_pay_seven_day_by_conversion_cost
  403. ,mcb_left_time_perf_cpa_bid
  404. ,mcb_left_time_perf_roi_ratio
  405. ,mcb_all_day_perf_cpa_bid
  406. ,mcb_all_day_perf_roi_ratio
  407. ,live_room_avg_played_seconds
  408. ,ad_live_share
  409. ,ad_live_comment
  410. ,live_played_started
  411. ,live_played_started_cost
  412. ,ad_live_follow
  413. ,ad_live_follow_cost
  414. ,simple_live_played_started
  415. ,standard_live_played_started
  416. ,conversion_component_impression
  417. ,conversion_component_click
  418. ,conversion_component_rate
  419. ,ad_landing_page_impression
  420. ,ad_app_download_half_impression
  421. from media_api.kuaishou_account_report_hourly report
  422. left join
  423. `jeecg-boot`.ctop_account_attribution_info sys
  424. on report.advertiser_id = sys.account_id
  425. where report.stat_date between {start_time} and {end_time}
  426. order by account_id,hour,stat_date limit {num},20000
  427. """.format(start_time=self.start_time, end_time=self.end_time, num=i * 20000)
  428. print(sql)
  429. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  430. self.conn.commit()
  431. TidbConn.conn_close(conn=self.conn)
  432. return 0
  433. except Exception as e:
  434. logging.error(e)
  435. return -1
  436. if __name__ == '__main__':
  437. status = AccountHourlyReport().daily_report()
  438. if status == 0:
  439. print("作业执行成功")
  440. exit(0)
  441. else:
  442. print("作业执行失败")
  443. exit(-1)