AppKuaishouUnitReportBusi.py 16 KB

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