AppKuaishouProjectReportBusi.py 14 KB

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