AccountHourlyReport.py 17 KB

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