AccountDailyReport.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/9/27 10:50 上午
  4. # @Site :
  5. # @File : AccountDailyReport.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.kwai_etl.report.ReplaceAccountInfo import ReplaceAccountInfo
  12. from Apietl.utlis.Logger import Logger
  13. from Apietl.utlis.Utils import Utils
  14. from Apietl.conn.TidbConn import TidbConn
  15. class AccountDailyReport:
  16. def __init__(self):
  17. self.conn = TidbConn.get_connection()
  18. self.parm = Utils.get_args()
  19. self.start_time = self.parm[0]
  20. self.end_time = self.parm[1]
  21. def daily_report(self):
  22. try:
  23. sql = """
  24. replace into application.kuaishou_account_report_daily_dw
  25. (account_id, stat_date, project_id, product_id, advertiser_id, company_id, account_name, project_name, advertiser_name,
  26. company_name, sale_id, user_id, org_code, charge, `show`, photo_click, aclick, bclick, photo_click_ratio,
  27. play_3s_ratio, action_ratio, impression_1k_cost, photo_click_cost, action_cost, share, comment, `like`, follow,
  28. cancel_follow, report, block, negative, submit, download_started, download_completed, activation, event_pay_first_day,
  29. event_pay_purchase_amount_first_day, event_pay_first_day_roi, event_pay, event_pay_purchase_amount, event_pay_roi,
  30. event_register, event_register_cost, event_register_ratio, event_jin_jian_app, event_jin_jian_app_cost,
  31. event_credit_grant_app, event_credit_grant_app_cost, event_credit_grant_app_ratio, event_order_paid,
  32. event_order_paid_purchase_amount, event_order_paid_cost, event_next_day_stay, event_next_day_stay_cost,
  33. event_next_day_stay_ratio, form_count, form_cost, form_action_ratio, event_jin_jian_landing_page,
  34. event_jin_jian_landing_page_cost, event_credit_grant_landing_page, event_credit_grant_landing_page_cost,
  35. event_credit_grant_landing_page_ratio, event_valid_clues, event_valid_clues_cost, event_add_wechat,
  36. event_add_wechat_cost, event_add_wechat_ratio, event_get_through, event_get_through_cost, event_get_through_ratio,
  37. event_app_invoked, event_app_invoked_cost, event_app_invoked_ratio, event_credit_grant_landing_ratio, play_5s_ratio,
  38. play_end_ratio, event_new_user_pay, event_new_user_pay_cost, event_new_user_pay_ratio, click_1k_cost, ad_product_cnt,
  39. event_goods_view, merchant_reco_fans, event_order_amount_roi, event_goods_view_cost, merchant_reco_fans_cost,
  40. event_button_click, event_button_click_cost, event_button_click_ratio, event_order_paid_roi,
  41. event_new_user_jinjian_app, event_new_user_jinjian_app_cost, event_new_user_jinjian_app_roi,
  42. event_new_user_credit_grant_app, event_new_user_credit_grant_app_cost, event_new_user_credit_grant_app_roi,
  43. event_new_user_jinjian_page, event_new_user_jinjian_page_cost, event_new_user_jinjian_page_roi,
  44. event_new_user_credit_grant_page, event_new_user_credit_grant_page_cost, event_new_user_credit_grant_page_roi,
  45. event_appoint_form, event_appoint_form_cost, event_appoint_form_ratio, event_appoint_jump_click,
  46. event_appoint_jump_click_cost, event_appoint_jump_click_ratio, union_event_pay_purchase_amount_7d,
  47. union_event_pay_purchase_amount_7d_roi, placement_type, ad_scene, event_order_successed, ad_photo_played_10s_ratio,
  48. key_action_cost, event_add_shopping_cart_cost, event_ad_watch_times_ratio, event_outbound_call_cost,
  49. event_outbound_call, action_new_ratio, event_watch_app_ad, event_multi_conversion_cost, event_phone_card_activate,
  50. ad_photo_played_75percent_ratio, key_action_ratio, event_ad_watch_times_cost, event_add_shopping_cart, key_action,
  51. event_multi_conversion, event_wechat_connected, event_dsp_gift_form, event_intention_confirmed,
  52. event_phone_get_through, event_multi_conversion_ratio, event_measurement_house, ad_photo_played_2s_ratio,
  53. event_outbound_call_ratio, event_ad_watch_times,event_pay_purchase_amount_one_day_by_conversion,
  54. ad_photo_played_10s,
  55. ad_photo_played_2s,
  56. ad_photo_played_75percent,
  57. approx_pay_cost,
  58. approx_pay_count,
  59. approx_pay_ratio,cancel_like,
  60. click_conversion_ratio,
  61. conversion_cost,conversion_cost_by_impression_7d,conversion_num,conversion_num_by_impression_7d,conversion_num_cost,conversion_ratio,conversion_ratio_by_impression_7d,deep_conversion_cost,deep_conversion_cost_by_impression_7d,deep_conversion_num,deep_conversion_num_by_impression_7d,deep_conversion_ratio,deep_conversion_ratio_by_impression_7d,download_completed_cost,download_completed_ratio,download_conversion_ratio,download_installed,download_started_cost,download_started_ratio,event_24h_stay,event_24h_stay_by_conversion,event_24h_stay_by_conversion_cost,event_24h_stay_by_conversion_ratio,event_24h_stay_cost,event_24h_stay_ratio,event_ad_watch_10_times,event_ad_watch_10_times_cost,event_ad_watch_10_times_ratio,event_ad_watch_20_times,event_ad_watch_20_times_cost,event_ad_watch_20_times_ratio,event_ad_watch_5_times,event_ad_watch_5_times_cost,event_ad_watch_5_times_ratio,event_audition,event_consultation_valid_retained,event_consultation_valid_retained_cost,event_consultation_valid_retained_ratio,event_conversion_click_cost,event_conversion_click_ratio,event_credit_grant_first_day_app,event_credit_grant_first_day_app_cost,event_credit_grant_first_day_app_ratio,event_credit_grant_first_day_landing_page,event_credit_grant_first_day_landing_page_cost,event_credit_grant_first_day_landing_page_ratio,event_making_calls,event_making_calls_cost,event_making_calls_ratio,event_order_submit,event_pay_purchase_amount_one_day,event_pay_purchase_amount_one_day_by_conversion_roi,event_pay_purchase_amount_one_day_roi,event_pay_purchase_amount_three_day_by_conversion,event_pay_purchase_amount_three_day_by_conversion_roi,
  62. event_pay_purchase_amount_week_by_conversion,event_pay_purchase_amount_week_by_conversion_roi,
  63. event_pay_week_by_conversion,event_pay_week_by_conversion_cost,event_pay_weighted_purchase_amount,
  64. event_pay_weighted_purchase_amount_first_day,event_pre_component_consultation_valid_retained,
  65. event_three_day_stay_by_conversion,event_three_day_stay_by_conversion_cost,
  66. event_three_day_stay_by_conversion_ratio,event_wechat_qr_code_link_click,event_week_stay_by_conversion,
  67. event_week_stay_by_conversion_cost,event_week_stay_by_conversion_ratio,live_event_goods_view,
  68. live_played_3s,played_end,played_five_seconds,played_three_seconds,event_next_day_stay_new,
  69. event_next_day_stay_new_cost,event_next_day_stay_new_ratio,ad_show,event_credit_card_recheck,
  70. event_credit_card_recheck_first_day,event_no_intention,event_multi_pay_seven_day_by_conversion,
  71. event_multi_pay_seven_day_by_conversion_cost,mcb_left_time_perf_cpa_bid,mcb_left_time_perf_roi_ratio,
  72. mcb_all_day_perf_cpa_bid,mcb_all_day_perf_roi_ratio,live_room_avg_played_seconds,ad_live_share,
  73. ad_live_comment,live_played_started,live_played_started_cost,ad_live_follow,ad_live_follow_cost,
  74. simple_live_played_started,standard_live_played_started,conversion_component_impression,
  75. conversion_component_click,conversion_component_rate,
  76. ad_landing_page_impression,ad_app_download_half_impression,event_draw_credit_line,
  77. minigame_iaa_purchase_roi,
  78. minigame_iaa_purchase_amount
  79. )
  80. select report.advertiser_id as account_id,
  81. stat_date,
  82. project_id,
  83. product_id,
  84. sys.advertiser_id,
  85. company_id,
  86. account_name,
  87. project_name,
  88. advertiser_name,
  89. company_name,
  90. sale_id,
  91. user_id,
  92. org_code,
  93. charge,
  94. `show`,
  95. photo_click,
  96. aclick,
  97. bclick,
  98. photo_click_ratio,
  99. play_3s_ratio,
  100. action_ratio,
  101. impression_1k_cost,
  102. photo_click_cost,
  103. action_cost,
  104. share,
  105. comment,
  106. `like`,
  107. follow,
  108. cancel_follow,
  109. report,
  110. block,
  111. negative,
  112. submit,
  113. download_started,
  114. download_completed,
  115. activation,
  116. event_pay_first_day,
  117. event_pay_purchase_amount_first_day,
  118. event_pay_first_day_roi,
  119. event_pay,
  120. event_pay_purchase_amount,
  121. event_pay_roi,
  122. event_register,
  123. event_register_cost,
  124. event_register_ratio,
  125. event_jin_jian_app,
  126. event_jin_jian_app_cost,
  127. event_credit_grant_app,
  128. event_credit_grant_app_cost,
  129. event_credit_grant_app_ratio,
  130. event_order_paid,
  131. event_order_paid_purchase_amount,
  132. event_order_paid_cost,
  133. event_next_day_stay,
  134. event_next_day_stay_cost,
  135. event_next_day_stay_ratio,
  136. form_count,
  137. form_cost,
  138. form_action_ratio,
  139. event_jin_jian_landing_page,
  140. event_jin_jian_landing_page_cost,
  141. event_credit_grant_landing_page,
  142. event_credit_grant_landing_page_cost,
  143. event_credit_grant_landing_page_ratio,
  144. event_valid_clues,
  145. event_valid_clues_cost,
  146. event_add_wechat,
  147. event_add_wechat_cost,
  148. event_add_wechat_ratio,
  149. event_get_through,
  150. event_get_through_cost,
  151. event_get_through_ratio,
  152. event_app_invoked,
  153. event_app_invoked_cost,
  154. event_app_invoked_ratio,
  155. event_credit_grant_landing_ratio,
  156. play_5s_ratio,
  157. play_end_ratio,
  158. event_new_user_pay,
  159. event_new_user_pay_cost,
  160. event_new_user_pay_ratio,
  161. click_1k_cost,
  162. ad_product_cnt,
  163. event_goods_view,
  164. merchant_reco_fans,
  165. event_order_amount_roi,
  166. event_goods_view_cost,
  167. merchant_reco_fans_cost,
  168. event_button_click,
  169. event_button_click_cost,
  170. event_button_click_ratio,
  171. event_order_paid_roi,
  172. event_new_user_jinjian_app,
  173. event_new_user_jinjian_app_cost,
  174. event_new_user_jinjian_app_roi,
  175. event_new_user_credit_grant_app,
  176. event_new_user_credit_grant_app_cost,
  177. event_new_user_credit_grant_app_roi,
  178. event_new_user_jinjian_page,
  179. event_new_user_jinjian_page_cost,
  180. event_new_user_jinjian_page_roi,
  181. event_new_user_credit_grant_page,
  182. event_new_user_credit_grant_page_cost,
  183. event_new_user_credit_grant_page_roi,
  184. event_appoint_form,
  185. event_appoint_form_cost,
  186. event_appoint_form_ratio,
  187. event_appoint_jump_click,
  188. event_appoint_jump_click_cost,
  189. event_appoint_jump_click_ratio,
  190. union_event_pay_purchase_amount_7d,
  191. union_event_pay_purchase_amount_7d_roi,
  192. placement_type,
  193. ad_scene,
  194. event_order_successed,
  195. ad_photo_played_10s_ratio,
  196. key_action_cost,
  197. event_add_shopping_cart_cost,
  198. event_ad_watch_times_ratio,
  199. event_outbound_call_cost,
  200. event_outbound_call,
  201. action_new_ratio,
  202. event_watch_app_ad,
  203. event_multi_conversion_cost,
  204. event_phone_card_activate,
  205. ad_photo_played_75percent_ratio,
  206. key_action_ratio,
  207. event_ad_watch_times_cost,
  208. event_add_shopping_cart,
  209. key_action,
  210. event_multi_conversion,
  211. event_wechat_connected,
  212. event_dsp_gift_form,
  213. event_intention_confirmed,
  214. event_phone_get_through,
  215. event_multi_conversion_ratio,
  216. event_measurement_house,
  217. ad_photo_played_2s_ratio,
  218. event_outbound_call_ratio,
  219. event_ad_watch_times,
  220. event_pay_purchase_amount_one_day_by_conversion,
  221. ad_photo_played_10s
  222. ,ad_photo_played_2s
  223. ,ad_photo_played_75percent
  224. ,approx_pay_cost
  225. ,approx_pay_count
  226. ,approx_pay_ratio
  227. ,cancel_like
  228. ,click_conversion_ratio
  229. ,conversion_cost
  230. ,conversion_cost_by_impression_7d
  231. ,conversion_num
  232. ,conversion_num_by_impression_7d
  233. ,conversion_num_cost
  234. ,conversion_ratio
  235. ,conversion_ratio_by_impression_7d
  236. ,deep_conversion_cost
  237. ,deep_conversion_cost_by_impression_7d
  238. ,deep_conversion_num
  239. ,deep_conversion_num_by_impression_7d
  240. ,deep_conversion_ratio
  241. ,deep_conversion_ratio_by_impression_7d
  242. ,download_completed_cost
  243. ,download_completed_ratio
  244. ,download_conversion_ratio
  245. ,download_installed
  246. ,download_started_cost
  247. ,download_started_ratio
  248. ,event_24h_stay
  249. ,event_24h_stay_by_conversion
  250. ,event_24h_stay_by_conversion_cost
  251. ,event_24h_stay_by_conversion_ratio
  252. ,event_24h_stay_cost
  253. ,event_24h_stay_ratio
  254. ,event_ad_watch_10_times
  255. ,event_ad_watch_10_times_cost
  256. ,event_ad_watch_10_times_ratio
  257. ,event_ad_watch_20_times
  258. ,event_ad_watch_20_times_cost
  259. ,event_ad_watch_20_times_ratio
  260. ,event_ad_watch_5_times
  261. ,event_ad_watch_5_times_cost
  262. ,event_ad_watch_5_times_ratio
  263. ,event_audition
  264. ,event_consultation_valid_retained
  265. ,event_consultation_valid_retained_cost
  266. ,event_consultation_valid_retained_ratio
  267. ,event_conversion_click_cost
  268. ,event_conversion_click_ratio
  269. ,event_credit_grant_first_day_app
  270. ,event_credit_grant_first_day_app_cost
  271. ,event_credit_grant_first_day_app_ratio
  272. ,event_credit_grant_first_day_landing_page
  273. ,event_credit_grant_first_day_landing_page_cost
  274. ,event_credit_grant_first_day_landing_page_ratio
  275. ,event_making_calls
  276. ,event_making_calls_cost
  277. ,event_making_calls_ratio
  278. ,event_order_submit
  279. ,event_pay_purchase_amount_one_day
  280. ,event_pay_purchase_amount_one_day_by_conversion_roi
  281. ,event_pay_purchase_amount_one_day_roi
  282. ,event_pay_purchase_amount_three_day_by_conversion
  283. ,event_pay_purchase_amount_three_day_by_conversion_roi
  284. ,event_pay_purchase_amount_week_by_conversion
  285. ,event_pay_purchase_amount_week_by_conversion_roi
  286. ,event_pay_week_by_conversion
  287. ,event_pay_week_by_conversion_cost
  288. ,event_pay_weighted_purchase_amount
  289. ,event_pay_weighted_purchase_amount_first_day
  290. ,event_pre_component_consultation_valid_retained
  291. ,event_three_day_stay_by_conversion
  292. ,event_three_day_stay_by_conversion_cost
  293. ,event_three_day_stay_by_conversion_ratio
  294. ,event_wechat_qr_code_link_click
  295. ,event_week_stay_by_conversion
  296. ,event_week_stay_by_conversion_cost
  297. ,event_week_stay_by_conversion_ratio
  298. ,live_event_goods_view
  299. ,live_played_3s
  300. ,played_end
  301. ,played_five_seconds
  302. ,played_three_seconds
  303. ,event_next_day_stay_new
  304. ,event_next_day_stay_new_cost
  305. ,event_next_day_stay_new_ratio
  306. ,ad_show
  307. ,event_credit_card_recheck
  308. ,event_credit_card_recheck_first_day
  309. ,event_no_intention
  310. ,event_multi_pay_seven_day_by_conversion
  311. ,event_multi_pay_seven_day_by_conversion_cost
  312. ,mcb_left_time_perf_cpa_bid
  313. ,mcb_left_time_perf_roi_ratio
  314. ,mcb_all_day_perf_cpa_bid
  315. ,mcb_all_day_perf_roi_ratio
  316. ,live_room_avg_played_seconds
  317. ,ad_live_share
  318. ,ad_live_comment
  319. ,live_played_started
  320. ,live_played_started_cost
  321. ,ad_live_follow
  322. ,ad_live_follow_cost
  323. ,simple_live_played_started
  324. ,standard_live_played_started
  325. ,conversion_component_impression
  326. ,conversion_component_click
  327. ,conversion_component_rate
  328. ,ad_landing_page_impression
  329. ,ad_app_download_half_impression
  330. ,event_draw_credit_line
  331. ,minigame_iaa_purchase_roi
  332. ,minigame_iaa_purchase_amount
  333. from media_api.kuaishou_account_report_daily report
  334. left join
  335. (SELECT
  336. cua.account_id id,
  337. cua.account_id account_id,
  338. cp.id project_id,
  339. product.id product_id,
  340. ca.id advertiser_id,
  341. uc.company_id company_id,
  342. cua.auth_name account_name,
  343. cp.project_name project_name,
  344. ca.`name` advertiser_name,
  345. uc.company_name company_name,
  346. cp.sale_id sale_id,
  347. su.id user_id,
  348. su.leader_id,
  349. su.org_code org_code
  350. FROM
  351. `jeecg-boot`.ctop_user_allocation cua
  352. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  353. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  354. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  355. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  356. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  357. WHERE
  358. cua.account_id IS NOT NULL
  359. ) sys
  360. on report.advertiser_id = sys.account_id
  361. where report.stat_date between {start_time} and {end_time}
  362. """.format(start_time=self.start_time, end_time=self.end_time)
  363. print(sql)
  364. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  365. self.conn.commit()
  366. TidbConn.conn_close(conn=self.conn)
  367. return 0
  368. except Exception as e:
  369. logging.error(e)
  370. return -1
  371. if __name__ == '__main__':
  372. status = AccountDailyReport().daily_report()
  373. if status == 0:
  374. print("作业执行成功")
  375. exit(0)
  376. else:
  377. print("作业执行失败")
  378. exit(-1)