AppBytedanceAdvertiserReportBusiMonth.py 26 KB

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