AppBytedanceProjectReportBusi.py 18 KB

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