AdvertiserHourlyReport.py 15 KB

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