AdvertiserReport.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/9/15 5:10 下午
  4. # @Site :
  5. # @File : AdvertiserReport.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. import logging
  9. from Apietl.conn.TidbConn import TidbConn
  10. from Apietl.utlis.Utils import Utils
  11. class AdvertiserReport:
  12. def __init__(self):
  13. self.conn = TidbConn.get_connection()
  14. def daily_report(self, account_id, start_date, end_date):
  15. try:
  16. sql = """
  17. replace into application.bytedance_advertiser_report_daily_dw(account_id, stat_datetime, project_id, product_id,
  18. advertiser_id, company_id, account_name, project_name,
  19. advertiser_name, company_name, sale_id, user_id, org_code,
  20. cost, `show`, avg_show_cost, click, avg_click_cost, ctr,
  21. `convert`, convert_cost, convert_rate, deep_convert,
  22. deep_convert_cost, deep_convert_rate, attribution_convert,
  23. attribution_convert_cost, attribution_deep_convert,
  24. attribution_deep_convert_cost, download_start,
  25. download_start_cost, download_start_rate, download_finish,
  26. download_finish_cost, download_finish_rate, click_install,
  27. install_finish, install_finish_cost, install_finish_rate,
  28. active, active_cost, active_rate, register,
  29. active_register_cost, active_register_rate, next_day_open,
  30. next_day_open_cost, next_day_open_rate,
  31. attribution_next_day_open_cnt,
  32. attribution_next_day_open_cost,
  33. attribution_next_day_open_rate, game_addiction,
  34. game_addiction_cost, game_addiction_rate, pay_count,
  35. active_pay_cost, active_pay_rate, loan_completion,
  36. loan_completion_cost, loan_completion_rate,
  37. pre_loan_credit, pre_loan_credit_cost, loan_credit,
  38. loan_credit_cost, loan_credit_rate, in_app_uv,
  39. in_app_detail_uv, in_app_cart, in_app_pay, in_app_order,
  40. attribution_game_pay_7d_count,
  41. attribution_game_pay_7d_cost,
  42. attribution_active_pay_7d_per_count, game_pay_cost,
  43. game_pay_count, phone, form, map_search, button, view,
  44. download, qq, lottery, vote, message, redirect, shopping,
  45. consult, wechat, phone_confirm, phone_connect,
  46. consult_effective, coupon, coupon_single_page,
  47. redirect_to_shop, poi_collect, poi_address_click,
  48. luban_order_cnt, luban_order_stat_amount, luban_order_roi,
  49. luban_live_enter_cnt, live_watch_one_minute_count,
  50. luban_live_follow_cnt, live_fans_club_join_cnt,
  51. luban_live_comment_cnt, luban_live_share_cnt,
  52. luban_live_gift_cnt, luban_live_gift_amount,
  53. luban_live_slidecart_click_cnt,
  54. luban_live_click_product_cnt, luban_live_pay_order_count,
  55. luban_live_pay_order_stat_cost,
  56. live_component_click_count, live_component_click_cost,
  57. live_component_click_rate, wechat_login_count,
  58. attribution_wechat_login_30d_count, wechat_login_cost,
  59. attribution_wechat_login_30d_cost, wechat_first_pay_count,
  60. attribution_wechat_first_pay_30d_count,
  61. wechat_first_pay_cost,
  62. attribution_wechat_first_pay_30d_cost,
  63. wechat_first_pay_rate,
  64. attribution_wechat_first_pay_30d_rate, wechat_pay_amount,
  65. attribution_wechat_pay_30d_amount,
  66. attribution_wechat_pay_30d_roi, phone_effective,
  67. total_play, valid_play, valid_play_cost, valid_play_rate,
  68. play_25_feed_break, play_50_feed_break,
  69. play_75_feed_break, play_100_feed_break,
  70. average_play_time_per_play, play_over_rate,
  71. wifi_play_rate, wifi_play, play_duration_sum,
  72. advanced_creative_phone_click,
  73. advanced_creative_counsel_click,
  74. advanced_creative_form_click,
  75. advanced_creative_coupon_addition,
  76. advanced_creative_form_submit, card_show, share, comment,
  77. `like`, follow, home_visited, ies_challenge_click,
  78. ies_music_click, location_click, message_action,
  79. click_landing_page, click_shopwindow, click_website,
  80. click_download, click_call_dy, submit_certification_count,
  81. approval_count, first_rental_order_count,
  82. first_order_count, commute_first_pay_count)
  83. select a.advertiser_id account_id,
  84. a.stat_datetime,
  85. b.project_id,
  86. b.product_id,
  87. b.advertiser_id,
  88. b.company_id,
  89. b.account_name,
  90. b.project_name,
  91. b.advertiser_name,
  92. b.company_name,
  93. b.sale_id,
  94. b.user_id,
  95. b.org_code,
  96. a.cost,
  97. a.show,
  98. a.avg_show_cost,
  99. a.click,
  100. a.avg_click_cost,
  101. a.ctr,
  102. a.convert,
  103. a.convert_cost,
  104. a.convert_rate,
  105. a.deep_convert,
  106. a.deep_convert_cost,
  107. a.deep_convert_rate,
  108. a.attribution_convert,
  109. a.attribution_convert_cost,
  110. a.attribution_deep_convert,
  111. a.attribution_deep_convert_cost,
  112. a.download_start,
  113. a.download_start_cost,
  114. a.download_start_rate,
  115. a.download_finish,
  116. a.download_finish_cost,
  117. a.download_finish_rate,
  118. a.click_install,
  119. a.install_finish,
  120. a.install_finish_cost,
  121. a.install_finish_rate,
  122. a.active,
  123. a.active_cost,
  124. a.active_rate,
  125. a.register,
  126. a.active_register_cost,
  127. a.active_register_rate,
  128. a.next_day_open,
  129. a.next_day_open_cost,
  130. a.next_day_open_rate,
  131. a.attribution_next_day_open_cnt,
  132. a.attribution_next_day_open_cost,
  133. a.attribution_next_day_open_rate,
  134. a.game_addiction,
  135. a.game_addiction_cost,
  136. a.game_addiction_rate,
  137. a.pay_count,
  138. a.active_pay_cost,
  139. a.active_pay_rate,
  140. a.loan_completion,
  141. a.loan_completion_cost,
  142. a.loan_completion_rate,
  143. a.pre_loan_credit,
  144. a.pre_loan_credit_cost,
  145. a.loan_credit,
  146. a.loan_credit_cost,
  147. a.loan_credit_rate,
  148. a.in_app_uv,
  149. a.in_app_detail_uv,
  150. a.in_app_cart,
  151. a.in_app_pay,
  152. a.in_app_order,
  153. a.attribution_game_pay_7d_count,
  154. a.attribution_game_pay_7d_cost,
  155. a.attribution_active_pay_7d_per_count,
  156. a.game_pay_cost,
  157. a.game_pay_count,
  158. a.phone,
  159. a.form,
  160. a.map_search,
  161. a.button,
  162. a.view,
  163. a.download,
  164. a.qq,
  165. a.lottery,
  166. a.vote,
  167. a.message,
  168. a.redirect,
  169. a.shopping,
  170. a.consult,
  171. a.wechat,
  172. a.phone_confirm,
  173. a.phone_connect,
  174. a.consult_effective,
  175. a.coupon,
  176. a.coupon_single_page,
  177. a.redirect_to_shop,
  178. a.poi_collect,
  179. a.poi_address_click,
  180. a.luban_order_cnt,
  181. a.luban_order_stat_amount,
  182. a.luban_order_roi,
  183. a.luban_live_enter_cnt,
  184. a.live_watch_one_minute_count,
  185. a.luban_live_follow_cnt,
  186. a.live_fans_club_join_cnt,
  187. a.luban_live_comment_cnt,
  188. a.luban_live_share_cnt,
  189. a.luban_live_gift_cnt,
  190. a.luban_live_gift_amount,
  191. a.luban_live_slidecart_click_cnt,
  192. a.luban_live_click_product_cnt,
  193. a.luban_live_pay_order_count,
  194. a.luban_live_pay_order_stat_cost,
  195. a.live_component_click_count,
  196. a.live_component_click_cost,
  197. a.live_component_click_rate,
  198. a.wechat_login_count,
  199. a.attribution_wechat_login_30d_count,
  200. a.wechat_login_cost,
  201. a.attribution_wechat_login_30d_cost,
  202. a.wechat_first_pay_count,
  203. a.attribution_wechat_first_pay_30d_count,
  204. a.wechat_first_pay_cost,
  205. a.attribution_wechat_first_pay_30d_cost,
  206. a.wechat_first_pay_rate,
  207. a.attribution_wechat_first_pay_30d_rate,
  208. a.wechat_pay_amount,
  209. a.attribution_wechat_pay_30d_amount,
  210. a.attribution_wechat_pay_30d_roi,
  211. a.phone_effective,
  212. a.total_play,
  213. a.valid_play,
  214. a.valid_play_cost,
  215. a.valid_play_rate,
  216. a.play_25_feed_break,
  217. a.play_50_feed_break,
  218. a.play_75_feed_break,
  219. a.play_100_feed_break,
  220. a.average_play_time_per_play,
  221. a.play_over_rate,
  222. a.wifi_play_rate,
  223. a.wifi_play,
  224. a.play_duration_sum,
  225. a.advanced_creative_phone_click,
  226. a.advanced_creative_counsel_click,
  227. a.advanced_creative_form_click,
  228. a.advanced_creative_coupon_addition,
  229. a.advanced_creative_form_submit,
  230. a.card_show,
  231. a.share,
  232. a.comment,
  233. a.like,
  234. a.follow,
  235. a.home_visited,
  236. a.ies_challenge_click,
  237. a.ies_music_click,
  238. a.location_click,
  239. a.message_action,
  240. a.click_landing_page,
  241. a.click_shopwindow,
  242. a.click_website,
  243. a.click_download,
  244. a.click_call_dy,
  245. a.submit_certification_count,
  246. a.approval_count,
  247. a.first_rental_order_count,
  248. a.first_order_count,
  249. a.commute_first_pay_count
  250. from `media_api`.bytedance_advertiser_report_daily a
  251. left join
  252. (select cua.account_id id,
  253. cua.account_id account_id,
  254. cp.id project_id,
  255. product.id product_id,
  256. ca.id advertiser_id,
  257. uc.company_id company_id,
  258. cua.auth_name account_name,
  259. cp.project_name project_name,
  260. ca.name advertiser_name,
  261. uc.company_name company_name,
  262. cp.sale_id sale_id,
  263. su.id user_id,
  264. su.org_code org_code
  265. from `jeecg-boot`.ctop_user_allocation cua
  266. left join `jeecg-boot`.ctop_project cp on cua.project_id = cp.id
  267. left join `jeecg-boot`.ctop_advertiser ca on ca.id = cua.advertiser_id
  268. left join `jeecg-boot`.sys_user su on su.id = cua.user_id
  269. left join `jeecg-boot`.user_company uc on uc.user_id = cua.user_id
  270. left join `jeecg-boot`.ctop_product product on product.id = cp.product_id
  271. where cua.account_id is not null) b
  272. on a.advertiser_id = b.account_id
  273. where a.stat_datetime between {start_date} and {end_date}
  274. and a.advertiser_id = {account_id}
  275. """.format(start_date=start_date, end_date=end_date, account_id=account_id)
  276. print(sql)
  277. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  278. self.conn.commit()
  279. TidbConn.conn_close(conn=self.conn)
  280. except Exception as e:
  281. logging.error('失败详细信息:', repr(e))
  282. def hourly_report(self, account_id, start_date, end_date):
  283. try:
  284. sql = """
  285. replace into application.bytedance_advertiser_report_hourly_dw(account_id, stat_datetime, hour, project_id, product_id,
  286. advertiser_id, company_id, account_name, project_name,
  287. advertiser_name, company_name, sale_id, user_id,
  288. org_code, cost, `show`, avg_show_cost, click,
  289. avg_click_cost, ctr, `convert`, convert_cost,
  290. convert_rate, deep_convert, deep_convert_cost,
  291. deep_convert_rate, attribution_convert,
  292. attribution_convert_cost, attribution_deep_convert,
  293. attribution_deep_convert_cost, download_start,
  294. download_start_cost, download_start_rate,
  295. download_finish, download_finish_cost,
  296. download_finish_rate, click_install, install_finish,
  297. install_finish_cost, install_finish_rate, active,
  298. active_cost, active_rate, register, active_register_cost,
  299. active_register_rate, next_day_open, next_day_open_cost,
  300. next_day_open_rate, attribution_next_day_open_cnt,
  301. attribution_next_day_open_cost,
  302. attribution_next_day_open_rate, game_addiction,
  303. game_addiction_cost, game_addiction_rate, pay_count,
  304. active_pay_cost, active_pay_rate, loan_completion,
  305. loan_completion_cost, loan_completion_rate,
  306. pre_loan_credit, pre_loan_credit_cost, loan_credit,
  307. loan_credit_cost, loan_credit_rate, in_app_uv,
  308. in_app_detail_uv, in_app_cart, in_app_pay, in_app_order,
  309. attribution_game_pay_7d_count,
  310. attribution_game_pay_7d_cost,
  311. attribution_active_pay_7d_per_count, game_pay_cost,
  312. game_pay_count, phone, form, map_search, button, view,
  313. download, qq, lottery, vote, message, redirect, shopping,
  314. consult, wechat, phone_confirm, phone_connect,
  315. consult_effective, coupon, coupon_single_page,
  316. redirect_to_shop, poi_collect, poi_address_click,
  317. luban_order_cnt, luban_order_stat_amount,
  318. luban_order_roi, luban_live_enter_cnt,
  319. live_watch_one_minute_count, luban_live_follow_cnt,
  320. live_fans_club_join_cnt, luban_live_comment_cnt,
  321. luban_live_share_cnt, luban_live_gift_cnt,
  322. luban_live_gift_amount, luban_live_slidecart_click_cnt,
  323. luban_live_click_product_cnt, luban_live_pay_order_count,
  324. luban_live_pay_order_stat_cost,
  325. live_component_click_count, live_component_click_cost,
  326. live_component_click_rate, wechat_login_count,
  327. attribution_wechat_login_30d_count, wechat_login_cost,
  328. attribution_wechat_login_30d_cost,
  329. wechat_first_pay_count,
  330. attribution_wechat_first_pay_30d_count,
  331. wechat_first_pay_cost,
  332. attribution_wechat_first_pay_30d_cost,
  333. wechat_first_pay_rate,
  334. attribution_wechat_first_pay_30d_rate, wechat_pay_amount,
  335. attribution_wechat_pay_30d_amount,
  336. attribution_wechat_pay_30d_roi, phone_effective,
  337. total_play, valid_play, valid_play_cost, valid_play_rate,
  338. play_25_feed_break, play_50_feed_break,
  339. play_75_feed_break, play_100_feed_break,
  340. average_play_time_per_play, play_over_rate,
  341. wifi_play_rate, wifi_play, play_duration_sum,
  342. advanced_creative_phone_click,
  343. advanced_creative_counsel_click,
  344. advanced_creative_form_click,
  345. advanced_creative_coupon_addition,
  346. advanced_creative_form_submit, card_show, share, comment,
  347. `like`, follow, home_visited, ies_challenge_click,
  348. ies_music_click, location_click, message_action,
  349. click_landing_page, click_shopwindow, click_website,
  350. click_download, click_call_dy,
  351. submit_certification_count, approval_count,
  352. first_rental_order_count, first_order_count,
  353. commute_first_pay_count)
  354. select a.advertiser_id account_id,
  355. a.stat_datetime,
  356. a.`hour`,
  357. b.project_id,
  358. b.product_id,
  359. b.advertiser_id,
  360. b.company_id,
  361. b.account_name,
  362. b.project_name,
  363. b.advertiser_name,
  364. b.company_name,
  365. b.sale_id,
  366. b.user_id,
  367. b.org_code,
  368. a.cost,
  369. a.show,
  370. a.avg_show_cost,
  371. a.click,
  372. a.avg_click_cost,
  373. a.ctr,
  374. a.convert,
  375. a.convert_cost,
  376. a.convert_rate,
  377. a.deep_convert,
  378. a.deep_convert_cost,
  379. a.deep_convert_rate,
  380. a.attribution_convert,
  381. a.attribution_convert_cost,
  382. a.attribution_deep_convert,
  383. a.attribution_deep_convert_cost,
  384. a.download_start,
  385. a.download_start_cost,
  386. a.download_start_rate,
  387. a.download_finish,
  388. a.download_finish_cost,
  389. a.download_finish_rate,
  390. a.click_install,
  391. a.install_finish,
  392. a.install_finish_cost,
  393. a.install_finish_rate,
  394. a.active,
  395. a.active_cost,
  396. a.active_rate,
  397. a.register,
  398. a.active_register_cost,
  399. a.active_register_rate,
  400. a.next_day_open,
  401. a.next_day_open_cost,
  402. a.next_day_open_rate,
  403. a.attribution_next_day_open_cnt,
  404. a.attribution_next_day_open_cost,
  405. a.attribution_next_day_open_rate,
  406. a.game_addiction,
  407. a.game_addiction_cost,
  408. a.game_addiction_rate,
  409. a.pay_count,
  410. a.active_pay_cost,
  411. a.active_pay_rate,
  412. a.loan_completion,
  413. a.loan_completion_cost,
  414. a.loan_completion_rate,
  415. a.pre_loan_credit,
  416. a.pre_loan_credit_cost,
  417. a.loan_credit,
  418. a.loan_credit_cost,
  419. a.loan_credit_rate,
  420. a.in_app_uv,
  421. a.in_app_detail_uv,
  422. a.in_app_cart,
  423. a.in_app_pay,
  424. a.in_app_order,
  425. a.attribution_game_pay_7d_count,
  426. a.attribution_game_pay_7d_cost,
  427. a.attribution_active_pay_7d_per_count,
  428. a.game_pay_cost,
  429. a.game_pay_count,
  430. a.phone,
  431. a.form,
  432. a.map_search,
  433. a.button,
  434. a.view,
  435. a.download,
  436. a.qq,
  437. a.lottery,
  438. a.vote,
  439. a.message,
  440. a.redirect,
  441. a.shopping,
  442. a.consult,
  443. a.wechat,
  444. a.phone_confirm,
  445. a.phone_connect,
  446. a.consult_effective,
  447. a.coupon,
  448. a.coupon_single_page,
  449. a.redirect_to_shop,
  450. a.poi_collect,
  451. a.poi_address_click,
  452. a.luban_order_cnt,
  453. a.luban_order_stat_amount,
  454. a.luban_order_roi,
  455. a.luban_live_enter_cnt,
  456. a.live_watch_one_minute_count,
  457. a.luban_live_follow_cnt,
  458. a.live_fans_club_join_cnt,
  459. a.luban_live_comment_cnt,
  460. a.luban_live_share_cnt,
  461. a.luban_live_gift_cnt,
  462. a.luban_live_gift_amount,
  463. a.luban_live_slidecart_click_cnt,
  464. a.luban_live_click_product_cnt,
  465. a.luban_live_pay_order_count,
  466. a.luban_live_pay_order_stat_cost,
  467. a.live_component_click_count,
  468. a.live_component_click_cost,
  469. a.live_component_click_rate,
  470. a.wechat_login_count,
  471. a.attribution_wechat_login_30d_count,
  472. a.wechat_login_cost,
  473. a.attribution_wechat_login_30d_cost,
  474. a.wechat_first_pay_count,
  475. a.attribution_wechat_first_pay_30d_count,
  476. a.wechat_first_pay_cost,
  477. a.attribution_wechat_first_pay_30d_cost,
  478. a.wechat_first_pay_rate,
  479. a.attribution_wechat_first_pay_30d_rate,
  480. a.wechat_pay_amount,
  481. a.attribution_wechat_pay_30d_amount,
  482. a.attribution_wechat_pay_30d_roi,
  483. a.phone_effective,
  484. a.total_play,
  485. a.valid_play,
  486. a.valid_play_cost,
  487. a.valid_play_rate,
  488. a.play_25_feed_break,
  489. a.play_50_feed_break,
  490. a.play_75_feed_break,
  491. a.play_100_feed_break,
  492. a.average_play_time_per_play,
  493. a.play_over_rate,
  494. a.wifi_play_rate,
  495. a.wifi_play,
  496. a.play_duration_sum,
  497. a.advanced_creative_phone_click,
  498. a.advanced_creative_counsel_click,
  499. a.advanced_creative_form_click,
  500. a.advanced_creative_coupon_addition,
  501. a.advanced_creative_form_submit,
  502. a.card_show,
  503. a.share,
  504. a.comment,
  505. a.like,
  506. a.follow,
  507. a.home_visited,
  508. a.ies_challenge_click,
  509. a.ies_music_click,
  510. a.location_click,
  511. a.message_action,
  512. a.click_landing_page,
  513. a.click_shopwindow,
  514. a.click_website,
  515. a.click_download,
  516. a.click_call_dy,
  517. a.submit_certification_count,
  518. a.approval_count,
  519. a.first_rental_order_count,
  520. a.first_order_count,
  521. a.commute_first_pay_count
  522. from `media_api`.bytedance_advertiser_report_hourly a
  523. left join
  524. (select cua.account_id id,
  525. cua.account_id account_id,
  526. cp.id project_id,
  527. product.id product_id,
  528. ca.id advertiser_id,
  529. uc.company_id company_id,
  530. cua.auth_name account_name,
  531. cp.project_name project_name,
  532. ca.name advertiser_name,
  533. uc.company_name company_name,
  534. cp.sale_id sale_id,
  535. su.id user_id,
  536. su.org_code org_code
  537. from `jeecg-boot`.ctop_user_allocation cua
  538. left join `jeecg-boot`.ctop_project cp on cua.project_id = cp.id
  539. left join `jeecg-boot`.ctop_advertiser ca on ca.id = cua.advertiser_id
  540. left join `jeecg-boot`.sys_user su on su.id = cua.user_id
  541. left join `jeecg-boot`.user_company uc on uc.user_id = cua.user_id
  542. left join `jeecg-boot`.ctop_product product on product.id = cp.product_id
  543. where cua.account_id is not null) b
  544. on a.advertiser_id = b.account_id
  545. where a.stat_datetime between {start_date} and {end_date}
  546. and a.advertiser_id = {account_id}
  547. """.format(start_date=start_date, end_date=end_date, account_id=account_id)
  548. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  549. self.conn.commit()
  550. TidbConn.conn_close(conn=self.conn)
  551. except Exception as e:
  552. logging.error('失败详细信息:', repr(e))
  553. def handler(self, date_type, account_id, start_date, end_date):
  554. if date_type == "daily":
  555. self.daily_report(account_id, start_date, end_date)
  556. else:
  557. self.hourly_report(account_id, start_date, end_date)