AppBytedanceCompanyUserReportBusi.py 15 KB

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