AppBytedanceCompanyUserReportBusiMonth.py 15 KB

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