AdDailyReport.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/9/27 10:50 上午
  4. # @Site :
  5. # @File : AdDailyReport.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import logging
  10. from math import ceil
  11. from Apietl.utlis.Utils import Utils
  12. from Apietl.conn.TidbConn import TidbConn
  13. class AdDailyReport:
  14. def __init__(self):
  15. self.conn = TidbConn.get_connection()
  16. self.parm = Utils.get_args()
  17. self.start_time = self.parm[0]
  18. self.end_time = self.parm[1]
  19. def daily_report(self):
  20. try:
  21. sql = """select count(1) from media_api.bytedance_ad_report_daily where
  22. stat_datetime between {start_time} and {end_time}""".format(
  23. start_time=self.start_time, end_time=self.end_time)
  24. totalNum = TidbConn.quary(self.conn, sql)[0][0]
  25. pageCount = ceil(totalNum / 20000)
  26. for i in range(pageCount):
  27. sql = """
  28. replace into application.bytedance_ad_report_daily_dw(account_id, campaign_id, campaign_name, ad_id, ad_name,
  29. stat_datetime, project_id, product_id, advertiser_id, company_id,
  30. account_name, project_name, advertiser_name, company_name,
  31. sale_id, user_id, org_code, cost, `show`, avg_show_cost, click,
  32. avg_click_cost, ctr, `convert`, convert_cost, convert_rate,
  33. deep_convert, deep_convert_cost, deep_convert_rate,
  34. attribution_convert, attribution_convert_cost,
  35. attribution_deep_convert, attribution_deep_convert_cost,
  36. download_start, download_start_cost, download_start_rate,
  37. download_finish, download_finish_cost, download_finish_rate,
  38. click_install, install_finish, install_finish_cost,
  39. install_finish_rate, active, active_cost, active_rate, register,
  40. active_register_cost, active_register_rate, next_day_open,
  41. next_day_open_cost, next_day_open_rate,
  42. attribution_next_day_open_cnt, attribution_next_day_open_cost,
  43. attribution_next_day_open_rate, game_addiction,
  44. game_addiction_cost, game_addiction_rate, pay_count,
  45. active_pay_cost, active_pay_rate, loan_completion,
  46. loan_completion_cost, loan_completion_rate, pre_loan_credit,
  47. pre_loan_credit_cost, loan_credit, loan_credit_cost,
  48. loan_credit_rate, in_app_uv, in_app_detail_uv, in_app_cart,
  49. in_app_pay, in_app_order, attribution_game_pay_7d_count,
  50. attribution_game_pay_7d_cost, attribution_active_pay_7d_per_count,
  51. game_pay_cost, game_pay_count, phone, form, map_search, button,
  52. view, download, qq, lottery, vote, message, redirect, shopping,
  53. consult, wechat, phone_confirm, phone_connect, consult_effective,
  54. coupon, coupon_single_page, redirect_to_shop, poi_collect,
  55. poi_address_click, luban_order_cnt, luban_order_stat_amount,
  56. luban_order_roi, luban_live_enter_cnt,
  57. live_watch_one_minute_count, luban_live_follow_cnt,
  58. live_fans_club_join_cnt, luban_live_comment_cnt,
  59. luban_live_share_cnt, luban_live_gift_cnt, luban_live_gift_amount,
  60. luban_live_slidecart_click_cnt, luban_live_click_product_cnt,
  61. luban_live_pay_order_count, luban_live_pay_order_stat_cost,
  62. live_component_click_count, live_component_click_cost,
  63. live_component_click_rate, wechat_login_count,
  64. attribution_wechat_login_30d_count, wechat_login_cost,
  65. attribution_wechat_login_30d_cost, wechat_first_pay_count,
  66. attribution_wechat_first_pay_30d_count, wechat_first_pay_cost,
  67. attribution_wechat_first_pay_30d_cost, wechat_first_pay_rate,
  68. attribution_wechat_first_pay_30d_rate, wechat_pay_amount,
  69. attribution_wechat_pay_30d_amount, attribution_wechat_pay_30d_roi,
  70. phone_effective, total_play, valid_play, valid_play_cost,
  71. valid_play_rate, play_25_feed_break, play_50_feed_break,
  72. play_75_feed_break, play_100_feed_break,
  73. average_play_time_per_play, play_over_rate, wifi_play_rate,
  74. wifi_play, play_duration_sum, advanced_creative_phone_click,
  75. advanced_creative_counsel_click, advanced_creative_form_click,
  76. advanced_creative_coupon_addition, advanced_creative_form_submit,
  77. card_show, share, comment, `like`, follow, home_visited,
  78. ies_challenge_click, ies_music_click, location_click,
  79. message_action, click_landing_page, click_shopwindow,
  80. click_website, click_download, click_call_dy,
  81. submit_certification_count, approval_count,
  82. first_rental_order_count, first_order_count,
  83. commute_first_pay_count)
  84. select a.advertiser_id account_id,
  85. a.campaign_id,
  86. a.campaign_name,
  87. a.ad_id,
  88. a.ad_name,
  89. a.stat_datetime,
  90. b.project_id,
  91. b.product_id,
  92. b.advertiser_id,
  93. b.company_id,
  94. b.account_name,
  95. b.project_name,
  96. b.advertiser_name,
  97. b.company_name,
  98. b.sale_id,
  99. b.user_id,
  100. b.org_code,
  101. a.cost,
  102. a.show,
  103. a.avg_show_cost,
  104. a.click,
  105. a.avg_click_cost,
  106. a.ctr,
  107. a.convert,
  108. a.convert_cost,
  109. a.convert_rate,
  110. a.deep_convert,
  111. a.deep_convert_cost,
  112. a.deep_convert_rate,
  113. a.attribution_convert,
  114. a.attribution_convert_cost,
  115. a.attribution_deep_convert,
  116. a.attribution_deep_convert_cost,
  117. a.download_start,
  118. a.download_start_cost,
  119. a.download_start_rate,
  120. a.download_finish,
  121. a.download_finish_cost,
  122. a.download_finish_rate,
  123. a.click_install,
  124. a.install_finish,
  125. a.install_finish_cost,
  126. a.install_finish_rate,
  127. a.active,
  128. a.active_cost,
  129. a.active_rate,
  130. a.register,
  131. a.active_register_cost,
  132. a.active_register_rate,
  133. a.next_day_open,
  134. a.next_day_open_cost,
  135. a.next_day_open_rate,
  136. a.attribution_next_day_open_cnt,
  137. a.attribution_next_day_open_cost,
  138. a.attribution_next_day_open_rate,
  139. a.game_addiction,
  140. a.game_addiction_cost,
  141. a.game_addiction_rate,
  142. a.pay_count,
  143. a.active_pay_cost,
  144. a.active_pay_rate,
  145. a.loan_completion,
  146. a.loan_completion_cost,
  147. a.loan_completion_rate,
  148. a.pre_loan_credit,
  149. a.pre_loan_credit_cost,
  150. a.loan_credit,
  151. a.loan_credit_cost,
  152. a.loan_credit_rate,
  153. a.in_app_uv,
  154. a.in_app_detail_uv,
  155. a.in_app_cart,
  156. a.in_app_pay,
  157. a.in_app_order,
  158. a.attribution_game_pay_7d_count,
  159. a.attribution_game_pay_7d_cost,
  160. a.attribution_active_pay_7d_per_count,
  161. a.game_pay_cost,
  162. a.game_pay_count,
  163. a.phone,
  164. a.form,
  165. a.map_search,
  166. a.button,
  167. a.view,
  168. a.download,
  169. a.qq,
  170. a.lottery,
  171. a.vote,
  172. a.message,
  173. a.redirect,
  174. a.shopping,
  175. a.consult,
  176. a.wechat,
  177. a.phone_confirm,
  178. a.phone_connect,
  179. a.consult_effective,
  180. a.coupon,
  181. a.coupon_single_page,
  182. a.redirect_to_shop,
  183. a.poi_collect,
  184. a.poi_address_click,
  185. a.luban_order_cnt,
  186. a.luban_order_stat_amount,
  187. a.luban_order_roi,
  188. a.luban_live_enter_cnt,
  189. a.live_watch_one_minute_count,
  190. a.luban_live_follow_cnt,
  191. a.live_fans_club_join_cnt,
  192. a.luban_live_comment_cnt,
  193. a.luban_live_share_cnt,
  194. a.luban_live_gift_cnt,
  195. a.luban_live_gift_amount,
  196. a.luban_live_slidecart_click_cnt,
  197. a.luban_live_click_product_cnt,
  198. a.luban_live_pay_order_count,
  199. a.luban_live_pay_order_stat_cost,
  200. a.live_component_click_count,
  201. a.live_component_click_cost,
  202. a.live_component_click_rate,
  203. a.wechat_login_count,
  204. a.attribution_wechat_login_30d_count,
  205. a.wechat_login_cost,
  206. a.attribution_wechat_login_30d_cost,
  207. a.wechat_first_pay_count,
  208. a.attribution_wechat_first_pay_30d_count,
  209. a.wechat_first_pay_cost,
  210. a.attribution_wechat_first_pay_30d_cost,
  211. a.wechat_first_pay_rate,
  212. a.attribution_wechat_first_pay_30d_rate,
  213. a.wechat_pay_amount,
  214. a.attribution_wechat_pay_30d_amount,
  215. a.attribution_wechat_pay_30d_roi,
  216. a.phone_effective,
  217. a.total_play,
  218. a.valid_play,
  219. a.valid_play_cost,
  220. a.valid_play_rate,
  221. a.play_25_feed_break,
  222. a.play_50_feed_break,
  223. a.play_75_feed_break,
  224. a.play_100_feed_break,
  225. a.average_play_time_per_play,
  226. a.play_over_rate,
  227. a.wifi_play_rate,
  228. a.wifi_play,
  229. a.play_duration_sum,
  230. a.advanced_creative_phone_click,
  231. a.advanced_creative_counsel_click,
  232. a.advanced_creative_form_click,
  233. a.advanced_creative_coupon_addition,
  234. a.advanced_creative_form_submit,
  235. a.card_show,
  236. a.share,
  237. a.comment,
  238. a.like,
  239. a.follow,
  240. a.home_visited,
  241. a.ies_challenge_click,
  242. a.ies_music_click,
  243. a.location_click,
  244. a.message_action,
  245. a.click_landing_page,
  246. a.click_shopwindow,
  247. a.click_website,
  248. a.click_download,
  249. a.click_call_dy,
  250. a.submit_certification_count,
  251. a.approval_count,
  252. a.first_rental_order_count,
  253. a.first_order_count,
  254. a.commute_first_pay_count
  255. from `media_api`.bytedance_ad_report_daily a
  256. left join
  257. (select cua.account_id id,
  258. cua.account_id account_id,
  259. cp.id project_id,
  260. product.id product_id,
  261. ca.id advertiser_id,
  262. uc.company_id company_id,
  263. cua.auth_name account_name,
  264. cp.project_name project_name,
  265. ca.name advertiser_name,
  266. uc.company_name company_name,
  267. cp.sale_id sale_id,
  268. su.id user_id,
  269. su.org_code org_code
  270. from `jeecg-boot`.ctop_user_allocation cua
  271. left join `jeecg-boot`.ctop_project cp on cua.project_id = cp.id
  272. left join `jeecg-boot`.ctop_advertiser ca on ca.id = cua.advertiser_id
  273. left join `jeecg-boot`.sys_user su on su.id = cua.user_id
  274. left join `jeecg-boot`.user_company uc on uc.user_id = cua.user_id
  275. left join `jeecg-boot`.ctop_product product on product.id = cp.product_id
  276. where cua.account_id is not null) b
  277. on a.advertiser_id = b.account_id
  278. where a.stat_datetime between {start_time} and {end_time}
  279. order by a.advertiser_id, a.stat_datetime,a.ad_id
  280. limit {num},20000;
  281. """.format(start_time=self.start_time, end_time=self.end_time, num=i * 20000)
  282. print(sql)
  283. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  284. self.conn.commit()
  285. TidbConn.conn_close(conn=self.conn)
  286. return 0
  287. except Exception as e:
  288. logging.error(e)
  289. return -1
  290. if __name__ == '__main__':
  291. status = AdDailyReport().daily_report()
  292. if status == 0:
  293. print("作业执行成功")
  294. exit(0)
  295. else:
  296. print("作业执行失败")
  297. exit(-1)