AppBytedanceCompanyUserReportBusiMonth.py 17 KB

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