AppBytedanceCompanyUserReportBusi.py 17 KB

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