AdHourlyReport.py 13 KB

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