AppBytedanceCampaignReportBusiMonth.py 16 KB

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