AccountHourlyReport.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. try:
  23. sql = """select advertiser_id from media_api.kuaishou_account_report_hourly where
  24. stat_date between {start_time} and {end_time}
  25. group by advertiser_id
  26. """.format(
  27. start_time=self.start_time, end_time=self.end_time)
  28. adv_list = TidbConn.quary(self.conn, sql)
  29. for account_id in adv_list:
  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. ,event_draw_credit_line
  170. ,minigame_iaa_purchase_amount
  171. ,minigame_iaa_purchase_roi
  172. )
  173. select report.advertiser_id as account_id,
  174. stat_date,
  175. `hour`,
  176. project_id,
  177. product_id,
  178. sys.advertiser_id,
  179. company_id,
  180. account_name,
  181. project_name,
  182. advertiser_name,
  183. company_name,
  184. sale_id,
  185. user_id,
  186. org_code,
  187. charge,
  188. `show`,
  189. photo_click,
  190. aclick,
  191. bclick,
  192. photo_click_ratio,
  193. play_3s_ratio,
  194. action_ratio,
  195. impression_1k_cost,
  196. photo_click_cost,
  197. action_cost,
  198. share,
  199. comment,
  200. `like`,
  201. follow,
  202. cancel_follow,
  203. report,
  204. block,
  205. negative,
  206. submit,
  207. download_started,
  208. download_completed,
  209. activation,
  210. event_pay_first_day,
  211. event_pay_purchase_amount_first_day,
  212. event_pay_first_day_roi,
  213. event_pay,
  214. event_pay_purchase_amount,
  215. event_pay_roi,
  216. event_register,
  217. event_register_cost,
  218. event_register_ratio,
  219. event_jin_jian_app,
  220. event_jin_jian_app_cost,
  221. event_credit_grant_app,
  222. event_credit_grant_app_cost,
  223. event_credit_grant_app_ratio,
  224. event_order_paid,
  225. event_order_paid_purchase_amount,
  226. event_order_paid_cost,
  227. event_next_day_stay,
  228. event_next_day_stay_cost,
  229. event_next_day_stay_ratio,
  230. form_count,
  231. form_cost,
  232. form_action_ratio,
  233. event_jin_jian_landing_page,
  234. event_jin_jian_landing_page_cost,
  235. event_credit_grant_landing_page,
  236. event_credit_grant_landing_page_cost,
  237. event_credit_grant_landing_page_ratio,
  238. event_valid_clues,
  239. event_valid_clues_cost,
  240. event_add_wechat,
  241. event_add_wechat_cost,
  242. event_add_wechat_ratio,
  243. event_get_through,
  244. event_get_through_cost,
  245. event_get_through_ratio,
  246. event_app_invoked,
  247. event_app_invoked_cost,
  248. event_app_invoked_ratio,
  249. event_credit_grant_landing_ratio,
  250. play_5s_ratio,
  251. play_end_ratio,
  252. event_new_user_pay,
  253. event_new_user_pay_cost,
  254. event_new_user_pay_ratio,
  255. click_1k_cost,
  256. ad_product_cnt,
  257. event_goods_view,
  258. merchant_reco_fans,
  259. event_order_amount_roi,
  260. event_goods_view_cost,
  261. merchant_reco_fans_cost,
  262. event_button_click,
  263. event_button_click_cost,
  264. event_button_click_ratio,
  265. event_order_paid_roi,
  266. event_new_user_jinjian_app,
  267. event_new_user_jinjian_app_cost,
  268. event_new_user_jinjian_app_roi,
  269. event_new_user_credit_grant_app,
  270. event_new_user_credit_grant_app_cost,
  271. event_new_user_credit_grant_app_roi,
  272. event_new_user_jinjian_page,
  273. event_new_user_jinjian_page_cost,
  274. event_new_user_jinjian_page_roi,
  275. event_new_user_credit_grant_page,
  276. event_new_user_credit_grant_page_cost,
  277. event_new_user_credit_grant_page_roi,
  278. event_appoint_form,
  279. event_appoint_form_cost,
  280. event_appoint_form_ratio,
  281. event_appoint_jump_click,
  282. event_appoint_jump_click_cost,
  283. event_appoint_jump_click_ratio,
  284. union_event_pay_purchase_amount_7d,
  285. union_event_pay_purchase_amount_7d_roi,
  286. placement_type,
  287. ad_scene,
  288. event_order_successed,
  289. ad_photo_played_10s_ratio,
  290. key_action_cost,
  291. event_add_shopping_cart_cost,
  292. event_ad_watch_times_ratio,
  293. event_outbound_call_cost,
  294. event_outbound_call,
  295. action_new_ratio,
  296. event_watch_app_ad,
  297. event_multi_conversion_cost,
  298. event_phone_card_activate,
  299. ad_photo_played_75percent_ratio,
  300. key_action_ratio,
  301. event_ad_watch_times_cost,
  302. event_add_shopping_cart,
  303. key_action,
  304. event_multi_conversion,
  305. event_wechat_connected,
  306. event_dsp_gift_form,
  307. event_intention_confirmed,
  308. event_phone_get_through,
  309. event_multi_conversion_ratio,
  310. event_measurement_house,
  311. ad_photo_played_2s_ratio,
  312. event_outbound_call_ratio,
  313. event_ad_watch_times,
  314. event_pay_purchase_amount_one_day_by_conversion,
  315. ad_photo_played_10s
  316. ,ad_photo_played_2s
  317. ,ad_photo_played_75percent
  318. ,approx_pay_cost
  319. ,approx_pay_count
  320. ,approx_pay_ratio
  321. ,cancel_like
  322. ,click_conversion_ratio
  323. ,conversion_cost
  324. ,conversion_cost_by_impression_7d
  325. ,conversion_num
  326. ,conversion_num_by_impression_7d
  327. ,conversion_num_cost
  328. ,conversion_ratio
  329. ,conversion_ratio_by_impression_7d
  330. ,deep_conversion_cost
  331. ,deep_conversion_cost_by_impression_7d
  332. ,deep_conversion_num
  333. ,deep_conversion_num_by_impression_7d
  334. ,deep_conversion_ratio
  335. ,deep_conversion_ratio_by_impression_7d
  336. ,download_completed_cost
  337. ,download_completed_ratio
  338. ,download_conversion_ratio
  339. ,download_installed
  340. ,download_started_cost
  341. ,download_started_ratio
  342. ,event_24h_stay
  343. ,event_24h_stay_by_conversion
  344. ,event_24h_stay_by_conversion_cost
  345. ,event_24h_stay_by_conversion_ratio
  346. ,event_24h_stay_cost
  347. ,event_24h_stay_ratio
  348. ,event_ad_watch_10_times
  349. ,event_ad_watch_10_times_cost
  350. ,event_ad_watch_10_times_ratio
  351. ,event_ad_watch_20_times
  352. ,event_ad_watch_20_times_cost
  353. ,event_ad_watch_20_times_ratio
  354. ,event_ad_watch_5_times
  355. ,event_ad_watch_5_times_cost
  356. ,event_ad_watch_5_times_ratio
  357. ,event_audition
  358. ,event_consultation_valid_retained
  359. ,event_consultation_valid_retained_cost
  360. ,event_consultation_valid_retained_ratio
  361. ,event_conversion_click_cost
  362. ,event_conversion_click_ratio
  363. ,event_credit_grant_first_day_app
  364. ,event_credit_grant_first_day_app_cost
  365. ,event_credit_grant_first_day_app_ratio
  366. ,event_credit_grant_first_day_landing_page
  367. ,event_credit_grant_first_day_landing_page_cost
  368. ,event_credit_grant_first_day_landing_page_ratio
  369. ,event_making_calls
  370. ,event_making_calls_cost
  371. ,event_making_calls_ratio
  372. ,event_order_submit
  373. ,event_pay_purchase_amount_one_day
  374. ,event_pay_purchase_amount_one_day_by_conversion_roi
  375. ,event_pay_purchase_amount_one_day_roi
  376. ,event_pay_purchase_amount_three_day_by_conversion
  377. ,event_pay_purchase_amount_three_day_by_conversion_roi
  378. ,event_pay_purchase_amount_week_by_conversion
  379. ,event_pay_purchase_amount_week_by_conversion_roi
  380. ,event_pay_week_by_conversion
  381. ,event_pay_week_by_conversion_cost
  382. ,event_pay_weighted_purchase_amount
  383. ,event_pay_weighted_purchase_amount_first_day
  384. ,event_pre_component_consultation_valid_retained
  385. ,event_three_day_stay_by_conversion
  386. ,event_three_day_stay_by_conversion_cost
  387. ,event_three_day_stay_by_conversion_ratio
  388. ,event_wechat_qr_code_link_click
  389. ,event_week_stay_by_conversion
  390. ,event_week_stay_by_conversion_cost
  391. ,event_week_stay_by_conversion_ratio
  392. ,live_event_goods_view
  393. ,live_played_3s
  394. ,played_end
  395. ,played_five_seconds
  396. ,played_three_seconds
  397. ,event_next_day_stay_new
  398. ,event_next_day_stay_new_cost
  399. ,event_next_day_stay_new_ratio
  400. ,ad_show
  401. ,event_credit_card_recheck
  402. ,event_credit_card_recheck_first_day
  403. ,event_no_intention
  404. ,event_multi_pay_seven_day_by_conversion
  405. ,event_multi_pay_seven_day_by_conversion_cost
  406. ,mcb_left_time_perf_cpa_bid
  407. ,mcb_left_time_perf_roi_ratio
  408. ,mcb_all_day_perf_cpa_bid
  409. ,mcb_all_day_perf_roi_ratio
  410. ,live_room_avg_played_seconds
  411. ,ad_live_share
  412. ,ad_live_comment
  413. ,live_played_started
  414. ,live_played_started_cost
  415. ,ad_live_follow
  416. ,ad_live_follow_cost
  417. ,simple_live_played_started
  418. ,standard_live_played_started
  419. ,conversion_component_impression
  420. ,conversion_component_click
  421. ,conversion_component_rate
  422. ,ad_landing_page_impression
  423. ,ad_app_download_half_impression
  424. ,event_draw_credit_line
  425. ,minigame_iaa_purchase_amount
  426. ,minigame_iaa_purchase_roi
  427. from media_api.kuaishou_account_report_hourly report
  428. left join
  429. (SELECT
  430. cua.account_id id,
  431. cua.account_id account_id,
  432. cp.id project_id,
  433. product.id product_id,
  434. ca.id advertiser_id,
  435. uc.company_id company_id,
  436. cua.auth_name account_name,
  437. cp.project_name project_name,
  438. ca.`name` advertiser_name,
  439. uc.company_name company_name,
  440. cp.sale_id sale_id,
  441. su.id user_id,
  442. su.leader_id,
  443. su.org_code org_code
  444. FROM
  445. `jeecg-boot`.ctop_user_allocation cua
  446. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  447. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  448. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  449. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  450. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  451. WHERE
  452. cua.account_id IS NOT NULL
  453. ) sys
  454. on report.advertiser_id = sys.account_id
  455. where report.stat_date between {start_time} and {end_time}
  456. and report.advertiser_id = {account_id}
  457. """.format(start_time=self.start_time, end_time=self.end_time, account_id=account_id[0])
  458. print(sql)
  459. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  460. self.conn.commit()
  461. TidbConn.conn_close(conn=self.conn)
  462. return 0
  463. except Exception as e:
  464. logging.error(e)
  465. return -1
  466. if __name__ == '__main__':
  467. status = AccountHourlyReport().daily_report()
  468. if status == 0:
  469. print("作业执行成功")
  470. exit(0)
  471. else:
  472. print("作业执行失败")
  473. exit(-1)