CampaignDailyReport.py 14 KB

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