AppBytedanceAdReportBusiMonth.py 27 KB

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