AppBytedanceAdReportBusi.py 27 KB

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