AppBytedanceCompanyUserReportBusiMonth.py 17 KB

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