AppBytedanceImageReportBusi.py 20 KB

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