AppKuaishouCompanyUserReportBusi.py 15 KB

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