AppBytedanceAdvertiserReportBusiMonth.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/11/10 3:37 下午
  4. # @Site :
  5. # @File : AppKuaishouVideoReportBusiMonth.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import logging
  10. from Apietl.conn.TidbConn import TidbConn
  11. from Apietl.utlis.Utils import Utils
  12. class AppKuaishouAccountReportBusiMonth:
  13. def __init__(self):
  14. self.conn = TidbConn.get_connection()
  15. self.day_count = 30
  16. def taskHandler(self):
  17. try:
  18. sql = """
  19. SET sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  20. REPLACE INTO application.app_kuaishou_account_report_busi_m
  21. SELECT
  22. date_format(20211101,'%Y%m') stat_mon,
  23. report.account_id,
  24. bei.project_id,
  25. bei.product_id,
  26. bei.advertiser_id,
  27. bei.company_id,
  28. bei.account_name,
  29. bei.project_name,
  30. bei.advertiser_name,
  31. bei.company_name,
  32. bei.sale_id,
  33. bei.user_id,
  34. bei.leader_id,
  35. bei.org_code,
  36. report.charge,
  37. report.`show`,
  38. report.photo_click,
  39. report.aclick,
  40. report.bclick,
  41. report.photo_click_ratio,
  42. report.play_3s_ratio,
  43. report.action_ratio,
  44. report.impression_1k_cost,
  45. report.photo_click_cost,
  46. report.action_cost,
  47. report.`share`,
  48. report.`comment`,
  49. report.`like`,
  50. report.follow,
  51. report.cancel_follow,
  52. report.report,
  53. report.block,
  54. report.negative,
  55. report.submit,
  56. report.download_started,
  57. report.download_completed,
  58. report.activation,
  59. report.event_pay_first_day,
  60. report.event_pay_purchase_amount_first_day,
  61. report.event_pay_first_day_roi,
  62. report.event_pay,
  63. report.event_pay_purchase_amount,
  64. report.event_pay_roi,
  65. report.event_register,
  66. report.event_register_cost,
  67. report.event_register_ratio,
  68. report.event_jin_jian_app,
  69. report.event_jin_jian_app_cost,
  70. report.event_credit_grant_app,
  71. report.event_credit_grant_app_cost,
  72. report.event_credit_grant_app_ratio,
  73. report.event_order_paid,
  74. report.event_order_paid_purchase_amount,
  75. report.event_order_paid_cost,
  76. report.event_next_day_stay,
  77. report.event_next_day_stay_cost,
  78. report.event_next_day_stay_ratio,
  79. report.form_count,
  80. report.form_cost,
  81. report.form_action_ratio,
  82. report.event_jin_jian_landing_page,
  83. report.event_jin_jian_landing_page_cost,
  84. report.event_credit_grant_landing_page,
  85. report.event_credit_grant_landing_page_cost,
  86. report.event_credit_grant_landing_page_ratio,
  87. report.event_valid_clues,
  88. report.event_valid_clues_cost,
  89. report.event_add_wechat,
  90. report.event_add_wechat_cost,
  91. report.event_add_wechat_ratio,
  92. report.event_get_through,
  93. report.event_get_through_cost,
  94. report.event_get_through_ratio,
  95. report.event_app_invoked,
  96. report.event_app_invoked_cost,
  97. report.event_app_invoked_ratio,
  98. report.event_credit_grant_landing_ratio,
  99. report.play_5s_ratio,
  100. report.play_end_ratio,
  101. report.event_new_user_pay,
  102. report.event_new_user_pay_cost,
  103. report.event_new_user_pay_ratio,
  104. report.click_1k_cost,
  105. report.ad_product_cnt,
  106. report.event_goods_view,
  107. report.merchant_reco_fans,
  108. report.event_order_amount_roi,
  109. report.event_goods_view_cost,
  110. report.merchant_reco_fans_cost,
  111. report.event_button_click,
  112. report.event_button_click_cost,
  113. report.event_button_click_ratio,
  114. report.event_order_paid_roi,
  115. report.event_new_user_jinjian_app,
  116. report.event_new_user_jinjian_app_cost,
  117. report.event_new_user_jinjian_app_roi,
  118. report.event_new_user_credit_grant_app,
  119. report.event_new_user_credit_grant_app_cost,
  120. report.event_new_user_credit_grant_app_roi,
  121. report.event_new_user_jinjian_page,
  122. report.event_new_user_jinjian_page_cost,
  123. report.event_new_user_jinjian_page_roi,
  124. report.event_new_user_credit_grant_page,
  125. report.event_new_user_credit_grant_page_cost,
  126. report.event_new_user_credit_grant_page_roi,
  127. report.event_appoint_form,
  128. report.event_appoint_form_cost,
  129. report.event_appoint_form_ratio,
  130. report.event_appoint_jump_click,
  131. report.event_appoint_jump_click_cost,
  132. report.event_appoint_jump_click_ratio,
  133. report.union_event_pay_purchase_amount_7d,
  134. report.union_event_pay_purchase_amount_7d_roi,
  135. report.event_order_successed,
  136. report.ad_photo_played_10s_ratio,
  137. report.key_action_cost,
  138. report.event_add_shopping_cart_cost,
  139. report.event_ad_watch_times_ratio,
  140. report.event_outbound_call_cost,
  141. report.event_outbound_call,
  142. report.action_new_ratio,
  143. report.event_watch_app_ad,
  144. report.event_multi_conversion_cost,
  145. report.event_phone_card_activate,
  146. report.ad_photo_played_75percent_ratio,
  147. report.key_action_ratio,
  148. report.event_ad_watch_times_cost,
  149. report.event_add_shopping_cart,
  150. report.key_action,
  151. report.event_multi_conversion,
  152. report.event_wechat_connected,
  153. report.event_dsp_gift_form,
  154. report.event_intention_confirmed,
  155. report.event_phone_get_through,
  156. report.event_multi_conversion_ratio,
  157. report.event_measurement_house,
  158. report.ad_photo_played_2s_ratio,
  159. report.event_outbound_call_ratio,
  160. report.event_ad_watch_times
  161. from
  162. (select
  163. advertiser_id account_id,
  164. sum(charge) charge,
  165. sum(`show`) `show`,
  166. sum(photo_click)photo_click,
  167. sum(aclick) aclick,
  168. sum(bclick) bclick,
  169. ifnull(round(sum(photo_click)/sum(`show`),3),0) photo_click_ratio,
  170. ifnull(round(sum(play_3s_ratio)/{day_count}*100,3),0) play_3s_ratio,
  171. ifnull(round(sum(bclick)/sum(aclick)*100,3),0) action_ratio,
  172. ifnull(round(sum(impression_1k_cost)/{day_count},3),0) impression_1k_cost,
  173. ifnull(round(sum(charge)/sum(photo_click),3),0) photo_click_cost,
  174. ifnull(round(sum(charge)/sum(bclick),3),0) action_cost,
  175. sum(`share`) `share`,
  176. sum(`comment`) `comment`,
  177. sum(`like`) `like`,
  178. sum(follow) follow,
  179. sum(cancel_follow) cancel_follow,
  180. sum(report) report,
  181. sum(block) block,
  182. sum(negative) negative,
  183. sum(submit) submit,
  184. sum(download_started) download_started,
  185. sum(download_completed) download_completed,
  186. sum(activation) activation,
  187. sum(event_pay_first_day) event_pay_first_day,
  188. sum(event_pay_purchase_amount_first_day) event_pay_purchase_amount_first_day,
  189. ifnull(round(sum(event_pay_purchase_amount_first_day)/sum(charge)*100,3),0) event_pay_first_day_roi ,
  190. sum(event_pay) event_pay,
  191. sum(event_pay_purchase_amount) event_pay_purchase_amount,
  192. ifnull(round(sum(event_pay_purchase_amount)/sum(charge)*100,3),0) event_pay_roi,
  193. sum(event_register) event_register,
  194. ifnull(round(sum(charge)/sum(event_register),3),0) event_register_cost,
  195. ifnull(round(sum(event_register)/sum(activation)*100,3),0) event_register_ratio,
  196. sum(event_jin_jian_app) event_jin_jian_app,
  197. ifnull(round(sum(charge)/sum(event_jin_jian_app),3),0) event_jin_jian_app_cost,
  198. sum(event_credit_grant_app) event_credit_grant_app,
  199. ifnull(round(sum(charge)/sum(event_credit_grant_app),3),0) event_credit_grant_app_cost ,
  200. ifnull(round(sum(event_credit_grant_app_ratio)/{day_count}*100,3),0) event_credit_grant_app_ratio,
  201. sum(event_order_paid) event_order_paid,
  202. sum(event_order_paid_purchase_amount) event_order_paid_purchase_amount,
  203. ifnull(round(sum(event_order_paid_purchase_amount)/sum(event_order_paid) ,3),0) event_order_paid_cost,
  204. sum(event_next_day_stay) event_next_day_stay,
  205. ifnull(round(sum(charge)/sum(event_next_day_stay),3),0) event_next_day_stay_cost,
  206. ifnull(round(sum(event_next_day_stay)/sum(activation)*100,3),0) event_next_day_stay_ratio,
  207. sum(form_count) form_count,
  208. ifnull(round(sum(charge)/sum(form_count),3),0) form_cost,
  209. ifnull(round(sum(form_count)/sum(`bclick`)*100,3),0) form_action_ratio,
  210. sum(event_jin_jian_landing_page) event_jin_jian_landing_page,
  211. ifnull(round(sum(charge)/sum(event_jin_jian_landing_page),3),0) event_jin_jian_landing_page_cost,
  212. sum(event_credit_grant_landing_page) event_credit_grant_landing_page,
  213. ifnull(round(sum(charge)/sum(event_credit_grant_landing_page),3),0) event_credit_grant_landing_page_cost,
  214. ifnull(round(sum(event_credit_grant_landing_page)/{day_count}*100,3),0) event_credit_grant_landing_page_ratio,
  215. sum(event_valid_clues) event_valid_clues,
  216. ifnull(round(sum(charge)/sum(event_valid_clues),3),0) event_valid_clues_cost,
  217. sum(event_add_wechat) event_add_wechat,
  218. ifnull(round(sum(charge)/sum(event_add_wechat),3),0) event_add_wechat_cost,
  219. ifnull(round(sum(event_add_wechat)/sum(bclick)*100,3),0) event_add_wechat_ratio,
  220. sum(event_get_through) event_get_through,
  221. ifnull(round(sum(charge)/sum(event_get_through),3),0) event_get_through_cost,
  222. ifnull(round(sum(event_get_through)/sum(bclick)*100,3),0) event_get_through_ratio,
  223. sum(event_app_invoked) event_app_invoked,
  224. ifnull(round(sum(charge)/sum(event_app_invoked),3),0) event_app_invoked_cost ,
  225. ifnull(round(sum(event_app_invoked)/sum(bclick)*100,3),0) event_app_invoked_ratio,
  226. ifnull(round(sum(event_credit_grant_landing_ratio)/{day_count}*100,3),0) event_credit_grant_landing_ratio,
  227. ifnull(round(sum(play_5s_ratio)/{day_count}*100,3),0) play_5s_ratio,
  228. ifnull(round(sum(play_end_ratio)/{day_count}*100,3),0) play_end_ratio,
  229. sum(event_new_user_pay) event_new_user_pay,
  230. ifnull(round(sum(charge)/sum(event_new_user_pay),3),0) event_new_user_pay_cost,
  231. ifnull(round(sum(event_new_user_pay)/sum(activation)*100,3),0) event_new_user_pay_ratio,
  232. ifnull(round(sum(click_1k_cost)/{day_count}*100,3),0) click_1k_cost,
  233. sum(ad_product_cnt) ad_product_cnt,
  234. sum(event_goods_view) event_goods_view,
  235. sum(merchant_reco_fans) merchant_reco_fans,
  236. ifnull(round(sum(event_order_amount_roi)/{day_count}*100,3),0) event_order_amount_roi,
  237. ifnull(round(sum(charge)/sum(event_goods_view) ,3),0) event_goods_view_cost,
  238. ifnull(round(sum(charge)/sum(merchant_reco_fans),3),0) merchant_reco_fans_cost,
  239. sum(event_button_click) event_button_click,
  240. ifnull(round(sum(charge)/sum(event_button_click),3),0) event_button_click_cost,
  241. ifnull(round(sum(event_button_click)/sum(bclick)*100,3),0) event_button_click_ratio ,
  242. ifnull(round(sum(event_order_paid_roi)/{day_count}*100,3),0) event_order_paid_roi,
  243. sum(event_new_user_jinjian_app) event_new_user_jinjian_app,
  244. ifnull(round(sum(charge)/sum(event_new_user_jinjian_app),3),0) event_new_user_jinjian_app_cost,
  245. ifnull(round(sum(event_new_user_jinjian_app)/sum(form_count)*100,3),0) event_new_user_jinjian_app_roi ,
  246. sum(event_new_user_credit_grant_app) event_new_user_credit_grant_app,
  247. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_app),3),0) event_new_user_credit_grant_app_cost,
  248. ifnull(round(sum(event_new_user_credit_grant_app)/sum(form_count)*100,3),0) event_new_user_credit_grant_app_roi,
  249. sum(event_new_user_jinjian_page) event_new_user_jinjian_page,
  250. ifnull(round(sum(charge)/sum(event_new_user_jinjian_page),3),0) event_new_user_jinjian_page_cost,
  251. ifnull(round(sum(event_new_user_jinjian_page)/sum(form_count)*100,3),0) event_new_user_jinjian_page_roi ,
  252. sum(event_new_user_credit_grant_page) event_new_user_credit_grant_page,
  253. ifnull(round(sum(charge)/sum(event_new_user_credit_grant_page) ,3),0) event_new_user_credit_grant_page_cost,
  254. ifnull(round(sum(event_new_user_credit_grant_page)/sum(form_count)*100,3),0) event_new_user_credit_grant_page_roi,
  255. sum(event_appoint_form) event_appoint_form,
  256. ifnull(round(sum(charge)/sum(event_appoint_form),3),0) event_appoint_form_cost,
  257. ifnull(round(sum(event_appoint_form)/sum(form_count)*100,3),0) event_appoint_form_ratio,
  258. sum(event_appoint_jump_click) event_appoint_jump_click,
  259. ifnull(round(sum(charge)/sum(event_appoint_jump_click),3),0) event_appoint_jump_click_cost,
  260. ifnull(round(sum(event_appoint_jump_click_cost)/sum(form_count)*100,3),0) event_appoint_jump_click_ratio,
  261. sum(union_event_pay_purchase_amount_7d) union_event_pay_purchase_amount_7d,
  262. ifnull(round(sum(union_event_pay_purchase_amount_7d_roi)/{day_count}*100,3),0) union_event_pay_purchase_amount_7d_roi,
  263. sum(event_order_successed) event_order_successed,
  264. ifnull(round(sum(ad_photo_played_10s_ratio)/{day_count}*100,3),0) ad_photo_played_10s_ratio,
  265. ifnull(round(sum(charge)/sum(key_action),3),0) key_action_cost,
  266. ifnull(round(sum(charge)/sum(event_add_shopping_cart) ,3),0) event_add_shopping_cart_cost ,
  267. ifnull(round(sum(event_ad_watch_times)/sum(bclick)*100,3),0) event_ad_watch_times_ratio,
  268. ifnull(round(sum(charge)/sum(event_outbound_call),3),0) event_outbound_call_cost,
  269. sum(event_outbound_call) event_outbound_call,
  270. ifnull(round(sum(action_new_ratio)/{day_count}*100,3),0) action_new_ratio,
  271. sum(event_watch_app_ad) event_watch_app_ad,
  272. ifnull(round(sum(charge)/sum(event_multi_conversion) ,3),0) event_multi_conversion_cost,
  273. sum(event_phone_card_activate) event_phone_card_activate,
  274. ifnull(round(sum(ad_photo_played_75percent_ratio)/{day_count}*100,3),0) ad_photo_played_75percent_ratio,
  275. ifnull(round(sum(key_action)/sum(activation)*100,3),0) key_action_ratio ,
  276. ifnull(round(sum(charge)/sum(event_ad_watch_times),3),0) event_ad_watch_times_cost,
  277. sum(event_add_shopping_cart) event_add_shopping_cart,
  278. sum(key_action) key_action,
  279. sum(event_multi_conversion) event_multi_conversion,
  280. sum(event_wechat_connected) event_wechat_connected,
  281. sum(event_dsp_gift_form) event_dsp_gift_form,
  282. sum(event_intention_confirmed) event_intention_confirmed,
  283. sum(event_phone_get_through) event_phone_get_through,
  284. ifnull(round(sum(event_multi_conversion)/sum(activation)*100,3),0) event_multi_conversion_ratio,
  285. sum(event_measurement_house) event_measurement_house,
  286. ifnull(round(sum(ad_photo_played_2s_ratio)/{day_count}*100,3),0) ad_photo_played_2s_ratio,
  287. ifnull(round(sum(event_outbound_call_ratio)/sum(activation)*100,3),0) event_outbound_call_ratio,
  288. sum(event_ad_watch_times) event_ad_watch_times
  289. from
  290. media_api.kuaishou_account_report_daily a
  291. where stat_date between 20211101 and 20211130
  292. group by advertiser_id) report
  293. LEFT JOIN (
  294. SELECT
  295. cua.account_id id,
  296. cua.account_id account_id,
  297. cp.id project_id,
  298. product.id product_id,
  299. ca.id advertiser_id,
  300. uc.company_id company_id,
  301. cua.auth_name account_name,
  302. cp.project_name project_name,
  303. ca.`name` advertiser_name,
  304. uc.company_name company_name,
  305. cp.sale_id sale_id,
  306. su.id user_id,
  307. su.leader_id,
  308. su.org_code org_code
  309. FROM
  310. `jeecg-boot`.ctop_user_allocation cua
  311. LEFT JOIN `jeecg-boot`.ctop_project cp ON cua.project_id = cp.id
  312. LEFT JOIN `jeecg-boot`.ctop_advertiser ca ON ca.id = cua.advertiser_id
  313. LEFT JOIN `jeecg-boot`.sys_user su ON su.id = cua.user_id
  314. LEFT JOIN `jeecg-boot`.user_company uc ON uc.user_id = cua.user_id
  315. LEFT JOIN `jeecg-boot`.ctop_product product ON product.id = cp.product_id
  316. WHERE
  317. cua.account_id IS NOT NULL) bei
  318. ON report.account_id = bei.account_id;
  319. """.format(day_count=self.day_count)
  320. print(sql)
  321. TidbConn.upsert(conn=self.conn, sql_buff=sql)
  322. self.conn.commit()
  323. TidbConn.conn_close(conn=self.conn)
  324. return 0
  325. except Exception as e:
  326. logging.error(e)
  327. return -1
  328. if __name__ == '__main__':
  329. status = AppKuaishouAccountReportBusiMonth().taskHandler()
  330. if status == 0:
  331. print("作业执行成功")
  332. exit(0)
  333. else:
  334. print("作业执行失败")
  335. exit(-1)