AppBytedanceCompanyDesignReportBusiMonth.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/11/10 3:37 下午
  4. # @Site :
  5. # @File : AppBytedanceCompanyDesignReportBusi.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 AppBytedanceCompanyDesignReportBusi:
  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. delete from application.app_bytedance_company_design_report_busi_m where stat_mon=date_format({date_time},'%Y%m');
  22. SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  23. REPLACE INTO application.app_bytedance_company_design_report_busi_m
  24. SELECT
  25. date_format({date_time},'%Y%m') as stat_mon,
  26. ifnull(company_id,'-') company_id,
  27. company_name,
  28. org_code,
  29. org_type,
  30. id,
  31. depart_name,
  32. ifnull(leader_id,'-') leader_id,
  33. leader_name,
  34. count(DISTINCT signature) num,
  35. count(DISTINCT innovate_code) innovate_num,
  36. sum(cost) cost,
  37. sum(`show`) `show`,
  38. ifnull(round(sum(cost) / sum(`show`), 3),0) avg_show_cost,
  39. sum(click) click,
  40. ifnull(round(sum(cost) / sum(`click`), 3),0) avg_click_cost,
  41. ifnull(round(sum(click) / sum(`show`) * 100,3),0) ctr,
  42. sum(`convert`) `convert`,
  43. ifnull(round(sum(cost) / sum(`convert`), 3),0) convert_cost,
  44. ifnull(round(sum(`convert`) / sum(click) * 100,3),0) convert_rate,
  45. sum(deep_convert) deep_convert,
  46. ifnull(round(sum(cost) / sum(deep_convert),3),0) deep_convert_cost,
  47. ifnull(round(sum(deep_convert) / sum(`convert`) * 100,3),0) deep_convert_rate,
  48. sum(attribution_convert) attribution_convert,
  49. ifnull(round(sum(cost) / sum(attribution_convert),3),0) attribution_convert_cost,
  50. sum(attribution_deep_convert) attribution_deep_convert,
  51. ifnull(round(sum(cost) / sum(attribution_deep_convert),3),0) attribution_deep_convert_cost,
  52. sum(download_start) download_start,
  53. ifnull(round(sum(cost) / sum(download_start),3),0) download_start_cost,
  54. ifnull(round(sum(download_start) / sum(click),3),0) download_start_rate,
  55. sum(download_finish) download_finish,
  56. ifnull(round(sum(cost) / sum(download_finish),3),0) download_finish_cost,
  57. ifnull(round(sum(download_finish) / sum(download_start) * 100,3),0) download_finish_rate,
  58. sum(click_install) click_install,
  59. sum(install_finish) install_finish,
  60. ifnull(round(sum(cost) / sum(install_finish),3),0) install_finish_cost,
  61. ifnull(round(sum(install_finish) / sum(download_finish) * 100,3),0) install_finish_rate,
  62. sum(active) active,
  63. ifnull(round(sum(cost) / sum(active), 3),0) active_cost,
  64. ifnull(round(sum(active) / sum(click) * 100,3),0) active_rate,
  65. sum(register) register,
  66. ifnull(round(sum(cost) / sum(register), 3),0) active_register_cost,
  67. ifnull(round(sum(active_register_rate) /{day_count} * 100,3),0) active_register_rate,
  68. sum(active_pay_amount) active_pay_amount,
  69. sum(next_day_open) next_day_open,
  70. ifnull(round(sum(cost) / sum(next_day_open),3),0) next_day_open_cost,
  71. ifnull(round(sum(next_day_open) / sum(active) * 100,3),0) next_day_open_rate,
  72. sum(attribution_next_day_open_cnt) attribution_next_day_open_cnt,
  73. ifnull(round(sum(cost) / sum( attribution_next_day_open_cnt),3),0) attribution_next_day_open_cost,
  74. ifnull(round(sum( attribution_next_day_open_cnt) / sum(active) * 100,3),0) attribution_next_day_open_rate,
  75. sum(game_addiction) game_addiction,
  76. ifnull(round(sum(cost) / sum(game_addiction),3),0) game_addiction_cost,
  77. ifnull(round(sum(game_addiction_rate) /{day_count} * 100,3),0) game_addiction_rate,
  78. sum(pay_count) pay_count,
  79. ifnull(round(sum(cost) / sum(pay_count), 3),0) active_pay_cost,
  80. ifnull(round(sum(pay_count) / sum(active) * 100,3),0) active_pay_rate,
  81. sum(loan_completion) loan_completion,
  82. ifnull(round(sum(cost) / sum(loan_completion),3),0) loan_completion_cost,
  83. ifnull(round(sum(loan_completion) / sum(register) * 100,3),0) loan_completion_rate,
  84. ifnull(round(sum(pre_loan_credit), 3),0) pre_loan_credit,
  85. ifnull(round(sum(cost) / sum(pre_loan_credit),3),0) pre_loan_credit_cost,
  86. sum(loan_credit) loan_credit,
  87. ifnull(round(sum(cost) / sum(loan_credit),3),0) loan_credit_cost,
  88. ifnull(round(sum(loan_credit) / sum(loan_completion) * 100,3),0) loan_credit_rate,
  89. sum(in_app_uv) in_app_uv,
  90. sum(in_app_detail_uv) in_app_detail_uv,
  91. sum(in_app_cart) in_app_cart,
  92. sum(in_app_pay) in_app_pay,
  93. sum(in_app_order) in_app_order,
  94. ifnull(round(sum( attribution_game_pay_7d_count) /{day_count}, 3),0) attribution_game_pay_7d_count,
  95. ifnull(round(sum( attribution_game_pay_7d_cost) /{day_count}, 3),0) attribution_game_pay_7d_cost,
  96. ifnull(round(sum( attribution_active_pay_7d_per_count) /{day_count}, 3),0) attribution_active_pay_7d_per_count,
  97. sum(game_pay_cost) game_pay_cost,
  98. sum(game_pay_count) game_pay_count,
  99. sum(phone) phone,
  100. sum(form) form,
  101. sum(map_search) map_search,
  102. sum(button) button,
  103. sum(`view`) `view`,
  104. sum(download) download,
  105. sum(qq) qq,
  106. sum(lottery) lottery,
  107. sum(vote) vote,
  108. sum(message) message,
  109. sum(redirect) redirect,
  110. sum(shopping) shopping,
  111. sum(consult) consult,
  112. sum(wechat) wechat,
  113. sum(phone_confirm) phone_confirm,
  114. sum(phone_connect) phone_connect,
  115. sum(consult_effective) consult_effective,
  116. sum(coupon) coupon,
  117. sum(coupon_single_page) coupon_single_page,
  118. sum(redirect_to_shop) redirect_to_shop,
  119. sum(poi_collect) poi_collect,
  120. sum(poi_address_click) poi_address_click,
  121. sum(luban_order_cnt) luban_order_cnt,
  122. sum(luban_order_stat_amount) luban_order_stat_amount,
  123. sum(luban_order_roi) luban_order_roi,
  124. sum(luban_live_enter_cnt) luban_live_enter_cnt,
  125. sum(live_watch_one_minute_count) live_watch_one_minute_count,
  126. sum(luban_live_follow_cnt) luban_live_follow_cnt,
  127. sum(live_fans_club_join_cnt) live_fans_club_join_cnt,
  128. sum(luban_live_comment_cnt) luban_live_comment_cnt,
  129. sum(luban_live_share_cnt) luban_live_share_cnt,
  130. sum(luban_live_gift_cnt) luban_live_gift_cnt,
  131. sum(luban_live_gift_amount) luban_live_gift_amount,
  132. sum(luban_live_slidecart_click_cnt) luban_live_slidecart_click_cnt,
  133. sum(luban_live_click_product_cnt) luban_live_click_product_cnt,
  134. sum(luban_live_pay_order_count) luban_live_pay_order_count,
  135. sum(luban_live_pay_order_stat_cost) luban_live_pay_order_stat_cost,
  136. sum(wechat_login_count) wechat_login_count,
  137. sum(attribution_wechat_login_30d_count) attribution_wechat_login_30d_count,
  138. ifnull(round(sum(cost) / sum(wechat_login_count),3),0) wechat_login_cost,
  139. ifnull(round(sum(cost) / sum( attribution_wechat_login_30d_count),3),0) attribution_wechat_login_30d_cost,
  140. sum(wechat_first_pay_count) wechat_first_pay_count,
  141. sum(attribution_wechat_first_pay_30d_count) attribution_wechat_first_pay_30d_count,
  142. ifnull(round(sum(cost) / sum(wechat_first_pay_count),3),0) wechat_first_pay_cost,
  143. ifnull(round(sum(cost) / sum( attribution_wechat_first_pay_30d_count),3),0) attribution_wechat_first_pay_30d_cost,
  144. ifnull(round(sum(wechat_first_pay_count) / sum(wechat_login_count) * 100,3),0) wechat_first_pay_rate,
  145. ifnull(round(sum( attribution_wechat_first_pay_30d_count) / sum( attribution_wechat_login_30d_count) * 100,3),0) attribution_wechat_first_pay_30d_rate,
  146. sum(wechat_pay_amount) wechat_pay_amount,
  147. sum(attribution_wechat_pay_30d_amount) attribution_wechat_pay_30d_amount,
  148. sum(attribution_wechat_pay_30d_roi) attribution_wechat_pay_30d_roi,
  149. sum(phone_effective) phone_effective,
  150. sum(total_play) total_play,
  151. sum(valid_play) valid_play,
  152. ifnull(round(sum(cost) / sum(valid_play),3),0) valid_play_cost,
  153. ifnull(round(sum(valid_play) / sum(`show`) * 100,3),0) valid_play_rate,
  154. sum(play_25_feed_break) play_25_feed_break,
  155. sum(play_50_feed_break) play_50_feed_break,
  156. sum(play_75_feed_break) play_75_feed_break,
  157. sum(play_100_feed_break) play_100_feed_break,
  158. ifnull(round(sum(average_play_time_per_play) /{day_count}, 3),0) average_play_time_per_play,
  159. ifnull(round(sum(play_over_rate) /{day_count} * 100,3),0) play_over_rate,
  160. ifnull(round(sum(wifi_play) / sum(total_play) * 100,3),0) wifi_play_rate,
  161. sum(wifi_play) wifi_play,
  162. ifnull(round(sum(play_duration_sum) /{day_count}, 3),0) play_duration_sum,
  163. sum(advanced_creative_phone_click) advanced_creative_phone_click,
  164. sum(advanced_creative_counsel_click) advanced_creative_counsel_click,
  165. sum(advanced_creative_form_click) advanced_creative_form_click,
  166. sum(advanced_creative_coupon_addition) advanced_creative_coupon_addition,
  167. sum(advanced_creative_form_submit) advanced_creative_form_submit,
  168. sum(card_show) card_show,
  169. sum(`share`) `share`,
  170. sum(`comment`) `comment`,
  171. sum(`like`) `like`,
  172. sum(follow) follow,
  173. sum(home_visited) home_visited,
  174. sum(ies_challenge_click) ies_challenge_click,
  175. sum(ies_music_click) ies_music_click,
  176. sum(location_click) location_click,
  177. sum(message_action) message_action,
  178. sum(click_landing_page) click_landing_page,
  179. sum(click_shopwindow) click_shopwindow,
  180. sum(click_website) click_website,
  181. sum(click_download) click_download,
  182. sum(click_call_dy) click_call_dy,
  183. sum(cpc) cpc,
  184. sum(cpm) cpm,
  185. sum(cpa) cpa,
  186. ifnull(round(sum(interact_per_cost) /{day_count}, 3),0) interact_per_cost,
  187. ifnull(round(sum(convert_show_rate) /{day_count}, 3),0) convert_show_rate,
  188. ifnull(round(sum(play_duration_10s) / sum(total_play) * 100,3),0) play_duration_10s_rate,
  189. ifnull(round(sum(play_50_feed_break) / sum(total_play) * 100,3),0) play_50_feed_break_rate,
  190. ifnull(round(sum(play_25_feed_break) / sum(total_play) * 100,3),0) play_25_feed_break_rate,
  191. ifnull(round(sum(play_duration_5s_rate) /{day_count}, 3),0) play_duration_5s_rate,
  192. ifnull(round(sum(play_duration_2s) / sum(total_play) * 100,3),0) play_duration_2s_rate,
  193. ifnull(round(sum(average_play_progress) /{day_count}, 3),0) average_play_progress,
  194. ifnull(round(sum(play_100_feed_break) / sum(total_play) * 100,3),0) play_100_feed_break_rate,
  195. ifnull(round(sum(play_duration_3s) / sum(total_play) * 100,3),0) play_duration_3s_rate,
  196. ifnull(round(sum(play_75_feed_break) / sum(total_play) * 100,3),0) play_75_feed_break_rate,
  197. sum(play_duration) play_duration,
  198. sum(play_duration_3s) play_duration_3s,
  199. sum(play_duration_2s) play_duration_2s,
  200. sum(play_duration_10s) play_duration_10s,
  201. ifnull(round(sum(average_video_play) /{day_count}, 3),0) average_video_play,
  202. sum(avg_rank) avg_rank,
  203. sum(submit_certification_count) submit_certification_count,
  204. sum(approval_count) approval_count,
  205. sum(first_order_count) first_rental_order_count,
  206. sum(first_rental_order_count) first_order_count,
  207. sum(commute_first_pay_count) commute_first_pay_count
  208. FROM (
  209. SELECT video.signature,
  210. uc.company_id,
  211. CASE
  212. WHEN uc.company_id = 'd57fecdcf7a94d009736d9c850731582' THEN
  213. '北京汇创思拓数字科技有限公司'
  214. WHEN uc.company_id = '6608ed13c0dd42de93c790dbdf124234' THEN
  215. '广州汇创思拓数字科技有限公司'
  216. WHEN uc.company_id = '4b10089775c040119e139087517aed88' THEN
  217. '上海汇创思拓数字科技有限公司'
  218. ELSE
  219. uc.company_name
  220. END company_name,
  221. cma.leader_id,
  222. us.realname as leader_name,
  223. ur.org_type,
  224. ur.org_code,
  225. ur.id,
  226. ur.depart_name,
  227. case when cmi.material_innovate = 2 then video.signature else '' end innovate_code,
  228. report.cost,
  229. report.`show`,
  230. report.avg_show_cost,
  231. report.click,
  232. report.avg_click_cost,
  233. report.ctr,
  234. report.`convert`,
  235. report.convert_cost,
  236. report.convert_rate,
  237. report.deep_convert,
  238. report.deep_convert_cost,
  239. report.deep_convert_rate,
  240. report.attribution_convert,
  241. report.attribution_convert_cost,
  242. report.attribution_deep_convert,
  243. report.attribution_deep_convert_cost,
  244. report.download_start,
  245. report.download_start_cost,
  246. report.download_start_rate,
  247. report.download_finish,
  248. report.download_finish_cost,
  249. report.download_finish_rate,
  250. report.click_install,
  251. report.install_finish,
  252. report.install_finish_cost,
  253. report.install_finish_rate,
  254. report.active,
  255. report.active_cost,
  256. report.active_rate,
  257. report.register,
  258. report.active_register_cost,
  259. report.active_register_rate,
  260. report.active_pay_amount,
  261. report.next_day_open,
  262. report.next_day_open_cost,
  263. report.next_day_open_rate,
  264. report.attribution_next_day_open_cnt,
  265. report.attribution_next_day_open_cost,
  266. report.attribution_next_day_open_rate,
  267. report.game_addiction,
  268. report.game_addiction_cost,
  269. report.game_addiction_rate,
  270. report.pay_count,
  271. report.active_pay_cost,
  272. report.active_pay_rate,
  273. report.loan_completion,
  274. report.loan_completion_cost,
  275. report.loan_completion_rate,
  276. report.pre_loan_credit,
  277. report.pre_loan_credit_cost,
  278. report.loan_credit,
  279. report.loan_credit_cost,
  280. report.loan_credit_rate,
  281. report.in_app_uv,
  282. report.in_app_detail_uv,
  283. report.in_app_cart,
  284. report.in_app_pay,
  285. report.in_app_order,
  286. report.attribution_game_pay_7d_count,
  287. report.attribution_game_pay_7d_cost,
  288. report.attribution_active_pay_7d_per_count,
  289. report.game_pay_cost,
  290. report.game_pay_count,
  291. report.phone,
  292. report.form,
  293. report.map_search,
  294. report.button,
  295. report.`view`,
  296. report.download,
  297. report.qq,
  298. report.lottery,
  299. report.vote,
  300. report.message,
  301. report.redirect,
  302. report.shopping,
  303. report.consult,
  304. report.wechat,
  305. report.phone_confirm,
  306. report.phone_connect,
  307. report.consult_effective,
  308. report.coupon,
  309. report.coupon_single_page,
  310. report.redirect_to_shop,
  311. report.poi_collect,
  312. report.poi_address_click,
  313. report.luban_order_cnt,
  314. report.luban_order_stat_amount,
  315. report.luban_order_roi,
  316. report.luban_live_enter_cnt,
  317. report.live_watch_one_minute_count,
  318. report.luban_live_follow_cnt,
  319. report.live_fans_club_join_cnt,
  320. report.luban_live_comment_cnt,
  321. report.luban_live_share_cnt,
  322. report.luban_live_gift_cnt,
  323. report.luban_live_gift_amount,
  324. report.luban_live_slidecart_click_cnt,
  325. report.luban_live_click_product_cnt,
  326. report.luban_live_pay_order_count,
  327. report.luban_live_pay_order_stat_cost,
  328. report.wechat_login_count,
  329. report.attribution_wechat_login_30d_count,
  330. report.wechat_login_cost,
  331. report.attribution_wechat_login_30d_cost,
  332. report.wechat_first_pay_count,
  333. report.attribution_wechat_first_pay_30d_count,
  334. report.wechat_first_pay_cost,
  335. report.attribution_wechat_first_pay_30d_cost,
  336. report.wechat_first_pay_rate,
  337. report.attribution_wechat_first_pay_30d_rate,
  338. report.wechat_pay_amount,
  339. report.attribution_wechat_pay_30d_amount,
  340. report.attribution_wechat_pay_30d_roi,
  341. report.phone_effective,
  342. report.total_play,
  343. report.valid_play,
  344. report.valid_play_cost,
  345. report.valid_play_rate,
  346. report.play_25_feed_break,
  347. report.play_50_feed_break,
  348. report.play_75_feed_break,
  349. report.play_100_feed_break,
  350. report.average_play_time_per_play,
  351. report.play_over_rate,
  352. report.wifi_play_rate,
  353. report.wifi_play,
  354. report.play_duration_sum,
  355. report.advanced_creative_phone_click,
  356. report.advanced_creative_counsel_click,
  357. report.advanced_creative_form_click,
  358. report.advanced_creative_coupon_addition,
  359. report.advanced_creative_form_submit,
  360. report.card_show,
  361. report.`share`,
  362. report.`comment`,
  363. report.`like`,
  364. report.follow,
  365. report.home_visited,
  366. report.ies_challenge_click,
  367. report.ies_music_click,
  368. report.location_click,
  369. report.message_action,
  370. report.click_landing_page,
  371. report.click_shopwindow,
  372. report.click_website,
  373. report.click_download,
  374. report.click_call_dy,
  375. report.`cpc`,
  376. report.`cpm`,
  377. report.`cpa`,
  378. report.`interact_per_cost`,
  379. report.`convert_show_rate`,
  380. report.`play_duration_10s_rate`,
  381. report.`play_50_feed_break_rate`,
  382. report.`play_25_feed_break_rate`,
  383. report.`play_duration_5s_rate`,
  384. report.`play_duration_2s_rate`,
  385. report.`average_play_progress`,
  386. report.`play_100_feed_break_rate`,
  387. report.`play_duration_3s_rate`,
  388. report.`play_75_feed_break_rate`,
  389. report.`play_duration`,
  390. report.`play_duration_3s`,
  391. report.`play_duration_2s`,
  392. report.`play_duration_10s`,
  393. report.`average_video_play`,
  394. report.`avg_rank`,
  395. report.submit_certification_count,
  396. report.approval_count,
  397. report.first_rental_order_count,
  398. report.first_order_count,
  399. report.commute_first_pay_count
  400. FROM media_api.bytedance_material_report_daily report
  401. LEFT JOIN (
  402. SELECT advertiser_id,
  403. signature,
  404. material_id
  405. FROM `media_api`.bytedance_file_video_get
  406. GROUP BY advertiser_id,
  407. signature,
  408. material_id
  409. ) video ON report.advertiser_id = video.advertiser_id
  410. AND report.material_id = video.material_id
  411. LEFT JOIN (
  412. SELECT CODE,
  413. material_innovate,
  414. cover_url
  415. FROM `jeecg-boot`.ctop_material_info
  416. GROUP BY CODE,
  417. material_innovate,
  418. cover_url
  419. ) cmi ON video.signature = cmi.CODE
  420. LEFT JOIN (
  421. SELECT material_id,
  422. leader_id
  423. FROM (
  424. SELECT material_id,
  425. leader_id,
  426. row_number() over (
  427. PARTITION BY material_id
  428. ORDER BY
  429. create_time
  430. ) rn
  431. FROM `jeecg-boot`.ctop_material_ascription
  432. ) m
  433. WHERE rn = 1
  434. ) cma ON video.signature = cma.material_id
  435. LEFT JOIN `jeecg-boot`.user_company uc ON cma.leader_id = uc.user_id
  436. LEFT JOIN `jeecg-boot`.sys_user_depart de ON cma.leader_id = de.user_id
  437. LEFT JOIN `jeecg-boot`.sys_depart ur ON de.dep_id = ur.id
  438. LEFT JOIN `jeecg-boot`.sys_user us on us.id =cma.leader_id
  439. WHERE image_mode IN (
  440. 'CREATIVE_IMAGE_MODE_VIDEO',
  441. 'CREATIVE_IMAGE_MODE_VIDEO_VERTICAL',
  442. 'CREATIVE_IMAGE_MODE_UNION_SPLASH_VIDEO')
  443. AND stat_datetime >= date_format( date_sub({date_time}, INTERVAL {day_count} DAY),'%Y%m%d')) info
  444. GROUP BY company_id,
  445. company_name,
  446. leader_id,
  447. org_type,
  448. org_code,
  449. id,
  450. depart_name;
  451. """.format(date_time=self.date_time, day_count=self.day_count)
  452. print(sql)
  453. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  454. self.conn.commit()
  455. TidbConn.conn_close(conn=self.conn)
  456. return 0
  457. except Exception as e:
  458. logging.error(e)
  459. return -1
  460. if __name__ == '__main__':
  461. status = AppBytedanceCompanyDesignReportBusi().taskHandler()
  462. if status == 0:
  463. print("作业执行成功")
  464. exit(0)
  465. else:
  466. print("作业执行失败")
  467. exit(-1)